-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpu_info.c
109 lines (90 loc) · 3.34 KB
/
gpu_info.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
/*
* gpu_info.c - get information on AMD ROCm GPU using libamdsmi
*/
#if AMDGPU
#include <stdio.h>
#include <stdlib.h>
#include "amd_smi/amdsmi.h"
#include "gpu_info.h"
#include "error.h"
amdsmi_socket_handle *sockets;
amdsmi_processor_handle *processor_handles;
int num_gpu = 0;
void **gpu_handles;
void gpu_info_query(struct gpu_query_data *qd){
amdsmi_gpu_metrics_t metric_info;
int status;
if (num_gpu > 0)
status = amdsmi_get_gpu_metrics_info(gpu_handles[0],&metric_info);
if (status != AMDSMI_STATUS_SUCCESS)
fatal("unable to get gpu metrics\n");
if (!qd) return;
qd->temperature = metric_info.temperature_edge;
qd->gfx_activity = metric_info.average_gfx_activity;
qd->umc_activity = metric_info.average_umc_activity;
qd->mm_activity = metric_info.average_mm_activity;
// notice("gfxclk %d MHz\n",metric_info.average_gfxclk_frequency);
// notice("gfx activity acc %d\n",metric_info.gfx_activity_acc);
// notice("mem activity acc %d\n",metric_info.mem_activity_acc);
// notice("socket power %d watts\n",metric_info.current_socket_power);
}
void gpu_info_initialize(void){
int status;
int i,j;
unsigned int socket_count;
unsigned int device_count;
unsigned long value;
status = amdsmi_init(AMDSMI_INIT_AMD_GPUS);
if (status != AMDSMI_STATUS_SUCCESS)
fatal("unable to initialize amdsmi\n");
// get the socket count and allocate memory
status = amdsmi_get_socket_handles(&socket_count,NULL);
if (status != AMDSMI_STATUS_SUCCESS)
fatal("unable to get amdsmi socket handles\n");
sockets = calloc(socket_count,sizeof(amdsmi_socket_handle));
// fill in the socket handles
status = amdsmi_get_socket_handles(&socket_count,sockets);
if (status != AMDSMI_STATUS_SUCCESS)
fatal("unable to get amdsmi socket handles\n");
// for each socket, get identifier and devices
for (i=0;i<socket_count;i++){
char socket_info[128];
status = amdsmi_get_socket_info(sockets[i],sizeof(socket_info),socket_info);
if (status != AMDSMI_STATUS_SUCCESS)
fatal("unable to get amdsmi socket info\n");
// get the device count for the socket and allocate memory
status = amdsmi_get_processor_handles(sockets[i],&device_count,NULL);
if (status != AMDSMI_STATUS_SUCCESS)
fatal("unable to get amdsmi device count\n");
if (num_gpu == 0){
gpu_handles = malloc(device_count * sizeof(gpu_handles[0]));
} else {
gpu_handles = realloc(gpu_handles,(device_count+num_gpu)*sizeof(gpu_handles[0]));
}
// processor_handles = calloc(device_count,sizeof(amdsmi_processor_handle));
// get the devices of the socket
status = amdsmi_get_processor_handles(sockets[i],&device_count,/*processor_handles*/&gpu_handles[num_gpu]);
if (status != AMDSMI_STATUS_SUCCESS)
fatal("unable to get amdsmi device count\n");
num_gpu += device_count;
}
}
void gpu_info_finalize(void){
int status;
status = amdsmi_shut_down();
if (status != AMDSMI_STATUS_SUCCESS)
fatal("unable to shut down amdsmi\n");
}
#if TEST_GPU_INFO
int main(void){
struct gpu_query_data qd;
gpu_info_initialize();
gpu_info_query(&qd);
notice("temperature = %dC\n",qd.temperature);
notice("gfx = %d%%\n",qd.gfx_activity);
notice("umc = %d%%\n",qd.umc_activity);
notice("mm = %d%%\n",qd.mm_activity);
gpu_info_finalize();
}
#endif
#endif