Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit e8b4cdc

Browse files
committed
chore: system_info_utils
1 parent 1814040 commit e8b4cdc

File tree

4 files changed

+148
-136
lines changed

4 files changed

+148
-136
lines changed

engine/cli/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ add_executable(${TARGET_NAME} main.cc
8686
${CMAKE_CURRENT_SOURCE_DIR}/utils/download_progress.cc
8787
${CMAKE_CURRENT_SOURCE_DIR}/../utils/config_yaml_utils.cc
8888
${CMAKE_CURRENT_SOURCE_DIR}/../utils/file_manager_utils.cc
89-
${CMAKE_CURRENT_SOURCE_DIR}/../utils/curl_utils.cc
89+
${CMAKE_CURRENT_SOURCE_DIR}/../utils/curl_utils.cc
90+
${CMAKE_CURRENT_SOURCE_DIR}/../utils/system_info_utils.cc
9091
)
9192

9293
target_link_libraries(${TARGET_NAME} PRIVATE CLI11::CLI11)

engine/test/components/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ add_executable(${PROJECT_NAME}
1515
${CMAKE_CURRENT_SOURCE_DIR}/../../utils/config_yaml_utils.cc
1616
${CMAKE_CURRENT_SOURCE_DIR}/../../utils/file_manager_utils.cc
1717
${CMAKE_CURRENT_SOURCE_DIR}/../../utils/curl_utils.cc
18+
${CMAKE_CURRENT_SOURCE_DIR}/../../utils/system_info_utils.cc
1819
)
1920

2021
find_package(Drogon CONFIG REQUIRED)

engine/utils/system_info_utils.cc

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#include "system_info_utils.h"
2+
#include "utils/logging_utils.h"
3+
4+
namespace system_info_utils {
5+
std::pair<std::string, std::string> GetDriverAndCudaVersion() {
6+
if (!IsNvidiaSmiAvailable()) {
7+
CTL_INF("nvidia-smi is not available!");
8+
return {};
9+
}
10+
try {
11+
std::string driver_version;
12+
std::string cuda_version;
13+
CommandExecutor cmd("nvidia-smi");
14+
auto output = cmd.execute();
15+
16+
const std::regex driver_version_reg(kDriverVersionRegex);
17+
std::smatch driver_match;
18+
19+
if (std::regex_search(output, driver_match, driver_version_reg)) {
20+
LOG_INFO << "Gpu Driver Version: " << driver_match[1].str();
21+
driver_version = driver_match[1].str();
22+
} else {
23+
LOG_ERROR << "Gpu Driver not found!";
24+
return {};
25+
}
26+
27+
const std::regex cuda_version_reg(kCudaVersionRegex);
28+
std::smatch cuda_match;
29+
30+
if (std::regex_search(output, cuda_match, cuda_version_reg)) {
31+
LOG_INFO << "CUDA Version: " << cuda_match[1].str();
32+
cuda_version = cuda_match[1].str();
33+
} else {
34+
LOG_ERROR << "CUDA Version not found!";
35+
return {};
36+
}
37+
return std::pair(driver_version, cuda_version);
38+
} catch (const std::exception& e) {
39+
LOG_ERROR << "Error: " << e.what();
40+
return {};
41+
}
42+
}
43+
44+
std::vector<GpuInfo> GetGpuInfoListVulkan() {
45+
std::vector<GpuInfo> gpuInfoList;
46+
47+
try {
48+
// NOTE: current ly we don't have logic to download vulkaninfoSDK
49+
#ifdef _WIN32
50+
CommandExecutor cmd("vulkaninfoSDK.exe --summary");
51+
#else
52+
CommandExecutor cmd("vulkaninfoSDK --summary");
53+
#endif
54+
auto output = cmd.execute();
55+
56+
// Regular expression patterns to match each field
57+
std::regex gpu_block_reg(R"(GPU(\d+):)");
58+
std::regex field_pattern(R"(\s*(\w+)\s*=\s*(.*))");
59+
60+
std::sregex_iterator iter(output.begin(), output.end(), gpu_block_reg);
61+
std::sregex_iterator end;
62+
63+
while (iter != end) {
64+
GpuInfo gpuInfo;
65+
66+
// Extract GPU ID from the GPU block pattern (e.g., GPU0 -> id = "0")
67+
gpuInfo.id = (*iter)[1].str();
68+
69+
auto gpu_start_pos = iter->position(0) + iter->length(0);
70+
auto gpu_end_pos = std::next(iter) != end ? std::next(iter)->position(0)
71+
: std::string::npos;
72+
std::string gpu_block =
73+
output.substr(gpu_start_pos, gpu_end_pos - gpu_start_pos);
74+
75+
std::sregex_iterator field_iter(gpu_block.begin(), gpu_block.end(),
76+
field_pattern);
77+
78+
while (field_iter != end) {
79+
std::string key = (*field_iter)[1].str();
80+
std::string value = (*field_iter)[2].str();
81+
82+
if (key == "deviceName")
83+
gpuInfo.name = value;
84+
else if (key == "apiVersion")
85+
gpuInfo.compute_cap = value;
86+
87+
gpuInfo.vram_total = ""; // not available
88+
gpuInfo.arch = GetGpuArch(gpuInfo.name);
89+
90+
++field_iter;
91+
}
92+
93+
gpuInfoList.push_back(gpuInfo);
94+
++iter;
95+
}
96+
} catch (const std::exception& e) {
97+
LOG_ERROR << "Error: " << e.what();
98+
}
99+
100+
return gpuInfoList;
101+
}
102+
103+
std::vector<GpuInfo> GetGpuInfoList() {
104+
std::vector<GpuInfo> gpuInfoList;
105+
if (!IsNvidiaSmiAvailable())
106+
return gpuInfoList;
107+
try {
108+
auto [driver_version, cuda_version] = GetDriverAndCudaVersion();
109+
if (driver_version.empty() || cuda_version.empty())
110+
return gpuInfoList;
111+
112+
CommandExecutor cmd(kGpuQueryCommand);
113+
auto output = cmd.execute();
114+
115+
const std::regex gpu_info_reg(kGpuInfoRegex);
116+
std::smatch match;
117+
std::string::const_iterator search_start(output.cbegin());
118+
119+
while (
120+
std::regex_search(search_start, output.cend(), match, gpu_info_reg)) {
121+
GpuInfo gpuInfo = {
122+
match[1].str(), // id
123+
match[2].str(), // vram_total
124+
match[3].str(), // vram_free
125+
match[4].str(), // name
126+
GetGpuArch(match[4].str()), // arch
127+
driver_version, // driver_version
128+
cuda_version, // cuda_driver_version
129+
match[5].str(), // compute_cap
130+
match[6].str() // uuid
131+
};
132+
gpuInfoList.push_back(gpuInfo);
133+
search_start = match.suffix().first;
134+
}
135+
} catch (const std::exception& e) {
136+
std::cerr << "Error: " << e.what() << std::endl;
137+
}
138+
139+
return gpuInfoList;
140+
}
141+
} // namespace system_info_utils

