forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem_info_utils.cc
More file actions
146 lines (123 loc) · 4.68 KB
/
system_info_utils.cc
File metadata and controls
146 lines (123 loc) · 4.68 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
#include "system_info_utils.h"
#include "utils/logging_utils.h"
namespace system_info_utils {
std::pair<std::string, std::string> GetDriverAndCudaVersion() {
if (!IsNvidiaSmiAvailable()) {
CTL_INF("nvidia-smi is not available!");
return {};
}
try {
std::string driver_version;
std::string cuda_version;
CommandExecutor cmd("nvidia-smi");
auto output = cmd.execute();
const std::regex driver_version_reg(kDriverVersionRegex);
std::smatch driver_match;
if (std::regex_search(output, driver_match, driver_version_reg)) {
LOG_INFO << "Gpu Driver Version: " << driver_match[1].str();
driver_version = driver_match[1].str();
} else {
LOG_ERROR << "Gpu Driver not found!";
return {};
}
const std::regex cuda_version_reg(kCudaVersionRegex);
std::smatch cuda_match;
if (std::regex_search(output, cuda_match, cuda_version_reg)) {
LOG_INFO << "CUDA Version: " << cuda_match[1].str();
cuda_version = cuda_match[1].str();
} else {
LOG_ERROR << "CUDA Version not found!";
return {};
}
return std::pair(driver_version, cuda_version);
} catch (const std::exception& e) {
LOG_ERROR << "Error: " << e.what();
return {};
}
}
std::vector<GpuInfo> GetGpuInfoListVulkan() {
std::vector<GpuInfo> gpuInfoList;
try {
// NOTE: current ly we don't have logic to download vulkaninfoSDK
#ifdef _WIN32
CommandExecutor cmd("vulkaninfoSDK.exe --summary");
#else
CommandExecutor cmd("vulkaninfoSDK --summary");
#endif
auto output = cmd.execute();
// Regular expression patterns to match each field
std::regex gpu_block_reg(R"(GPU(\d+):)");
std::regex field_pattern(R"(\s*(\w+)\s*=\s*(.*))");
std::sregex_iterator iter(output.begin(), output.end(), gpu_block_reg);
std::sregex_iterator end;
while (iter != end) {
GpuInfo gpuInfo;
// Extract GPU ID from the GPU block pattern (e.g., GPU0 -> id = "0")
gpuInfo.id = (*iter)[1].str();
auto gpu_start_pos = iter->position(0) + iter->length(0);
auto gpu_end_pos = std::next(iter) != end ? std::next(iter)->position(0)
: std::string::npos;
std::string gpu_block =
output.substr(gpu_start_pos, gpu_end_pos - gpu_start_pos);
std::sregex_iterator field_iter(gpu_block.begin(), gpu_block.end(),
field_pattern);
while (field_iter != end) {
std::string key = (*field_iter)[1].str();
std::string value = (*field_iter)[2].str();
if (key == "deviceName")
gpuInfo.name = value;
else if (key == "apiVersion")
gpuInfo.compute_cap = value;
gpuInfo.vram_total = ""; // not available
gpuInfo.arch = GetGpuArch(gpuInfo.name);
++field_iter;
}
gpuInfoList.push_back(gpuInfo);
++iter;
}
} catch (const std::exception& e) {
LOG_ERROR << "Error: " << e.what();
}
return gpuInfoList;
}
std::vector<GpuInfo> GetGpuInfoList() {
std::vector<GpuInfo> gpuInfoList;
if (!IsNvidiaSmiAvailable())
return gpuInfoList;
try {
auto [driver_version, cuda_version] = GetDriverAndCudaVersion();
if (driver_version.empty() || cuda_version.empty())
return gpuInfoList;
bool need_fallback = false;
CommandExecutor cmd(kGpuQueryCommand);
auto output = cmd.execute();
if (output.find("NVIDIA") == std::string::npos) {
need_fallback = true;
output = CommandExecutor(kGpuQueryCommandFb).execute();
}
std::string rg = need_fallback ? kGpuInfoRegexFb : kGpuInfoRegex;
const std::regex gpu_info_reg(rg);
std::smatch match;
std::string::const_iterator search_start(output.cbegin());
int rg_count = need_fallback ? 5 : 6;
while (
std::regex_search(search_start, output.cend(), match, gpu_info_reg)) {
GpuInfo gpuInfo = {match[1].str(), // id
match[2].str(), // vram_total
match[3].str(), // vram_free
match[4].str(), // name
GetGpuArch(match[4].str()), // arch
driver_version, // driver_version
cuda_version, // cuda_driver_version
need_fallback ? "0" : match[5].str(), // compute_cap
match[rg_count].str(), // uuid
"NVIDIA"};
gpuInfoList.push_back(gpuInfo);
search_start = match.suffix().first;
}
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return gpuInfoList;
}
} // namespace system_info_utils