Skip to content

fix: Optimize kernel log segment index initialization logic - #413

Merged
max-lvs merged 1 commit into
linuxdeepin:release/eaglefrom
LiHua000:release/eagle
Oct 10, 2025
Merged

max-lvs merged 1 commit into
linuxdeepin:release/eaglefrom
LiHua000:release/eagle

Conversation

@LiHua000

Copy link
Copy Markdown

Return index 0 directly on first entry for kernel log type to avoid unnecessary file reading Read file to get total line count only after authentication, improving performance and response speed Maintain existing logic while adding special handling for first entry

Log: fix bug

Bug: https://pms.uniontech.com/bug-view-292033.html

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: LiHua000, max-lvs

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@LiHua000

LiHua000 commented Oct 10, 2025 •

Copy link
Copy Markdown
Author

/merge

@deepin-bot

deepin-bot Bot commented Oct 10, 2025

Copy link
Copy Markdown
Contributor

This pr cannot be merged! (status: unstable)

Return index 0 directly on first entry for kernel log type to avoid unnecessary file reading
Read file to get total line count only after authentication, improving performance and response speed
Maintain existing logic while adding special handling for first entry

Log: fix bug

Bug: https://pms.uniontech.com/bug-view-292033.html
@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

这段代码审查如下:

  1. 语法逻辑:
  • 新增的代码块在逻辑上是合理的,它处理了当type为KERN且bNext为true时,segementIndex为-1(即首次进入)的情况。
  • 但是这段代码放在了if (type == KERN)条件内部,意味着这个优化只对KERN类型有效,如果其他日志类型也需要类似的优化,需要为它们也添加类似的处理。
  1. 代码质量:
  • 代码注释可以更详细一些,解释为什么首次进入时直接返回0而不读取文件。
  • 变量名nSegementIndex可以改为更具描述性的segmentIndex,遵循驼峰命名规范。
  • 代码缩进和格式符合标准,但建议在条件判断前后增加空行,提高可读性。
  1. 代码性能:
  • 这段优化确实可以提高首次进入时的性能,避免了不必要的文件读取操作。
  • 但是,对于非首次进入的情况,仍然需要遍历文件路径并获取行数,这部分可能存在性能瓶颈,特别是当文件数量较多时。
  1. 代码安全:
  • 代码中没有明显的安全问题,但建议对m_type2Filter[type].segementIndex的操作加锁,防止多线程环境下的竞态条件。
  • 对于文件路径的处理,建议添加空值检查,防止DLDBusHandler返回无效路径导致后续操作失败。

改进建议:

if (type == KERN) {
    // 首次进入且请求下一个分段时,直接返回0而不读取文件
    if (m_type2Filter[type].segementIndex == -1 && bNext) {
        m_type2Filter[type].segementIndex = 0;
        return 0;
    }
    
    // 确保文件路径有效
    QStringList filePaths = DLDBusHandler::instance(this)->getFileInfo("kern");
    if (filePaths.isEmpty()) {
        return -1;
    }
    
    qint64 totalLineCount = 0;
    for (const auto& file : filePaths) {
        if (file.isEmpty()) {
            continue;
        }
        totalLineCount += DLDBusHandler::instance(this)->getLineCount(file);
        // 可以考虑添加行数检查,防止totalLineCount溢出
        if (totalLineCount < 0) {
            return -1;
        }
    }
    // ... 后续代码
}

其他建议:

  1. 考虑将这个首次进入的逻辑提取为一个单独的私有函数,提高代码的可重用性和可维护性。
  2. 对于文件操作,建议添加错误处理机制,例如文件不存在或无法读取时的处理。
  3. 考虑添加日志记录,方便追踪和调试。

@max-lvs
max-lvs merged commit 3ef0022 into linuxdeepin:release/eagle Oct 10, 2025
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants