2 * Copyright (c) 2014 Red Hat.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 #include <pcp/pmapi.h>
16 #include "localnvml.h"
19 * Testing library for exercising the NVIDIA GPU PMDA. By injecting this
20 * library into the PMDA to supply values that the real nvidia-ml usually
21 * would, we are able to obtain synthesized statistics and exercise many
22 * of the code paths through pmda_nvidia. No substitute for using actual
23 * hardware, of course - but better than a poke in the eye with a sharp
24 * stick (or worse, no testing at all).
28 * Table of the GPU hardware we'll be faking.
29 * Using simple static values here so that tests are deterministic.
31 #define NUM_GPUS (sizeof(gpu_table)/sizeof(gpu_table[0]))
33 char name
[NVML_DEVICE_NAME_BUFFER_SIZE
];
34 nvmlPciInfo_t pciinfo
;
35 unsigned int fanspeed
;
36 unsigned int temperature
;
37 nvmlUtilization_t util
;
42 .name
= "GeForce 100M Series",
44 .busId
= "0:1:0x2:3:4",
59 .total
= 256 * 1024 * 1024,
60 .free
= 156 * 1024 * 1024,
61 .used
= 100 * 1024 * 1024,
65 .name
= "Quadro FX 200M Series",
67 .busId
= "20:21:0x2:23:24",
82 .total
= 8ULL * 1024 * 1024 * 1024,
83 .free
= 2ULL * 1024 * 1024 * 1024,
84 .used
= 6ULL * 1024 * 1024 * 1024,
95 if (pmDebug
& DBG_TRACE_APPL0
)
96 fprintf(stderr
, "qa-nvidia-ml: nvmlInit [%d - %d]\n",
97 refcount
- 1, refcount
);
105 if (pmDebug
& DBG_TRACE_APPL0
)
106 fprintf(stderr
, "qa-nvidia-ml: nvmlShutdown [%d - %d]\n",
107 refcount
+ 1, refcount
);
112 nvmlDeviceGetCount(unsigned int *count
)
114 *count
= sizeof(gpu_table
) / sizeof(gpu_table
[0]);
115 if (pmDebug
& DBG_TRACE_APPL0
)
116 fprintf(stderr
, "qa-nvidia-ml: nvmlDeviceGetCount [%u]\n", *count
);
120 #define CHECK_INDEX(index) { \
121 if ((index) >= NUM_GPUS) return NVML_ERROR_GPU_IS_LOST; }
123 #define CHECK_DEVICE(devp) { \
124 if ((devp) < gpu_table) return NVML_ERROR_INVALID_ARGUMENT; \
125 if ((devp) >= gpu_table + NUM_GPUS) return NVML_ERROR_GPU_IS_LOST; }
128 nvmlDeviceGetHandleByIndex(unsigned int index
, nvmlDevice_t
*dp
)
130 if (pmDebug
& DBG_TRACE_APPL0
)
131 fprintf(stderr
, "qa-nvidia-ml: nvmlDeviceGetHandleByIndex %u\n", index
);
133 *dp
= &gpu_table
[index
];
138 nvmlDeviceGetName(nvmlDevice_t device
, char *buffer
, unsigned int length
)
140 struct gputab
*dev
= (struct gputab
*)device
;
141 if (pmDebug
& DBG_TRACE_APPL0
)
142 fprintf(stderr
, "qa-nvidia-ml: nvmlDeviceGetName\n");
144 strncpy(buffer
, dev
->name
, length
);
145 buffer
[length
-1] = '\0';
150 nvmlDeviceGetPciInfo(nvmlDevice_t device
, nvmlPciInfo_t
*info
)
152 struct gputab
*dev
= (struct gputab
*)device
;
153 if (pmDebug
& DBG_TRACE_APPL0
)
154 fprintf(stderr
, "qa-nvidia-ml: nvmlDeviceGetPciInfo\n");
156 *info
= dev
->pciinfo
;
161 nvmlDeviceGetFanSpeed(nvmlDevice_t device
, unsigned int *speed
)
163 struct gputab
*dev
= (struct gputab
*)device
;
164 if (pmDebug
& DBG_TRACE_APPL0
)
165 fprintf(stderr
, "qa-nvidia-ml: nvmlDeviceGetFanSpeed\n");
167 *speed
= dev
->fanspeed
;
172 nvmlDeviceGetTemperature(nvmlDevice_t device
, nvmlTemperatureSensors_t sensor
, unsigned int *value
)
174 struct gputab
*dev
= (struct gputab
*)device
;
175 if (pmDebug
& DBG_TRACE_APPL0
)
176 fprintf(stderr
, "qa-nvidia-ml: nvmlDeviceGetTemperature\n");
178 if (sensor
>= NVML_TEMPERATURE_COUNT
)
179 return NVML_ERROR_INVALID_ARGUMENT
;
180 *value
= dev
->temperature
;
185 nvmlDeviceGetUtilizationRates(nvmlDevice_t device
, nvmlUtilization_t
*util
)
187 struct gputab
*dev
= (struct gputab
*)device
;
188 if (pmDebug
& DBG_TRACE_APPL0
)
189 fprintf(stderr
, "qa-nvidia-ml: nvmlDeviceGetUtilizationRates\n");
196 nvmlDeviceGetMemoryInfo(nvmlDevice_t device
, nvmlMemory_t
*mem
)
198 struct gputab
*dev
= (struct gputab
*)device
;
199 if (pmDebug
& DBG_TRACE_APPL0
)
200 fprintf(stderr
, "qa-nvidia-ml: nvmlDeviceGetMemoryInfo\n");
207 nvmlDeviceGetPerformanceState(nvmlDevice_t device
, nvmlPstates_t
*state
)
209 struct gputab
*dev
= (struct gputab
*)device
;
210 if (pmDebug
& DBG_TRACE_APPL0
)
211 fprintf(stderr
, "qa-nvidia-ml: nvmlDeviceGetPerformanceState\n");