Skip to content

Feat: add CPU header info storage and cache size formatting#651

Merged
deepin-bot[bot] merged 1 commit intolinuxdeepin:develop/eaglefrom
GongHeng2017:202605071408-dev-eagle-feat
May 7, 2026
Merged

Feat: add CPU header info storage and cache size formatting#651
deepin-bot[bot] merged 1 commit intolinuxdeepin:develop/eaglefrom
GongHeng2017:202605071408-dev-eagle-feat

Conversation

@GongHeng2017
Copy link
Copy Markdown
Contributor

@GongHeng2017 GongHeng2017 commented May 7, 2026

  • Add DeviceManager CPU header info getters/setters and storage member
  • Adjust main window initial height to 802
  • Rename HeaderInfoTableWidget clear() to resetTableContents()
  • Add Common::formatTotalCache for converting per-thread cache to total cache and formatting with KiB/MiB/GiB units
  • Update SPDX year range in DeviceManager files

Log: add feature for cpu info show
Task: https://pms.uniontech.com/task-view-387697.html

Summary by Sourcery

Add storage and accessors for CPU header information and introduce formatted total CPU cache size display, alongside minor UI and metadata tweaks.

New Features:

  • Store per-CPU header information in DeviceManager with corresponding setters and getters.
  • Provide a utility to convert per-thread CPU cache values into formatted total cache sizes with KiB/MiB/GiB units.

Enhancements:

  • Increase the main window initial height to 802 for improved layout.
  • Update SPDX copyright year ranges in DeviceManager source files.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai Bot commented May 7, 2026

Reviewer's Guide

Implements CPU header info storage in DeviceManager, introduces a common helper for aggregating and formatting total CPU cache from per-thread values, adjusts main window default height, and updates SPDX headers.

Class diagram for updated Common and DeviceManager CPU cache and header info

classDiagram
    class Common {
        +static bool isShowScreenSize()
        +static QString formatTotalCache(QString perThreadCache, int coreCount)
    }

    class DeviceManager {
        +void setCpuFrequencyIsCur(const bool & flag)
        +void setCpuHeaderInfo(const QList~QList~QPair~QString, QString~~~~ & info)
        +void getCpuHeaderInfo(QList~QList~QPair~QString, QString~~~~ & info) const
        -QList~QList~QPair~QString, QString~~~~ m_ListCpuHeaderInfo
    }

    DeviceManager ..> Common : uses formatTotalCache
Loading

File-Level Changes

Change Details Files
Add reusable helper for converting per-thread CPU cache size to total cache and formatting with IEC units.
  • Introduce Common::formatTotalCache API in commonfunction.{h,cpp}.
  • Parse numeric and unit parts from a per-thread cache string, normalize to KiB regardless of original unit, and multiply by core count.
  • Choose an appropriate unit (KiB/MiB/GiB) for the aggregated value and format with integer or single decimal precision depending on fractional part.
  • Handle unknown or missing units robustly, defaulting to KiB or treating raw numbers as bytes when necessary.
deepin-devicemanager/src/commonfunction.cpp
deepin-devicemanager/src/commonfunction.h
Persist and expose CPU header information in DeviceManager.
  • Add m_ListCpuHeaderInfo member to store per-CPU header metadata as nested QList<QPair<QString, QString>>.
  • Implement DeviceManager::setCpuHeaderInfo and getCpuHeaderInfo for setting and retrieving the stored header info.
  • Declare the new getter/setter in DeviceManager.h and keep them const-correct for read access.
deepin-devicemanager/src/DeviceManager/DeviceManager.cpp
deepin-devicemanager/src/DeviceManager/DeviceManager.h
Adjust UI defaults and update license metadata.
  • Increase main window initial height constant from 720 to 802 pixels to better fit new content.
  • Update SPDX-FileCopyrightText year range in DeviceManager sources to cover 2019–2026.
deepin-devicemanager/src/Page/MainWindow.cpp
deepin-devicemanager/src/DeviceManager/DeviceManager.cpp
deepin-devicemanager/src/DeviceManager/DeviceManager.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

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

Hey - I've found 1 issue, and left some high level feedback:

  • In Common::formatTotalCache, consider validating coreCount (e.g., treating non-positive values as 0 or 1) and early-returning to avoid misleading results when the multiplier is unexpectedly zero or negative.
  • The unit parsing logic in Common::formatTotalCache mixes startsWith checks with specific equality checks (e.g., startsWith("K") or unitStr == "KB"), which is redundant; simplifying this to a consistent normalization and lookup (e.g., mapping normalized units to multipliers) would make the code clearer and less error-prone.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- In Common::formatTotalCache, consider validating coreCount (e.g., treating non-positive values as 0 or 1) and early-returning to avoid misleading results when the multiplier is unexpectedly zero or negative.
