qa: update 660 to generate index.html, fixing pcp-testsuite runs
[pcp.git] / qa / src / nvidia-ml.c
blob4e75b747fa4b7c01d2223154d261c5921b42ae25
1 /*
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
12 * for more details.
14 #include <pcp/pmapi.h>
15 #include <pcp/impl.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]))
32 struct gputab {
33 char name[NVML_DEVICE_NAME_BUFFER_SIZE];
34 nvmlPciInfo_t pciinfo;
35 unsigned int fanspeed;
36 unsigned int temperature;
37 nvmlUtilization_t util;
38 nvmlPstates_t state;
39 nvmlMemory_t mem;
40 } gpu_table[] = {
42 .name = "GeForce 100M Series",
43 .pciinfo = {
44 .busId = "0:1:0x2:3:4",
45 .domain = 0,
46 .bus = 1,
47 .device = 0x2,
48 .pciDeviceId = 3,
49 .pciSubSystemId = 4,
51 .fanspeed = 5,
52 .temperature = 6,
53 .util = {
54 .gpu = 7,
55 .memory = 8,
57 .state = 9,
58 .mem = {
59 .total = 256 * 1024 * 1024,
60 .free = 156 * 1024 * 1024,
61 .used = 100 * 1024 * 1024,
65 .name = "Quadro FX 200M Series",
66 .pciinfo = {
67 .busId = "20:21:0x2:23:24",
68 .domain = 20,
69 .bus = 21,
70 .device = 0x22,
71 .pciDeviceId = 23,
72 .pciSubSystemId = 24,
74 .fanspeed = 25,
75 .temperature = 26,
76 .util = {
77 .gpu = 27,
78 .memory = 28,
80 .state = 29,
81 .mem = {
82 .total = 8ULL * 1024 * 1024 * 1024,
83 .free = 2ULL * 1024 * 1024 * 1024,
84 .used = 6ULL * 1024 * 1024 * 1024,
89 static int refcount;
91 int
92 nvmlInit(void)
94 refcount++;
95 if (pmDebug & DBG_TRACE_APPL0)
96 fprintf(stderr, "qa-nvidia-ml: nvmlInit [%d - %d]\n",
97 refcount - 1, refcount);
98 return 0;
102 nvmlShutdown(void)
104 refcount--;
105 if (pmDebug & DBG_TRACE_APPL0)
106 fprintf(stderr, "qa-nvidia-ml: nvmlShutdown [%d - %d]\n",
107 refcount + 1, refcount);
108 return NVML_SUCCESS;
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);
117 return NVML_SUCCESS;
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);
132 CHECK_INDEX(index);
133 *dp = &gpu_table[index];
134 return NVML_SUCCESS;
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");
143 CHECK_DEVICE(dev);
144 strncpy(buffer, dev->name, length);
145 buffer[length-1] = '\0';
146 return NVML_SUCCESS;
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");
155 CHECK_DEVICE(dev);
156 *info = dev->pciinfo;
157 return NVML_SUCCESS;
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");
166 CHECK_DEVICE(dev);
167 *speed = dev->fanspeed;
168 return NVML_SUCCESS;
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");
177 CHECK_DEVICE(dev);
178 if (sensor >= NVML_TEMPERATURE_COUNT)
179 return NVML_ERROR_INVALID_ARGUMENT;
180 *value = dev->temperature;
181 return NVML_SUCCESS;
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");
190 CHECK_DEVICE(dev);
191 *util = dev->util;
192 return NVML_SUCCESS;
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");
201 CHECK_DEVICE(dev);
202 *mem = dev->mem;
203 return NVML_SUCCESS;
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");
212 CHECK_DEVICE(dev);
213 *state = dev->state;
214 return NVML_SUCCESS;