xref: /openbmc/qemu/target/i386/kvm/vmsr_energy.h (revision b9c0a2e0)
1 /*
2  * QEMU KVM support -- x86 virtual energy-related 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 #ifndef VMSR_ENERGY_H
15 #define VMSR_ENERGY_H
16 
17 #include <stdint.h>
18 #include "qemu/osdep.h"
19 #include "io/channel-socket.h"
20 #include "hw/i386/topology.h"
21 
22 /*
23  * Define the interval time in micro seconds between 2 samples of
24  * energy related MSRs
25  */
26 #define MSR_ENERGY_THREAD_SLEEP_US 1000000.0
27 
28 /*
29  * Thread statistic
30  * @ thread_id: TID (thread ID)
31  * @ is_vcpu: true if TID is vCPU thread
32  * @ cpu_id: CPU number last executed on
33  * @ pkg_id: package number of the CPU
34  * @ vcpu_id: vCPU ID
35  * @ vpkg: virtual package number
36  * @ acpi_id: APIC id of the vCPU
37  * @ utime: amount of clock ticks the thread
38  *          has been scheduled in User mode
39  * @ stime: amount of clock ticks the thread
40  *          has been scheduled in System mode
41  * @ delta_ticks: delta of utime+stime between
42  *          the two samples (before/after sleep)
43  */
44 struct vmsr_thread_stat {
45     unsigned int thread_id;
46     bool is_vcpu;
47     unsigned int cpu_id;
48     unsigned int pkg_id;
49     unsigned int vpkg_id;
50     unsigned int vcpu_id;
51     unsigned long acpi_id;
52     unsigned long long *utime;
53     unsigned long long *stime;
54     unsigned long long delta_ticks;
55 };
56 
57 /*
58  * Package statistic
59  * @ e_start: package energy counter before the sleep
60  * @ e_end: package energy counter after the sleep
61  * @ e_delta: delta of package energy counter
62  * @ e_ratio: store the energy ratio of non-vCPU thread
63  * @ nb_vcpu: number of vCPU running on this package
64  */
65 struct vmsr_package_energy_stat {
66     uint64_t e_start;
67     uint64_t e_end;
68     uint64_t e_delta;
69     uint64_t e_ratio;
70     unsigned int nb_vcpu;
71 };
72 
73 typedef struct vmsr_thread_stat vmsr_thread_stat;
74 typedef struct vmsr_package_energy_stat vmsr_package_energy_stat;
75 
76 char *vmsr_compute_default_paths(void);
77 void vmsr_read_thread_stat(pid_t pid,
78                       unsigned int thread_id,
79                       unsigned long long *utime,
80                       unsigned long long *stime,
81                       unsigned int *cpu_id);
82 
83 QIOChannelSocket *vmsr_open_socket(const char *path);
84 uint64_t vmsr_read_msr(uint32_t reg, uint32_t cpu_id,
85                        uint32_t tid, QIOChannelSocket *sioc);
86 void vmsr_delta_ticks(vmsr_thread_stat *thd_stat, int i);
87 unsigned int vmsr_get_maxcpus(void);
88 unsigned int vmsr_get_max_physical_package(unsigned int max_cpus);
89 unsigned int vmsr_count_cpus_per_package(unsigned int *package_count,
90                                          unsigned int max_pkgs);
91 int vmsr_get_physical_package_id(int cpu_id);
92 pid_t *vmsr_get_thread_ids(pid_t pid, unsigned int *num_threads);
93 double vmsr_get_ratio(uint64_t e_delta,
94                       unsigned long long delta_ticks,
95                       unsigned int maxticks);
96 void vmsr_init_topo_info(X86CPUTopoInfo *topo_info, const MachineState *ms);
97 bool is_host_cpu_intel(void);
98 int is_rapl_enabled(void);
99 #endif /* VMSR_ENERGY_H */
100