Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/target/i386/kvm/vmsr_energy.c
Line
Count
Source
1
/*
2
 * QEMU KVM support -- x86 virtual RAPL msr
3
 *
4
 * Copyright 2024 Red Hat, Inc. 2024
5
 *
6
 *  Author:
7
 *      Anthony Harivel <aharivel@redhat.com>
8
 *
9
 * This work is licensed under the terms of the GNU GPL, version 2 or later.
10
 * See the COPYING file in the top-level directory.
11
 *
12
 */
13
14
#include "qemu/osdep.h"
15
#include "qemu/error-report.h"
16
#include "vmsr_energy.h"
17
#include "io/channel.h"
18
#include "io/channel-socket.h"
19
#include "hw/core/boards.h"
20
#include "cpu.h"
21
#include "host-cpu.h"
22
23
char *vmsr_compute_default_paths(void)
24
0
{
25
0
    g_autofree char *state = qemu_get_local_state_dir();
26
27
0
    return g_build_filename(state, "run", "qemu-vmsr-helper.sock", NULL);
28
0
}
29
30
int is_rapl_enabled(void)
31
0
{
32
0
    const char *path = "/sys/class/powercap/intel-rapl/enabled";
33
0
    FILE *file = fopen(path, "r");
34
0
    int value = 0;
35
36
0
    if (file != NULL) {
37
0
        if (fscanf(file, "%d", &value) != 1) {
38
0
            error_report("INTEL RAPL not enabled");
39
0
        }
40
0
        fclose(file);
41
0
    } else {
42
0
        error_report("Error opening %s", path);
43
0
    }
44
45
0
    return value;
46
0
}
47
48
QIOChannelSocket *vmsr_open_socket(const char *path)
49
0
{
50
0
    g_autofree char *socket_path = NULL;
51
52
0
    socket_path = g_strdup(path);
53
54
0
    SocketAddress saddr = {
55
0
        .type = SOCKET_ADDRESS_TYPE_UNIX,
56
0
        .u.q_unix.path = socket_path
57
0
    };
58
59
0
    QIOChannelSocket *sioc = qio_channel_socket_new();
60
61
0
    qio_channel_set_name(QIO_CHANNEL(sioc), "vmsr-helper");
62
0
    if (qio_channel_socket_connect_sync(sioc, &saddr, NULL) < 0) {
63
        /* Close socket. */
64
0
        qio_channel_close(QIO_CHANNEL(sioc), NULL);
65
0
        object_unref(OBJECT(sioc));
66
0
        sioc = NULL;
67
0
        goto out;
68
0
    }
69
70
0
    qio_channel_set_delay(QIO_CHANNEL(sioc), false);
71
0
out:
72
0
    return sioc;
73
0
}
74
75
uint64_t vmsr_read_msr(uint32_t reg, uint32_t cpu_id, uint32_t tid,
76
                       QIOChannelSocket *sioc)