engine/utils/system_info_utils.h

Lines changed: 4 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <vector>
99
#include "utils/command_executor.h"
1010
#include "utils/engine_constants.h"
11-
#include "utils/logging_utils.h"
11+
1212
#ifdef _WIN32
1313
#include <windows.h>
1414
#endif
@@ -101,44 +101,7 @@ inline bool IsNvidiaSmiAvailable() {
101101
#endif
102102
}
103103

104-
inline std::pair<std::string, std::string> GetDriverAndCudaVersion() {
105-
if (!IsNvidiaSmiAvailable()) {
106-
CTL_INF("nvidia-smi is not available!");
107-
return {};
108-
}
109-
try {
110-
std::string driver_version;
111-
std::string cuda_version;
112-
CommandExecutor cmd("nvidia-smi");
113-
auto output = cmd.execute();
114-
115-
const std::regex driver_version_reg(kDriverVersionRegex);
116-
std::smatch driver_match;
117-
118-
if (std::regex_search(output, driver_match, driver_version_reg)) {
119-
LOG_INFO << "Gpu Driver Version: " << driver_match[1].str();
120-
driver_version = driver_match[1].str();
121-
} else {
122-
LOG_ERROR << "Gpu Driver not found!";
123-
return {};
124-
}
125-
126-
const std::regex cuda_version_reg(kCudaVersionRegex);
127-
std::smatch cuda_match;
128-
129-
if (std::regex_search(output, cuda_match, cuda_version_reg)) {
130-
LOG_INFO << "CUDA Version: " << cuda_match[1].str();
131-
cuda_version = cuda_match[1].str();
132-
} else {
133-
LOG_ERROR << "CUDA Version not found!";
134-
return {};
135-
}
136-
return std::pair(driver_version, cuda_version);
137-
} catch (const std::exception& e) {
138-
LOG_ERROR << "Error: " << e.what();
139-
return {};
140-
}
141-
}
104+
std::pair<std::string, std::string> GetDriverAndCudaVersion();
142105

