forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhardware_list_cmd.cc
More file actions
183 lines (165 loc) · 6.44 KB
/
hardware_list_cmd.cc
File metadata and controls
183 lines (165 loc) · 6.44 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "hardware_list_cmd.h"
#include <json/reader.h>
#include <json/value.h>
#include <iostream>
#include <vector>
#include "server_start_cmd.h"
#include "services/hardware_service.h"
#include "utils/curl_utils.h"
#include "utils/logging_utils.h"
// clang-format off
#include <tabulate/table.hpp>
// clang-format on
namespace commands {
using namespace tabulate;
using Row_t =
std::vector<variant<std::string, const char*, string_view, Table>>;
bool HardwareListCmd::Exec(const std::string& host, int port,
const std::optional<HarwareOptions>& ho) {
// Start server if server is not started yet
if (!commands::IsServerAlive(host, port)) {
CLI_LOG("Starting server ...");
commands::ServerStartCmd ssc;
if (!ssc.Exec(host, port)) {
return false;
}
}
auto url = url_parser::Url{
/* .protocol = */ "http",
/* .host = */ host + ":" + std::to_string(port),
/* .pathParams = */ {"v1", "hardware"},
/* .queries = */ {},
};
auto result = curl_utils::SimpleGetJson(url.ToFullPath());
if (result.has_error()) {
CTL_ERR(result.error());
return false;
}
if (!ho.has_value() || ho.value().show_cpu) {
std::cout << "CPU Information:" << std::endl;
Table table;
std::vector<std::string> column_headers{"#", "Arch", "Cores",
"Model", "Usage", "Instructions"};
Row_t header{column_headers.begin(), column_headers.end()};
table.add_row(header);
table.format().font_color(Color::green);
std::vector<std::string> row = {"1"};
cortex::hw::CPU cpu = cortex::hw::cpu::FromJson(result.value()["cpu"]);
row.emplace_back(cpu.arch);
row.emplace_back(std::to_string(cpu.cores));
row.emplace_back(cpu.model);
row.emplace_back(std::to_string(cpu.usage));
std::string insts;
for (auto const& i : cpu.instructions) {
insts += i + " ";
};
row.emplace_back(insts);
table.add_row({row.begin(), row.end()});
std::cout << table << std::endl;
std::cout << std::endl;
}
if (!ho.has_value() || ho.value().show_os) {
std::cout << "OS Information:" << std::endl;
Table table;
std::vector<std::string> column_headers{"#", "Version", "Name"};
Row_t header{column_headers.begin(), column_headers.end()};
table.add_row(header);
table.format().font_color(Color::green);
std::vector<std::string> row = {"1"};
cortex::hw::OS os = cortex::hw::os::FromJson(result.value()["os"]);
row.emplace_back(os.version);
row.emplace_back(os.name);
table.add_row({row.begin(), row.end()});
std::cout << table << std::endl;
std::cout << std::endl;
}
if (!ho.has_value() || ho.value().show_ram) {
std::cout << "RAM Information:" << std::endl;
Table table;
std::vector<std::string> column_headers{"#", "Total (MiB)",
"Available (MiB)"};
Row_t header{column_headers.begin(), column_headers.end()};
table.add_row(header);
table.format().font_color(Color::green);
std::vector<std::string> row = {"1"};
cortex::hw::Memory m = cortex::hw::memory::FromJson(result.value()["ram"]);
row.emplace_back(std::to_string(m.total_MiB));
row.emplace_back(std::to_string(m.available_MiB));
table.add_row({row.begin(), row.end()});
std::cout << table << std::endl;
std::cout << std::endl;
}
if (!ho.has_value() || ho.value().show_gpu) {
std::cout << "GPU Information:" << std::endl;
Table table;
std::vector<std::string> column_headers{"#",
"GPU ID",
"Name",
"Version",
"Total (MiB)",
"Available (MiB)",
"Driver Version",
"Compute Capability",
"Activated"};
Row_t header{column_headers.begin(), column_headers.end()};
table.add_row(header);
table.format().font_color(Color::green);
int count = 1;
std::vector<cortex::hw::GPU> gpus =
cortex::hw::gpu::FromJson(result.value()["gpus"]);
for (auto const& gpu : gpus) {
std::vector<std::string> row = {std::to_string(count)};
row.emplace_back(gpu.id);
row.emplace_back(gpu.name);
row.emplace_back(gpu.version);
row.emplace_back(std::to_string(gpu.total_vram));
row.emplace_back(std::to_string(gpu.free_vram));
row.emplace_back(
std::get<cortex::hw::NvidiaAddInfo>(gpu.add_info).driver_version);
row.emplace_back(
std::get<cortex::hw::NvidiaAddInfo>(gpu.add_info).compute_cap);
row.emplace_back(gpu.is_activated ? "Yes" : "No");
table.add_row({row.begin(), row.end()});
count++;
}
std::cout << table << std::endl;
std::cout << std::endl;
}
if (!ho.has_value() || ho.value().show_storage) {
std::cout << "Storage Information:" << std::endl;
Table table;
std::vector<std::string> column_headers{"#", "Total (GiB)",
"Available (GiB)"};
Row_t header{column_headers.begin(), column_headers.end()};
table.add_row(header);
table.format().font_color(Color::green);
std::vector<std::string> row = {"1"};
cortex::hw::StorageInfo si =
cortex::hw::storage::FromJson(result.value()["storage"]);
row.emplace_back(std::to_string(si.total));
row.emplace_back(std::to_string(si.available));
table.add_row({row.begin(), row.end()});
std::cout << table << std::endl;
std::cout << std::endl;
}
if (!ho.has_value() || ho.value().show_power) {
std::cout << "Power Information:" << std::endl;
Table table;
std::vector<std::string> column_headers{"#", "Battery Life",
"Charging Status", "Power Saving"};
Row_t header{column_headers.begin(), column_headers.end()};
table.add_row(header);
table.format().font_color(Color::green);
std::vector<std::string> row = {"1"};
cortex::hw::PowerInfo pi =
cortex::hw::power::FromJson(result.value()["power"]);
row.emplace_back(std::to_string(pi.battery_life));
row.emplace_back(pi.charging_status);
row.emplace_back(pi.is_power_saving ? "Yes" : "No");
table.add_row({row.begin(), row.end()});
std::cout << table << std::endl;
std::cout << std::endl;
}
return true;
}
} // namespace commands