Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/tests/nvml/nvml_test.c # CUDApp CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / tests / nvml / nvml_test.c` 459 lines · 21094 bytes Raw Blame History 1 /* ORACLE: foreign C caller of the generated ABI; not shipped */ 2 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 3 /* Host test of the CUDA++ NVML subset (compat/nvml). 4 * Default: Omega RM fake (tests/nvml/rmfake.om). usage: nvml_test ERRSTR_ORACLE.tsv 5 * Live (no fake, NVIDIA's libnvidia-ml never linked): compile with -DCUDAPP_NVML_LIVE and link 6 * the generated compat/nvml/libnvidia-ml.c, src/cudapp_om_nvml_step.c, src/cudapp_om_nvml_errstr.c 7 * and the #281 transport src/cudapp_om_nvrm_transport.c. usage: nvml-live --live 8 * Chip run belongs in an exclusive spark-window. This file does not open the GPU by itself 9 * except through nvmlInit_v2 when --live is passed. */ 10 #include 11 #include 12 #include 13 14 #include "nvml.h" 15 #ifndef CUDAPP_NVML_LIVE 16 #include "transport.h" 17 #endif 18 19 #ifdef CUDAPP_NVML_LIVE 20 21 /* Read-only probe. One line per field so the nvidia-smi oracle can join on the key. 22 * Negative control: handle index == device count must be NVML_ERROR_INVALID_ARGUMENT (2). 23 * Exit 0 only when init, every query, the negative control and shutdown all succeed. 24 * Exit 3 when init does not succeed (driver not available): not a chip pass. */ 25 static int live_fail(const char *what, nvmlReturn_t got, nvmlReturn_t want) { 26 fprintf(stderr, "FAIL %s: rc %d, want %d\n", what, (int)got, (int)want); 27 nvmlShutdown(); 28 return 1; 29 } 30 31 /* NVML_VALUE_NOT_AVAILABLE is -1. Print NA so a compare does not treat 32 * `18446744073709551615` as a byte count. One call at a time: the buffer is shared. */ 33 static char live_byte_buf[32]; 34 static const char *live_bytes(unsigned long long n) { 35 if (n == (unsigned long long)NVML_VALUE_NOT_AVAILABLE) return "NA"; 36 snprintf(live_byte_buf, sizeof live_byte_buf, "%llu", n); 37 return live_byte_buf; 38 } 39 40 static int live_probe(void) { 41 unsigned count = 0; 42 nvmlReturn_t rc = nvmlInit_v2(); 43 if (rc != NVML_SUCCESS) { 44 printf("live init rc=%d\n", (int)rc); 45 fprintf(stderr, "NOT_RUN: nvmlInit_v2 rc %d (no chip pass)\n", (int)rc); 46 return 3; 47 } 48 rc = nvmlDeviceGetCount_v2(&count); 49 if (rc != NVML_SUCCESS) return live_fail("count", rc, NVML_SUCCESS); 50 printf("live count=%u\n", count); 51 52 for (unsigned i = 0; i < count; i++) { 53 nvmlDevice_t dev = NULL; 54 char name[NVML_DEVICE_NAME_V2_BUFFER_SIZE]; 55 char uuid[NVML_DEVICE_UUID_V2_BUFFER_SIZE]; 56 unsigned temp = 0; 57 unsigned procs_n = 0; 58 nvmlMemory_t mem; 59 nvmlProcessInfo_t infos[64]; 60 char what[64]; 61 62 snprintf(what, sizeof what, "handle %u", i); 63 rc = nvmlDeviceGetHandleByIndex_v2(i, &dev); 64 if (rc != NVML_SUCCESS) return live_fail(what, rc, NVML_SUCCESS); 65 snprintf(what, sizeof what, "name %u", i); 66 rc = nvmlDeviceGetName(dev, name, sizeof name); 67 if (rc != NVML_SUCCESS) return live_fail(what, rc, NVML_SUCCESS); 68 snprintf(what, sizeof what, "uuid %u", i); 69 rc = nvmlDeviceGetUUID(dev, uuid, sizeof uuid); 70 if (rc != NVML_SUCCESS) return live_fail(what, rc, NVML_SUCCESS); 71 snprintf(what, sizeof what, "temperature %u", i); 72 rc = nvmlDeviceGetTemperature(dev, NVML_TEMPERATURE_GPU, &temp); 73 if (rc != NVML_SUCCESS) return live_fail(what, rc, NVML_SUCCESS); 74 snprintf(what, sizeof what, "memory %u", i); 75 rc = nvmlDeviceGetMemoryInfo(dev, &mem); 76 if (rc != NVML_SUCCESS) return live_fail(what, rc, NVML_SUCCESS); 77 procs_n = 64; 78 snprintf(what, sizeof what, "procs %u", i); 79 rc = nvmlDeviceGetComputeRunningProcesses_v3(dev, &procs_n, infos); 80 if (rc != NVML_SUCCESS) return live_fail(what, rc, NVML_SUCCESS); 81 82 printf("live gpu%u name=%s\n", i, name); 83 printf("live gpu%u uuid=%s\n", i, uuid); 84 printf("live gpu%u temperature_c=%u\n", i, temp); 85 printf("live gpu%u memory_total_bytes=%s\n", i, live_bytes(mem.total)); 86 printf("live gpu%u memory_used_bytes=%s\n", i, live_bytes(mem.used)); 87 printf("live gpu%u procs=%u\n", i, procs_n); 88 for (unsigned p = 0; p < procs_n; p++) { 89 printf("proc pid=%u used_bytes=%s\n", infos[p].pid, live_bytes(infos[p].usedGpuMemory)); 90 } 91 } 92 93 /* Negative control: one past the last real index. A passing probe that also accepts 94 * this index has not established the bound. */ 95 { 96 nvmlDevice_t bad = NULL; 97 rc = nvmlDeviceGetHandleByIndex_v2(count, &bad); 98 printf("live negative index=%u rc=%d want=%d\n", count, (int)rc, 99 (int)NVML_ERROR_INVALID_ARGUMENT); 100 if (rc != NVML_ERROR_INVALID_ARGUMENT) { 101 fprintf(stderr, "FAIL negative control: index %u rc %d, want %d\n", count, (int)rc, 102 (int)NVML_ERROR_INVALID_ARGUMENT); 103 nvmlShutdown(); 104 return 1; 105 } 106 } 107 rc = nvmlShutdown(); 108 if (rc != NVML_SUCCESS) { 109 fprintf(stderr, "FAIL shutdown: rc %d, want %d\n", (int)rc, (int)NVML_SUCCESS); 110 return 1; 111 } 112 printf("live shutdown rc=0\n"); 113 printf("CUDAPP-NVML LIVE QUERIES DONE\n"); 114 return 0; 115 } 116 117 int main(int argc, char **argv) { 118 if (argc != 2 || strcmp(argv[1], "--live") != 0) { 119 fprintf(stderr, "usage: nvml-live --live\n"); 120 return 2; 121 } 122 return live_probe(); 123 } 124 125 #else 126 static unsigned checks, fails; 127 128 #define CHECK(cond, ...) \ 129 do { \ 130 checks++; \ 131 if (!(cond)) { \ 132 fails++; \ 133 printf("FAIL line %d: ", __LINE__); \ 134 printf(__VA_ARGS__); \ 135 printf("\n"); \ 136 } \ 137 } while (0) 138 139 #define RC(call, want, what) \ 140 do { \ 141 nvmlReturn_t got_ = (call); \ 142 CHECK(got_ == (want), "%s: rc %d, want %d", what, (int)got_, (int)(want)); \ 143 } while (0) 144 145 static const char *const UUID0 = "GPU-`fb8e53f0`-3207-e89b-d02f-`acdfe2695b59`"; 146 static const char *const UUID1 = "GPU-`fb8e53f0`-3207-e89b-d02f-`acdfe2695b5a`"; 147 148 /* Every fd closed, every RM object freed, no request outside the RM contract. */ 149 static void clean(const char *what) { 150 CHECK(fake_get(14) == 0, "%s: open fd mask %llx", what, (unsigned long long)fake_get(14)); 151 CHECK(fake_get(15) == 0, "%s: %llu live RM objects", what, (unsigned long long)fake_get(15)); 152 CHECK(fake_get(16) == 0, "%s: root client still allocated", what); 153 CHECK(fake_get(8) == fake_get(9), "%s: %llu opens, %llu closes", what, 154 (unsigned long long)fake_get(8), (unsigned long long)fake_get(9)); 155 CHECK(fake_get(13) == 0, "%s: %llu RM contract violations", what, 156 (unsigned long long)fake_get(13)); 157 } 158 159 static void errstrings(const char *path) { 160 FILE *f = fopen(path, "r"); 161 CHECK(f != NULL, "open %s", path); 162 if (f == NULL) return; 163 char line[256]; 164 unsigned rows = 0; 165 while (fgets(line, sizeof line, f)) { 166 char *tab = strchr(line, '\t'); 167 if (tab == NULL) continue; 168 *tab = 0; 169 char *want = tab + 1; 170 want[strcspn(want, "\n")] = 0; 171 long long code = strtoll(line, NULL, 10); 172 const char *got = nvmlErrorString((nvmlReturn_t)(int)(unsigned)code); 173 CHECK(got != NULL && strcmp(got, want) == 0, "nvmlErrorString(%lld) = \"%s\", want \"%s\"", 174 code, got ? got : "(null)", want); 175 rows++; 176 } 177 fclose(f); 178 CHECK(rows == 37, "oracle rows %u, want 37", rows); 179 CHECK(nvmlErrorString(NVML_SUCCESS) == nvmlErrorString(NVML_SUCCESS), "stable string pointer"); 180 CHECK(nvmlErrorString((nvmlReturn_t)31) == nvmlErrorString((nvmlReturn_t)999), 181 "one Unknown Error string"); 182 } 183 184 static void healthy(void) { 185 unsigned n = 77, t = 0; 186 nvmlDevice_t d = NULL, d1 = NULL; 187 char buf[128]; 188 fake_reset(0); 189 190 RC(nvmlDeviceGetCount_v2(&n), NVML_ERROR_UNINITIALIZED, "count before init"); 191 RC(nvmlDeviceGetHandleByIndex_v2(0, &d), NVML_ERROR_UNINITIALIZED, "handle before init"); 192 RC(nvmlShutdown(), NVML_ERROR_UNINITIALIZED, "shutdown before init"); 193 CHECK(fake_get(8) == 0, "no open before init"); 194 195 RC(nvmlInit_v2(), NVML_SUCCESS, "init"); 196 RC(nvmlDeviceGetCount_v2(&n), NVML_SUCCESS, "count"); 197 CHECK(n == 1, "count %u, want 1", n); 198 RC(nvmlDeviceGetCount_v2(NULL), NVML_ERROR_INVALID_ARGUMENT, "count NULL"); 199 200 RC(nvmlDeviceGetHandleByIndex_v2(0, &d), NVML_SUCCESS, "handle 0"); 201 CHECK(d != NULL, "handle 0 non-NULL"); 202 RC(nvmlDeviceGetHandleByIndex_v2(1, &d1), NVML_ERROR_INVALID_ARGUMENT, "handle 1"); 203 RC(nvmlDeviceGetHandleByIndex_v2(0, NULL), NVML_ERROR_INVALID_ARGUMENT, "handle NULL"); 204 205 memset(buf, 'x', sizeof buf); 206 RC(nvmlDeviceGetName(d, buf, NVML_DEVICE_NAME_V2_BUFFER_SIZE), NVML_SUCCESS, "name 96"); 207 CHECK(strcmp(buf, "NVIDIA GB10") == 0, "name \"%.64s\"", buf); 208 RC(nvmlDeviceGetName(d, buf, 11), NVML_ERROR_INSUFFICIENT_SIZE, "name length 11"); 209 memset(buf, 'x', sizeof buf); 210 RC(nvmlDeviceGetName(d, buf, 12), NVML_SUCCESS, "name length 12"); 211 CHECK(strcmp(buf, "NVIDIA GB10") == 0, "name 12 \"%.64s\"", buf); 212 RC(nvmlDeviceGetName(NULL, buf, 64), NVML_ERROR_INVALID_ARGUMENT, "name NULL device"); 213 RC(nvmlDeviceGetName((nvmlDevice_t)(void *)buf, buf, 64), NVML_ERROR_INVALID_ARGUMENT, 214 "name foreign device"); 215 RC(nvmlDeviceGetName(d, NULL, 64), NVML_ERROR_INVALID_ARGUMENT, "name NULL buffer"); 216 217 memset(buf, 'x', sizeof buf); 218 RC(nvmlDeviceGetUUID(d, buf, NVML_DEVICE_UUID_V2_BUFFER_SIZE), NVML_SUCCESS, "uuid 96"); 219 CHECK(strcmp(buf, UUID0) == 0, "uuid \"%.96s\"", buf); 220 RC(nvmlDeviceGetUUID(d, buf, 40), NVML_ERROR_INSUFFICIENT_SIZE, "uuid length 40"); 221 memset(buf, 'x', sizeof buf); 222 RC(nvmlDeviceGetUUID(d, buf, 41), NVML_SUCCESS, "uuid length 41"); 223 CHECK(strcmp(buf, UUID0) == 0, "uuid 41 \"%.96s\"", buf); 224 RC(nvmlDeviceGetUUID(d, NULL, 80), NVML_ERROR_INVALID_ARGUMENT, "uuid NULL buffer"); 225 226 RC(nvmlDeviceGetTemperature(d, NVML_TEMPERATURE_GPU, &t), NVML_SUCCESS, "temperature"); 227 CHECK(t == 40, "temperature %u, want 40", t); 228 RC(nvmlDeviceGetTemperature(d, NVML_TEMPERATURE_COUNT, &t), NVML_ERROR_INVALID_ARGUMENT, 229 "temperature sensor 1"); 230 RC(nvmlDeviceGetTemperature(d, NVML_TEMPERATURE_GPU, NULL), NVML_ERROR_INVALID_ARGUMENT, 231 "temperature NULL"); 232 233 nvmlMemory_t m; 234 memset(&m, 0xa5, sizeof m); 235 RC(nvmlDeviceGetMemoryInfo(d, &m), NVML_SUCCESS, "memory"); 236 CHECK(m.total == 131072000000ull && m.free == 102400000000ull && m.used == 28672000000ull, 237 "memory %llu/%llu/%llu", (unsigned long long)m.total, (unsigned long long)m.free, 238 (unsigned long long)m.used); 239 RC(nvmlDeviceGetMemoryInfo(d, NULL), NVML_ERROR_INVALID_ARGUMENT, "memory NULL"); 240 241 nvmlUtilization_t u = {77, 77}; 242 RC(nvmlDeviceGetUtilizationRates(d, &u), NVML_SUCCESS, "utilization"); 243 CHECK(u.gpu == 42 && u.memory == 13, "utilization %u/%u, want 42/13", u.gpu, u.memory); 244 RC(nvmlDeviceGetUtilizationRates(d, NULL), NVML_ERROR_INVALID_ARGUMENT, "utilization NULL"); 245 246 nvmlProcessInfo_t p[5]; 247 n = 0; 248 RC(nvmlDeviceGetComputeRunningProcesses_v3(d, &n, NULL), NVML_ERROR_INSUFFICIENT_SIZE, 249 "procs count 0"); 250 CHECK(n == 2, "procs needed %u, want 2", n); 251 n = 1; 252 RC(nvmlDeviceGetComputeRunningProcesses_v3(d, &n, p), NVML_ERROR_INSUFFICIENT_SIZE, 253 "procs count 1"); 254 CHECK(n == 2, "procs needed %u, want 2", n); 255 n = 2; 256 RC(nvmlDeviceGetComputeRunningProcesses_v3(d, &n, NULL), NVML_ERROR_INVALID_ARGUMENT, 257 "procs NULL infos"); 258 RC(nvmlDeviceGetComputeRunningProcesses_v3(d, NULL, p), NVML_ERROR_INVALID_ARGUMENT, 259 "procs NULL count"); 260 memset(p, 0, sizeof p); 261 n = 5; 262 RC(nvmlDeviceGetComputeRunningProcesses_v3(d, &n, p), NVML_SUCCESS, "procs count 5"); 263 CHECK(n == 2, "procs %u, want 2", n); 264 CHECK(p[0].pid == 68451 && p[0].usedGpuMemory == 351272960ull, "proc 0 %u %llu", p[0].pid, 265 (unsigned long long)p[0].usedGpuMemory); 266 CHECK(p[1].pid == 4242 && p[1].usedGpuMemory == 278003712ull, "proc 1 %u %llu", p[1].pid, 267 (unsigned long long)p[1].usedGpuMemory); 268 CHECK(p[0].gpuInstanceId == 0xFFFFFFFFu && p[0].computeInstanceId == 0xFFFFFFFFu && 269 p[1].gpuInstanceId == 0xFFFFFFFFu && p[1].computeInstanceId == 0xFFFFFFFFu, 270 "proc instance ids not NVML_VALUE_NOT_AVAILABLE"); 271 CHECK(p[2].pid == 0, "proc 2 written"); 272 273 RC(nvmlShutdown(), NVML_SUCCESS, "shutdown"); 274 RC(nvmlDeviceGetCount_v2(&n), NVML_ERROR_UNINITIALIZED, "count after shutdown"); 275 RC(nvmlDeviceGetName(d, buf, 64), NVML_ERROR_UNINITIALIZED, "name after shutdown"); 276 RC(nvmlShutdown(), NVML_ERROR_UNINITIALIZED, "second shutdown"); 277 clean("healthy"); 278 } 279 280 /* nvmlInit/nvmlShutdown keep a reference count; only the last shutdown tears down (nvml.h). */ 281 static void refcount(void) { 282 unsigned n = 0; 283 fake_reset(0); 284 RC(nvmlInit(), NVML_SUCCESS, "unversioned nvmlInit"); 285 uint64_t opens = fake_get(8); 286 CHECK(opens == 2, "init opened %llu files, want 2", (unsigned long long)opens); 287 RC(nvmlInit_v2(), NVML_SUCCESS, "second init"); 288 CHECK(fake_get(8) == opens, "second init reopened a device (%llu opens)", 289 (unsigned long long)fake_get(8)); 290 RC(nvmlShutdown(), NVML_SUCCESS, "first shutdown"); 291 RC(nvmlDeviceGetCount(&n), NVML_SUCCESS, "count while one reference remains"); 292 CHECK(fake_get(9) == 0, "first shutdown closed a device"); 293 RC(nvmlShutdown(), NVML_SUCCESS, "last shutdown"); 294 RC(nvmlDeviceGetCount_v2(&n), NVML_ERROR_UNINITIALIZED, "count after last shutdown"); 295 clean("refcount"); 296 RC(nvmlInit_v2(), NVML_SUCCESS, "re-init"); 297 RC(nvmlShutdown(), NVML_SUCCESS, "re-init shutdown"); 298 clean("re-init"); 299 } 300 301 enum call { NONE, COUNT, NAME, UUID, TEMP, MEM, UTIL, PROCS }; 302 303 struct fault { 304 char letter; 305 nvmlReturn_t init; 306 enum call call; 307 nvmlReturn_t rc; 308 }; 309 310 static const struct fault FAULTS[] = { 311 {'o', NVML_ERROR_DRIVER_NOT_LOADED, NONE, 0}, 312 {'a', NVML_ERROR_NO_PERMISSION, NONE, 0}, 313 {'d', NVML_ERROR_DRIVER_NOT_LOADED, NONE, 0}, 314 {'P', NVML_ERROR_NO_PERMISSION, NONE, 0}, 315 {'A', NVML_ERROR_NO_PERMISSION, NONE, 0}, 316 {'9', NVML_ERROR_UNKNOWN, NONE, 0}, 317 {'0', NVML_SUCCESS, COUNT, NVML_SUCCESS}, 318 {'2', NVML_SUCCESS, COUNT, NVML_SUCCESS}, 319 {'c', NVML_SUCCESS, NAME, NVML_ERROR_NOT_SUPPORTED}, 320 {'G', NVML_SUCCESS, NAME, NVML_ERROR_GPU_IS_LOST}, 321 {'n', NVML_SUCCESS, NAME, NVML_ERROR_UNKNOWN}, 322 {'E', NVML_SUCCESS, NAME, NVML_ERROR_UNKNOWN}, 323 {'S', NVML_SUCCESS, NAME, NVML_ERROR_UNKNOWN}, 324 {'g', NVML_SUCCESS, UUID, NVML_ERROR_UNKNOWN}, 325 {'t', NVML_SUCCESS, TEMP, NVML_ERROR_UNKNOWN}, 326 {'h', NVML_SUCCESS, TEMP, NVML_ERROR_UNKNOWN}, 327 {'v', NVML_SUCCESS, TEMP, NVML_ERROR_NOT_SUPPORTED}, 328 {'z', NVML_SUCCESS, MEM, NVML_ERROR_NOT_SUPPORTED}, 329 {'x', NVML_SUCCESS, MEM, NVML_ERROR_NOT_SUPPORTED}, 330 {'m', NVML_SUCCESS, MEM, NVML_ERROR_UNKNOWN}, 331 {'w', NVML_SUCCESS, UTIL, NVML_ERROR_NOT_SUPPORTED}, 332 {'y', NVML_SUCCESS, UTIL, NVML_ERROR_NO_PERMISSION}, 333 {'e', NVML_SUCCESS, UTIL, NVML_ERROR_UNKNOWN}, 334 {'r', NVML_SUCCESS, UTIL, NVML_ERROR_UNKNOWN}, 335 {'Z', NVML_SUCCESS, UTIL, NVML_ERROR_UNKNOWN}, 336 {'X', NVML_SUCCESS, UTIL, NVML_ERROR_UNKNOWN}, 337 {'k', NVML_SUCCESS, PROCS, NVML_ERROR_NOT_SUPPORTED}, 338 {'l', NVML_SUCCESS, PROCS, NVML_SUCCESS}, 339 {'p', NVML_SUCCESS, PROCS, NVML_ERROR_UNKNOWN}, 340 {'b', NVML_SUCCESS, PROCS, NVML_SUCCESS}, 341 {'i', NVML_SUCCESS, PROCS, NVML_SUCCESS}, 342 {'j', NVML_SUCCESS, PROCS, NVML_ERROR_UNKNOWN}, 343 {'f', NVML_SUCCESS, NONE, 0}, 344 }; 345 346 static nvmlProcessInfo_t procs[256]; 347 348 static void one(const struct fault *f) { 349 char what[32], buf[96]; 350 unsigned n = 0, t = 0; 351 nvmlDevice_t d = NULL; 352 nvmlMemory_t m; 353 nvmlUtilization_t u; 354 snprintf(what, sizeof what, "fault %c", f->letter); 355 fake_reset(f->letter); 356 nvmlReturn_t r = nvmlInit_v2(); 357 CHECK(r == f->init, "%s: init rc %d, want %d", what, (int)r, (int)f->init); 358 if (r != NVML_SUCCESS) { 359 RC(nvmlDeviceGetCount_v2(&n), NVML_ERROR_UNINITIALIZED, what); 360 clean(what); 361 return; 362 } 363 if (f->call != COUNT) RC(nvmlDeviceGetHandleByIndex_v2(0, &d), NVML_SUCCESS, what); 364 switch (f->call) { 365 case NONE: 366 break; 367 case COUNT: 368 RC(nvmlDeviceGetCount_v2(&n), f->rc, what); 369 if (f->letter == '0') { 370 CHECK(n == 0, "%s: count %u, want 0", what, n); 371 RC(nvmlDeviceGetHandleByIndex_v2(0, &d), NVML_ERROR_INVALID_ARGUMENT, what); 372 } else { 373 CHECK(n == 2, "%s: count %u, want 2", what, n); 374 nvmlDevice_t d0 = NULL, d1 = NULL; 375 RC(nvmlDeviceGetHandleByIndex_v2(0, &d0), NVML_SUCCESS, what); 376 RC(nvmlDeviceGetHandleByIndex_v2(1, &d1), NVML_SUCCESS, what); 377 CHECK(d0 != d1, "%s: distinct handles", what); 378 RC(nvmlDeviceGetUUID(d1, buf, sizeof buf), NVML_SUCCESS, what); 379 CHECK(strcmp(buf, UUID1) == 0, "%s: uuid 1 \"%s\"", what, buf); 380 RC(nvmlDeviceGetUUID(d0, buf, sizeof buf), NVML_SUCCESS, what); 381 CHECK(strcmp(buf, UUID0) == 0, "%s: uuid 0 \"%s\"", what, buf); 382 RC(nvmlDeviceGetTemperature(d1, NVML_TEMPERATURE_GPU, &t), NVML_SUCCESS, what); 383 CHECK(t == 41, "%s: temperature 1 %u, want 41", what, t); 384 RC(nvmlDeviceGetHandleByIndex_v2(2, &d), NVML_ERROR_INVALID_ARGUMENT, what); 385 } 386 break; 387 case NAME: 388 RC(nvmlDeviceGetName(d, buf, sizeof buf), f->rc, what); 389 break; 390 case UUID: 391 RC(nvmlDeviceGetUUID(d, buf, sizeof buf), f->rc, what); 392 break; 393 case TEMP: 394 RC(nvmlDeviceGetTemperature(d, NVML_TEMPERATURE_GPU, &t), f->rc, what); 395 break; 396 case MEM: 397 RC(nvmlDeviceGetMemoryInfo(d, &m), f->rc, what); 398 break; 399 case UTIL: 400 RC(nvmlDeviceGetUtilizationRates(d, &u), f->rc, what); 401 break; 402 case PROCS: 403 n = 256; 404 memset(procs, 0, sizeof procs); 405 RC(nvmlDeviceGetComputeRunningProcesses_v3(d, &n, procs), f->rc, what); 406 if (f->letter == 'l') CHECK(n == 0, "%s: procs %u, want 0", what, n); 407 if (f->letter == 'b') { 408 CHECK(n == 250, "%s: procs %u, want 250", what, n); 409 unsigned bad = 0; 410 for (unsigned i = 0; i < 250; i++) 411 if (procs[i].pid != 1000 + i || 412 procs[i].usedGpuMemory != (unsigned long long)(1000 + i) * 65536ull) 413 bad++; 414 CHECK(bad == 0, "%s: %u of 250 entries wrong", what, bad); 415 n = 249; 416 RC(nvmlDeviceGetComputeRunningProcesses_v3(d, &n, procs), NVML_ERROR_INSUFFICIENT_SIZE, 417 what); 418 CHECK(n == 250, "%s: needed %u, want 250", what, n); 419 } 420 if (f->letter == 'i') { 421 CHECK(n == 2 && procs[0].usedGpuMemory == 351272960ull && 422 procs[1].pid == 4242 && 423 procs[1].usedGpuMemory == (unsigned long long)NVML_VALUE_NOT_AVAILABLE, 424 "%s: procs %u, memory %llu %llu", what, n, 425 (unsigned long long)procs[0].usedGpuMemory, 426 (unsigned long long)procs[1].usedGpuMemory); 427 } 428 break; 429 } 430 if (f->letter == 'f') { 431 RC(nvmlShutdown(), NVML_ERROR_UNKNOWN, what); 432 RC(nvmlDeviceGetCount_v2(&n), NVML_ERROR_UNINITIALIZED, what); 433 CHECK(fake_get(14) == 0, "%s: fds left open %llx", what, (unsigned long long)fake_get(14)); 434 CHECK(fake_get(13) == 0, "%s: violations %llu", what, (unsigned long long)fake_get(13)); 435 return; 436 } 437 RC(nvmlShutdown(), NVML_SUCCESS, what); 438 clean(what); 439 } 440 441 int main(int argc, char **argv) { 442 if (argc != 2) { 443 fprintf(stderr, "usage: nvml_test ERRSTR_ORACLE.tsv\n"); 444 return 2; 445 } 446 errstrings(argv[1]); 447 healthy(); 448 refcount(); 449 for (unsigned i = 0; i < sizeof FAULTS / sizeof FAULTS[0]; i++) one(&FAULTS[i]); 450 healthy(); 451 CHECK(fake_trapped() == 0, "fake trapped %llu times", (unsigned long long)fake_trapped()); 452 if (fails) { 453 printf("CUDAPP-NVML HOST FAIL (%u of %u checks)\n", fails, checks); 454 return 1; 455 } 456 printf("CUDAPP-NVML HOST PASS (%u checks)\n", checks); 457 return 0; 458 } 459 #endif