Skip to content

Commit f596845

Browse files
committed
性能监控添加显示温度功能
1 parent a25ba5c commit f596845

File tree

3 files changed

+173
-125
lines changed

3 files changed

+173
-125
lines changed

lib/perfmon.dart

+49-9
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,28 @@ String colorPercentage(int percentage) {
4141
return pen(percentage.toString()) + '%';
4242
}
4343

44+
String? findHwmonPathByNamePart(String namePart) {
45+
const hwmonBasePath = "/sys/class/hwmon/hwmon";
46+
int index = 0;
47+
while (true) {
48+
var path = hwmonBasePath + index.toString();
49+
if (!Directory(path).existsSync()) {
50+
break;
51+
}
52+
var namePath = path + "/name";
53+
if (!File(namePath).existsSync()) {
54+
index++;
55+
continue;
56+
}
57+
var name = File(namePath).readAsStringSync();
58+
if (name.contains(namePart)) {
59+
return path;
60+
}
61+
index++;
62+
}
63+
return null;
64+
}
65+
4466
class CpuNormalizedDataSource extends PerfmonDataSource {
4567
static const path = '/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq';
4668
static const header_cpu_freq = 'cpu_freq_mhz';
@@ -81,42 +103,51 @@ class GpuDataSource extends PerfmonDataSource {
81103
static RegExp regex = RegExp(r'(\d+)@(\d+)Hz');
82104
static const header_gpu_load = 'gpu_load';
83105
static const header_gpu_freq = 'gpu_freq_mhz';
106+
static const header_gpu_temp = 'gpu_temp_mc';
107+
late String gpu_temp_path;
84108

85109
List<int> _getData() {
86110
var str = File(path).readAsStringSync();
87111
var matches = regex.firstMatch(str);
88112
if (matches?.groupCount != 2) {
89113
throw Exception('Cannot parse GPU data source: $str');
90114
}
115+
var gpu_temp = File(gpu_temp_path).readAsStringSync();
91116
return [
92117
int.parse(matches!.group(1)!),
93-
int.parse(matches.group(2)!) ~/ 1000000
118+
int.parse(matches.group(2)!) ~/ 1000000,
119+
int.parse(gpu_temp)
94120
];
95121
}
96122

97123
@override
98124
void init() {
99125
if (!File(path).existsSync()) {
100-
throw Exception('GPU data source not found');
126+
throw Exception('GPU load data source not found');
127+
}
128+
var hwmon_path = findHwmonPathByNamePart("gpu_thermal");
129+
if (hwmon_path == null) {
130+
throw Exception('GPU temp data source not found');
101131
}
132+
gpu_temp_path = hwmon_path + "/temp1_input";
102133
_getData();
103134
}
104135

105136
@override
106137
String getCsvHeader() {
107-
return '$header_gpu_load,$header_gpu_freq';
138+
return '$header_gpu_load,$header_gpu_freq,$header_gpu_temp';
108139
}
109140

110141
@override
111142
String getCsvLine() {
112143
var data = _getData();
113-
return '${data[0]},${data[1]}';
144+
return '${data[0]},${data[1]},${data[2]}';
114145
}
115146

116147
@override
117148
String getUserFriendlyLine() {
118149
var data = _getData();
119-
return 'GPU load: ${colorPercentage(data[0])}, freq: ${(data[1] / 1000).toStringAsFixed(1)}GHz';
150+
return 'GPU load: ${colorPercentage(data[0])}, freq: ${(data[1] / 1000).toStringAsFixed(1)}GHz, temp: ${(data[2] / 1000).toStringAsFixed(1)}°C';
120151
}
121152
}
122153

@@ -170,17 +201,21 @@ class NpuDataSource extends PerfmonDataSource {
170201
static RegExp regex =
171202
RegExp(r'Core0:\s+(\d+)%, Core1:\s+(\d+)%, Core2:\s+(\d+)%');
172203
static const header_npu_load = 'npu_load';
204+
static const header_npu_temp = 'npu_temp_mc';
205+
late String npu_temp_path;
173206

174207
List<int> _getData() {
175208
var str = File(path).readAsStringSync();
176209
var matches = regex.firstMatch(str);
177210
if (matches?.groupCount != 3) {
178211
throw Exception('Cannot parse NPU data source: $str');
179212
}
213+
var npu_temp = File(npu_temp_path).readAsStringSync();
180214
return [
181215
int.parse(matches!.group(1)!),
182216
int.parse(matches.group(2)!),
183-
int.parse(matches.group(3)!)
217+
int.parse(matches.group(3)!),
218+
int.parse(npu_temp)
184219
];
185220
}
186221

@@ -189,24 +224,29 @@ class NpuDataSource extends PerfmonDataSource {
189224
if (!File(path).existsSync()) {
190225
throw Exception('NPU data source not found');
191226
}
227+
var hwmon_path = findHwmonPathByNamePart("npu_thermal");
228+
if (hwmon_path == null) {
229+
throw Exception('NPU temp data source not found');
230+
}
231+
npu_temp_path = hwmon_path + "/temp1_input";
192232
_getData();
193233
}
194234

195235
@override
196236
String getCsvHeader() {
197-
return '${header_npu_load}_0,${header_npu_load}_1,${header_npu_load}_2';
237+
return '${header_npu_load}_0,${header_npu_load}_1,${header_npu_load}_2,${header_npu_temp}';
198238
}
199239

200240
@override
201241
String getCsvLine() {
202242
var data = _getData();
203-
return '${data[0]},${data[1]},${data[2]}';
243+
return '${data[0]},${data[1]},${data[2]},${data[3]}';
204244
}
205245

206246
@override
207247
String getUserFriendlyLine() {
208248
var data = _getData();
209-
return 'NPU load: ${colorPercentage(data[0])}, ${colorPercentage(data[1])}, ${colorPercentage(data[2])}';
249+
return 'NPU load: ${colorPercentage(data[0])}, ${colorPercentage(data[1])}, ${colorPercentage(data[2])}, temp: ${(data[3] / 1000).toStringAsFixed(1)}°C';
210250
}
211251
}
212252

0 commit comments

Comments
 (0)