-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnv_export.cpp
420 lines (375 loc) · 16.9 KB
/
nv_export.cpp
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#include <iostream>
#include <chrono>
#include <expected>
#include <thread>
#include <unordered_map>
#include <cstdlib>
#include <format>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <signal.h>
#ifndef NO_DRAM_TELEMETRY
extern "C" {
#include <pci/pci.h>
}
#endif
#include <reflect>
#include <range/v3/all.hpp>
#include <nvml.h>
template <typename T>
using nvField = std::expected<T, nvmlReturn_t>;
template <typename T>
auto nvCheckCall(nvField<T>& res, nvmlReturn_t val) -> bool {
if (val != NVML_SUCCESS) {
res = std::unexpected(val);
return false;
}
return true;
}
struct nvml_gpu_data
{
nvField<uint32_t> tag_index;
nvField<std::string> tag_name;
nvField<std::string> tag_bus_id;
nvField<std::string> tag_uuid;
nvField<uint16_t> dev_id;
nvField<uint32_t> pcie_link_gen_current;
nvField<uint32_t> pcie_link_gen_max;
nvField<uint32_t> pcie_link_width_current;
nvField<uint32_t> pcie_link_width_max;
nvField<uint32_t> persistence_mode;
nvField<uint32_t> fan_speed;
nvField<uint32_t> pstate;
nvField<bool> clock_throttle_reason_sw_power_cap;
nvField<bool> clock_throttle_reason_hw_slowdown;
nvField<bool> clock_throttle_reason_hw_thermal_slowdown;
nvField<bool> clock_throttle_reason_hw_power_brake_slowdown;
nvField<bool> clock_throttle_reason_sw_thermal_slowdown;
nvField<bool> clock_throttle_reason_sync_boost;
nvField<uint64_t> memory_total;
nvField<uint64_t> memory_used;
nvField<std::string> compute_cap;
nvField<float> utilization_gpu;
nvField<float> utilization_memory;
nvField<uint32_t> temperature_gpu;
nvField<uint32_t> power_draw;
nvField<uint32_t> power_limit;
nvField<uint32_t> clocks_gr;
nvField<uint32_t> clocks_sm;
nvField<uint32_t> clocks_mem;
nvField<uint32_t> clocks_video;
static nvml_gpu_data from_handle(nvmlDevice_t device) {
nvml_gpu_data data;
char name[NVML_DEVICE_NAME_BUFFER_SIZE];
if (nvCheckCall(data.tag_name, nvmlDeviceGetName(device, name, NVML_DEVICE_NAME_BUFFER_SIZE))) {
data.tag_name = name;
}
nvmlPciInfo_t pci_info;
if (nvCheckCall(data.tag_bus_id, nvmlDeviceGetPciInfo_v3(device, &pci_info))) {
data.tag_bus_id = pci_info.busId;
data.dev_id = pci_info.pciDeviceId;
}
char uuid[NVML_DEVICE_UUID_V2_BUFFER_SIZE];
if (nvCheckCall(data.tag_uuid, nvmlDeviceGetUUID(device, uuid, NVML_DEVICE_UUID_V2_BUFFER_SIZE))) {
data.tag_uuid = uuid;
}
uint32_t pcie_link_gen_current;
if (nvCheckCall(data.pcie_link_gen_current, nvmlDeviceGetCurrPcieLinkGeneration(device, &pcie_link_gen_current))) {
data.pcie_link_gen_current = pcie_link_gen_current;
}
uint32_t pcie_link_gen_max;
if (nvCheckCall(data.pcie_link_gen_max, nvmlDeviceGetMaxPcieLinkGeneration(device, &pcie_link_gen_max))) {
data.pcie_link_gen_max = pcie_link_gen_max;
}
uint32_t pcie_link_width_current;
if (nvCheckCall(data.pcie_link_width_current, nvmlDeviceGetCurrPcieLinkWidth(device, &pcie_link_width_current))) {
data.pcie_link_width_current = pcie_link_width_current;
}
uint32_t pcie_link_width_max;
if (nvCheckCall(data.pcie_link_width_max, nvmlDeviceGetMaxPcieLinkWidth(device, &pcie_link_width_max))) {
data.pcie_link_width_max = pcie_link_width_max;
}
nvmlEnableState_t persistence_mode;
if (nvCheckCall(data.persistence_mode, nvmlDeviceGetPersistenceMode(device, &persistence_mode))) {
data.persistence_mode = (persistence_mode == NVML_FEATURE_ENABLED);
}
uint32_t fan_speed;
if (nvCheckCall(data.fan_speed, nvmlDeviceGetFanSpeed(device, &fan_speed))) {
data.fan_speed = fan_speed;
}
nvmlPstates_t pstate;
if (nvCheckCall(data.pstate, nvmlDeviceGetPerformanceState(device, &pstate))) {
data.pstate = (int)pstate;
}
unsigned long long clock_event_reasons;
if (nvCheckCall(data.clock_throttle_reason_sw_power_cap, nvmlDeviceGetCurrentClocksEventReasons(device, &clock_event_reasons))) {
data.clock_throttle_reason_sw_power_cap = (clock_event_reasons & nvmlClocksThrottleReasonSwPowerCap) != 0;
data.clock_throttle_reason_hw_slowdown = (clock_event_reasons & nvmlClocksThrottleReasonHwSlowdown) != 0;
data.clock_throttle_reason_hw_thermal_slowdown = (clock_event_reasons & nvmlClocksThrottleReasonHwThermalSlowdown) != 0;
data.clock_throttle_reason_hw_power_brake_slowdown = (clock_event_reasons & nvmlClocksThrottleReasonHwPowerBrakeSlowdown) != 0;
data.clock_throttle_reason_sw_thermal_slowdown = (clock_event_reasons & nvmlClocksThrottleReasonSwThermalSlowdown) != 0;
data.clock_throttle_reason_sync_boost = (clock_event_reasons & nvmlClocksThrottleReasonSyncBoost) != 0;
}
nvmlMemory_t memory;
if (nvCheckCall(data.memory_total, nvmlDeviceGetMemoryInfo(device, &memory))) {
data.memory_total = memory.total;
data.memory_used = memory.used;
}
int32_t major, minor;
if (nvCheckCall(data.compute_cap, nvmlDeviceGetCudaComputeCapability(device, &major, &minor))) {
data.compute_cap = std::to_string(major) + "." + std::to_string(minor);
}
nvmlUtilization_t utilization;
if (nvCheckCall(data.utilization_gpu, nvmlDeviceGetUtilizationRates(device, &utilization))) {
data.utilization_gpu = utilization.gpu;
data.utilization_memory = utilization.memory;
}
uint32_t temperature_gpu;
if (nvCheckCall(data.temperature_gpu, nvmlDeviceGetTemperature(device, NVML_TEMPERATURE_GPU, &temperature_gpu))) {
data.temperature_gpu = temperature_gpu;
}
uint32_t power;
if (nvCheckCall(data.power_draw, nvmlDeviceGetPowerUsage(device, &power))) {
data.power_draw = power;
}
uint32_t power_limit;
if (nvCheckCall(data.power_limit, nvmlDeviceGetPowerManagementLimit(device, &power_limit))) {
data.power_limit = power_limit;
}
uint32_t clock_gr;
if (nvCheckCall(data.clocks_gr, nvmlDeviceGetClockInfo(device, NVML_CLOCK_GRAPHICS, &clock_gr))) {
data.clocks_gr = clock_gr;
}
uint32_t clock_sm;
if (nvCheckCall(data.clocks_sm, nvmlDeviceGetClockInfo(device, NVML_CLOCK_SM, &clock_sm))) {
data.clocks_sm = clock_sm;
}
uint32_t clock_mem;
if (nvCheckCall(data.clocks_mem, nvmlDeviceGetClockInfo(device, NVML_CLOCK_MEM, &clock_mem))) {
data.clocks_mem = clock_mem;
}
uint32_t clock_video;
if (nvCheckCall(data.clocks_video, nvmlDeviceGetClockInfo(device, NVML_CLOCK_VIDEO, &clock_video))) {
data.clocks_video = clock_video;
}
return data;
}
};
struct dev_table_entry
{
uint32_t offset;
const char *vram;
const char *arch;
const char *name;
};
static std::unordered_map<uint16_t, dev_table_entry> dev_table_map = {
// Ada
{ 0x2684, { .offset = 0x0000E2A8, .vram = "GDDR6X", .arch = "AD102", .name = "RTX 4090" } },
{ 0x2702, { .offset = 0x0000E2A8, .vram = "GDDR6X", .arch = "AD103", .name = "RTX 4080 Super" } },
{ 0x2704, { .offset = 0x0000E2A8, .vram = "GDDR6X", .arch = "AD103", .name = "RTX 4080" } },
{ 0x2705, { .offset = 0x0000E2A8, .vram = "GDDR6X", .arch = "AD103", .name = "RTX 4070 Ti Super" } },
{ 0x2782, { .offset = 0x0000E2A8, .vram = "GDDR6X", .arch = "AD104", .name = "RTX 4070 Ti" } },
{ 0x2783, { .offset = 0x0000E2A8, .vram = "GDDR6X", .arch = "AD104", .name = "RTX 4070 Super" } },
{ 0x2786, { .offset = 0x0000E2A8, .vram = "GDDR6X", .arch = "AD104", .name = "RTX 4070" } },
{ 0x2860, { .offset = 0x0000E2A8, .vram = "GDDR6", .arch = "AD106", .name = "RTX 4070 Max-Q / Mobile" } },
{ 0x26B1, { .offset = 0x0000E2A8, .vram = "GDDR6", .arch = "AD102", .name = "RTX A6000 (Ada)" } },
{ 0x27b8, { .offset = 0x0000E2A8, .vram = "GDDR6", .arch = "AD104", .name = "L4" } },
{ 0x26B9, { .offset = 0x0000E2A8, .vram = "GDDR6", .arch = "AD102", .name = "L40S" } },
{ 0x26B5, { .offset = 0x0000E2A8, .vram = "GDDR6", .arch = "AD102", .name = "L40" } },
// Ampere
{ 0x2203, { .offset = 0x0000E2A8, .vram = "GDDR6X", .arch = "GA102", .name = "RTX 3090 Ti" } },
{ 0x2204, { .offset = 0x0000E2A8, .vram = "GDDR6X", .arch = "GA102", .name = "RTX 3090" } },
{ 0x2208, { .offset = 0x0000E2A8, .vram = "GDDR6X", .arch = "GA102", .name = "RTX 3080 Ti" } },
{ 0x2206, { .offset = 0x0000E2A8, .vram = "GDDR6X", .arch = "GA102", .name = "RTX 3080" } },
{ 0x2216, { .offset = 0x0000E2A8, .vram = "GDDR6X", .arch = "GA102", .name = "RTX 3080 LHR" } },
{ 0x2484, { .offset = 0x0000EE50, .vram = "GDDR6", .arch = "GA104", .name = "RTX 3070" } },
{ 0x2488, { .offset = 0x0000EE50, .vram = "GDDR6", .arch = "GA104", .name = "RTX 3070 LHR" } },
{ 0x2531, { .offset = 0x0000E2A8, .vram = "GDDR6", .arch = "GA106", .name = "RTX A2000" } },
{ 0x2571, { .offset = 0x0000E2A8, .vram = "GDDR6", .arch = "GA106", .name = "RTX A2000" } },
{ 0x2232, { .offset = 0x0000E2A8, .vram = "GDDR6", .arch = "GA102", .name = "RTX A4500" } },
{ 0x2231, { .offset = 0x0000E2A8, .vram = "GDDR6", .arch = "GA102", .name = "RTX A5000" } },
{ 0x2236, { .offset = 0x0000E2A8, .vram = "GDDR6", .arch = "GA102", .name = "A10" } },
};
struct device
{
uint32_t bar0;
uint8_t bus, dev, func;
nvmlPciInfo_t pci_info;
dev_table_entry meta;
void *mapped_addr;
uint32_t phys_addr;
uint32_t base_offset;
nvmlDevice_t nvml_device;
};
auto escape_tag(std::string_view s) -> std::string {
auto escape_char = [](char c) -> std::string {
switch (c) {
case ',': return {"\\,"};
case '=': return {"\\="};
case ' ': return {"\\ "};
default: return {c};
}
};
return s
| ranges::views::transform(escape_char)
| ranges::views::join
| ranges::to<std::string>();
}
auto escape_string(std::string_view s) -> std::string {
auto escape_char = [](char c) -> std::string{
switch (c) {
case '"': return {'\\', '"'};
case '\\': return {'\\', '\\'};
default: return {c};
}
};
return s
| ranges::views::transform(escape_char)
| ranges::views::join
| ranges::to<std::string>();
};
int main() {
nvmlReturn_t result;
result = nvmlInit();
if (result != NVML_SUCCESS) {
std::cerr << "Failed to initialize NVML: " << nvmlErrorString(result) << std::endl;
return 1;
}
std::atexit([](){
nvmlShutdown();
});
uint32_t device_count;
result = nvmlDeviceGetCount(&device_count);
if (result != NVML_SUCCESS) {
std::cerr << "Failed to get device count: " << nvmlErrorString(result) << std::endl;
return 1;
}
static std::vector<device> devices(device_count);
for (uint32_t i = 0; i < device_count; ++i) {
result = nvmlDeviceGetHandleByIndex(i, &devices[i].nvml_device);
if (result != NVML_SUCCESS) {
std::cerr << "Failed to get handle for device " << i << ": " << nvmlErrorString(result) << std::endl;
return 1;
}
nvmlPciInfo_t pci_info;
if (nvmlDeviceGetPciInfo_v3(devices[i].nvml_device, &pci_info) != NVML_SUCCESS) {
std::cerr << "Failed to get Pci info for device " << i << ": " << nvmlErrorString(result) << std::endl;
return 1;
}
devices[i].pci_info = pci_info;
}
#ifndef NO_DRAM_TELEMETRY
static int pg_sz = sysconf(_SC_PAGE_SIZE);
static int memfd = open("/dev/mem", O_RDONLY);
if (memfd == -1) {
std::cerr << "Failed to open /dev/mem, are you root?" << std::endl;
return 1;
}
pci_access *pacc = NULL;
pci_dev *pci_dev = NULL;
pacc = pci_alloc();
pci_init(pacc);
pci_scan_bus(pacc);
for (pci_dev = pacc->devices; pci_dev != NULL; pci_dev = pci_dev->next) {
pci_fill_info(pci_dev, PCI_FILL_IDENT | PCI_FILL_BASES | PCI_FILL_CLASS);
auto it = dev_table_map.find(pci_dev->device_id);
if (it == dev_table_map.end())
continue;
for (auto& device : devices) {
if (device.pci_info.bus == pci_dev->bus
&& device.pci_info.device == pci_dev->dev
&& device.pci_info.domain == pci_dev->func) {
device.meta = it->second;
device.bar0 = (pci_dev->base_addr[0] & 0xffffffff);
device.bus = pci_dev->bus;
device.dev = pci_dev->dev;
device.func = pci_dev->func;
break;
}
}
}
for (auto& device : devices) {
if (device.meta.vram == NULL) {
std::cerr << "No matching device found for " << device.pci_info.bus << ":" << device.pci_info.device << ":" << device.pci_info.domain << std::endl
<< "Please add a matching entry to dev_table_map in nv_export.cpp or compile with NO_DRAM_TELEMETRY to disable this check." << std::endl;
return 1;
}
device.phys_addr = (device.bar0 + device.meta.offset);
device.base_offset = device.phys_addr & ~(pg_sz - 1);
device.mapped_addr = mmap(0, pg_sz, PROT_READ, MAP_SHARED, memfd, device.base_offset);
if (device.mapped_addr == MAP_FAILED)
{
device.mapped_addr = NULL;
std::cerr << std::format("Memory mapping failed for pci={:x}:{:x}:{:x}\n", device.pci_info.bus, device.pci_info.device, device.pci_info.domain);
std::cerr << "Did you enable iomem=relaxed? Are you root?\n";
return 1;
}
}
atexit([](){
for (auto& device : devices) {
if (device.mapped_addr != NULL && device.mapped_addr != MAP_FAILED) {
munmap(device.mapped_addr, pg_sz);
device.mapped_addr = NULL;
}
}
if (memfd != -1) {
close(memfd);
memfd = -1;
}
});
#endif
while (true) {
auto now = std::chrono::system_clock::now();
auto timestamp = std::chrono::duration_cast<std::chrono::nanoseconds>(
now.time_since_epoch()
).count();
for (auto const [idx, device] : ranges::views::enumerate(devices)) {
nvml_gpu_data gpu = nvml_gpu_data::from_handle(device.nvml_device);
gpu.tag_index = idx;
std::vector<std::string> metrics;
std::vector<std::string> errors;
reflect::for_each([&](auto I) {
auto field = reflect::get<I>(gpu);
std::string_view name = reflect::member_name<I>(gpu);
if (name.starts_with("tag_")) return;
if (field) {
using field_T = std::remove_cvref_t<decltype(*field)>;
std::string value;
if constexpr (std::is_same_v<field_T, std::string>) value = std::format("\"{}\"", escape_string(*field));
else if constexpr (std::is_same_v<field_T, bool>) value = (*field ? "true" : "false");
else if constexpr (std::is_same_v<field_T, uint64_t>) value = std::format("{}u", *field);
else value = std::format("{}", *field);
metrics.push_back(std::format("{}={}", name, value));
} else {
errors.push_back(std::format("{} ({})", name, reflect::enum_name(field.error())));
}
}, gpu);
#ifndef NO_DRAM_TELEMETRY
if (device.mapped_addr != NULL && device.mapped_addr != MAP_FAILED) {
void *virt_addr = (uint8_t *)device.mapped_addr + (device.phys_addr - device.base_offset);
uint32_t read_result = *((uint32_t *)virt_addr);
uint32_t temp = ((read_result & 0x00000fff) / 0x20);
metrics.push_back(std::format("temperature_memory={}", temp));
}
#endif
if (!errors.empty()) {
auto error_str = errors
| ranges::views::join(",")
| ranges::to<std::string>();
metrics.push_back(std::format("errors=\"{}\"", escape_string(error_str)));
}
std::cout << "nv_export"
<< ",index=" << *gpu.tag_index
<< ",name=" << escape_tag(*gpu.tag_name)
<< ",bus_id=" << escape_tag(*gpu.tag_bus_id)
<< ",uuid=" << escape_tag(*gpu.tag_uuid)
<< " " << (metrics | ranges::views::join(",") | ranges::to<std::string>())
<< " " << timestamp << std::endl;
}
std::flush(std::cout);
std::this_thread::sleep_for(std::chrono::seconds(1));
}
return 0;
}