-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
109 lines (80 loc) · 2.63 KB
/
main.cpp
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
#include <ios>
#include <fstream>
#include <iostream>
#include <string>
#include <filesystem>
#include <benchmark/benchmark.h>
std::string GetCpuName() {
std::string line, modelName;
if (!std::filesystem::exists("/proc/cpuinfo")) {
return "Unknown";
}
std::ifstream cpuinfo("/proc/cpuinfo", std::ios::binary);
while (std::getline(cpuinfo, line)) {
if (line.starts_with("model name")) {
modelName = line.substr(line.find(':') + 2);
break;
}
}
cpuinfo.close();
return modelName;
}
std::int32_t GetCpuCoreCount() {
std::string line, lastProcessorNumber;
std::int32_t coreCount;
if (!std::filesystem::exists("/proc/cpuinfo")) {
return -1;
}
std::ifstream cpuinfo("/proc/cpuinfo", std::ios::binary);
while (std::getline(cpuinfo, line)) {
if (line.starts_with("processor")) {
lastProcessorNumber = line.substr(line.find(':') + 2);
}
}
cpuinfo.close();
coreCount = std::stoi(lastProcessorNumber) + 1;
return coreCount;
}
std::int32_t GetCpuFrequency() {
std::string line, cpuMhz;
if (!std::filesystem::exists("/proc/cpuinfo")) {
return -1;
}
std::ifstream cpuinfo("/proc/cpuinfo", std::ios::binary);
while (std::getline(cpuinfo, line)) {
if (line.starts_with("cpu MHz")) {
cpuMhz = line.substr(line.find(':') + 2);
}
}
cpuinfo.close();
return std::stoi(cpuMhz);
}
std::int32_t GetNumaNodeCount() {
std::int32_t numaNodeCount = 0;
if (!std::filesystem::exists("/sys/devices/system/node/")) {
return -1;
}
for (const auto & file : std::filesystem::directory_iterator("/sys/devices/system/node/")) {
if (!file.is_directory() || !file.path().filename().string().starts_with("node")) {
continue;
}
std::int32_t currentNumaNode = std::stoi(file.path().filename().string().substr(4));
if (currentNumaNode <= numaNodeCount) {
continue;
}
numaNodeCount = currentNumaNode;
}
return numaNodeCount + 1;
};
int main(int argc, char** argv) {
::benchmark::Initialize(&argc, argv);
if (::benchmark::ReportUnrecognizedArguments(argc, argv)) {
return 1;
}
::benchmark::AddCustomContext("CPU Name", GetCpuName());
::benchmark::AddCustomContext("CPU Core Count", std::to_string(GetCpuCoreCount()));
::benchmark::AddCustomContext("CPU Frequency", std::to_string(GetCpuFrequency()));
::benchmark::AddCustomContext("NUMA Node Count", std::to_string(GetNumaNodeCount()));
::benchmark::RunSpecifiedBenchmarks();
return 0;
}