- The unit parsing logic in Common::formatTotalCache mixes startsWith checks with specific equality checks (e.g., startsWith("K") or unitStr == "KB"), which is redundant; simplifying this to a consistent normalization and lookup (e.g., mapping normalized units to multipliers) would make the code clearer and less error-prone.

## Individual Comments

### Comment 1
<location path="deepin-devicemanager/src/commonfunction.cpp" line_range="266-270" />
<code_context>
+    while (i >= 0 && !s[i].isDigit() && s[i] != '.')
+        --i;
+
+    QString numStr = s.left(i + 1);
+    QString unitStr = s.mid(i + 1).toUpper();
+
+    bool ok;
+    double num = numStr.toDouble(&ok);
+    if (!ok)
+        return QString();
</code_context>
<issue_to_address>
**issue (bug_risk):** Input formats with prefixes (e.g. "L3 256 KiB") will fail to parse.

Because the backward scan only skips trailing non-digits, `s.left(i + 1)` still includes any leading label (e.g. "L3 256 "), so `toDouble` fails and the function returns empty. If inputs can include such prefixes, you’ll need to strip non-numeric content before the numeric part (e.g. scan forward for the first digit or use a regex to extract the number and unit).
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment thread deepin-devicemanager/src/commonfunction.cpp
@GongHeng2017 GongHeng2017 force-pushed the 202605071408-dev-eagle-feat branch from 2d33350 to 67f9625 Compare May 7, 2026 06:22
@deepin-ci-robot
Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: GongHeng2017, 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

- Add DeviceManager CPU header info getters/setters and storage member
- Adjust main window initial height to 802
- Rename HeaderInfoTableWidget clear() to resetTableContents()
- Add Common::formatTotalCache for converting per-thread cache to total cache
  and formatting with KiB/MiB/GiB units
- Update SPDX year range in DeviceManager files

Log: add feature for cpu info show
Task: https://pms.uniontech.com/task-view-387697.html
@GongHeng2017 GongHeng2017 force-pushed the 202605071408-dev-eagle-feat branch from 67f9625 to 8a54efe Compare May 7, 2026 06:44
@deepin-ci-robot
Copy link
Copy Markdown

deepin pr auto review

这段代码主要是对设备管理器(DeviceManager)进行了一些更新,包括版权年份更新、新增CPU头部信息的存取接口、调整窗口初始高度,以及新增一个用于格式化总缓存大小的工具函数。以下是对这段diff的详细审查和改进建议:

1. 版权与许可证更新

-// SPDX-FileCopyrightText: 2022 UnionTech Software Technology Co., Ltd.
+// SPDX-FileCopyrightText: 2022 - 2026 UnionTech Software Technology Co., Ltd.

意见:这是一个常规的版权年份更新,将有效期延长至2026年。这是合理的,但建议在代码库中统一更新所有相关文件的版权年份,避免不同文件年份不一致。


2. 新增CPU头部信息接口

void DeviceManager::setCpuHeaderInfo(const QList<QList<QPair<QString, QString>>> &info)
{
    m_ListCpuHeaderInfo = info;
}

void DeviceManager::getCpuHeaderInfo(QList<QList<QPair<QString, QString>>> &info) const
{
    info = m_ListCpuHeaderInfo;
}

意见

  1. 命名规范

    • 成员变量 m_ListCpuHeaderInfo 的命名风格不一致。Qt风格指南建议成员变量以 m_ 开头,后续使用驼峰命名法,即 m_listCpuHeaderInfo 更为合适。
    • 函数名 getCpuHeaderInfo 不符合Qt的getter命名习惯,建议改为 cpuHeaderInfo
  2. 接口设计

    • getCpuHeaderInfo 使用了非const引用返回值,这不是Qt的典型做法。建议改为按值返回 QList<QList<QPair<QString, QString>>>,因为Qt的容器类使用了隐式共享(写时复制),按值返回的性能开销很小。
    • 改进后的接口:
      void setCpuHeaderInfo(const QList<QList<QPair<QString, QString>>> &info);
      QList<QList<QPair<QString, QString>>> cpuHeaderInfo() const;
  3. 线程安全

    • 如果这个类可能被多线程访问,建议添加互斥锁保护 m_ListCpuHeaderInfo 的读写操作。

3. 窗口初始高度调整

-#define INIT_HEIGHT 720     // 窗口的初始化高度
+#define INIT_HEIGHT 802     // 窗口的初始化高度

意见

  • 这是一个简单的UI调整,可能是为了适应新的布局需求。建议检查这个高度是否适合所有屏幕分辨率,特别是低分辨率屏幕(如1366x768),确保窗口不会超出屏幕范围。

4. 新增 formatTotalCache 函数

QString Common::formatTotalCache(const QString &perThreadCache, int coreCount)
{
    // ... (实现细节)
}

