forked from intel/intel-device-plugins-for-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzes.c
More file actions
448 lines (342 loc) · 11.3 KB
/
zes.c
File metadata and controls
448 lines (342 loc) · 11.3 KB
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
// Copyright 2024 Intel Corporation. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <sys/time.h>
#include <stdlib.h>
#include <zes_api.h>
#include "ze.h"
#define MAX_BDF_BUFSIZE 32
struct device_info {
char bdf[MAX_BDF_BUFSIZE];
};
zes_device_handle_t* zes_handles = NULL;
struct device_info* bdf_addresses = NULL;
uint32_t zes_handles_count = 0;
static bool device_enumerated = false;
typedef enum {
LOG_ERROR = 1,
LOG_WARNING,
LOG_INFO,
LOG_DEBUG
} log_level_t;
static log_level_t verbosity_level = LOG_ERROR;
static void print_log(log_level_t level, char* fmt, ...) __attribute__ ((format (printf, 2, 3)));
static void print_log(log_level_t level, char* fmt, ...)
{
if (verbosity_level >= level) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
}
}
void zes_set_verbosity(const int level)
{
verbosity_level = level;
fprintf(stderr, "C set verbosity level: %d\n", verbosity_level);
}
bool zes_try_initialize(void)
{
if (getenv("UNITTEST") != NULL) {
return false;
}
return zesInit(0) == ZE_RESULT_SUCCESS;
}
static ze_result_t enumerate_zes_devices(void)
{
ze_result_t res = zesInit(0);
if (res != ZE_RESULT_SUCCESS) {
return res;
}
uint32_t count = 0;
res = zesDriverGet(&count, NULL);
if (res != ZE_RESULT_SUCCESS) {
return res;
}
if (count == 0) {
return ZE_RESULT_ERROR_NOT_AVAILABLE;
}
if (count > 1) {
print_log(LOG_WARNING, "more than one zes driver detected, using first one\n");
}
count = 1;
zes_driver_handle_t handle;
res = zesDriverGet(&count, &handle);
if (res != ZE_RESULT_SUCCESS) {
return res;
}
count = 0;
res = zesDeviceGet(handle, &count, NULL);
if (res != ZE_RESULT_SUCCESS) {
return res;
}
if (count == 0) {
return ZE_RESULT_ERROR_NOT_AVAILABLE;
}
zes_handles = calloc(count, sizeof(zes_device_handle_t));
if (zes_handles == NULL) {
return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
res = zesDeviceGet(handle, &count, zes_handles);
if (res != ZE_RESULT_SUCCESS) {
free(zes_handles);
return res;
}
zes_handles_count = count;
bdf_addresses = (struct device_info*) calloc(count,sizeof(struct device_info));
if (bdf_addresses == NULL) {
free(zes_handles);
return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
// Iterate over the devices and store their info into the cache array
for (uint32_t i = 0; i < count; ++i) {
zes_device_handle_t dev_h = zes_handles[i];
zes_pci_properties_t pci_props = {
.pNext = NULL,
};
if (zesDevicePciGetProperties(dev_h, &pci_props) != ZE_RESULT_SUCCESS) {
print_log(LOG_WARNING, "Failed to get PCI properties for device %d: %X\n", i, res);
continue;
}
zes_pci_address_t* addr = &pci_props.address;
snprintf(bdf_addresses[i].bdf, sizeof(bdf_addresses[i].bdf),
"%04x:%02x:%02x.%x",
addr->domain, addr->bus, addr->device, addr->function
);
}
device_enumerated = true;
return res;
}
static zes_device_handle_t retrieve_handle_for_bdf(char* bdf_address)
{
zes_device_handle_t handle = 0;
for (uint32_t i = 0; i < zes_handles_count; ++i) {
struct device_info* di = &bdf_addresses[i];
if (strncmp(bdf_address, di->bdf, sizeof(di->bdf)) == 0) {
handle = zes_handles[i];
break;
}
}
return handle;
}
static bool is_integrated(zes_device_handle_t handle)
{
ze_result_t res = ZE_RESULT_SUCCESS;
zes_device_ext_properties_t ext = {
.stype = ZES_STRUCTURE_TYPE_DEVICE_EXT_PROPERTIES,
};
zes_device_properties_t props = {
.stype = ZES_STRUCTURE_TYPE_DEVICE_PROPERTIES,
.pNext = &ext,
};
if (res = zesDeviceGetProperties(handle, &props), res == ZE_RESULT_SUCCESS) {
if (ext.flags & ZES_DEVICE_PROPERTY_FLAG_INTEGRATED) {
return true;
}
}
return false;
}
/// @brief Retrieves memory amount for a specific device with bdf address
/// @param bdf_address
/// @return memory amount for the device
uint64_t zes_device_memory_amount(char* bdf_address, uint32_t* error)
{
if (getenv("UNITTEST") != NULL) {
return 0;
}
print_log(LOG_DEBUG, "Retrieve memory size for %s\n", bdf_address);
ze_result_t res = ZE_RESULT_SUCCESS;
if (!device_enumerated) {
res = enumerate_zes_devices();
if (res != ZE_RESULT_SUCCESS) {
*error = res;
return 0;
}
}
zes_device_handle_t handle = retrieve_handle_for_bdf(bdf_address);
if (handle == 0) {
*error = ZE_RESULT_ERROR_UNKNOWN;
return 0;
}
// Levelzero does not provide memory details for integrated
if (is_integrated(handle)) {
print_log(LOG_DEBUG, "Device is integrated => no memory\n");
return 0;
}
uint32_t modcount = 0;
uint64_t memory_size = 0;
if (!zesDeviceEnumMemoryModules(handle, &modcount, NULL) == ZE_RESULT_SUCCESS && modcount > 0) {
zes_mem_handle_t memhandles[modcount];
if (zesDeviceEnumMemoryModules(handle, &modcount, memhandles) == ZE_RESULT_SUCCESS) {
for (uint32_t mod_index = 0; mod_index < modcount; ++mod_index) {
zes_mem_state_t mem_state;
if (zesMemoryGetState(memhandles[mod_index], &mem_state) == ZE_RESULT_SUCCESS) {
memory_size += mem_state.size;
}
}
}
}
print_log(LOG_DEBUG, "> Memory size: %ld\n", memory_size);
return memory_size;
}
/// @brief Retrieve device memory's health status
/// @param bdf_address
/// @return true for good, false for bad
bool zes_device_memory_is_healthy(char* bdf_address, uint32_t* error)
{
if (getenv("UNITTEST") != NULL) {
return false;
}
print_log(LOG_DEBUG, "Fetching memory health for %s\n", bdf_address);
if (!device_enumerated) {
ze_result_t res = enumerate_zes_devices();
if (res != ZE_RESULT_SUCCESS) {
*error = res;
return true;
}
}
zes_device_handle_t handle = retrieve_handle_for_bdf(bdf_address);
if (handle == 0) {
*error = ZE_RESULT_ERROR_UNKNOWN;
return true;
}
// Levelzero does not provide memory details for integrated
if (is_integrated(handle)) {
return true;
}
uint32_t modcount = 0;
if (zesDeviceEnumMemoryModules(handle, &modcount, NULL) == ZE_RESULT_SUCCESS && modcount > 0) {
zes_mem_handle_t memhandles[modcount];
if (zesDeviceEnumMemoryModules(handle, &modcount, memhandles) == ZE_RESULT_SUCCESS) {
for (uint32_t mod_index = 0; mod_index < modcount; ++mod_index) {
zes_mem_state_t mem_state;
if (zesMemoryGetState(memhandles[mod_index], &mem_state) == ZE_RESULT_SUCCESS) {
if (mem_state.health >= ZES_MEM_HEALTH_CRITICAL) {
print_log(LOG_DEBUG, "> Health: Critical\n");
return false;
}
}
}
}
}
print_log(LOG_DEBUG, "> Health: OK\n");
return true;
}
/// @brief Retrieve device bus' health status
/// @param bdf_address
/// @return true for good, false for bad
bool zes_device_bus_is_healthy(char* bdf_address, uint32_t* error)
{
if (getenv("UNITTEST") != NULL) {
return false;
}
print_log(LOG_DEBUG, "Fetching bus health for %s\n", bdf_address);
if (!device_enumerated) {
ze_result_t res = enumerate_zes_devices();
if (res != ZE_RESULT_SUCCESS) {
*error = res;
return true;
}
}
zes_device_handle_t handle = retrieve_handle_for_bdf(bdf_address);
if (handle == 0) {
*error = ZE_RESULT_ERROR_UNKNOWN;
return true;
}
zes_pci_state_t pci_state = {
.pNext = NULL,
};
ze_result_t res = zesDevicePciGetState(handle, &pci_state);
if (res == ZE_RESULT_SUCCESS) {
if (pci_state.qualityIssues & ZES_PCI_LINK_QUAL_ISSUE_FLAG_SPEED) {
print_log(LOG_DEBUG, "> Health: Critical\n");
return false;
}
} else if (res != ZE_RESULT_ERROR_UNSUPPORTED_FEATURE) {
*error = res;
}
print_log(LOG_DEBUG, "> Health: OK\n");
return true;
}
/// @brief Retrieve device's temperatur for a sensor
/// @param bdf_address - bdf address
/// @param sensor - name of the sensor: global, gpu or memory
/// @return temperature for the sensor
double zes_device_temp_max(char* bdf_address, char* sensor, uint32_t* error)
{
if (getenv("UNITTEST") != NULL) {
return TEMP_ERROR_RET_VAL;
}
uint32_t requestedType = 0;
if (!strncmp("global", sensor, 6)) {
requestedType = ZES_TEMP_SENSORS_GLOBAL;
} else if (!strncmp("gpu", sensor, 3)) {
requestedType = ZES_TEMP_SENSORS_GPU;
} else if (!strncmp("memory", sensor, 6)) {
requestedType = ZES_TEMP_SENSORS_MEMORY;
} else {
*error = ZE_RESULT_ERROR_INVALID_ARGUMENT;
return TEMP_ERROR_RET_VAL;
}
print_log(LOG_DEBUG, "Fetch %s temperature for %s\n", sensor, bdf_address);
if (!device_enumerated) {
ze_result_t res = enumerate_zes_devices();
if (res != ZE_RESULT_SUCCESS) {
*error = res;
return TEMP_ERROR_RET_VAL;
}
}
zes_device_handle_t handle = retrieve_handle_for_bdf(bdf_address);
if (handle == 0) {
*error = ZE_RESULT_ERROR_UNKNOWN;
return TEMP_ERROR_RET_VAL;
}
uint32_t count = 0;
ze_result_t res = zesDeviceEnumTemperatureSensors(handle, &count, NULL);
if (res != ZE_RESULT_SUCCESS || count == 0) {
*error = res;
return TEMP_ERROR_RET_VAL;
}
zes_temp_handle_t tempHandles[count];
res = zesDeviceEnumTemperatureSensors(handle, &count, tempHandles);
if (res != ZE_RESULT_SUCCESS) {
*error = res;
return TEMP_ERROR_RET_VAL;
}
for (uint32_t i = 0; i < count; ++i) {
zes_temp_properties_t props = {
.pNext = NULL,
};
res = zesTemperatureGetProperties(tempHandles[i], &props);
if (res != ZE_RESULT_SUCCESS) {
*error = res;
return TEMP_ERROR_RET_VAL;
}
if (props.type != requestedType) {
continue;
}
double tempCelsius = 0.0;
res = zesTemperatureGetState(tempHandles[i], &tempCelsius);
if (res != ZE_RESULT_SUCCESS) {
*error = res;
return TEMP_ERROR_RET_VAL;
}
print_log(LOG_DEBUG, "> Temperature: %.1f\n", tempCelsius);
return tempCelsius;
}
*error = ZE_RESULT_ERROR_NOT_AVAILABLE;
return TEMP_ERROR_RET_VAL;
}