fix(cpu): fix CPU cache size calculation with shared list#665
Conversation
818706e to
178ceeb
Compare
Add support for reading CPU cache shared_cpu_list from sysfs and use it to calculate total cache size accurately for each cache level. 添加对CPU缓存shared_cpu_list的支持,从sysfs读取并用于准确 计算各级缓存总大小。 Log: 修复CPU缓存大小计算错误 PMS: BUG-361631 Influence: 修复后CPU各级缓存总大小计算准确,系统监视器显示的缓存信息更准确。
178ceeb to
752da2c
Compare
deepin pr auto review你好!我是CodeGeeX。我已经仔细审查了你提供的Git Diff,该变更主要是为了读取并计算CPU缓存共享的CPU列表,从而更精确地计算总缓存大小。 总体来说,这个补丁的逻辑方向是正确的,特别是在计算L3 Cache总大小时,从原先的硬编码 不过,在代码质量、性能、健壮性和安全性方面,我发现了几个需要改进的地方。以下是我的详细审查意见: 1. 代码逻辑与健壮性问题: if (trimmed.toInt(&ok) || ok) {
count += 1;
}
bool ok = false;
trimmed.toInt(&ok);
if (ok) {
count += 1;
}问题: QFile fileS(sharedPath);
if (fileS.open(QIODevice::ReadOnly)) {
sharedCpuList = fileS.readAll();
}
fileS.close();
// ... 后续直接使用 sharedCpuList
if (fileS.open(QIODevice::ReadOnly)) {
sharedCpuList = QString::fromUtf8(fileS.readAll()).trimmed();
}2. 代码性能问题:多余的
问题:
3. 代码安全与异常处理问题:除零风险 int groupCount = (sharedCount > 0 && logicalNum > 0) ? logicalNum / sharedCount : coreNum;
int groupCount = (sharedCount > 0 && logicalNum > 0) ? logicalNum / sharedCount : 1;
问题:
QString sharedCpuList;
QFile fileS(sharedPath);
if (fileS.open(QIODevice::ReadOnly)) {
sharedCpuList = QString::fromUtf8(fileS.readAll()).trimmed();
fileS.close(); // 显式关闭,或者依赖析构
}4. 代码风格与质量问题:Getter方法缺少 const QString &LogicalCpu::l1dSharedCpuList() // 缺少 const
const QString &LogicalCpu::l1dSharedCpuList() const;问题:版权时间修改的合理性 -// SPDX-FileCopyrightText: 2019 ~ 2023 UnionTech Software Technology Co., Ltd.
+// SPDX-FileCopyrightText: 2019 - 2026 UnionTech Software Technology Co., Ltd.
综合修改建议代码片段针对 int Common::parseSharedCpuCount(const QString &sharedCpuList)
{
QString s = sharedCpuList.trimmed();
if (s.isEmpty())
return 0;
int count = 0;
QStringList parts = s.split(',', Qt::SkipEmptyParts);
for (const QString &part : parts) {
QString trimmed = part.trimmed();
if (trimmed.contains('-')) {
QStringList range = trimmed.split('-');
if (range.size() == 2) {
bool ok1 = false, ok2 = false;
int start = range[0].trimmed().toInt(&ok1);
int end = range[1].trimmed().toInt(&ok2);
if (ok1 && ok2 && end >= start) {
count += end - start + 1;
}
}
} else {
bool ok = false;
trimmed.toInt(&ok); // 移除晦涩的 || ok 逻辑
if (ok) {
count += 1;
}
}
}
return count;
}针对 // .h
const QString &l1dSharedCpuList() const;
const QString &l1iSharedCpuList() const;
const QString &l2SharedCpuList() const;
const QString &l3SharedCpuList() const;
const QString &l4SharedCpuList() const;
// .cpp
const QString &LogicalCpu::l1dSharedCpuList() const
{
Q_D(const LogicalCpu); // 注意这里也要用 const
return d->l1d_shared_cpu_list;
}
// ... 其他同理针对 // get shared_cpu_list
QString sharedCpuList;
QFile fileS(path + "/shared_cpu_list");
if (fileS.open(QIODevice::ReadOnly)) {
sharedCpuList = QString::fromUtf8(fileS.readAll()).trimmed();
fileS.close();
}希望这些建议对你有所帮助!如果有任何疑问,欢迎继续讨论。 |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: add-uos, lzwind The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/forcemerge |
|
This pr force merged! (status: unstable) |
Add support for reading CPU cache shared_cpu_list from sysfs and use it to calculate total cache size accurately for each cache level.
添加对CPU缓存shared_cpu_list的支持,从sysfs读取并用于准确
计算各级缓存总大小。
Log: 修复CPU缓存大小计算错误
PMS: BUG-361631
Influence: 修复后CPU各级缓存总大小计算准确,系统监视器显示的缓存信息更准确。