-
Notifications
You must be signed in to change notification settings - Fork 411
Expand file tree
/
Copy pathcpu.cpp
More file actions
894 lines (725 loc) · 27.7 KB
/
Copy pathcpu.cpp
File metadata and controls
894 lines (725 loc) · 27.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
#include "cpu.h"
#include <memory>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <dirent.h>
#include <string.h>
#include <algorithm>
#include <regex>
#include <inttypes.h>
#include <spdlog/spdlog.h>
#include "string_utils.h"
#include "gpu.h"
#include "hud_elements.h"
#ifndef TEST_ONLY
#include "hud_elements.h"
#endif
#ifndef PROCDIR
#define PROCDIR "/proc"
#endif
#ifndef PROCSTATFILE
#define PROCSTATFILE PROCDIR "/stat"
#endif
#ifndef PROCMEMINFOFILE
#define PROCMEMINFOFILE PROCDIR "/meminfo"
#endif
#ifndef PROCCPUINFOFILE
#define PROCCPUINFOFILE PROCDIR "/cpuinfo"
#endif
#include "file_utils.h"
static void calculateCPUData(CPUData& cpuData,
unsigned long long int usertime,
unsigned long long int nicetime,
unsigned long long int systemtime,
unsigned long long int idletime,
unsigned long long int ioWait,
unsigned long long int irq,
unsigned long long int softIrq,
unsigned long long int steal,
unsigned long long int guest,
unsigned long long int guestnice)
{
// Guest time is already accounted in usertime
usertime = usertime - guest;
nicetime = nicetime - guestnice;
// Fields existing on kernels >= 2.6
// (and RHEL's patched kernel 2.4...)
unsigned long long int idlealltime = idletime + ioWait;
unsigned long long int systemalltime = systemtime + irq + softIrq;
unsigned long long int virtalltime = guest + guestnice;
unsigned long long int totaltime = usertime + nicetime + systemalltime + idlealltime + steal + virtalltime;
// Since we do a subtraction (usertime - guest) and cputime64_to_clock_t()
// used in /proc/stat rounds down numbers, it can lead to a case where the
// integer overflow.
#define WRAP_SUBTRACT(a,b) (a > b) ? a - b : 0
cpuData.userPeriod = WRAP_SUBTRACT(usertime, cpuData.userTime);
cpuData.nicePeriod = WRAP_SUBTRACT(nicetime, cpuData.niceTime);
cpuData.systemPeriod = WRAP_SUBTRACT(systemtime, cpuData.systemTime);
cpuData.systemAllPeriod = WRAP_SUBTRACT(systemalltime, cpuData.systemAllTime);
cpuData.idleAllPeriod = WRAP_SUBTRACT(idlealltime, cpuData.idleAllTime);
cpuData.idlePeriod = WRAP_SUBTRACT(idletime, cpuData.idleTime);
cpuData.ioWaitPeriod = WRAP_SUBTRACT(ioWait, cpuData.ioWaitTime);
cpuData.irqPeriod = WRAP_SUBTRACT(irq, cpuData.irqTime);
cpuData.softIrqPeriod = WRAP_SUBTRACT(softIrq, cpuData.softIrqTime);
cpuData.stealPeriod = WRAP_SUBTRACT(steal, cpuData.stealTime);
cpuData.guestPeriod = WRAP_SUBTRACT(virtalltime, cpuData.guestTime);
cpuData.totalPeriod = WRAP_SUBTRACT(totaltime, cpuData.totalTime);
#undef WRAP_SUBTRACT
cpuData.userTime = usertime;
cpuData.niceTime = nicetime;
cpuData.systemTime = systemtime;
cpuData.systemAllTime = systemalltime;
cpuData.idleAllTime = idlealltime;
cpuData.idleTime = idletime;
cpuData.ioWaitTime = ioWait;
cpuData.irqTime = irq;
cpuData.softIrqTime = softIrq;
cpuData.stealTime = steal;
cpuData.guestTime = virtalltime;
cpuData.totalTime = totaltime;
if (cpuData.totalPeriod == 0)
return;
float total = (float)cpuData.totalPeriod;
float v[4];
v[0] = cpuData.nicePeriod * 100.0f / total;
v[1] = cpuData.userPeriod * 100.0f / total;
/* if not detailed */
v[2] = cpuData.systemAllPeriod * 100.0f / total;
v[3] = (cpuData.stealPeriod + cpuData.guestPeriod) * 100.0f / total;
//cpuData.percent = std::clamp(v[0]+v[1]+v[2]+v[3], 0.0f, 100.0f);
cpuData.percent = std::min(std::max(v[0]+v[1]+v[2]+v[3], 0.0f), 100.0f);
}
CPUStats::CPUStats()
{
}
CPUStats::~CPUStats()
{
if (m_cpuTempFile) {
fclose(m_cpuTempFile);
m_cpuTempFile = nullptr;
}
}
bool CPUStats::Init()
{
if (m_inited)
return true;
std::string line;
std::ifstream file (PROCSTATFILE);
bool first = true;
m_cpuData.clear();
if (!file.is_open()) {
SPDLOG_ERROR("Failed to opening " PROCSTATFILE);
return false;
}
do {
if (!std::getline(file, line)) {
SPDLOG_DEBUG("Failed to read all of " PROCSTATFILE);
return false;
} else if (starts_with(line, "cpu")) {
if (first) {
first =false;
continue;
}
CPUData cpu = {};
cpu.totalTime = 1;
cpu.totalPeriod = 1;
sscanf(line.c_str(), "cpu%4d ", &cpu.cpu_id);
m_cpuData.push_back(cpu);
} else if (starts_with(line, "btime ")) {
// C++ way, kind of noisy
//std::istringstream token( line );
//std::string s;
//token >> s;
//token >> m_boottime;
// assume that if btime got read, that everything else is OK too
sscanf(line.c_str(), "btime %lld\n", &m_boottime);
break;
}
} while(true);
#ifndef TEST_ONLY
if (get_params()->enabled[OVERLAY_PARAM_ENABLED_core_type])
get_cpu_cores_types();
#endif
m_inited = true;
return UpdateCPUData();
}
bool CPUStats::Reinit()
{
m_inited = false;
return Init();
}
//TODO take sampling interval into account?
bool CPUStats::UpdateCPUData()
{
unsigned long long int usertime, nicetime, systemtime, idletime;
unsigned long long int ioWait, irq, softIrq, steal, guest, guestnice;
int cpuid = -1;
size_t cpu_count = 0;
if (!m_inited)
return false;
std::string line;
std::ifstream file (PROCSTATFILE);
bool ret = false;
if (!file.is_open()) {
SPDLOG_ERROR("Failed to opening " PROCSTATFILE);
return false;
}
do {
if (!std::getline(file, line)) {
break;
} else if (!ret && sscanf(line.c_str(), "cpu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu",
&usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal, &guest, &guestnice) == 10) {
ret = true;
calculateCPUData(m_cpuDataTotal, usertime, nicetime, systemtime, idletime, ioWait, irq, softIrq, steal, guest, guestnice);
} else if (sscanf(line.c_str(), "cpu%4d %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu %16llu",
&cpuid, &usertime, &nicetime, &systemtime, &idletime, &ioWait, &irq, &softIrq, &steal, &guest, &guestnice) == 11) {
//SPDLOG_DEBUG("Parsing 'cpu{}' line:{}", cpuid, line);
if (!ret) {
SPDLOG_DEBUG("Failed to parse 'cpu' line:{}", line);
return false;
}
if (cpuid < 0 /* can it? */) {
SPDLOG_DEBUG("Cpu id '{}' is out of bounds", cpuid);
return false;
}
if (cpu_count + 1 > m_cpuData.size() || m_cpuData[cpu_count].cpu_id != cpuid) {
SPDLOG_DEBUG("Cpu id '{}' is out of bounds or wrong index, reiniting", cpuid);
return Reinit();
}
CPUData& cpuData = m_cpuData[cpu_count];
calculateCPUData(cpuData, usertime, nicetime, systemtime, idletime, ioWait, irq, softIrq, steal, guest, guestnice);
cpuid = -1;
cpu_count++;
} else {
break;
}
} while(true);
if (cpu_count < m_cpuData.size())
m_cpuData.resize(cpu_count);
m_cpuPeriod = (double)m_cpuData[0].totalPeriod / m_cpuData.size();
m_updatedCPUs = true;
return ret;
}
bool CPUStats::UpdateCoreMhz() {
m_coreMhz.clear();
FILE *fp;
static bool scaling_freq = true;
if (scaling_freq){
for (auto& cpu : m_cpuData){
std::string path = "/sys/devices/system/cpu/cpu" + std::to_string(cpu.cpu_id) + "/cpufreq/scaling_cur_freq";
if ((fp = fopen(path.c_str(), "r"))){
int64_t temp;
if (fscanf(fp, "%" PRId64, &temp) != 1)
temp = 0;
cpu.mhz = temp / 1000;
fclose(fp);
scaling_freq = true;
} else {
scaling_freq = false;
break;
}
}
} else {
static std::ifstream cpuInfo(PROCCPUINFOFILE);
static std::string row;
size_t i = 0;
while (std::getline(cpuInfo, row) && i < m_cpuData.size()) {
if (row.find("MHz") != std::string::npos){
row = std::regex_replace(row, std::regex(R"([^0-9.])"), "");
if (!try_stoi(m_cpuData[i].mhz, row))
m_cpuData[i].mhz = 0;
i++;
}
}
}
m_cpuDataTotal.cpu_mhz = 0;
for (auto data : m_cpuData)
if (data.mhz > m_cpuDataTotal.cpu_mhz)
m_cpuDataTotal.cpu_mhz = data.mhz;
return true;
}
bool CPUStats::ReadcpuTempFile(int& temp) {
if (!m_cpuTempFile)
return false;
temp = read_as<int>(m_cpuTempFile).value_or(0) / 1000;
return temp == 0;
}
bool CPUStats::UpdateCpuTemp() {
if (gpus) {
for (auto gpu : gpus->available_gpus) {
if (gpu->is_apu()) {
m_cpuDataTotal.temp = gpu->metrics.apu_cpu_temp;
return true;
}
}
}
int temp = 0;
bool ret = ReadcpuTempFile(temp);
m_cpuDataTotal.temp = temp;
return ret;
}
static bool get_cpu_power_k10temp(CPUPowerData* cpuPowerData, float& power) {
CPUPowerData_k10temp* powerData_k10temp = (CPUPowerData_k10temp*)cpuPowerData;
if(powerData_k10temp->corePowerFile || powerData_k10temp->socPowerFile)
{
auto const corePower = read_as<int>(powerData_k10temp->corePowerFile);
auto const socPower = read_as<int>(powerData_k10temp->socPowerFile);
if (corePower and socPower) {
power = (corePower.value() + socPower.value()) / 1000000;
return true;
}
}
if (!powerData_k10temp->coreVoltageFile || !powerData_k10temp->coreCurrentFile || !powerData_k10temp->socVoltageFile || !powerData_k10temp->socCurrentFile)
return false;
auto const coreVoltage = read_as<int>(powerData_k10temp->coreVoltageFile);
if (not coreVoltage)
return false;
auto const coreCurrent = read_as<int>(powerData_k10temp->coreCurrentFile);
if (not coreCurrent)
return false;
auto const socVoltage = read_as<int>(powerData_k10temp->socVoltageFile);
if (not socVoltage)
return false;
auto const socCurrent = read_as<int>(powerData_k10temp->socCurrentFile);
if (not socCurrent)
return false;
power = (coreVoltage.value() * coreCurrent.value() +
socVoltage.value() * socCurrent.value()) / 1000000;
return true;
}
static bool get_cpu_power_zenpower(CPUPowerData* cpuPowerData, float& power) {
CPUPowerData_zenpower* powerData_zenpower = (CPUPowerData_zenpower*)cpuPowerData;
if (!powerData_zenpower->corePowerFile || !powerData_zenpower->socPowerFile)
return false;
auto const corePower = read_as<int>(powerData_zenpower->corePowerFile);
if (not corePower)
return false;
auto const socPower = read_as<int>(powerData_zenpower->socPowerFile);
if (not socPower)
return false;
power = (corePower.value() + socPower.value()) / 1000000;
return true;
}
static bool get_cpu_power_zenergy(CPUPowerData* cpuPowerData, float& power) {
CPUPowerData_zenergy* powerData_zenergy = (CPUPowerData_zenergy*)cpuPowerData;
if (!powerData_zenergy->energyCounterFile)
return false;
auto const energyCounterValue = read_as<uint64_t>(powerData_zenergy->energyCounterFile);
if (not energyCounterValue)
return false;
Clock::time_point now = Clock::now();
Clock::duration timeDiff = now - powerData_zenergy->lastCounterValueTime;
int64_t timeDiffMicro = std::chrono::duration_cast<std::chrono::microseconds>(timeDiff).count();
uint64_t energyCounterDiff = energyCounterValue.value() - powerData_zenergy->lastCounterValue;
if (powerData_zenergy->lastCounterValue > 0 && energyCounterValue > powerData_zenergy->lastCounterValue)
power = (float) energyCounterDiff / (float) timeDiffMicro;
powerData_zenergy->lastCounterValue = energyCounterValue.value();
powerData_zenergy->lastCounterValueTime = now;
return true;
}
static bool get_cpu_power_rapl(CPUPowerData* cpuPowerData, float& power) {
CPUPowerData_rapl* powerData_rapl = (CPUPowerData_rapl*)cpuPowerData;
if (!powerData_rapl->energyCounterFile)
return false;
auto const energyCounterValue = read_as<uint64_t>(powerData_rapl->energyCounterFile);
if (not energyCounterValue)
return false;
Clock::time_point now = Clock::now();
Clock::duration timeDiff = now - powerData_rapl->lastCounterValueTime;
int64_t timeDiffMicro = std::chrono::duration_cast<std::chrono::microseconds>(timeDiff).count();
uint64_t energyCounterDiff = energyCounterValue.value() - powerData_rapl->lastCounterValue;
if (powerData_rapl->lastCounterValue > 0 && energyCounterValue > powerData_rapl->lastCounterValue)
power = energyCounterDiff / timeDiffMicro;
powerData_rapl->lastCounterValue = energyCounterValue.value();
powerData_rapl->lastCounterValueTime = now;
return true;
}
static bool get_cpu_power_amdgpu(float& power) {
if (gpus)
for (auto gpu : gpus->available_gpus)
if (gpu->is_apu()) {
power = gpu->metrics.apu_cpu_power;
return true;
}
return false;
}
static bool get_cpu_power_xgene(CPUPowerData* cpuPowerData, float& power) {
CPUPowerData_xgene* powerData_xgene = (CPUPowerData_xgene*)cpuPowerData;
if (!powerData_xgene->powerFile)
return false;
auto const powerValue = read_as<uint64_t>(powerData_xgene->powerFile);
if (not powerValue)
return false;
power = (float) powerValue.value() / 1000000.0f;
return true;
}
bool CPUStats::UpdateCpuPower() {
InitCpuPowerData();
if(!m_cpuPowerData)
return false;
float power = 0;
switch(m_cpuPowerData->source) {
case CPU_POWER_K10TEMP:
if (!get_cpu_power_k10temp(m_cpuPowerData.get(), power)) return false;
break;
case CPU_POWER_ZENPOWER:
if (!get_cpu_power_zenpower(m_cpuPowerData.get(), power)) return false;
break;
case CPU_POWER_ZENERGY:
if (!get_cpu_power_zenergy(m_cpuPowerData.get(), power)) return false;
break;
case CPU_POWER_RAPL:
if (!get_cpu_power_rapl(m_cpuPowerData.get(), power)) return false;
break;
case CPU_POWER_AMDGPU:
if (!get_cpu_power_amdgpu(power)) return false;
break;
case CPU_POWER_XGENE:
if (!get_cpu_power_xgene(m_cpuPowerData.get(), power)) return false;
break;
default:
return false;
}
m_cpuDataTotal.power = power;
return true;
}
static bool find_input(const std::string& path, const char* input_prefix, std::string& input, const std::string& name)
{
auto files = ls(path.c_str(), input_prefix, LS_FILES);
for (auto& file : files) {
if (!ends_with(file, "_label"))
continue;
auto label = read_line(path + "/" + file);
if (label != name)
continue;
auto uscore = file.find_first_of("_");
if (uscore != std::string::npos) {
file.erase(uscore, std::string::npos);
input = path + "/" + file + "_input";
//9 characters should not overflow the 32-bit int
return std::stoi(read_line(input).substr(0, 9)) > 0;
}
}
return false;
}
static bool find_fallback_input(const std::string& path, const char* input_prefix, std::string& input)
{
auto files = ls(path.c_str(), input_prefix, LS_FILES);
if (!files.size())
return false;
std::sort(files.begin(), files.end());
for (auto& file : files) {
if (!ends_with(file, "_input"))
continue;
input = path + "/" + file;
SPDLOG_DEBUG("fallback cpu {} input: {}", input_prefix, input);
return true;
}
return false;
}
static void check_thermal_zones(std::string& path, std::string& input) {
std::string sysfs_thermal = "/sys/class/thermal/";
if (!fs::exists(sysfs_thermal))
return;
for (auto& d : fs::directory_iterator(sysfs_thermal)) {
if (d.path().filename().string().substr(0, 12) != "thermal_zone")
continue;
std::string type = read_line(d / "type");
if (type.substr(0, 6) != "cpuss-")
continue;
path = d.path();
input = d / "temp";
return;
}
}
bool CPUStats::GetCpuFile() {
if (m_cpuTempFile)
return true;
std::string name, path, input;
std::string hwmon = "/sys/class/hwmon/";
std::smatch match;
auto dirs = ls(hwmon.c_str());
for (auto& dir : dirs) {
path = hwmon + dir;
name = read_line(path + "/name");
SPDLOG_DEBUG("hwmon: sensor name: {}", name);
std::map<std::string, std::string> custom_sensor = get_params()->cpu_custom_temp_sensor;
if (!custom_sensor["hwmon_name"].empty() && !custom_sensor["hwmon_input"].empty()) {
if (name != custom_sensor["hwmon_name"])
continue;
find_fallback_input(path, custom_sensor["hwmon_input"].c_str(), input);
break;
} else if (name == "coretemp") {
find_input(path, "temp", input, "Package id 0");
break;
} else if ((name == "zenpower" || name == "k10temp")) {
if (!find_input(path, "temp", input, "Tdie"))
find_input(path, "temp", input, "Tctl");
break;
} else if (name == "atk0110") {
find_input(path, "temp", input, "CPU Temperature");
break;
} else if (name == "it8603") {
find_input(path, "temp", input, "temp1");
break;
} else if (starts_with(name, "cpuss0_")) {
find_fallback_input(path, "temp1", input);
break;
} else if (starts_with(name, "nct")) {
// Only break if nct module has TSI0_TEMP node
if (find_input(path, "temp", input, "TSI0_TEMP"))
break;
} else if (name == "asusec") {
// Only break if module has CPU node
if (find_input(path, "temp", input, "CPU"))
break;
} else if (name == "l_pcs") {
// E2K (Elbrus 2000) CPU temperature module
find_input(path, "temp", input, "Node 0 Max");
break;
} else if (std::regex_match(name, match, std::regex("cpu\\d*_thermal"))) {
find_fallback_input(path, "temp1", input);
break;
} else if (name == "apm_xgene") {
find_input(path, "temp", input, "SoC Temperature");
break;
} else {
path.clear();
}
}
if (path.empty()) {
try {
check_thermal_zones(path, input);
} catch (fs::filesystem_error& ex) {
SPDLOG_DEBUG("check_thermal_zones: {}", ex.what());
}
}
if (input.empty() || !file_exists(input)) {
SPDLOG_ERROR("Could not find cpu temp sensor location");
return false;
}
SPDLOG_INFO("hwmon: using input: {}", input);
m_cpuTempFile = fopen(input.c_str(), "r");
return true;
}
static CPUPowerData_k10temp* init_cpu_power_data_k10temp(const std::string path) {
auto powerData = std::make_unique<CPUPowerData_k10temp>();
std::string coreVoltageInput, coreCurrentInput;
std::string socVoltageInput, socCurrentInput;
std::string socPowerInput, corePowerInput;
if(find_input(path, "power", corePowerInput, "Pcore") && find_input(path, "power", socPowerInput, "Psoc")) {
powerData->corePowerFile = fopen(corePowerInput.c_str(), "r");
powerData->socPowerFile = fopen(socPowerInput.c_str(), "r");
SPDLOG_DEBUG("hwmon: using input: {}", corePowerInput);
SPDLOG_DEBUG("hwmon: using input: {}", socPowerInput);
return powerData.release();
}
if(!find_input(path, "in", coreVoltageInput, "Vcore")) return nullptr;
if(!find_input(path, "curr", coreCurrentInput, "Icore")) return nullptr;
if(!find_input(path, "in", socVoltageInput, "Vsoc")) return nullptr;
if(!find_input(path, "curr", socCurrentInput, "Isoc")) return nullptr;
SPDLOG_DEBUG("hwmon: using input: {}", coreVoltageInput);
SPDLOG_DEBUG("hwmon: using input: {}", coreCurrentInput);
SPDLOG_DEBUG("hwmon: using input: {}", socVoltageInput);
SPDLOG_DEBUG("hwmon: using input: {}", socCurrentInput);
powerData->coreVoltageFile = fopen(coreVoltageInput.c_str(), "r");
powerData->coreCurrentFile = fopen(coreCurrentInput.c_str(), "r");
powerData->socVoltageFile = fopen(socVoltageInput.c_str(), "r");
powerData->socCurrentFile = fopen(socCurrentInput.c_str(), "r");
return powerData.release();
}
static CPUPowerData_zenpower* init_cpu_power_data_zenpower(const std::string path) {
auto powerData = std::make_unique<CPUPowerData_zenpower>();
std::string corePowerInput, socPowerInput;
if(!find_input(path, "power", corePowerInput, "SVI2_P_Core")) return nullptr;
if(!find_input(path, "power", socPowerInput, "SVI2_P_SoC")) return nullptr;
SPDLOG_DEBUG("hwmon: using input: {}", corePowerInput);
SPDLOG_DEBUG("hwmon: using input: {}", socPowerInput);
powerData->corePowerFile = fopen(corePowerInput.c_str(), "r");
powerData->socPowerFile = fopen(socPowerInput.c_str(), "r");
return powerData.release();
}
static CPUPowerData_zenergy* init_cpu_power_data_zenergy(const std::string path) {
auto powerData = std::make_unique<CPUPowerData_zenergy>();
std::string energyCounterPath;
if(!find_input(path, "energy", energyCounterPath, "Esocket0")) return nullptr;
SPDLOG_DEBUG("hwmon: using input: {}", energyCounterPath);
powerData->energyCounterFile = fopen(energyCounterPath.c_str(), "r");
return powerData.release();
}
static CPUPowerData_rapl* init_cpu_power_data_rapl(const std::string path) {
auto powerData = std::make_unique<CPUPowerData_rapl>();
std::string energyCounterPath = path + "/energy_uj";
if (!file_exists(energyCounterPath)) return nullptr;
powerData->energyCounterFile = fopen(energyCounterPath.c_str(), "r");
if (!powerData->energyCounterFile) {
SPDLOG_DEBUG("Rapl: energy_uj is not accessible");
powerData->energyCounterFile = nullptr;
return nullptr;
}
return powerData.release();
}
static CPUPowerData_xgene* init_cpu_power_data_xgene(const std::string path) {
auto powerData = std::make_unique<CPUPowerData_xgene>();
std::string powerPath;
if(!find_input(path, "power", powerPath, "CPU power")) return nullptr;
SPDLOG_DEBUG("hwmon: using input: {}", powerPath);
powerData->powerFile = fopen(powerPath.c_str(), "r");
return powerData.release();
}
bool CPUStats::InitCpuPowerData() {
if(m_cpuPowerData != nullptr)
return true;
// only try to find a valid method 5 times
static int retries = 0;
if (retries >= 5)
return true;
retries++;
std::string name, path;
std::string hwmon = "/sys/class/hwmon/";
CPUPowerData* cpuPowerData = nullptr;
auto dirs = ls(hwmon.c_str());
for (auto& dir : dirs) {
path = hwmon + dir;
name = read_line(path + "/name");
SPDLOG_DEBUG("hwmon: sensor name: {}", name);
if (name == "k10temp") {
cpuPowerData = (CPUPowerData*)init_cpu_power_data_k10temp(path);
} else if (name == "zenpower") {
cpuPowerData = (CPUPowerData*)init_cpu_power_data_zenpower(path);
break;
} else if (name == "zenergy") {
cpuPowerData = (CPUPowerData*)init_cpu_power_data_zenergy(path);
break;
} else if (name == "apm_xgene") {
cpuPowerData = (CPUPowerData*)init_cpu_power_data_xgene(path);
break;
}
}
if (!cpuPowerData) {
if (gpus) {
for (auto gpu : gpus->available_gpus) {
if (gpu->vendor_id == 0x1002 && gpu->is_apu() && gpu->get_metrics().apu_cpu_power > 0) {
auto powerData = std::make_unique<CPUPowerData_amdgpu>();
cpuPowerData = (CPUPowerData*)powerData.release();
}
}
}
}
if (!cpuPowerData) {
std::string powercap = "/sys/class/powercap/";
auto powercap_dirs = ls(powercap.c_str());
for (auto& dir : powercap_dirs) {
path = powercap + dir;
name = read_line(path + "/name");
SPDLOG_DEBUG("powercap: name: {}", name);
if (name == "package-0") {
cpuPowerData = (CPUPowerData*)init_cpu_power_data_rapl(path);
break;
}
}
}
if(cpuPowerData == nullptr) {
SPDLOG_ERROR("Failed to initialize CPU power data");
return false;
}
m_cpuPowerData.reset(cpuPowerData);
return true;
}
void CPUStats::get_cpu_cores_types() {
#if defined(__x86_64__) || defined(__i386__)
std::ifstream cpuinfo(PROCCPUINFOFILE);
if (!cpuinfo.is_open()) {
SPDLOG_ERROR("failed to open {}", PROCCPUINFOFILE);
return;
}
std::string vendor = "unknown";
for (std::string line; std::getline(cpuinfo, line);) {
if (line.empty() || line.find(":") + 1 == line.length())
continue;
std::string key = line.substr(0, line.find(":") - 1);
std::string val = line.substr(key.length() + 3);
if (key == "vendor_id") {
vendor = val;
break;
}
}
SPDLOG_INFO("cpu vendor: {}", vendor);
if (vendor == "GenuineIntel")
get_cpu_cores_types_intel();
#endif
#if defined(__arm__) || defined(__aarch64__)
get_cpu_cores_types_arm();
#endif
}
void CPUStats::get_cpu_cores_types_intel() {
for (auto const& it : intel_cores) {
auto key = it.first;
auto file = it.second;
std::ifstream core_file(file);
if (!core_file.is_open()) {
SPDLOG_ERROR("failed to open core info file");
return;
}
std::string cpus;
std::getline(core_file, cpus);
std::regex rx("(\\d+)-(\\d+)");
std::smatch matches;
if (!std::regex_match(cpus, matches, rx) || matches.size() != 3)
continue;
int start = 0, end = 0;
try {
start = std::stoi(matches[1]);
end = std::stoi(matches[2]) + 1;
} catch (...) {
SPDLOG_ERROR("error parsing cpus \"{}\"", cpus);
}
for (int i = start; i < end; i++) {
for (size_t k = 0; k < m_cpuData.size(); k++) {
if (m_cpuData[k].cpu_id != i)
continue;
m_cpuData[k].label = key;
break;
}
}
}
}
void CPUStats::get_cpu_cores_types_arm() {
std::ifstream cpuinfo(PROCCPUINFOFILE);
if (!cpuinfo.is_open()) {
SPDLOG_ERROR("failed to open {}", PROCCPUINFOFILE);
return;
}
uint8_t cur_core = 0;
bool detected_first_core = false;
for (std::string line; std::getline(cpuinfo, line);) {
if (line.empty() || line.find(":") + 1 == line.length())
continue;
auto key = line.substr(0, line.find(":") - 1);
auto val = line.substr(key.length() + 3);
if (key != "CPU part")
continue;
if (detected_first_core)
cur_core += 1;
else
detected_first_core = true;
std::string core_type;
try {
core_type = arm_cores.at(val);
SPDLOG_INFO("found {} core", core_type);
}
catch(const std::out_of_range& ex) {
SPDLOG_WARN("unknown cpu part {}", val);
continue;
}
// just in case
for (size_t i = 0; i < m_cpuData.size(); i++) {
if (m_cpuData[i].cpu_id != cur_core)
continue;
m_cpuData[i].label = core_type;
}
}
}
CPUStats cpuStats;