-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdump_fb_test.cpp
434 lines (352 loc) · 12.4 KB
/
dump_fb_test.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
421
422
423
424
425
426
427
428
429
430
431
432
433
/////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2014 NVidia Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
/////////////////////////////////////////////////////////////////////////////////
//
//
// v1.0, first distribution on 9/18/2014.
// Added check to make sure application is running with root privs, which is
// required for correct operation. Clarified some error conditions and improved
// checks for bad GPU UUID values.
// All 9/14/2014 by Golden G. Richard III / @nolaforensix.
//
//
#include "gtest/gtest.h"
extern "C" {
#include "common-utils.h"
#include "nvgetopt.h"
}
#include "dump_fb.h"
#include "uvm.h"
#include <nvml.h>
#include <stdlib.h>
#include <malloc.h>
#include <sys/mman.h>
#include <time.h>
static const NvU32 PAGE_SIZE = 4096;
const char* uuid = NULL;
class DumpFbTest : public ::testing::Test {
public:
void SetUp();
void TearDown();
protected:
UvmGpuUuid uvmUuid;
};
int getNumGpus() {
unsigned int gpuCount = 0;
nvmlReturn_t nvmlStatus = nvmlDeviceGetCount(&gpuCount);
if (nvmlStatus != NVML_SUCCESS) {
nv_error_msg("Could not get GPU count\n");
return -1;
}
return gpuCount;
}
void nvmlUuidToUvmUuid(const char* nvmlUuid, UvmGpuUuid* uvmUuid) {
sscanf(nvmlUuid, "GPU-%2x%2x%2x%2x-%2x%2x-%2x%2x-%2x%2x-%2x%2x%2x%2x%2x%2x",
(unsigned int*) &uvmUuid->uuid[0],
(unsigned int*) &uvmUuid->uuid[1],
(unsigned int*) &uvmUuid->uuid[2],
(unsigned int*) &uvmUuid->uuid[3],
(unsigned int*) &uvmUuid->uuid[4],
(unsigned int*) &uvmUuid->uuid[5],
(unsigned int*) &uvmUuid->uuid[6],
(unsigned int*) &uvmUuid->uuid[7],
(unsigned int*) &uvmUuid->uuid[8],
(unsigned int*) &uvmUuid->uuid[9],
(unsigned int*) &uvmUuid->uuid[10],
(unsigned int*) &uvmUuid->uuid[11],
(unsigned int*) &uvmUuid->uuid[12],
(unsigned int*) &uvmUuid->uuid[13],
(unsigned int*) &uvmUuid->uuid[14],
(unsigned int*) &uvmUuid->uuid[15]);
}
const char* getRequestedUuid(const char* uuid) {
unsigned int gpuCount = 0;
unsigned int i;
nvmlReturn_t nvmlStatus;
char devuuid[NVML_DEVICE_UUID_BUFFER_SIZE];
char *retuuid = NULL;
gpuCount = getNumGpus();
if (strlen(uuid) >= NVML_DEVICE_UUID_BUFFER_SIZE) {
printf("Requested UUID is too long (Max UUID length %d)\n",
NVML_DEVICE_UUID_BUFFER_SIZE);
return NULL;
}
for (i = 0; i < gpuCount; i++) {
nvmlDevice_t device;
nvmlStatus = nvmlDeviceGetHandleByIndex(i, &device);
if (nvmlStatus != NVML_SUCCESS) {
printf("Could not retrieve device index %d\n", i);
continue;
}
nvmlStatus = nvmlDeviceGetUUID(device, devuuid, sizeof(devuuid));
if (nvmlStatus != NVML_SUCCESS) {
printf("Could not retrieve UUID for device index %d\n", i);
continue;
}
if (strncmp(uuid, &devuuid[4], strlen(uuid)) == 0) {
if (retuuid) {
printf("Ambiguous UUID fragment\n");
free(retuuid);
return NULL;
}
retuuid = strdup(devuuid);
}
}
return retuuid;
}
void DumpFbTest::SetUp() {
ASSERT_EQ(nvmlInit(), NVML_SUCCESS);
ASSERT_EQ(UvmInitialize(), RM_OK);
const char *u = getRequestedUuid(uuid);
if (! u) {
nv_error_msg("Bad GPU UUID. Use nvidia-smi -L to see\n"
"a list of UUIDs. Omit the \"GPU-\" portion for -g.\n");
exit(-1);
}
nvmlUuidToUvmUuid(u, &uvmUuid);
}
void DumpFbTest::TearDown() {
UvmDeinitialize();
nvmlShutdown();
}
TEST_F(DumpFbTest, ZeroLength) {
void* ptr = mmap(NULL, PAGE_SIZE,
PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE,
-1, 0);
ASSERT_TRUE(ptr);
memset(ptr, 0x42, PAGE_SIZE);
ASSERT_EQ(UvmDumpGpuMemory(&uvmUuid, ptr, 0, 0),
(RM_STATUS)RM_OK);
ASSERT_EQ(*(NvU8*)ptr, 0x42);
munmap(ptr, PAGE_SIZE);
}
TEST_F(DumpFbTest, NullPtr) {
ASSERT_NE(UvmDumpGpuMemory(&uvmUuid, NULL, 0, PAGE_SIZE),
(RM_STATUS)RM_OK);
}
TEST_F(DumpFbTest, RandomPtr) {
void* ptr = (void*) (random() & 0xFFFFFFFF);
ASSERT_NE(UvmDumpGpuMemory(&uvmUuid, ptr, 0, PAGE_SIZE),
(RM_STATUS)RM_OK);
}
TEST_F(DumpFbTest, ReadOnly) {
void* ptr = mmap(NULL, PAGE_SIZE, PROT_READ, MAP_ANONYMOUS|MAP_PRIVATE,
-1, 0);
ASSERT_NE(ptr, MAP_FAILED);
ASSERT_NE(UvmDumpGpuMemory(&uvmUuid, ptr, 0, PAGE_SIZE),
(RM_STATUS)RM_OK);
munmap(ptr, PAGE_SIZE);
}
TEST_F(DumpFbTest, BasicTest) {
void* ptr = mmap(NULL, PAGE_SIZE,
PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE,
-1, 0);
ASSERT_NE(ptr, MAP_FAILED);
ASSERT_EQ(UvmDumpGpuMemory(&uvmUuid, ptr, 0, PAGE_SIZE),
(RM_STATUS)RM_OK);
munmap(ptr, PAGE_SIZE);
}
TEST_F(DumpFbTest, LargeTest) {
void* ptr = mmap(NULL, 64*1024*1024,
PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE,
-1, 0);
ASSERT_NE(ptr, MAP_FAILED);
ASSERT_EQ(UvmDumpGpuMemory(&uvmUuid, ptr, 0, 64*1024*1024),
(RM_STATUS)RM_OK);
munmap(ptr, 64*1024*1024);
}
// Relies on the first 4 KB pages of FB not being the same
TEST_F(DumpFbTest, OffsetTest) {
void* ptr = mmap(NULL, PAGE_SIZE,
PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE,
-1, 0);
void* ptr2 = mmap(NULL, PAGE_SIZE,
PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE,
-1, 0);
ASSERT_NE(ptr, MAP_FAILED);
ASSERT_NE(ptr2, MAP_FAILED);
ASSERT_EQ(UvmDumpGpuMemory(&uvmUuid, ptr, 0, PAGE_SIZE),
(RM_STATUS)RM_OK);
ASSERT_EQ(UvmDumpGpuMemory(&uvmUuid, ptr2, PAGE_SIZE, PAGE_SIZE),
(RM_STATUS)RM_OK);
ASSERT_NE(memcmp(ptr, ptr2, PAGE_SIZE), 0);
}
//
// Copies the same FB location multiple times, relies on someone not changing
// the memory while testing. We use address 0, which may be where VGA lives.
// So it is possible this may fail when using the GPU as a VGA adapter.
//
TEST_F(DumpFbTest, ConsistencyCheck) {
void* ptr = mmap(NULL, PAGE_SIZE,
PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE,
-1, 0);
void* ptr2 = mmap(NULL, PAGE_SIZE,
PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE,
-1, 0);
ASSERT_NE(ptr, MAP_FAILED);
ASSERT_NE(ptr2, MAP_FAILED);
ASSERT_EQ(UvmDumpGpuMemory(&uvmUuid, ptr, 0, PAGE_SIZE),
(RM_STATUS)RM_OK);
ASSERT_EQ(UvmDumpGpuMemory(&uvmUuid, ptr2, 0, PAGE_SIZE),
(RM_STATUS)RM_OK);
ASSERT_EQ(memcmp(ptr, ptr2, PAGE_SIZE), 0);
}
TEST_F(DumpFbTest, UnalignedCpuAddress) {
void* ptr = mmap(NULL, PAGE_SIZE,
PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE,
-1, 0);
ASSERT_NE(UvmDumpGpuMemory(&uvmUuid, ptr, 1, PAGE_SIZE),
(RM_STATUS)RM_OK);
munmap(ptr, PAGE_SIZE);
}
TEST_F(DumpFbTest, OverflowTest) {
void* ptr = mmap(NULL, PAGE_SIZE,
PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE,
-1, 0);
unsigned long long size = (0ll) - (unsigned long long) ptr + 1;
ASSERT_NE(UvmDumpGpuMemory(&uvmUuid, ptr, 1, size),
(RM_STATUS)RM_OK);
munmap(ptr, PAGE_SIZE);
}
class PerformanceTest : public ::testing::TestWithParam<NvLength> {
public:
static void SetUpTestCase();
static void TearDownTestCase();
static UvmGpuUuid uvmUuid;
void SetUp();
void TearDown();
protected:
void* ptr;
};
UvmGpuUuid PerformanceTest::uvmUuid;
// Share the init as it takes a while (has to load the driver)
void PerformanceTest::SetUpTestCase() {
ASSERT_EQ(nvmlInit(), NVML_SUCCESS);
ASSERT_EQ(UvmInitialize(), RM_OK);
const char *u = getRequestedUuid(uuid);
if (! u) {
nv_error_msg("Bad GPU UUID. Use nvidia-smi -L to see\n"
"a list of UUIDs. Omit the \"GPU-\" portion for -g.\n");
exit(-1);
}
nvmlUuidToUvmUuid(u, &uvmUuid);
}
void PerformanceTest::TearDownTestCase() {
UvmDeinitialize();
nvmlShutdown();
}
void PerformanceTest::SetUp() {
ptr = mmap(NULL, GetParam(), PROT_READ|PROT_WRITE,
MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
}
void PerformanceTest::TearDown() {
munmap(ptr, GetParam());
}
unsigned long diff(const timespec start, const timespec end) {
unsigned long t1 = start.tv_sec*1000000000 + start.tv_nsec;
unsigned long t2 = end.tv_sec*1000000000 + end.tv_nsec;
return t2-t1;
}
TEST_P(PerformanceTest, TestBandwidth) {
timespec time1, time2;
clock_gettime(CLOCK_REALTIME, &time1);
ASSERT_EQ(UvmDumpGpuMemory(&uvmUuid, ptr, 0, GetParam()),
(RM_STATUS)RM_OK);
clock_gettime(CLOCK_REALTIME, &time2);
unsigned long elapsed = diff(time1, time2);
std::cout << elapsed/1000000.0 << "ms\n";
std::cout << (GetParam()/(1024.0*1024*1024))/(elapsed/1000000000.0) << "GB/s\n";
}
TEST_P(PerformanceTest, TestBandwidthLocked) {
timespec time1, time2;
EXPECT_EQ(mlock(ptr, GetParam()), 0);
clock_gettime(CLOCK_REALTIME, &time1);
ASSERT_EQ(UvmDumpGpuMemory(&uvmUuid, ptr, 0, GetParam()),
(RM_STATUS)RM_OK);
clock_gettime(CLOCK_REALTIME, &time2);
munlock(ptr, GetParam());
unsigned long elapsed = diff(time1, time2);
std::cout << elapsed/1000000.0 << "ms\n";
std::cout << (GetParam()/(1024.0*1024*1024))/(elapsed/1000000000.0) << "GB/s\n";
}
INSTANTIATE_TEST_CASE_P(PerformanceTest, PerformanceTest,
::testing::Values(4096, 1024*1024, 64*1024*1024,
128*1024*1024, 1024*1024*1024));
static const NVGetoptOption __options[] = {
{ "help",
'h',
NVGETOPT_HELP_ALWAYS,
NULL,
"Print usage information for the command line options and exit." },
{ "gpu",
'g',
NVGETOPT_STRING_ARGUMENT | NVGETOPT_HELP_ALWAYS,
"GPU-UUID",
"The GPU UUID to extract data from. To get the available UUIDs "
"use nvidia-smi -L (note don't include the \"GPU-\" prefix"
},
{ NULL, 0, 0, NULL, NULL },
};
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
while (1) {
int c, intval;
char *strval = NULL;
c = nvgetopt(argc,
argv,
__options,
&strval, /* strval */
NULL, /* boolval */
&intval,
NULL, /* doubleval */
NULL); /* disable */
if (c == -1) break;
switch (c) {
case 'g':
uuid = strval;
break;
default:
nv_error_msg("Invalid commandline, please run `%s --help` "
"for usage information.\n", argv[0]);
return -1;
}
}
if (!uuid) {
nv_error_msg("Must provide a UUID using -g. Use nvidia-smi -L to see "
"a list of UUIDs. Omit the \"GPU-\" portion for -g.\n");
return -1;
}
if (getuid() != 0 && geteuid() != 0 ) {
nv_error_msg("Must be run with root privileges. Try sudo ./dump_fb_test <args> instead.\n");
return -1;
}
return RUN_ALL_TESTS();
}