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+ }
0 commit comments