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

Commit 4886edb

Browse files
committed
fix: comments
1 parent 33676f2 commit 4886edb

File tree

16 files changed

+470
-255
lines changed

16 files changed

+470
-255
lines changed

engine/cli/commands/hardware_list_cmd.cc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ bool HardwareListCmd::Exec(const std::string& host, int port,
5757
table.add_row(header);
5858
table.format().font_color(Color::green);
5959
std::vector<std::string> row = {"1"};
60-
hardware::CPU cpu = hardware::cpu::FromJson(result.value()["cpu"]);
60+
cortex::hw::CPU cpu = cortex::hw::cpu::FromJson(result.value()["cpu"]);
6161
row.emplace_back(cpu.arch);
6262
row.emplace_back(std::to_string(cpu.cores));
6363
row.emplace_back(cpu.model);
@@ -80,7 +80,7 @@ bool HardwareListCmd::Exec(const std::string& host, int port,
8080
table.add_row(header);
8181
table.format().font_color(Color::green);
8282
std::vector<std::string> row = {"1"};
83-
hardware::OS os = hardware::os::FromJson(result.value()["os"]);
83+
cortex::hw::OS os = cortex::hw::os::FromJson(result.value()["os"]);
8484
row.emplace_back(os.version);
8585
row.emplace_back(os.name);
8686
table.add_row({row.begin(), row.end()});
@@ -98,7 +98,7 @@ bool HardwareListCmd::Exec(const std::string& host, int port,
9898
table.add_row(header);
9999
table.format().font_color(Color::green);
100100
std::vector<std::string> row = {"1"};
101-
hardware::Memory m = hardware::memory::FromJson(result.value()["ram"]);
101+
cortex::hw::Memory m = cortex::hw::memory::FromJson(result.value()["ram"]);
102102
row.emplace_back(std::to_string(m.total_MiB));
103103
row.emplace_back(std::to_string(m.available_MiB));
104104
table.add_row({row.begin(), row.end()});
@@ -120,8 +120,8 @@ bool HardwareListCmd::Exec(const std::string& host, int port,
120120
table.format().font_color(Color::green);
121121
int count = 1;
122122

123-
std::vector<hardware::GPU> gpus =
124-
hardware::gpu::FromJson(result.value()["gpus"]);
123+
std::vector<cortex::hw::GPU> gpus =
124+
cortex::hw::gpu::FromJson(result.value()["gpus"]);
125125
for (auto const& gpu : gpus) {
126126
std::vector<std::string> row = {std::to_string(count)};
127127
row.emplace_back(gpu.id);
@@ -130,9 +130,9 @@ bool HardwareListCmd::Exec(const std::string& host, int port,
130130
row.emplace_back(std::to_string(gpu.total_vram));
131131
row.emplace_back(std::to_string(gpu.free_vram));
132132
row.emplace_back(
133-
std::get<hardware::NvidiaAddInfo>(gpu.add_info).driver_version);
133+
std::get<cortex::hw::NvidiaAddInfo>(gpu.add_info).driver_version);
134134
row.emplace_back(
135-
std::get<hardware::NvidiaAddInfo>(gpu.add_info).compute_cap);
135+
std::get<cortex::hw::NvidiaAddInfo>(gpu.add_info).compute_cap);
136136
row.emplace_back(gpu.is_activated ? "Yes" : "No");
137137
table.add_row({row.begin(), row.end()});
138138
}
@@ -151,8 +151,8 @@ bool HardwareListCmd::Exec(const std::string& host, int port,
151151
table.add_row(header);
152152
table.format().font_color(Color::green);
153153
std::vector<std::string> row = {"1"};
154-
hardware::StorageInfo si =
155-
hardware::storage::FromJson(result.value()["storage"]);
154+
cortex::hw::StorageInfo si =
155+
cortex::hw::storage::FromJson(result.value()["storage"]);
156156
row.emplace_back(std::to_string(si.total));
157157
row.emplace_back(std::to_string(si.available));
158158
table.add_row({row.begin(), row.end()});
@@ -170,7 +170,7 @@ bool HardwareListCmd::Exec(const std::string& host, int port,
170170
table.add_row(header);
171171
table.format().font_color(Color::green);
172172
std::vector<std::string> row = {"1"};
173-
hardware::PowerInfo pi = hardware::power::FromJson(result.value()["power"]);
173+
cortex::hw::PowerInfo pi = cortex::hw::power::FromJson(result.value()["power"]);
174174
row.emplace_back(std::to_string(pi.battery_life));
175175
row.emplace_back(pi.charging_status);
176176
row.emplace_back(pi.is_power_saving ? "Yes" : "No");

engine/common/hardware_common.h

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
#pragma once
2+
#include <json/json.h>
3+
#include <string>
4+
#include <variant>
5+
#include <vector>
6+
#include <assert.h>
7+
8+
namespace cortex::hw {
9+
10+
namespace {
11+
inline constexpr std::string_view GetArch() {
12+
#if defined(__i386__) || defined(__x86_64__) || defined(__amd64__) || \
13+
defined(__amd64) || defined(__x86_64) || defined(_M_AMD64)
14+
return "amd64";
15+
#elif defined(__arm__) || defined(__arm) || defined(__arm64__) || \
16+
defined(__aarch64__) || defined(__thumb__) || \
17+
defined(__TARGET_ARCH_ARM) || defined(__TARGET_ARCH_THUMB) || \
18+
defined(_ARM) || defined(_M_ARM) || defined(_M_ARMT)
19+
return "arm64";
20+
#else
21+
return "Unsupported";
22+
#endif
23+
}
24+
} // namespace
25+
struct CPU {
26+
int cores;
27+
std::string arch;
28+
std::string model;
29+
std::vector<std::string> instructions;
30+
};
31+
32+
inline Json::Value ToJson(const CPU& cpu) {
33+
Json::Value res;
34+
res["arch"] = cpu.arch;
35+
res["cores"] = cpu.cores;
36+
res["model"] = cpu.model;
37+
Json::Value insts(Json::arrayValue);
38+
for (auto const& i : cpu.instructions) {
39+
insts.append(i);
40+
}
41+
res["instructions"] = insts;
42+
return res;
43+
}
44+
45+
namespace cpu {
46+
inline CPU FromJson(const Json::Value& root) {
47+
int cores = root["cores"].asInt();
48+
std::string arch = root["arch"].asString();
49+
std::string model = root["model"].asString();
50+
std::vector<std::string> insts;
51+
for (auto const& i : root["instructions"]) {
52+
insts.emplace_back(i.asString());
53+
}
54+
return {.cores = cores, .arch = arch, .model = model, .instructions = insts};
55+
}
56+
} // namespace cpu
57+
58+
// This can be different depends on gpu types
59+
struct NvidiaAddInfo {
60+
std::string driver_version;
61+
std::string compute_cap;
62+
};
63+
struct AmdAddInfo {};
64+
using GPUAddInfo = std::variant<NvidiaAddInfo, AmdAddInfo>;
65+
struct GPU {
66+
std::string id;
67+
std::string name;
68+
std::string version;
69+
GPUAddInfo add_info;
70+
int64_t free_vram;
71+
int64_t total_vram;
72+
std::string uuid;
73+
bool is_activated = true;
74+
};
75+
76+
inline Json::Value ToJson(const std::vector<GPU>& gpus) {
77+
Json::Value res(Json::arrayValue);
78+
for (size_t i = 0; i < gpus.size(); i++) {
79+
Json::Value gpu;
80+
gpu["id"] = std::to_string(i);
81+
gpu["name"] = gpus[i].name;
82+
gpu["version"] = gpus[i].version;
83+
Json::Value add_info;
84+
if (std::holds_alternative<NvidiaAddInfo>(gpus[i].add_info)) {
85+
auto& v = std::get<NvidiaAddInfo>(gpus[i].add_info);
86+
add_info["driver_version"] = v.driver_version;
87+
add_info["compute_cap"] = v.compute_cap;
88+
}
89+
gpu["additional_information"] = add_info;
90+
91+
gpu["free_vram"] = gpus[i].free_vram;
92+
gpu["total_vram"] = gpus[i].total_vram;
93+
gpu["uuid"] = gpus[i].uuid;
94+
gpu["activated"] = gpus[i].is_activated;
95+
res.append(gpu);
96+
}
97+
return res;
98+
}
99+
100+
namespace gpu {
101+
inline std::vector<GPU> FromJson(const Json::Value& root) {
102+
assert(root.isArray());
103+
std::vector<GPU> res;
104+
for (auto const& gpu_json : root) {
105+
GPU gpu;
106+
gpu.id = gpu_json["id"].asString();
107+
gpu.name = gpu_json["name"].asString();
108+
gpu.version = gpu_json["version"].asString();
109+
NvidiaAddInfo add_inf;
110+
add_inf.driver_version =
111+
gpu_json["additional_information"]["driver_version"].asString();
112+
add_inf.compute_cap =
113+
gpu_json["additional_information"]["compute_cap"].asString();
114+
gpu.add_info = add_inf;
115+
gpu.free_vram = gpu_json["free_vram"].asInt64();
116+
gpu.total_vram = gpu_json["total_vram"].asInt64();
117+
gpu.uuid = gpu_json["uuid"].asString();
118+
gpu.is_activated = gpu_json["activated"].asBool();
119+
res.emplace_back(gpu);
120+
}
121+
return res;
122+
}
123+
} // namespace gpu
124+
125+
struct OS {
126+
std::string name;
127+
std::string version;
128+
std::string arch;
129+
};
130+
131+
inline Json::Value ToJson(const OS& os) {
132+
Json::Value res;
133+
res["version"] = os.version;
134+
res["name"] = os.name;
135+
return res;
136+
}
137+
138+
namespace os {
139+
inline OS FromJson(const Json::Value& root) {
140+
return {.name = root["name"].asString(),
141+
.version = root["version"].asString()};
142+
}
143+
} // namespace os
144+
145+
146+
struct PowerInfo {
147+
std::string charging_status;
148+
int battery_life;
149+
bool is_power_saving;
150+
};
151+
152+
inline Json::Value ToJson(const PowerInfo& pi) {
153+
Json::Value res;
154+
res["charging_status"] = pi.charging_status;
155+
res["battery_life"] = pi.battery_life;
156+
res["is_power_saving"] = pi.is_power_saving;
157+
return res;
158+
}
159+
160+
namespace power {
161+
inline PowerInfo FromJson(const Json::Value& root) {
162+
return {.charging_status = root["charging_status"].asString(),
163+
.battery_life = root["battery_life"].asInt(),
164+
.is_power_saving = root["is_power_saving"].asBool()};
165+
}
166+
} // namespace power
167+
168+
169+
namespace {
170+
int64_t ByteToMiB(int64_t b) {
171+
return b / 1024 / 1024;
172+
}
173+
} // namespace
174+
struct Memory {
175+
int64_t total_MiB;
176+
int64_t available_MiB;
177+
std::string type;
178+
};
179+
180+
inline Json::Value ToJson(const Memory& m) {
181+
Json::Value res;
182+
res["total"] = m.total_MiB;
183+
res["available"] = m.available_MiB;
184+
res["type"] = m.type;
185+
return res;
186+
}
187+
188+
namespace memory {
189+
inline Memory FromJson(const Json::Value& root) {
190+
return {.total_MiB = root["total"].asInt64(),
191+
.available_MiB = root["available"].asInt64(),
192+
.type = root["type"].asString()};
193+
}
194+
} // namespace memory
195+
196+
struct StorageInfo {
197+
std::string type;
198+
int64_t total;
199+
int64_t available;
200+
};
201+
202+
inline Json::Value ToJson(const StorageInfo& si) {
203+
Json::Value res;
204+
res["total"] = si.total;
205+
res["available"] = si.available;
206+
res["type"] = si.type;
207+
return res;
208+
}
209+
210+
namespace storage {
211+
inline StorageInfo FromJson(const Json::Value& root) {
212+
return {.type = root["type"].asString(),
213+
.total = root["total"].asInt64(),
214+
.available = root["available"].asInt64()};
215+
}
216+
} // namespace storage
217+
}

engine/controllers/hardware.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ void Hardware::GetHardwareInfo(
99
std::function<void(const HttpResponsePtr&)>&& callback) {
1010
auto hw_inf = hw_svc_->GetHardwareInfo();
1111
Json::Value ret;
12-
ret["cpu"] = hardware::ToJson(hw_inf.cpu);
13-
ret["os"] = hardware::ToJson(hw_inf.os);
14-
ret["ram"] = hardware::ToJson(hw_inf.ram);
15-
ret["storage"] = hardware::ToJson(hw_inf.storage);
16-
ret["gpus"] = hardware::ToJson(hw_inf.gpus);
17-
ret["power"] = hardware::ToJson(hw_inf.power);
12+
ret["cpu"] = cortex::hw::ToJson(hw_inf.cpu);
13+
ret["os"] = cortex::hw::ToJson(hw_inf.os);
14+
ret["ram"] = cortex::hw::ToJson(hw_inf.ram);
15+
ret["storage"] = cortex::hw::ToJson(hw_inf.storage);
16+
ret["gpus"] = cortex::hw::ToJson(hw_inf.gpus);
17+
ret["power"] = cortex::hw::ToJson(hw_inf.power);
1818
auto resp = cortex_utils::CreateCortexHttpJsonResponse(ret);
1919
resp->setStatusCode(k200OK);
2020
callback(resp);

engine/e2e-test/test_cli_engine_list.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class TestCliEngineList:
88

99
@pytest.fixture(autouse=True)
1010
def setup_and_teardown(self):
11-
# Setup
11+
# Setup TODO(sang) should make all the test isolate
1212
stop_server()
1313
success = start_server()
1414
if not success:

engine/services/hardware_service.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ bool TryConnectToServer(const std::string& host, int port) {
3535
HardwareInfo HardwareService::GetHardwareInfo() {
3636
// append active state
3737
cortex::db::Hardwares hw_db;
38-
auto gpus = hardware::GetGPUInfo();
38+
auto gpus = cortex::hw::GetGPUInfo();
3939
auto res = hw_db.LoadHardwareList();
4040
if (res.has_value()) {
4141
// Only a few elements, brute-force is enough
@@ -48,12 +48,12 @@ HardwareInfo HardwareService::GetHardwareInfo() {
4848
};
4949
}
5050

51-
return HardwareInfo{.cpu = hardware::GetCPUInfo(),
52-
.os = hardware::GetOSInfo(),
53-
.ram = hardware::GetMemoryInfo(),
54-
.storage = hardware::GetStorageInfo(),
51+
return HardwareInfo{.cpu = cortex::hw::GetCPUInfo(),
52+
.os = cortex::hw::GetOSInfo(),
53+
.ram = cortex::hw::GetMemoryInfo(),
54+
.storage = cortex::hw::GetStorageInfo(),
5555
.gpus = gpus,
56-
.power = hardware::GetPowerInfo()};
56+
.power = cortex::hw::GetPowerInfo()};
5757
}
5858

5959
bool HardwareService::Restart(const std::string& host, int port) {
@@ -227,7 +227,7 @@ bool HardwareService::SetActivateHardwareConfig(
227227

228228
void HardwareService::UpdateHardwareInfos() {
229229
using HwEntry = cortex::db::HardwareEntry;
230-
auto gpus = hardware::GetGPUInfo();
230+
auto gpus = cortex::hw::GetGPUInfo();
231231
cortex::db::Hardwares hw_db;
232232
auto b = hw_db.LoadHardwareList();
233233
std::vector<int> activated_gpu_bf;

engine/services/hardware_service.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
namespace services {
1515

1616
struct HardwareInfo {
17-
hardware::CPU cpu;
18-
hardware::OS os;
19-
hardware::Memory ram;
20-
hardware::StorageInfo storage;
21-
std::vector<hardware::GPU> gpus;
22-
hardware::PowerInfo power;
17+
cortex::hw::CPU cpu;
18+
cortex::hw::OS os;
19+
cortex::hw::Memory ram;
20+
cortex::hw::StorageInfo storage;
21+
std::vector<cortex::hw::GPU> gpus;
22+
cortex::hw::PowerInfo power;
2323
};
2424

2525
class HardwareService {

engine/services/model_service.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,6 +661,7 @@ cpp::result<StartModelResult, std::string> ModelService::StartModel(
661661
#undef ASSIGN_IF_PRESENT
662662

663663
CTL_INF(json_data.toStyledString());
664+
// TODO(sang) move this into another function
664665
// Calculate ram/vram needed to load model
665666
services::HardwareService hw_svc;
666667
auto hw_info = hw_svc.GetHardwareInfo();

0 commit comments

Comments
 (0)