-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclocks.c
293 lines (267 loc) · 7.95 KB
/
clocks.c
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
/*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License") 1.1!
* You may not use this file except in compliance with the License.
*
* See https://spdx.org/licenses/CDDL-1.1.html for the specific
* language governing permissions and limitations under the License.
*
* Copyright 2021 Jens Elkner ([email protected])
*/
#include <stdlib.h>
#include <string.h>
#include <values.h>
#include <assert.h>
#include "clocks.h"
#ifndef MININT
#define MININT (-MAXINT - 1)
#endif
static const char *domain[] = { "GRAPHICS", "SM", "MEM", "VIDEO" };
static void
getMinMaxClock(nvmlDevice_t gpu, int *minMem, int *maxMem,
int *minGra, int *maxGra)
{
nvmlReturn_t res;
uint *clocks, k;
*maxMem = *maxGra = MININT;
*minMem = *minGra = MAXINT;
// list of allowed memory clocks. Kepler+
clocks = NULL;
k = 0;
res = nvmlDeviceGetSupportedMemoryClocks(gpu, &k, clocks);
if (NVML_ERROR_INSUFFICIENT_SIZE == res) {
clocks = malloc(sizeof(uint) * k);
res = nvmlDeviceGetSupportedMemoryClocks(gpu, &k, clocks);
}
if (NVML_SUCCESS != res || k == 0) {
PROM_DEBUG("%s.clock min/max: %s",domain[NVML_CLOCK_MEM],nverror(res));
goto end;
}
*maxMem = clocks[0];
*minMem = clocks[k-1];
PROM_DEBUG("Mem: Min=%d max=%d", *minMem, *maxMem);
free(clocks);
clocks = NULL;
k = 0;
res = nvmlDeviceGetSupportedGraphicsClocks(gpu, *minMem, &k, clocks);
if (NVML_ERROR_INSUFFICIENT_SIZE == res) {
clocks = malloc(sizeof(uint) * k);
res = nvmlDeviceGetSupportedGraphicsClocks(gpu, *minMem, &k, clocks);
}
if (NVML_SUCCESS == res && k > 0)
*minGra = clocks[k-1];
else
PROM_DEBUG("%s.clock min: %s",domain[NVML_CLOCK_GRAPHICS],nverror(res));
free(clocks);
clocks = NULL;
k = 0;
res = nvmlDeviceGetSupportedGraphicsClocks(gpu, *maxMem, &k, clocks);
if (NVML_ERROR_INSUFFICIENT_SIZE == res) {
clocks = malloc(sizeof(uint) * k);
res = nvmlDeviceGetSupportedGraphicsClocks(gpu, *maxMem, &k, clocks);
}
if (NVML_SUCCESS == res && k > 0)
*maxGra = clocks[0];
else
PROM_DEBUG("%s.clock max: %s",domain[NVML_CLOCK_GRAPHICS],nverror(res));
PROM_DEBUG("Graphics: Min=%d max=%d", *minGra, *maxGra);
end:
free(clocks);
clocks = NULL;
}
static uint
getBoostClock(nvmlDevice_t gpu, uint clock) {
nvmlReturn_t res;
uint clockMHz;
// maximum clock speeds for the device. Fermi+
res = nvmlDeviceGetMaxClockInfo(gpu, clock, &clockMHz);
if (NVML_SUCCESS == res && clockMHz != 0) {
PROM_DEBUG("maxClock[%u] = %u", clock, clockMHz);
return clockMHz;
}
// customer defined max boost clock. Pascal+
res = nvmlDeviceGetMaxCustomerBoostClock(gpu, clock, &clockMHz);
if (NVML_SUCCESS == res) {
PROM_DEBUG("maxBoost[%u] = %u", clock, clockMHz);
return clockMHz;
}
return 0;
}
static void
setStaticClockVals(gpu_t *gpu, bool compact) {
nvmlReturn_t res;
char buf[MBUF_SZ << 1], *p;
int minMem, maxMem, minGra, maxGra;
uint boost, k;
if (gpu->minMaxClock == NULL) {
size_t sz = sizeof(char *) * NVML_CLOCK_COUNT;
char **c = (char **) malloc(sz);
if (c == NULL)
return;
memset(c, 0, sz);
gpu->minMaxClock = c;
c = (char **) malloc(sz);
if (c == NULL)
return;
memset(c, 0, sz);
gpu->defaultClock = c;
}
PROM_DEBUG("Getting static clocks for gpu[%u]", gpu->idx);
getMinMaxClock(gpu->dev, &minMem, &maxMem, &minGra, &maxGra);
k = NVML_CLOCK_MEM;
boost = getBoostClock(gpu->dev, k);
if (maxMem == MININT)
maxMem = boost;
else if (boost == 0)
boost = maxMem;
if (maxMem != (int) boost) {
PROM_WARN("mem.clock.max != mem.clock.boost (%d != %d)", maxMem, boost);
if ((int) boost > maxMem)
maxMem = boost;
}
p = buf;
if (maxMem > 0) {
p += sprintf(buf, NVMEXM_CLOCK_N
"{gpu=\"%d\",domain=\"%s\",clock=\"max\",uuid=\"%s\"} %d\n",
gpu->idx, domain[k], gpu->uuid, maxMem);
} else if (compact) {
buf[0] = '\0';
} else {
p += sprintf(buf, "# %s.clock.max n/a\n", domain[k]);
}
if (minMem != MAXINT) {
p += snprintf(p, sizeof(buf) - (p - buf), NVMEXM_CLOCK_N
"{gpu=\"%d\",domain=\"%s\",clock=\"min\",uuid=\"%s\"} %d\n",
gpu->idx, domain[k], gpu->uuid, minMem);
} else if (!compact) {
p += snprintf(p, sizeof(buf) - (p - buf), "# %s.clock.min n/a\n",
domain[k]);
}
gpu->minMaxClock[k] = strdup(buf);
k = NVML_CLOCK_GRAPHICS;
boost = getBoostClock(gpu->dev, k);
if (maxGra == MININT)
maxGra = boost;
else if (boost == 0)
boost = maxGra;
if (maxGra != (int) boost) {
PROM_WARN("gra.clock.max != gra.clock.boost (%d != %d)", maxGra, boost);
if ((int) boost > maxGra)
maxGra = boost;
}
p = buf;
if (maxGra > 0) {
p += sprintf(p, NVMEXM_CLOCK_N
"{gpu=\"%d\",domain=\"%s\",clock=\"max\",uuid=\"%s\"} %d\n",
gpu->idx, domain[k], gpu->uuid, maxGra);
} else if (compact) {
buf[0] = '\0';
} else {
p += sprintf(p, "# %s.clock.max n/a\n", domain[k]);
}
if (minGra != MAXINT) {
p += snprintf(p, sizeof(buf) - (p - buf), NVMEXM_CLOCK_N
"{gpu=\"%d\",domain=\"%s\",clock=\"min\",uuid=\"%s\"} %d\n",
gpu->idx, domain[k], gpu->uuid, minGra);
} else if (!compact) {
p += snprintf(p, sizeof(buf)-(p-buf),"# %s.clock.min n/a\n", domain[k]);
}
gpu->minMaxClock[k] = strdup(buf);
for (k = 0; k < NVML_CLOCK_COUNT; k++) {
// value that GPU boots with or defaults to. Kepler+
res = nvmlDeviceGetDefaultApplicationsClock(gpu->dev, k, &boost);
if (NVML_SUCCESS == res) {
sprintf(buf, NVMEXM_CLOCK_N
"{gpu=\"%d\",domain=\"%s\",clock=\"default\",uuid=\"%s\"} %u\n",
gpu->idx, domain[k], gpu->uuid, boost);
} else if (compact) {
buf[0] = '\0';
} else {
sprintf(buf, "# %s.clock.default n/a\n", domain[k]);
}
gpu->defaultClock[k] = strdup(buf);
if (k == NVML_CLOCK_MEM || k == NVML_CLOCK_GRAPHICS)
continue;
boost = getBoostClock(gpu->dev, NVML_CLOCK_GRAPHICS);
if (boost == 0) {
if (compact)
buf[0] = '\0';
else
sprintf(buf, "# %s.clock.max n/a\n", domain[k]);
} else {
sprintf(buf, NVMEXM_CLOCK_N
"{gpu=\"%d\",domain=\"%s\",clock=\"max\",uuid=\"%s\"} %u\n",
gpu->idx, domain[k], gpu->uuid, boost);
}
gpu->minMaxClock[k] = strdup(buf);
}
}
bool
getClocks(psb_t *sb, bool compact, uint devs, gpu_t devList[]) {
nvmlReturn_t res;
uint clockMHz, i, k;
char buf[MBUF_SZ];
gpu_t *gpu;
bool free_sb = sb == NULL;
size_t sz;
assert(NVML_CLOCK_COUNT == 4);
if (devs == 0)
return false;
if (free_sb)
sb = psb_new();
sz = psb_len(sb);
if (!compact)
addPromInfo(NVMEXM_CLOCK);
for (i = 0; i < devs; i++) {
gpu = &(devList[i]);
if (gpu->dev == NULL)
continue;
if (gpu->minMaxClock == NULL || gpu->defaultClock == NULL)
setStaticClockVals(gpu, compact);
for (k = 0; k < NVML_CLOCK_COUNT; k++) {
psb_add_str(sb, (gpu->defaultClock)[k]);
psb_add_str(sb, gpu->minMaxClock[k]);
// current clock speed for the device. Fermi+
res = nvmlDeviceGetClockInfo(gpu->dev, k, &clockMHz);
if (NVML_SUCCESS == res) {
snprintf(buf, MBUF_SZ, NVMEXM_CLOCK_N
"{gpu=\"%d\",domain=\"%s\",clock=\"now\",uuid=\"%s\"} %u\n",
i, domain[k], gpu->uuid, clockMHz);
psb_add_str(sb, buf);
}
// value to use unless an overspec situation. Kepler+
res = nvmlDeviceGetApplicationsClock(gpu->dev, k, &clockMHz);
if (NVML_SUCCESS == res) {
snprintf(buf, MBUF_SZ, NVMEXM_CLOCK_N
"{gpu=\"%d\",domain=\"%s\",clock=\"set\",uuid=\"%s\"} %u\n",
i, domain[k], gpu->uuid, clockMHz);
psb_add_str(sb, buf);
}
}
}
if (!compact)
addPromInfo(NVMEXM_CLOCK_THROTTLE);
for (i = 0; i < devs; i++) {
unsigned long long reasons;
gpu = &(devList[i]);
if (gpu->dev == NULL || gpu->hasClockThrottle == -1)
continue;
res = nvmlDeviceGetCurrentClocksThrottleReasons(gpu->dev, &reasons);
if (NVML_SUCCESS == res) {
snprintf(buf, MBUF_SZ,
NVMEXM_CLOCK_THROTTLE_N "{gpu=\"%d\",uuid=\"%s\"} %lld\n",
i, gpu->uuid, reasons);
psb_add_str(sb, buf);
gpu->hasClockThrottle = 1;
} else if (NOT_AVAIL(res)) {
PROM_DEBUG("gpu.hasClockThrottle = -1", "");
gpu->hasClockThrottle = -1;
}
}
sz = psb_len(sb) - sz;
if (free_sb) {
fprintf(stdout, "\n%s", psb_str(sb));
psb_destroy(sb);
}
return sz != 0;
}