意见

  1. 功能分析

    • 该函数将单线程缓存大小乘以核心数,并格式化为合适的单位(KiB/MiB/GiB)。
    • 逻辑清晰,但有一些可以改进的地方。
  2. 单位处理

    • 当前代码区分了 KBKiB,但实际计算时都按1024进制处理。建议明确统一使用二进制单位(KiB/MiB/GiB),避免混淆。
    • 对于未知单位,代码按原数值当作KiB处理,这可能导致错误结果。建议记录警告日志或返回空字符串。
  3. 数值处理

    • 使用 std::modf 判断是否为整数的方法正确,但可以简化:
      if (qFuzzyCompare(value, qRound(value))) {
          return QString::number(static_cast<long long>(value)) + " " + unit;
      } else {
          return QString::number(value, 'f', 1) + " " + unit;
      }
      使用 qFuzzyCompareqRound 更符合Qt风格,且更安全。
  4. 性能优化

    • 如果 coreCount 为0或负数,函数会返回0,但建议显式检查并处理:
      if (coreCount <= 0) {
          return QString();
      }
  5. 国际化

    • 函数返回的字符串是硬编码的英文单位(如"GiB"),如果需要支持多语言,建议使用 tr() 包装:
      return QString::number(value, 'f', 1) + " " + tr("GiB");

5. 其他建议

  1. 代码注释

    • m_ListCpuHeaderInfo 的注释是中文,建议统一使用英文注释,或确保团队内注释语言一致。
    • formatTotalCache 函数缺少注释,建议添加函数说明,明确输入输出格式和单位转换规则。
  2. 测试覆盖

    • 新增的 formatTotalCache 函数逻辑较复杂,建议添加单元测试,覆盖以下场景:
      • 不同单位(KB/MB/GB/TB)的输入。
      • 边界值(如0、极大值)。
      • 非法输入(如空字符串、无效单位)。
  3. 代码复用

    • 单位转换逻辑可能在其他地方也需要用到,建议提取为独立的工具函数,如 convertToKiBformatSize

改进后的 formatTotalCache 示例

QString Common::formatTotalCache(const QString &perThreadCache, int coreCount)
{
    if (coreCount <= 0 || perThreadCache.isEmpty()) {
        return QString();
    }

    QString s = perThreadCache.trimmed();
    int i = s.length() - 1;
    while (i >= 0 && !s[i].isDigit() && s[i] != '.') {
        --i;
    }

    QString numStr = s.left(i + 1);
    QString unitStr = s.mid(i + 1).toUpper();

    bool ok;
    double num = numStr.toDouble(&ok);
    if (!ok) {
        return QString();
    }

    double perCoreKiB = 0.0;
    if (unitStr.startsWith("K") || unitStr == "KB" || unitStr == "KIB") {
        perCoreKiB = num;
    } else if (unitStr.startsWith("M") || unitStr == "MB" || unitStr == "MIB") {
        perCoreKiB = num * 1024.0;
    } else if (unitStr.startsWith("G") || unitStr == "GB" || unitStr == "GIB") {
        perCoreKiB = num * 1024.0 * 1024.0;
    } else if (unitStr.startsWith("T") || unitStr == "TB" || unitStr == "TIB") {
        perCoreKiB = num * 1024.0 * 1024.0 * 1024.0;
    } else if (unitStr.isEmpty() || unitStr == "B") {
        perCoreKiB = num / 1024.0;
    } else {
        qWarning() << "Unknown cache unit:" << unitStr;
        return QString();
    }

    double totalKiB = perCoreKiB * coreCount;

    double value;
    QString unit;
    if (totalKiB >= 1024.0 * 1024.0) {
        value = totalKiB / (1024.0 * 1024.0);
        unit = "GiB";
    } else if (totalKiB >= 1024.0) {
        value = totalKiB / 1024.0;
        unit = "MiB";
    } else {
        value = totalKiB;
        unit = "KiB";
    }

    if (qFuzzyCompare(value, qRound(value))) {
        return QString::number(static_cast<long long>(value)) + " " + unit;
    } else {
        return QString::number(value, 'f', 1) + " " + unit;
    }
}

总结

这段代码整体质量尚可,但有一些命名规范、接口设计和错误处理方面可以改进。建议:

  1. 统一命名风格和注释语言。
  2. 优化接口设计,符合Qt风格。
  3. 增强错误处理和日志记录。
  4. 添加单元测试覆盖新功能。
  5. 考虑代码复用和国际化支持。

@GongHeng2017
Copy link
Copy Markdown
Contributor Author

/merge

@deepin-bot
Copy link
Copy Markdown
Contributor

deepin-bot Bot commented May 7, 2026

This pr cannot be merged! (status: unstable)

@GongHeng2017
Copy link
Copy Markdown
Contributor Author

/forcemerge

@deepin-bot
Copy link
Copy Markdown
Contributor

deepin-bot Bot commented May 7, 2026

This pr force merged! (status: unstable)

@deepin-bot deepin-bot Bot merged commit 27cd21b into linuxdeepin:develop/eagle May 7, 2026
19 of 22 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