77
0
{
78
0
    uint64_t data = 0;
79
0
    int r = 0;
80
0
    Error *local_err = NULL;
81
0
    uint32_t buffer[3];
82
    /*
83
     * Send the required arguments:
84
     * 1. RAPL MSR register to read
85
     * 2. On which CPU ID
86
     * 3. From which vCPU (Thread ID)
87
     */
88
0
    buffer[0] = reg;
89
0
    buffer[1] = cpu_id;
90
0
    buffer[2] = tid;
91
92
0
    r = qio_channel_write_all(QIO_CHANNEL(sioc),
93
0
                              (char *)buffer, sizeof(buffer),
94
0
                              &local_err);
95
0
    if (r < 0) {
96
0
        goto out_close;
97
0
    }
98
99
0
    r = qio_channel_read(QIO_CHANNEL(sioc),
100
0
                             (char *)&data, sizeof(data),
101
0
                             &local_err);
102
0
    if (r < 0) {
103
0
        data = 0;
104
0
        goto out_close;
105
0
    }
106
107
0
out_close:
108
0
   return data;
109
0
}
110
111
/* Retrieve the max number of physical package */
112
unsigned int vmsr_get_max_physical_package(unsigned int max_cpus)
113
0
{
114
0
    const char *dir = "/sys/devices/system/cpu/";
115
0
    const char *topo_path = "topology/physical_package_id";
116
0
    g_autofree int *uniquePackages = g_new0(int, max_cpus);
117
0
    unsigned int packageCount = 0;
118
0
    FILE *file = NULL;
119
120
0
    for (int i = 0; i < max_cpus; i++) {
121
0
        g_autofree char *filePath = NULL;
122
0
        g_autofree char *cpuid = g_strdup_printf("cpu%d", i);
123
124
0
        filePath = g_build_filename(dir, cpuid, topo_path, NULL);
125
126
0
        file = fopen(filePath, "r");
127
128
0
        if (file == NULL) {
129
0
            error_report("Error opening physical_package_id file");
130
0
            return 0;
131
0
        }
132
133
0
        char packageId[10];
134
0
        if (fgets(packageId, sizeof(packageId), file) == NULL) {
135
0
            packageCount = 0;
136
0
        }
137
138
0
        fclose(file);
139
140
0
        int currentPackageId = atoi(packageId);
141
142
0
        bool isUnique = true;
143
0
        for (int j = 0; j < packageCount; j++) {
144
0
            if (uniquePackages[j] == currentPackageId) {
145
0
                isUnique = false;
146
0
                break;
147
0
            }
148
0
        }
149
150
0
        if (isUnique) {
151
0
            uniquePackages[packageCount] = currentPackageId;
152
0
            packageCount++;
153
154
0
            if (packageCount >= max_cpus) {
155
0
                break;
156
0
            }
157
0
        }
158
0
    }
159
160
0
    return (packageCount == 0) ? 1 : packageCount;
161
0
}
162
163
/* Retrieve the max number of physical cpu on the host */
164
unsigned int vmsr_get_maxcpus(void)
165
0
{
166
0
    GDir *dir;
167
0
    const gchar *entry_name;
168
0
    unsigned int cpu_count = 0;
169
0
    const char *path = "/sys/devices/system/cpu/";
170
171
0
    dir = g_dir_open(path, 0, NULL);
172
0
    if (dir == NULL) {
173
0
        error_report("Unable to open cpu directory");
174
0
        return -1;
175
0
    }
176
177
0
    while ((entry_name = g_dir_read_name(dir)) != NULL) {
178
0
        if (g_ascii_strncasecmp(entry_name, "cpu", 3) == 0 &&
179
0
            isdigit(entry_name[3])) {
180
0
            cpu_count++;
181
0
        }
182
0
    }
183
184
0
    g_dir_close(dir);
185
186
0
    return cpu_count;
187
0
}
188
189
/* Count the number of physical cpu on each packages */
190
unsigned int vmsr_count_cpus_per_package(unsigned int *package_count,
191
                                         unsigned int max_pkgs)
192
0
{
193
0
    g_autofree char *file_contents = NULL;
194
0
    g_autofree char *path = NULL;
195
0
    g_autofree char *path_name = NULL;
196
0
    gsize length;
197
198
    /* Iterate over cpus and count cpus in each package */
199
0
    for (int cpu_id = 0; ; cpu_id++) {
200
0
        path_name = g_strdup_printf("/sys/devices/system/cpu/cpu%d/"
201
0
            "topology/physical_package_id", cpu_id);
202
203
0
        path = g_build_filename(path_name, NULL);
204
205
0
        if (!g_file_get_contents(path, &file_contents, &length, NULL)) {
206
0
            break; /* No more cpus */
207
0
        }
208
209
        /* Get the physical package ID for this CPU */
210
0
        int package_id = atoi(file_contents);
211
212
        /* Check if the package ID is within the known number of packages */
213
0
        if (package_id >= 0 && package_id < max_pkgs) {
214
            /* If yes, count the cpu for this package*/
215
0
            package_count[package_id]++;
216
0
        }
217
0
    }
218
219
0
    return 0;
220
0
}
221
222
/* Get the physical package id from a given cpu id */
223
int vmsr_get_physical_package_id(int cpu_id)
224
0
{
225
0
    g_autofree char *file_contents = NULL;
226
0
    g_autofree char *file_path = NULL;
227
0
    int package_id = -1;
228
0
    gsize length;
229
230
0
    file_path = g_strdup_printf("/sys/devices/system/cpu/cpu%d"
231
0
        "/topology/physical_package_id", cpu_id);
232
233
0
    if (!g_file_get_contents(file_path, &file_contents, &length, NULL)) {
234
0
        goto out;
235
0
    }
236
237
0
    package_id = atoi(file_contents);
238
239
0
out:
240
0
    return package_id;
241
0
}
242
243
/* Read the scheduled time for a given thread of a give pid */
244
void vmsr_read_thread_stat(pid_t pid,
245
                      unsigned int thread_id,
246
                      unsigned long long *utime,
247
                      unsigned long long *stime,
248
                      unsigned int *cpu_id)