143106
struct GpuInfo {
144107
std::string id;
@@ -153,101 +116,7 @@ struct GpuInfo {
153116
std::string uuid;
154117
};
155118

156-
inline std::vector<GpuInfo> GetGpuInfoListVulkan() {
157-
std::vector<GpuInfo> gpuInfoList;
158-
159-
try {
160-
// NOTE: current ly we don't have logic to download vulkaninfoSDK
161-
#ifdef _WIN32
162-
CommandExecutor cmd("vulkaninfoSDK.exe --summary");
163-
#else
164-
CommandExecutor cmd("vulkaninfoSDK --summary");
165-
#endif
166-
auto output = cmd.execute();
167-
168-
// Regular expression patterns to match each field
169-
std::regex gpu_block_reg(R"(GPU(\d+):)");
170-
std::regex field_pattern(R"(\s*(\w+)\s*=\s*(.*))");
171-
172-
std::sregex_iterator iter(output.begin(), output.end(), gpu_block_reg);
173-
std::sregex_iterator end;
174-
175-
while (iter != end) {
176-
GpuInfo gpuInfo;
177-
178-
// Extract GPU ID from the GPU block pattern (e.g., GPU0 -> id = "0")
179-
gpuInfo.id = (*iter)[1].str();
180-
181-
auto gpu_start_pos = iter->position(0) + iter->length(0);
182-
auto gpu_end_pos = std::next(iter) != end ? std::next(iter)->position(0)
183-
: std::string::npos;
184-
std::string gpu_block =
185-
output.substr(gpu_start_pos, gpu_end_pos - gpu_start_pos);
119+
std::vector<GpuInfo> GetGpuInfoListVulkan();
186120

187-
std::sregex_iterator field_iter(gpu_block.begin(), gpu_block.end(),
188-
field_pattern);
189-
190-
while (field_iter != end) {
191-
std::string key = (*field_iter)[1].str();
192-
std::string value = (*field_iter)[2].str();
193-
194-
if (key == "deviceName")
195-
gpuInfo.name = value;
196-
else if (key == "apiVersion")
197-
gpuInfo.compute_cap = value;
198-
199-
gpuInfo.vram_total = ""; // not available
200-
gpuInfo.arch = GetGpuArch(gpuInfo.name);
201-
202-
++field_iter;
203-
}
204-
205-
gpuInfoList.push_back(gpuInfo);
206-
++iter;
207-
}
208-
} catch (const std::exception& e) {
209-
LOG_ERROR << "Error: " << e.what();
210-
}
211-
212-
return gpuInfoList;
213-
}
214-
215-
inline std::vector<GpuInfo> GetGpuInfoList() {
216-
std::vector<GpuInfo> gpuInfoList;
217-
if (!IsNvidiaSmiAvailable())
218-
return gpuInfoList;
219-
try {
220-
auto [driver_version, cuda_version] = GetDriverAndCudaVersion();
221-
if (driver_version.empty() || cuda_version.empty())
222-
return gpuInfoList;
223-
224-
CommandExecutor cmd(kGpuQueryCommand);
225-
auto output = cmd.execute();
226-
227-
const std::regex gpu_info_reg(kGpuInfoRegex);
228-
std::smatch match;
229-
std::string::const_iterator search_start(output.cbegin());
230-
231-
while (
232-
std::regex_search(search_start, output.cend(), match, gpu_info_reg)) {
233-
GpuInfo gpuInfo = {
234-
match[1].str(), // id
235-
match[2].str(), // vram_total
236-
match[3].str(), // vram_free
237-
match[4].str(), // name
238-
GetGpuArch(match[4].str()), // arch
239-
driver_version, // driver_version
240-
cuda_version, // cuda_driver_version
241-
match[5].str(), // compute_cap
242-
match[6].str() // uuid
243-
};
244-
gpuInfoList.push_back(gpuInfo);
245-
search_start = match.suffix().first;
246-
}
247-
} catch (const std::exception& e) {
248-
std::cerr << "Error: " << e.what() << std::endl;
249-
}
250-
251-
return gpuInfoList;
252-
}
121+
std::vector<GpuInfo> GetGpuInfoList();
253122
} // namespace system_info_utils

0 commit comments

Comments
 (0)