Skip to content

perf: cache redundant command outputs in disk health check - #229

Open
tianming-1996 wants to merge 1 commit into
linuxdeepin:masterfrom
tianming-1996:agent/pms-bug-bot/4e77c08ba199
Open

tianming-1996 wants to merge 1 commit into
linuxdeepin:masterfrom
tianming-1996:agent/pms-bug-bot/4e77c08ba199

Conversation

@tianming-1996

@tianming-1996 tianming-1996 commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

perf: cache redundant command outputs in disk health check

  1. Root cause: getDeviceHardInfo called smartctl --all, hwinfo, and
    lshw multiple times via separate child functions, each spawning
    external processes redundantly and causing 40s+ response time
  2. Fix: pre-execute smartctl --all, hwinfo --disk --only, and
    lshw -C disk once in getDeviceHardInfo, pass cached outputs to
    child functions via optional parameters with default values
  3. Impact: reduces disk health check from 40s+ to 15-20s, existing
    callers using default params are unaffected

Influence:

  1. Test disk health check response time on SSD and HDD devices
  2. Verify SMART info, model, and interface display correctly
  3. Test with devices requiring -d sat fallback for smartctl
  4. Verify no regression when functions called without cached params

perf: 缓存磁盘健康检测中的冗余命令输出

  1. 根因:getDeviceHardInfo 通过多个子函数分别调用 smartctl --all、
    hwinfo 和 lshw,每个子函数独立执行外部命令导致 40s+ 响应时间
  2. 方案:在 getDeviceHardInfo 中预执行 smartctl --all、
    hwinfo --disk --only、lshw -C disk 各一次,通过带默认值的可选
    参数将缓存结果传递给子函数复用
  3. 影响:磁盘健康检测从 40s+ 降至 15-20s,使用默认参数的现有
    调用方不受影响

Influence:

  1. 测试 SSD 和 HDD 设备的磁盘健康检测响应时间
  2. 验证 SMART 信息、型号和接口显示正确
  3. 测试需要 -d sat 回退的 smartctl 设备
  4. 验证不传缓存参数调用时无回归

PMS: BUG-275165

Summary by Sourcery

Enhancements:

  • Reduce disk health check latency by caching smartctl, hwinfo, and lshw command results for reuse across disk information gathering.

1. Root cause: getDeviceHardInfo called smartctl --all, hwinfo, and
   lshw multiple times via separate child functions, each spawning
   external processes redundantly and causing 40s+ response time
2. Fix: pre-execute smartctl --all, hwinfo --disk --only, and
   lshw -C disk once in getDeviceHardInfo, pass cached outputs to
   child functions via optional parameters with default values
3. Impact: reduces disk health check from 40s+ to 15-20s, existing
   callers using default params are unaffected

Influence:
1. Test disk health check response time on SSD and HDD devices
2. Verify SMART info, model, and interface display correctly
3. Test with devices requiring -d sat fallback for smartctl
4. Verify no regression when functions called without cached params

perf: 缓存磁盘健康检测中的冗余命令输出

1. 根因:getDeviceHardInfo 通过多个子函数分别调用 smartctl --all、
   hwinfo 和 lshw,每个子函数独立执行外部命令导致 40s+ 响应时间
2. 方案:在 getDeviceHardInfo 中预执行 smartctl --all、
   hwinfo --disk --only、lshw -C disk 各一次,通过带默认值的可选
   参数将缓存结果传递给子函数复用
3. 影响:磁盘健康检测从 40s+ 降至 15-20s,使用默认参数的现有
   调用方不受影响

Influence:
1. 测试 SSD 和 HDD 设备的磁盘健康检测响应时间
2. 验证 SMART 信息、型号和接口显示正确
3. 测试需要 -d sat 回退的 smartctl 设备
4. 验证不传缓存参数调用时无回归

PMS: BUG-275165
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: tianming-1996

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

@github-actions

Copy link
Copy Markdown
  • 检测到敏感词lshw变动
