-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.c
286 lines (228 loc) · 10.2 KB
/
metrics.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
// Compile:
// $ gcc ./metrics.c -I/usr/include/libdrm -ldrm -ldrm_amdgpu -lpciaccess -O2 -o metrics
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/param.h>
#include <assert.h>
#include <errno.h>
#include <xf86drm.h>
#include <libdrm/amdgpu_drm.h>
#include <libdrm/amdgpu.h>
#define NUM_HBM_INSTANCES 4
typedef struct metrics_table_header {
uint16_t structure_size;
uint8_t format_revision;
uint8_t content_revision;
} metrics_table_header_t;
// https://elixir.bootlin.com/linux/latest/source/drivers/gpu/drm/amd/include/kgd_pp_interface.h#L599
struct gpu_metrics_v1_3 {
struct metrics_table_header common_header;
/* Temperature */
uint16_t temperature_edge;
uint16_t temperature_hotspot;
uint16_t temperature_mem;
uint16_t temperature_vrgfx;
uint16_t temperature_vrsoc;
uint16_t temperature_vrmem;
/* Utilization */
uint16_t average_gfx_activity;
uint16_t average_umc_activity; // memory controller
uint16_t average_mm_activity; // UVD or VCN
/* Power/Energy */
uint16_t average_socket_power;
uint64_t energy_accumulator;
/* Driver attached timestamp (in ns) */
uint64_t system_clock_counter;
/* Average clocks */
uint16_t average_gfxclk_frequency;
uint16_t average_socclk_frequency;
uint16_t average_uclk_frequency;
uint16_t average_vclk0_frequency;
uint16_t average_dclk0_frequency;
uint16_t average_vclk1_frequency;
uint16_t average_dclk1_frequency;
/* Current clocks */
uint16_t current_gfxclk;
uint16_t current_socclk;
uint16_t current_uclk;
uint16_t current_vclk0;
uint16_t current_dclk0;
uint16_t current_vclk1;
uint16_t current_dclk1;
/* Throttle status */
uint32_t throttle_status;
/* Fans */
uint16_t current_fan_speed;
/* Link width/speed */
uint16_t pcie_link_width;
uint16_t pcie_link_speed; // in 0.1 GT/s
uint16_t padding;
uint32_t gfx_activity_acc;
uint32_t mem_activity_acc;
uint16_t temperature_hbm[NUM_HBM_INSTANCES];
/* PMFW attached timestamp (10ns resolution) */
uint64_t firmware_timestamp;
/* Voltage (mV) */
uint16_t voltage_soc;
uint16_t voltage_gfx;
uint16_t voltage_mem;
uint16_t padding1;
/* Throttle status (ASIC independent) */
uint64_t indep_throttle_status;
};
// https://elixir.bootlin.com/linux/latest/source/drivers/gpu/drm/amd/include/kgd_pp_interface.h#L775
struct gpu_metrics_v2_2 {
metrics_table_header_t common_header;
/* Temperature */
uint16_t temperature_gfx; // gfx temperature on APUs
uint16_t temperature_soc; // soc temperature on APUs
uint16_t temperature_core[8]; // CPU core temperature on APUs
uint16_t temperature_l3[2];
/* Utilization */
uint16_t average_gfx_activity;
uint16_t average_mm_activity; // UVD or VCN
/* Driver attached timestamp (in ns) */
uint64_t system_clock_counter;
/* Power/Energy */
uint16_t average_socket_power; // dGPU + APU power on A + A platform
uint16_t average_cpu_power;
uint16_t average_soc_power;
uint16_t average_gfx_power;
uint16_t average_core_power[8]; // CPU core power on APUs
/* Average clocks */
uint16_t average_gfxclk_frequency;
uint16_t average_socclk_frequency;
uint16_t average_uclk_frequency;
uint16_t average_fclk_frequency;
uint16_t average_vclk_frequency;
uint16_t average_dclk_frequency;
/* Current clocks */
uint16_t current_gfxclk;
uint16_t current_socclk;
uint16_t current_uclk;
uint16_t current_fclk;
uint16_t current_vclk;
uint16_t current_dclk;
uint16_t current_coreclk[8]; // CPU core clocks
uint16_t current_l3clk[2];
/* Throttle status (ASIC dependent) */
uint32_t throttle_status;
/* Fans */
uint16_t fan_pwm;
uint16_t padding[3];
/* Throttle status (ASIC independent) */
uint64_t indep_throttle_status;
};
// https://elixir.bootlin.com/linux/latest/source/drivers/gpu/drm/amd/include/kgd_pp_interface.h#L828
struct gpu_metrics_v2_3 {
struct metrics_table_header common_header;
/* Temperature */
uint16_t temperature_gfx; // gfx temperature on APUs
uint16_t temperature_soc; // soc temperature on APUs
uint16_t temperature_core[8]; // CPU core temperature on APUs
uint16_t temperature_l3[2];
/* Utilization */
uint16_t average_gfx_activity;
uint16_t average_mm_activity; // UVD or VCN
/* Driver attached timestamp (in ns) */
uint64_t system_clock_counter;
/* Power/Energy */
uint16_t average_socket_power; // dGPU + APU power on A + A platform
uint16_t average_cpu_power;
uint16_t average_soc_power;
uint16_t average_gfx_power;
uint16_t average_core_power[8]; // CPU core power on APUs
/* Average clocks */
uint16_t average_gfxclk_frequency;
uint16_t average_socclk_frequency;
uint16_t average_uclk_frequency;
uint16_t average_fclk_frequency;
uint16_t average_vclk_frequency;
uint16_t average_dclk_frequency;
/* Current clocks */
uint16_t current_gfxclk;
uint16_t current_socclk;
uint16_t current_uclk;
uint16_t current_fclk;
uint16_t current_vclk;
uint16_t current_dclk;
uint16_t current_coreclk[8]; // CPU core clocks
uint16_t current_l3clk[2];
/* Throttle status (ASIC dependent) */
uint32_t throttle_status;
/* Fans */
uint16_t fan_pwm;
uint16_t padding[3];
/* Throttle status (ASIC independent) */
uint64_t indep_throttle_status;
/* Average Temperature */
uint16_t average_temperature_gfx; // average gfx temperature on APUs
uint16_t average_temperature_soc; // average soc temperature on APUs
uint16_t average_temperature_core[8]; // average CPU core temperature on APUs
uint16_t average_temperature_l3[2];
};
int main(void)
{
void *buf[MAX(sizeof(struct gpu_metrics_v1_3), MAX(sizeof(struct gpu_metrics_v2_2), sizeof(struct gpu_metrics_v2_3)))];
metrics_table_header_t* header = (metrics_table_header_t*)buf;
// To get the correct path, run the following:
// $ find /sys/devices/ | grep gpu_metrics
#error "FIXME: UPDATE PATH:" // Comment this out
FILE *f = fopen("/sys/devices/pci0000:00/0000:00:08.1/0000:08:00.0/gpu_metrics", "rb");
while(true)
{
if (!fread(buf, sizeof(buf), 1, f) == 0)
{
perror("fread");
return 1;
}
rewind(f);
printf("format_revision = %d, content_revision = %d\n", header->format_revision, header->content_revision);
if (header->format_revision == 1)
{
if (header->content_revision == 3)
{
printf("Parsing gpu_metrics_v1_3\n");
struct gpu_metrics_v1_3 *amdgpu_metrics = (struct gpu_metrics_v1_3 *) buf;
printf("average_socket_power: %d W\n", amdgpu_metrics->average_socket_power);
}
}
else
if (header->format_revision == 2)
{
if (header->content_revision == 2)
{
printf("Parsing gpu_metrics_v2_2\n");
struct gpu_metrics_v2_2 *amdgpu_metrics = (struct gpu_metrics_v2_2 *) buf;
printf("average_socket_power: %d W\n", amdgpu_metrics->average_socket_power);
printf("average_cpu_power: %.2f W\n", amdgpu_metrics->average_cpu_power / 1000.f);
printf("average_soc_power: %.2f W\n", amdgpu_metrics->average_soc_power / 1000.f);
printf("average_gfx_power: %.2f W\n", amdgpu_metrics->average_gfx_power / 1000.f);
printf("average_gfxclk_frequency: %.2d MHz\n", amdgpu_metrics->average_gfxclk_frequency);
printf("average_socclk_frequency: %.2d MHz\n", amdgpu_metrics->average_socclk_frequency);
printf("average_core_power[0]: %d mW\n", amdgpu_metrics->average_core_power[0]);
printf("average_core_power[1]: %d mW\n", amdgpu_metrics->average_core_power[1]);
printf("average_core_power[2]: %d mW\n", amdgpu_metrics->average_core_power[2]);
printf("average_core_power[3]: %d mW\n", amdgpu_metrics->average_core_power[3]);
printf("average_core_power[4]: %d mW\n", amdgpu_metrics->average_core_power[4]);
printf("average_core_power[5]: %d mW\n", amdgpu_metrics->average_core_power[5]);
printf("average_core_power[6]: %d mW\n", amdgpu_metrics->average_core_power[6]);
printf("average_core_power[7]: %d mW\n", amdgpu_metrics->average_core_power[7]);
}
else
if (header->content_revision == 3)
{
printf("Parsing gpu_metrics_v2_3\n");
struct gpu_metrics_v2_3 *amdgpu_metrics = (struct gpu_metrics_v2_3 *) buf;
printf("average_socket_power: %d W\n", amdgpu_metrics->average_socket_power);
printf("average_cpu_power: %.2f W\n", amdgpu_metrics->average_cpu_power / 1000.f);
printf("average_soc_power: %.2f W\n", amdgpu_metrics->average_soc_power / 1000.f);
}
}
printf("\e[1;1H\e[2J");
usleep(1500000);
}
fclose(f);
return 0;
}