Skip to content

Commit e7ead1f

Browse files
itsXuStdeepin-bot[bot]
authored andcommitted
feat: add CPU frequency reading from CPU7 sysfs interface
- Add new config option 'readCPUFreqByCPU7' to control frequency reading method - Implement read_cpu_freq_range_by_cpu7() to read min/max frequency from sysfs - Add DConfig support to dynamically switch between lscpu and CPU7 frequency sources - Read frequency from /sys/devices/system/cpu/cpu7/cpufreq/scaling_{min,max}_freq - Convert frequency unit from KHz to MHz automatically Log: Add configurable CPU7 frequency reading feature with sysfs interface support Bug: https://pms.uniontech.com/bug-view-334529.html Task: https://pms.uniontech.com/task-view-384553.html (cherry picked from commit a2511d4)
1 parent e391ac5 commit e7ead1f

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

assets/configs/org.deepin.system-monitor.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@
3737
"description": "显示暂停恢复进程",
3838
"permissions": "readwrite",
3939
"visibility": "public"
40+
},
41+
"readCPUFreqByCPU7": {
42+
"value": 0,
43+
"serial": 0,
44+
"flags": ["global"],
45+
"name": "Read CPU frequency from CPU7",
46+
"name[zh_CN]": "从 CPU7 读取频率",
47+
"description": "Read CPU frequency range (min/max) from CPU7 sysfs interface",
48+
"description[zh_CN]": "从 CPU7 的 sysfs 接口读取 CPU 频率范围(最小值/最大值)",
49+
"permissions": "readwrite",
50+
"visibility": "public"
4051
}
4152
}
4253
}

deepin-system-monitor-main/system/cpu_set.cpp

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ extern "C" {
2323
#include <QTextStream>
2424
#include <QProcess>
2525
#include <QRegularExpression>
26+
#include <DConfig>
27+
DCORE_USE_NAMESPACE
28+
2629
#include <ctype.h>
2730
#include <errno.h>
2831
#include <sched.h>
@@ -1102,11 +1105,34 @@ void CPUSet::read_lscpu()
11021105
d->m_info.insert("CPU static MHz", ct->static_mhz);
11031106
}
11041107
if (ct->has_freq) {
1108+
auto KeyCPUMaxFreq = "CPU max MHz";
1109+
auto KeyCPUMinFreq = "CPU min MHz";
1110+
auto toDoubleStr = [](float v) {
1111+
return QString::number(static_cast<double>(v), 'f', 4);
1112+
};
1113+
11051114
float scal = lsblk_cputype_get_scalmhz(cxt, ct);
1106-
QString maxMHz = QString::number(static_cast<double>(lsblk_cputype_get_maxmhz(cxt, ct)), 'f', 4);
1107-
d->m_info.insert("CPU max MHz", maxMHz);
1108-
QString minMHz = QString::number(static_cast<double>(lsblk_cputype_get_minmhz(cxt, ct)), 'f', 4);
1109-
d->m_info.insert("CPU min MHz", minMHz);
1115+
QString maxMHz = toDoubleStr(lsblk_cputype_get_maxmhz(cxt, ct));
1116+
d->m_info.insert(KeyCPUMaxFreq, maxMHz);
1117+
QString minMHz = toDoubleStr(lsblk_cputype_get_minmhz(cxt, ct));
1118+
d->m_info.insert(KeyCPUMinFreq, minMHz);
1119+
1120+
// 根据配置项决定是否从 CPU7 读取频率
1121+
DConfig *dconfig = DConfig::create("org.deepin.system-monitor", "org.deepin.system-monitor");
1122+
bool readFromCPU7 = false;
1123+
if (dconfig && dconfig->isValid() && dconfig->keyList().contains("readCPUFreqByCPU7")) {
1124+
readFromCPU7 = (dconfig->value("readCPUFreqByCPU7").toInt() != 0);
1125+
}
1126+
if (dconfig) {
1127+
dconfig->deleteLater();
1128+
}
1129+
1130+
if (readFromCPU7) {
1131+
auto freq = read_cpu_freq_range_by_cpu7();
1132+
d->m_info.insert(KeyCPUMinFreq, toDoubleStr(freq.first));
1133+
d->m_info.insert(KeyCPUMaxFreq, toDoubleStr(freq.second));
1134+
}
1135+
11101136
// 取所有CPU频率有效值中最大值
11111137
QString nowMHz = QString::number(static_cast<double>(max_avaliable_cur_freq(cxt, ct)), 'f', 4);
11121138
// 取所有CPU频率有效值中平均值
@@ -1232,5 +1258,46 @@ qulonglong CPUSet::getUsageTotalDelta() const
12321258
return d->cpusageTotal[kCurrentStat] - d->cpusageTotal[kLastStat];
12331259
}
12341260

1261+
/**
1262+
* @brief 读取 CPU7 的频率范围(最小频率和最大频率)
1263+
* @return QPair<float, float> 第一个元素是最小频率(MHz),第二个元素是最大频率(MHz)
1264+
* 如果读取失败,返回 (0.0, 0.0)
1265+
*/
1266+
QPair<float, float> CPUSet::read_cpu_freq_range_by_cpu7()
1267+
{
1268+
float minFreq = 0.0f;
1269+
float maxFreq = 0.0f;
1270+
1271+
// 读取最小频率
1272+
QFile minFile("/sys/devices/system/cpu/cpu7/cpufreq/scaling_min_freq");
1273+
if (minFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
1274+
QTextStream in(&minFile);
1275+
QString content = in.readLine().trimmed();
1276+
bool ok = false;
1277+
unsigned long freqKhz = content.toULong(&ok);
1278+
if (ok) {
1279+
// 将 KHz 转换为 MHz
1280+
minFreq = freqKhz / 1000.0f;
1281+
}
1282+
minFile.close();
1283+
}
1284+
1285+
// 读取最大频率
1286+
QFile maxFile("/sys/devices/system/cpu/cpu7/cpufreq/scaling_max_freq");
1287+
if (maxFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
1288+
QTextStream in(&maxFile);
1289+
QString content = in.readLine().trimmed();
1290+
bool ok = false;
1291+
unsigned long freqKhz = content.toULong(&ok);
1292+
if (ok) {
1293+
// 将 KHz 转换为 MHz
1294+
maxFreq = freqKhz / 1000.0f;
1295+
}
1296+
maxFile.close();
1297+
}
1298+
1299+
return qMakePair(minFreq, maxFreq);
1300+
}
1301+
12351302
} // namespace system
12361303
} // namespace core

deepin-system-monitor-main/system/cpu_set.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class CPUSet
8484
*/
8585
void read_lscpu();
8686
void read_overall_info();
87+
QPair<float, float> read_cpu_freq_range_by_cpu7();
8788

8889
void read_cache_from_lscpu_cmd();
8990

0 commit comments

Comments
 (0)