249
0
{
250
0
    g_autofree char *path = NULL;
251
0
    g_autofree char *path_name = NULL;
252
253
0
    path_name = g_strdup_printf("/proc/%u/task/%d/stat", pid, thread_id);
254
255
0
    path = g_build_filename(path_name, NULL);
256
257
0
    FILE *file = fopen(path, "r");
258
0
    if (file == NULL) {
259
0
        error_report("Error opening %s", path_name);
260
0
        return;
261
0
    }
262
263
0
    if (fscanf(file, "%*d (%*[^)]) %*c %*d %*d %*d %*d %*d %*u %*u %*u %*u %*u"
264
0
        " %llu %llu %*d %*d %*d %*d %*d %*d %*u %*u %*d %*u %*u"
265
0
        " %*u %*u %*u %*u %*u %*u %*u %*u %*u %*d %*u %*u %u",
266
0
           utime, stime, cpu_id) != 3)
267
0
    {
268
0
        fclose(file);
269
0
        error_report("Error fscanf did not report the right amount of items");
270
0
        return;
271
0
    }
272
273
0
    fclose(file);
274
0
}
275
276
/* Read QEMU stat task folder to retrieve all QEMU threads ID */
277
pid_t *vmsr_get_thread_ids(pid_t pid, unsigned int *num_threads)
278
0
{
279
0
    g_autofree char *task_path = g_strdup_printf("%d/task", pid);
280
0
    g_autofree char *path = g_build_filename("/proc", task_path, NULL);
281
282
0
    DIR *dir = opendir(path);
283
0
    if (dir == NULL) {
284
0
        error_report("Error opening /proc/qemu/task");
285
0
        return NULL;
286
0
    }
287
288
0
    pid_t *thread_ids = NULL;
289
0
    unsigned int thread_count = 0;
290
291
0
    g_autofree struct dirent *ent = NULL;
292
0
    while ((ent = readdir(dir)) != NULL) {
293
0
        if (ent->d_name[0] == '.') {
294
0
            continue;
295
0
        }
296
0
        pid_t tid = atoi(ent->d_name);
297
0
        if (pid != tid) {
298
0
            thread_ids = g_renew(pid_t, thread_ids, (thread_count + 1));
299
0
            thread_ids[thread_count] = tid;
300
0
            thread_count++;
301
0
        }
302
0
    }
303
304
0
    closedir(dir);
305
306
0
    *num_threads = thread_count;
307
0
    return thread_ids;
308
0
}
309
310
void vmsr_delta_ticks(vmsr_thread_stat *thd_stat, int i)
311
0
{
312
0
    thd_stat[i].delta_ticks = (thd_stat[i].utime[1] + thd_stat[i].stime[1])
313
0
                            - (thd_stat[i].utime[0] + thd_stat[i].stime[0]);
314
0
}
315
316
double vmsr_get_ratio(uint64_t e_delta,
317
                      unsigned long long delta_ticks,
318
                      unsigned int maxticks)
319
0
{
320
0
    return (e_delta / 100.0) * ((100.0 / maxticks) * delta_ticks);
321
0
}
322
323
void vmsr_init_topo_info(X86CPUTopoInfo *topo_info,
324
                           const MachineState *ms)
325
0
{
326
0
    topo_info->dies_per_pkg = ms->smp.dies;
327
0
    topo_info->modules_per_die = ms->smp.modules;
328
0
    topo_info->cores_per_module = ms->smp.cores;
329
0
    topo_info->threads_per_core = ms->smp.threads;
330
0
}
331