详情
    {
    "lshw": {
        "service/diskoperation/DeviceStorage.cpp": [
            "        if (Utils::executCmd(\"lshw -C disk\", outPut, error) != 0) {",
            "            qDebug() << \"Failed to execute lshw command, error:\" << error;",
            "        exitcode = Utils::executCmd(\"lshw -C disk\", outPut, error);",
            "            qDebug() << \"Failed to execute lshw command, error:\" << error;"
        ],
        "service/diskoperation/partedcore.cpp": [
            "    QString lshwOutput;",
            "        Utils::executCmd(\"lshw -C disk\", lshwOutput, error);",
            "    device.getDiskInfoFromLshw(devicepath, lshwOutput);",
            "    device.getDiskInfoModel(devicepath, device.m_model, smartctlOutput, lshwOutput);"
        ]
    }
}

@sourcery-ai

sourcery-ai Bot commented Oct 21, 2026

Copy link
Copy Markdown

Reviewer's Guide

The disk health-check aggregation path now executes smartctl, hwinfo, and lshw once and shares their outputs with the relevant parsers, reducing redundant process launches while keeping existing direct callers compatible through default parameters and preserving smartctl -d sat fallback behavior.

Sequence diagram for cached disk health-check command outputs

sequenceDiagram
    participant PartedCore
    participant DeviceStorage
    participant smartctl
    participant hwinfo
    participant lshw

    PartedCore->>DeviceStorage: runSmartctlAll(devicepath, smartctlOutput)
    DeviceStorage->>smartctl: smartctl --all devicepath
    alt device type required
        DeviceStorage->>smartctl: smartctl --all -d sat devicepath
    end
    PartedCore->>hwinfo: hwinfo --disk --only devicepath
    PartedCore->>lshw: lshw -C disk
    PartedCore->>DeviceStorage: getDiskInfoFromHwinfo(devicepath, hwinfoOutput)
    PartedCore->>DeviceStorage: getDiskInfoFromLshw(devicepath, lshwOutput)
    PartedCore->>DeviceStorage: getDiskInfoFromSmartCtl(devicepath, smartctlOutput, smartctlExitCode)
    PartedCore->>DeviceStorage: getDiskInfoModel(devicepath, model, smartctlOutput, lshwOutput)
    PartedCore->>DeviceStorage: getDiskInfoInterface(devicepath, interface, model, hwinfoOutput)
Loading

File-Level Changes

Change Details Files
Centralize external disk-inspection command execution and reuse the resulting output across parsing stages.
  • Run smartctl once with shared -d sat fallback handling, plus hwinfo and lshw once, in the aggregate health-check path.
  • Pass command output and the smartctl exit code into downstream parsers.
  • Preserve standalone-call behavior through optional cached-output parameters that fall back to executing the commands.
  • Extract smartctl execution and fallback logic into a reusable helper.
service/diskoperation/partedcore.cpp
service/diskoperation/DeviceStorage.cpp
service/diskoperation/DeviceStorage.h
Update disk metadata parsers to consume cached command results while retaining their existing parsing responsibilities.
  • Use cached hwinfo and lshw output for interface, model, and device information parsing.
  • Use cached smartctl output and exit status for SMART data and model extraction.
  • Retain command execution and error handling when cached output is absent.
service/diskoperation/DeviceStorage.cpp
service/diskoperation/DeviceStorage.h

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 40 分 (通过阈值: 70分)

Fail


📊 总体评价

项目 结果
审查结论 代码审查未通过
评分详情 本次提交目的是性能优化(缓存冗余命令输出),方向正确,但引入了一个致命编译错误——新增的 runSmartctlAll 方法声明为 private static,却在 PartedCore 中跨类调用,导致无法编译;另存在 smartctl 缓存退出码未校验、hwinfo/lshw 预执行退出码未检查等逻辑缺陷,部分抵消缓存优化效果。无安全漏洞。

🔍 详细分析

1. 语法逻辑 ❌

评价: 存在逻辑缺陷 ❌ 不通过

潜在问题:
✅ 未发现明显问题

建议: ['1. 将 DeviceStorage.h 中 runSmartctlAll 的声明从 private: 移至 public: 区域,或为 PartedCore 添加 friend 声明,使其可被外部类调用。', '2. 为 getDiskInfoModel 增加 cachedExitCode 参数(与 getDiskInfoFromSmartCtl 保持一致),在缓存路径校验退出码,失败时提前返回。', '3. 在 partedcore.cpp 预执行 hwinfo/lshw 处检查 Utils::executCmd 返回值,失败时记录日志,避免空输出误导下游缓存判断。']


