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
|
/*
* QEMU KVM support -- x86 virtual RAPL msr
*
* Copyright 2024 Red Hat, Inc. 2024
*
* Author:
* Anthony Harivel <aharivel@redhat.com>
*
* This work is licensed under the terms of the GNU GPL, version 2 or later.
* See the COPYING file in the top-level directory.
*
*/
#include "qemu/osdep.h"
#include "qemu/error-report.h"
#include "vmsr_energy.h"
#include "io/channel.h"
#include "io/channel-socket.h"
#include "hw/boards.h"
#include "cpu.h"
#include "host-cpu.h"
char *vmsr_compute_default_paths(void)
{
g_autofree char *state = qemu_get_local_state_dir();
return g_build_filename(state, "run", "qemu-vmsr-helper.sock", NULL);
}
bool is_host_cpu_intel(void)
{
char vendor[CPUID_VENDOR_SZ + 1];
host_cpu_vendor_fms(vendor, NULL, NULL, NULL);
return g_str_equal(vendor, CPUID_VENDOR_INTEL);
}
int is_rapl_enabled(void)
{
const char *path = "/sys/class/powercap/intel-rapl/enabled";
FILE *file = fopen(path, "r");
int value = 0;
if (file != NULL) {
if (fscanf(file, "%d", &value) != 1) {
error_report("INTEL RAPL not enabled");
}
fclose(file);
} else {
error_report("Error opening %s", path);
}
return value;
}
QIOChannelSocket *vmsr_open_socket(const char *path)
{
g_autofree char *socket_path = NULL;
socket_path = g_strdup(path);
SocketAddress saddr = {
.type = SOCKET_ADDRESS_TYPE_UNIX,
.u.q_unix.path = socket_path
};
QIOChannelSocket *sioc = qio_channel_socket_new();
Error *local_err = NULL;
qio_channel_set_name(QIO_CHANNEL(sioc), "vmsr-helper");
qio_channel_socket_connect_sync(sioc,
&saddr,
&local_err);
if (local_err) {
/* Close socket. */
qio_channel_close(QIO_CHANNEL(sioc), NULL);
object_unref(OBJECT(sioc));
sioc = NULL;
goto out;
}
qio_channel_set_delay(QIO_CHANNEL(sioc), false);
out:
return sioc;
}
uint64_t vmsr_read_msr(uint32_t reg, uint32_t cpu_id, uint32_t tid,
QIOChannelSocket *sioc)
{
uint64_t data = 0;
int r = 0;
Error *local_err = NULL;
uint32_t buffer[3];
/*
* Send the required arguments:
* 1. RAPL MSR register to read
* 2. On which CPU ID
* 3. From which vCPU (Thread ID)
*/
buffer[0] = reg;
buffer[1] = cpu_id;
buffer[2] = tid;
r = qio_channel_write_all(QIO_CHANNEL(sioc),
(char *)buffer, sizeof(buffer),
&local_err);
if (r < 0) {
goto out_close;
}
r = qio_channel_read(QIO_CHANNEL(sioc),
(char *)&data, sizeof(data),
&local_err);
if (r < 0) {
data = 0;
goto out_close;
}
out_close:
return data;
}
/* Retrieve the max number of physical package */
unsigned int vmsr_get_max_physical_package(unsigned int max_cpus)
{
const char *dir = "/sys/devices/system/cpu/";
const char *topo_path = "topology/physical_package_id";
g_autofree int *uniquePackages = g_new0(int, max_cpus);
unsigned int packageCount = 0;
FILE *file = NULL;
for (int i = 0; i < max_cpus; i++) {
g_autofree char *filePath = NULL;
g_autofree char *cpuid = g_strdup_printf("cpu%d", i);
filePath = g_build_filename(dir, cpuid, topo_path, NULL);
file = fopen(filePath, "r");
if (file == NULL) {
error_report("Error opening physical_package_id file");
return 0;
}
char packageId[10];
if (fgets(packageId, sizeof(packageId), file) == NULL) {
packageCount = 0;
}
fclose(file);
int currentPackageId = atoi(packageId);
bool isUnique = true;
for (int j = 0; j < packageCount; j++) {
if (uniquePackages[j] == currentPackageId) {
isUnique = false;
break;
}
}
if (isUnique) {
uniquePackages[packageCount] = currentPackageId;
packageCount++;
if (packageCount >= max_cpus) {
break;
}
}
}
return (packageCount == 0) ? 1 : packageCount;
}
/* Retrieve the max number of physical cpu on the host */
unsigned int vmsr_get_maxcpus(void)
{
GDir *dir;
const gchar *entry_name;
unsigned int cpu_count = 0;
const char *path = "/sys/devices/system/cpu/";
dir = g_dir_open(path, 0, NULL);
if (dir == NULL) {
error_report("Unable to open cpu directory");
return -1;
}
while ((entry_name = g_dir_read_name(dir)) != NULL) {
if (g_ascii_strncasecmp(entry_name, "cpu", 3) == 0 &&
isdigit(entry_name[3])) {
cpu_count++;
}
}
g_dir_close(dir);
return cpu_count;
}
/* Count the number of physical cpu on each packages */
unsigned int vmsr_count_cpus_per_package(unsigned int *package_count,
unsigned int max_pkgs)
{
g_autofree char *file_contents = NULL;
g_autofree char *path = NULL;
g_autofree char *path_name = NULL;
gsize length;
/* Iterate over cpus and count cpus in each package */
for (int cpu_id = 0; ; cpu_id++) {
path_name = g_strdup_printf("/sys/devices/system/cpu/cpu%d/"
"topology/physical_package_id", cpu_id);
path = g_build_filename(path_name, NULL);
if (!g_file_get_contents(path, &file_contents, &length, NULL)) {
break; /* No more cpus */
}
/* Get the physical package ID for this CPU */
int package_id = atoi(file_contents);
/* Check if the package ID is within the known number of packages */
if (package_id >= 0 && package_id < max_pkgs) {
/* If yes, count the cpu for this package*/
package_count[package_id]++;
}
}
return 0;
}
/* Get the physical package id from a given cpu id */
int vmsr_get_physical_package_id(int cpu_id)
{
g_autofree char *file_contents = NULL;
g_autofree char *file_path = NULL;
int package_id = -1;
gsize length;
file_path = g_strdup_printf("/sys/devices/system/cpu/cpu%d"
"/topology/physical_package_id", cpu_id);
if (!g_file_get_contents(file_path, &file_contents, &length, NULL)) {
goto out;
}
package_id = atoi(file_contents);
out:
return package_id;
}
/* Read the scheduled time for a given thread of a give pid */
void vmsr_read_thread_stat(pid_t pid,
unsigned int thread_id,
unsigned long long *utime,
unsigned long long *stime,
unsigned int *cpu_id)
{
g_autofree char *path = NULL;
g_autofree char *path_name = NULL;
path_name = g_strdup_printf("/proc/%u/task/%d/stat", pid, thread_id);
path = g_build_filename(path_name, NULL);
FILE *file = fopen(path, "r");
if (file == NULL) {
error_report("Error opening %s", path_name);
return;
}
if (fscanf(file, "%*d (%*[^)]) %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u"
" %llu %llu %*d %*d %*d %*d %*d %*d %*u %*u %*d %*u %*u"
" %*u %*u %*u %*u %*u %*u %*u %*u %*u %*d %*u %*u %u",
utime, stime, cpu_id) != 3)
{
fclose(file);
error_report("Error fscanf did not report the right amount of items");
return;
}
fclose(file);
}
/* Read QEMU stat task folder to retrieve all QEMU threads ID */
pid_t *vmsr_get_thread_ids(pid_t pid, unsigned int *num_threads)
{
g_autofree char *task_path = g_strdup_printf("%d/task", pid);
g_autofree char *path = g_build_filename("/proc", task_path, NULL);
DIR *dir = opendir(path);
if (dir == NULL) {
error_report("Error opening /proc/qemu/task");
return NULL;
}
pid_t *thread_ids = NULL;
unsigned int thread_count = 0;
g_autofree struct dirent *ent = NULL;
while ((ent = readdir(dir)) != NULL) {
if (ent->d_name[0] == '.') {
continue;
}
pid_t tid = atoi(ent->d_name);
if (pid != tid) {
thread_ids = g_renew(pid_t, thread_ids, (thread_count + 1));
thread_ids[thread_count] = tid;
thread_count++;
}
}
closedir(dir);
*num_threads = thread_count;
return thread_ids;
}
void vmsr_delta_ticks(vmsr_thread_stat *thd_stat, int i)
{
thd_stat[i].delta_ticks = (thd_stat[i].utime[1] + thd_stat[i].stime[1])
- (thd_stat[i].utime[0] + thd_stat[i].stime[0]);
}
double vmsr_get_ratio(uint64_t e_delta,
unsigned long long delta_ticks,
unsigned int maxticks)
{
return (e_delta / 100.0) * ((100.0 / maxticks) * delta_ticks);
}
void vmsr_init_topo_info(X86CPUTopoInfo *topo_info,
const MachineState *ms)
{
topo_info->dies_per_pkg = ms->smp.dies;
topo_info->modules_per_die = ms->smp.modules;
topo_info->cores_per_module = ms->smp.cores;
topo_info->threads_per_core = ms->smp.threads;
}
|