2. 代码质量 ✅

评价: 代码结构清晰,注释完整 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: ['1. 删除 getDiskInfoModel 中未使用的 QString cmd; 声明。', '2. 让 runSmartctlAll 增加输出参数 QString &error,或将 qDebug 中的 << error 移除,避免打印空字符串。', '3. 将 cachedExitCode 默认值改为 -1,调用方未传时可在缓存路径识别并处理。']


3. 代码性能 ✅

评价: 性能良好,资源使用合理 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: ['1. 预执行处检查 Utils::executCmd 返回值,失败时记录日志并保持缓存输出为空但明确不重试,或调整下游函数在缓存模式下不因空输出而重执行。']


4. 代码安全 🔒

评价: 存在0个安全漏洞 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: ['无安全相关建议。']


💡 改进建议代码示例

// === DeviceStorage.h:将 runSmartctlAll 移至 public 区域 ===
class DeviceStorage
{
public:
    DeviceStorage();

    bool getDiskInfoFromHwinfo(const QString &devicePath, const QString &cachedOutput = QString());
    bool getDiskInfoFromLshw(const QString &devicePath, const QString &cachedOutput = QString());
    bool getDiskInfoFromLsblk(const QString &devicePath);
    bool getDiskInfoFromSmartCtl(const QString &devicePath, const QString &cachedOutput = QString(), int cachedExitCode = -1);
    void getDiskInfoModel(const QString &devicePath, QString &model,
                          const QString &cachedSmartctlOutput = QString(),
                          const QString &cachedLshwOutput = QString(),
                          int cachedSmartctlExitCode = -1);
    QString getDiskInfoMediaType(const QString &devicePath);
    void getDiskInfoInterface(const QString &devicePath, QString &interface, QString &model,
                              const QString &cachedHwinfoOutput = QString());
    void updateForHWDevice(const QString &devicePath);

    /**@brief:运行 smartctl --all 并处理 -d sat 重试,返回退出码*/
    static int runSmartctlAll(const QString &devicePath, QString &outPut, QString &error = QString());

// ...

// === DeviceStorage.cpp:getDiskInfoModel 缓存路径校验退出码,移除未使用变量 ===
void DeviceStorage::getDiskInfoModel(const QString &devicePath, QString &model,
                                     const QString &cachedSmartctlOutput,
                                     const QString &cachedLshwOutput,
                                     int cachedSmartctlExitCode)
{
    qDebug() << "DeviceStorage::getDiskInfoModel BEGIN";
    QString outPut, error;
    int exitcode = 0;
    if (cachedSmartctlOutput.isEmpty()) {
        exitcode = runSmartctlAll(devicePath, outPut, error);
        if (exitcode != 0) {
            qDebug() << "Failed to execute smartctl command, error:" << error;
            return;
        }
    } else {
        outPut = cachedSmartctlOutput;
        exitcode = cachedSmartctlExitCode;
        if (exitcode != 0) {
            qDebug() << "Cached smartctl command failed (exit code" << exitcode << "), skip parsing";
            return;
        }
    }
    // ... 解析逻辑保持不变 ...
}

// === partedcore.cpp:预执行检查退出码 ===
QString hwinfoOutput;
{
    QString cmd = QString("hwinfo --disk --only %1").arg(devicepath);
    QString error;
    if (Utils::executCmd(cmd, hwinfoOutput, error) != 0) {
        qDebug() << "Failed to pre-execute hwinfo command, error:" << error;
    }
}

QString lshwOutput;
{
    QString error;
    if (Utils::executCmd("lshw -C disk", lshwOutput, error) != 0) {
        qDebug() << "Failed to pre-execute lshw command, error:" << error;
    }
}

本报告由 AI 代码审查工具自动生成

@deepin-bot

deepin-bot Bot commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.0.28
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #251

@deepin-bot

deepin-bot Bot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.0.29
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #259

@deepin-bot

deepin-bot Bot commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.0.30
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #261

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.

2 participants