Add support for the RAPL MSRs series
v5 cover Add support for the RAPL MSRs series
Dear maintainers,
First of all, thank you very much for your review of my patch [1].
In this version (v5), I have attempted to address all the problems addressed by Daniel during the last review. I've been more careful with all the remarks made.
However, one question remains unanswered pointing the issue with the location of "/var/local/run/qemu-vmsr-helper.sock", created by compute_default_paths(). QEMU is not allowed to reach the socket here.
Thank you again for your continued guidance.
v4 -> v5
--------
- correct qio_channel_get_peerpid: return pid = -1 in case of error
- Vmsr_helper: compile only for x86
- Vmsr_helper: use qio_channel_read/write_all
- Vmsr_helper: abandon user/group
- Vmsr_energy.c: correct all error_report
- Vmsr thread: compute default socket path only once
- Vmsr thread: open socket only once
- Pass relevant QEMU CI
v3 -> v4
--------
- Correct memory leaks with AddressSanitizer
- Add sanity check for QEMU and qemu-vmsr-helper for checking if host is INTEL and if RAPL is activated.
- Rename poor variables naming for easier comprehension
- Move code that checks Host before creating the VMSR thread
- Get rid of libnuma: create function that read sysfs for reading the Host topology instead
v2 -> v3
--------
- Move all memory allocations from Clib to Glib
- Compile on *BSD (working on Linux only)
- No more limitation on the virtual package: each vCPU that belongs to the same virtual package is giving the same results like expected on a real CPU.
This has been tested topology like:
-smp 4,sockets=2
-smp 16,sockets=4,cores=2,threads=2v1 -> v2
--------
- To overcome the CVE-2020-8694 a socket communication is created to a priviliged helper
- Add the priviliged helper (qemu-vmsr-helper)
- Add SO_PEERCRED in qio channel socket
RFC -> v1
---------
- Add vmsr_* in front of all vmsr specific function
- Change malloc()/calloc()... with all glib equivalent
- Pre-allocate all dynamic memories when possible
- Add a Documentation of implementation, limitation and usage
Best regards,
Anthony
[1]: https://lists.gnu.org/archive/html/qemu-devel/2024-03/msg04417.html
Anthony Harivel (3):
qio: add support for SO_PEERCRED for socket channel
tools: build qemu-vmsr-helper
Add support for RAPL MSRs in KVM/Qemu
accel/kvm/kvm-all.c | 27 ++ contrib/systemd/qemu-vmsr-helper.service | 15 + contrib/systemd/qemu-vmsr-helper.socket | 9 + docs/specs/index.rst | 1 + docs/specs/rapl-msr.rst | 155 +++++++ docs/tools/index.rst | 1 + docs/tools/qemu-vmsr-helper.rst | 89 ++++ include/io/channel.h | 21 + include/sysemu/kvm.h | 2 + include/sysemu/kvm_int.h | 32 ++ io/channel-socket.c | 28 ++ io/channel.c | 13 + meson.build | 7 + target/i386/cpu.h | 8 + target/i386/kvm/kvm-cpu.c | 9 + target/i386/kvm/kvm.c | 428 ++++++++++++++++++ target/i386/kvm/meson.build | 1 + target/i386/kvm/vmsr_energy.c | 335 ++++++++++++++ target/i386/kvm/vmsr_energy.h | 99 +++++ tools/i386/qemu-vmsr-helper.c | 529 +++++++++++++++++++++++ tools/i386/rapl-msr-index.h | 28 ++ 21 files changed, 1837 insertions(+) create mode 100644 contrib/systemd/qemu-vmsr-helper.service create mode 100644 contrib/systemd/qemu-vmsr-helper.socket create mode 100644 docs/specs/rapl-msr.rst create mode 100644 docs/tools/qemu-vmsr-helper.rst create mode 100644 target/i386/kvm/vmsr_energy.c create mode 100644 target/i386/kvm/vmsr_energy.h create mode 100644 tools/i386/qemu-vmsr-helper.c create mode 100644 tools/i386/rapl-msr-index.h
Signature · 1 line
2.44.0
PATCH v5 1/3 qio: add support for SO_PEERCRED for socket channel
The function qio_channel_get_peercred() returns a pointer to the credentials of the peer process connected to this socket.
This credentials structure is defined in <sys/socket.h> as follows:
struct ucred {
pid_t pid; /* Process ID of the sending process */ uid_t uid; /* User ID of the sending process */ gid_t gid; /* Group ID of the sending process */
};
The use of this function is possible only for connected AF_UNIX stream sockets and for AF_UNIX stream and datagram socket pairs.
On platform other than Linux, the function return 0.
--- include/io/channel.h | 21 +++++++++++++++++++++ io/channel-socket.c | 28 ++++++++++++++++++++++++++++ io/channel.c | 13 +++++++++++++ 3 files changed, 62 insertions(+)
include/io/channel.h +21 -0
@@ -160,6 +160,9 @@ struct QIOChannelClass { void *opaque);int (*io_flush)(QIOChannel *ioc,Error **errp); +int (*io_peerpid)(QIOChannel *ioc, +unsigned int *pid, +Error **errp); };/* General I/O handling functions */
@@ -981,4 +984,22 @@ int coroutine_mixed_fn qio_channel_writev_full_all(QIOChannel *ioc,int qio_channel_flush (QIOChannel *ioc,Error **errp); +/** +* qio_channel_get_peercred: +* @ioc: the channel object +* @pid: pointer to pid +* @errp: pointer to a NULL-initialized error object +* +* Returns the pid of the peer process connected to this socket. +* +* The use of this function is possible only for connected +* AF_UNIX stream sockets and for AF_UNIX stream and datagram +* socket pairs on Linux. +* Return -1 on error with pid -1 for the non-Linux OS. +* +*/ +int qio_channel_get_peerpid (QIOChannel *ioc, +unsigned int *pid, +Error **errp); +#endif /* QIO_CHANNEL_H */
io/channel-socket.c +28 -0
@@ -841,6 +841,33 @@ qio_channel_socket_set_cork(QIOChannel *ioc,socket_set_cork (sioc->fd, v); } +static int +qio_channel_socket_get_peerpid (QIOChannel *ioc, +unsigned int *pid, +Error **errp) +{ +#ifdef CONFIG_LINUX +QIOChannelSocket *sioc =QIO_CHANNEL_SOCKET (ioc); +Error *err =NULL ; +socklen_t len =sizeof (struct ucred ); + +struct ucred cred; +if (getsockopt (sioc->fd, +SOL_SOCKET ,SO_PEERCRED , + &cred, &len) ==-1 ) { +error_setg_errno (&err, errno,"Unable to get peer credentials" ); +error_propagate (errp, err); + *pid =-1 ; +return -1 ; + } + *pid = (unsigned int )cred.pid; +return 0 ; +#else +error_setg (errp,"Unsupported feature" ); + *pid =-1 ; +return -1 ; +#endif +} static int qio_channel_socket_close(QIOChannel *ioc,
@@ -938,6 +965,7 @@ static void qio_channel_socket_class_init(ObjectClass *klass,#ifdef QEMU_MSG_ZEROCOPY ioc_klass->io_flush = qio_channel_socket_flush;#endif + ioc_klass->io_peerpid = qio_channel_socket_get_peerpid; }static const TypeInfo qio_channel_socket_info = {
io/channel.c +13 -0
@@ -548,6 +548,19 @@ void qio_channel_set_cork(QIOChannel *ioc, } } +int qio_channel_get_peerpid (QIOChannel *ioc, +unsigned int *pid, +Error **errp) +{ +QIOChannelClass *klass =QIO_CHANNEL_GET_CLASS (ioc); + +if (!klass->io_peerpid) { +error_setg (errp,"Channel does not support peer pid" ); +return -1 ; + } + klass->io_peerpid (ioc, pid, errp); +return 0 ; +} off_t qio_channel_io_seek(QIOChannel *ioc, off_t offset,
Signature · 1 line
2.44.0
Re: [PATCH v5 1/3] qio: add support for SO_PEERCRED for socket channel
Quoted 44 lines · Anthony Harivel
The function
qio_channel_get_peercred()returns a pointer to the credentials of the peer process connected to this socket.This credentials structure is defined in <
sys/socket.h> as follows:struct ucred {
pid_t pid; /* Process ID of the sending process */ uid_t uid; /* User ID of the sending process */ gid_t gid; /* Group ID of the sending process */};
The use of this function is possible only for connected
AF_UNIXstream sockets and forAF_UNIXstream and datagram socket pairs.On platform other than Linux, the function return 0.
Signed-off-by Anthony Harivel <[email protected]>--- include/io/channel.h | 21 +++++++++++++++++++++ io/channel-socket.c | 28 ++++++++++++++++++++++++++++ io/channel.c | 13 +++++++++++++ 3 files changed, 62 insertions(+)include/io/channel.h +5
-0@@ -160,6 +160,9 @@ struct QIOChannelClass { void *opaque);int (*io_flush)(QIOChannel *ioc,Error **errp); +int (*io_peerpid)(QIOChannel *ioc, +unsigned int *pid, +Error **errp); };/* General I/O handling functions */ @@ -981,4 +984,22 @@ int coroutine_mixed_fn qio_channel_writev_full_all(QIOChannel *ioc,int qio_channel_flush (QIOChannel *ioc,Error **errp); +/** +* qio_channel_get_peercred:
include/io/channel.h int coroutine_mixed_fn qio_channel_writev_full_all(QIOChannel *ioc, · line 998
Quoted 10 lines
include/io/channel.h +10
-0+* @ioc: the channel object +* @pid: pointer to pid +* @errp: pointer to a NULL-initialized error object +* +* Returns the pid of the peer process connected to this socket. +* +* The use of this function is possible only for connected +* AF_UNIX stream sockets and for AF_UNIX stream and datagram +* socket pairs on Linux. +* Return -1 on error with pid -1 for the non-Linux OS.
with pid -1 -> and set *pid to -1.
Quoted 8 lines
include/io/channel.h +1
-0+*/ static const TypeInfo qio_channel_socket_info = {io/channel.c +0
-0@@ -548,6 +548,19 @@ void qio_channel_set_cork(QIOChannel *ioc, }
io/channel.c void qio_channel_set_cork(QIOChannel *ioc, · line 558
Quoted 10 lines
io/channel.c +8
-0} +int qio_channel_get_peerpid (QIOChannel *ioc, +unsigned int *pid, +Error **errp) +{ +QIOChannelClass *klass =QIO_CHANNEL_GET_CLASS (ioc); + +if (!klass->io_peerpid) { +error_setg (errp,"Channel does not support peer pid" );
Missing for consistency:
+ *pid = -1;
io/channel.c void qio_channel_set_cork(QIOChannel *ioc, · line 562
Quoted 4 lines
io/channel.c +4
-0+return -1 ; + } + klass->io_peerpid (ioc, pid, errp); +return 0 ;
The error from klass->io_peerpid is ignored:
- klass->io_peerpid(ioc, pid, errp); - return 0; + return klass->io_peerpid(ioc, pid, errp);
Paolo
PATCH v5 2/3 tools: build qemu-vmsr-helper
In reply to Anthony HarivelIntroduce a privileged helper to access RAPL MSR.
The privileged helper tool, qemu-vmsr-helper, is designed to provide virtual machines with the ability to read specific RAPL (Running Average Power Limit) MSRs without requiring CAP_SYS_RAWIO privileges or relying on external, out-of-tree patches.
The helper tool leverages Unix permissions and SO_PEERCRED socket options to enforce access control, ensuring that only processes explicitly requesting read access via readmsr() from a valid Thread ID can access these MSRs.
The list of RAPL MSRs that are allowed to be read by the helper tool is defined in rapl-msr-index.h. This list corresponds to the RAPL MSRs that will be supported in the next commit titled "Add support for RAPL MSRs in KVM/QEMU."
The tool is intentionally designed to run on the Linux x86 platform. This initial implementation is tailored for Intel CPUs but can be extended to support AMD CPUs in the future.
--- contrib/systemd/qemu-vmsr-helper.service | 15 + contrib/systemd/qemu-vmsr-helper.socket | 9 + docs/tools/index.rst | 1 + docs/tools/qemu-vmsr-helper.rst | 89 ++++ meson.build | 7 + tools/i386/qemu-vmsr-helper.c | 529 +++++++++++++++++++++++ tools/i386/rapl-msr-index.h | 28 ++ 7 files changed, 678 insertions(+) create mode 100644 contrib/systemd/qemu-vmsr-helper.service create mode 100644 contrib/systemd/qemu-vmsr-helper.socket create mode 100644 docs/tools/qemu-vmsr-helper.rst create mode 100644 tools/i386/qemu-vmsr-helper.c create mode 100644 tools/i386/rapl-msr-index.h
contrib/systemd/qemu-vmsr-helper.service new file +15 -0
@@ -0,0 +1,15 @@ +[Unit] +Description=Virtual RAPL MSR Daemon for QEMU + +[Service] +WorkingDirectory=/tmp +Type=simple +ExecStart=/usr/bin/qemu-vmsr-helper +PrivateTmp=yes +ProtectSystem=strict +ReadWritePaths=/var/run +RestrictAddressFamilies=AF_UNIX +Restart=always +RestartSec=0 + +[Install]
contrib/systemd/qemu-vmsr-helper.socket new file +9 -0
@@ -0,0 +1,9 @@ +[Unit] +Description=Virtual RAPL MSR helper for QEMU + +[Socket] +ListenStream=/run/qemu-vmsr-helper.sock +SocketMode=0600 + +[Install] +WantedBy=multi-user.target
docs/tools/index.rst +1 -0
@@ -16,3 +16,4 @@ command line utilities and other standalone programs. qemu-pr-helper qemu-trace-stap virtfs-proxy-helper + qemu-vmsr-helper
docs/tools/qemu-vmsr-helper.rst new file +89 -0
89 lines not on this page · raw message
meson.build +7 -0
@@ -4052,6 +4052,13 @@ if have_tools dependencies: [authz, crypto, io, qom, qemuutil, libcap_ng, mpathpersist], install: true) + + if cpu in ['x86', 'x86_64'] + executable('qemu-vmsr-helper', files('tools/i386/qemu-vmsr-helper.c'), + dependencies: [authz, crypto, io, qom, qemuutil, + libcap_ng, mpathpersist], + install: true) + endif endif if have_ivshmem
tools/i386/qemu-vmsr-helper.c new file +529 -0
529 lines not on this page · raw message
tools/i386/rapl-msr-index.h new file +28 -0
@@ -0,0 +1,28 @@ +/* +* Allowed list of MSR for Privileged RAPL MSR helper commands for QEMU +* +* Copyright (C) 2023 Red Hat, Inc. <[email protected]> +* +* Author: Anthony Harivel <[email protected]> +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; under version 2 of the License. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, see <http://www.gnu.org/licenses/>. +*/ + +/* +* Should stay in sync with the RAPL MSR +* in target/i386/cpu.h +*/ +#define MSR_RAPL_POWER_UNIT 0x00000606 +#define MSR_PKG_POWER_LIMIT 0x00000610 +#define MSR_PKG_ENERGY_STATUS 0x00000611 +#define MSR_PKG_POWER_INFO 0x00000614
Signature · 1 line
2.44.0
PATCH v5 3/3 Add support for RAPL MSRs in KVM/Qemu
In reply to Anthony HarivelStarting with the "Sandy Bridge" generation, Intel CPUs provide a RAPL interface (Running Average Power Limit) for advertising the accumulated energy consumption of various power domains (e.g. CPU packages, DRAM, etc.).
The consumption is reported via MSRs (model specific registers) like MSR_PKG_ENERGY_STATUS for the CPU package power domain. These MSRs are 64 bits registers that represent the accumulated energy consumption in micro Joules. They are updated by microcode every ~1ms.
For now, KVM always returns 0 when the guest requests the value of these MSRs. Use the KVM MSR filtering mechanism to allow QEMU handle these MSRs dynamically in userspace.
To limit the amount of system calls for every MSR call, create a new thread in QEMU that updates the "virtual" MSR values asynchronously.
Each vCPU has its own vMSR to reflect the independence of vCPUs. The thread updates the vMSR values with the ratio of energy consumed of the whole physical CPU package the vCPU thread runs on and the thread's utime and stime values.
All other non-vCPU threads are also taken into account. Their energy consumption is evenly distributed among all vCPUs threads running on the same physical CPU package.
To overcome the problem that reading the RAPL MSR requires priviliged access, a socket communication between QEMU and the qemu-vmsr-helper is mandatory. You can specified the socket path in the parameter.
This feature is activated with -accel kvm,rapl=true,path=/path/sock.sock
Actual limitation:
- Works only on Intel host CPU because AMD CPUs are using different MSR adresses.
- Only the Package Power-Plane (MSR_PKG_ENERGY_STATUS) is reported at the moment.
--- accel/kvm/kvm-all.c | 27 +++ docs/specs/index.rst | 1 + docs/specs/rapl-msr.rst | 155 ++++++++++++ include/sysemu/kvm.h | 2 + include/sysemu/kvm_int.h | 32 +++ target/i386/cpu.h | 8 + target/i386/kvm/kvm-cpu.c | 9 + target/i386/kvm/kvm.c | 428 ++++++++++++++++++++++++++++++++++ target/i386/kvm/meson.build | 1 + target/i386/kvm/vmsr_energy.c | 335 ++++++++++++++++++++++++++ target/i386/kvm/vmsr_energy.h | 99 ++++++++ 11 files changed, 1097 insertions(+) create mode 100644 docs/specs/rapl-msr.rst create mode 100644 target/i386/kvm/vmsr_energy.c create mode 100644 target/i386/kvm/vmsr_energy.h
accel/kvm/kvm-all.c +27 -0
@@ -3613,6 +3613,21 @@ static void kvm_set_device(Object *obj, s->device =g_strdup (value); } +static void kvm_set_kvm_rapl (Object *obj,bool value,Error **errp) +{ +KVMState *s =KVM_STATE (obj); + s->msr_energy.enable = value; +} + +static void kvm_set_kvm_rapl_socket_path (Object *obj, +const char *str, +Error **errp) +{ +KVMState *s =KVM_STATE (obj); +g_free (s->msr_energy.socket_path); + s->msr_energy.socket_path =g_strdup (str); +} +static void kvm_accel_instance_init (Object *obj) {KVMState *s =KVM_STATE (obj);
@@ -3632,6 +3647,7 @@ static void kvm_accel_instance_init(Object *obj) s->xen_gnttab_max_frames =64 ; s->xen_evtchn_max_pirq =256 ; s->device =NULL ; + s->msr_energy.enable = false; }/**
@@ -3676,6 +3692,17 @@ static void kvm_accel_class_init(ObjectClass *oc, void *data)object_class_property_set_description (oc,"device" ,"Path to the device node to use (default: /dev/kvm)" ); +object_class_property_add_bool (oc,"rapl" , +NULL , + kvm_set_kvm_rapl); +object_class_property_set_description (oc,"rapl" , +"Allow energy related MSRs for RAPL interface in Guest" ); + +object_class_property_add_str (oc,"rapl-helper-socket" ,NULL , + kvm_set_kvm_rapl_socket_path); +object_class_property_set_description (oc,"rapl-helper-socket" , +"Socket Path for comminucating with the Virtual MSR helper daemon" ); +kvm_arch_accel_class_init (oc); }
docs/specs/index.rst +1 -0
@@ -33,3 +33,4 @@ guest hardware that is specific to QEMU. virt-ctlr vmcoreinfo vmgenid + rapl-msr
docs/specs/rapl-msr.rst new file +155 -0
155 lines not on this page · raw message
include/sysemu/kvm.h +2 -0
@@ -544,4 +544,6 @@ uint32_t kvm_dirty_ring_size(void);* reported for the VM. */ bool kvm_hwpoisoned_mem (void ); + +bool kvm_is_rapl_feat_enable (CPUState *cs);#endif
include/sysemu/kvm_int.h +32 -0
@@ -14,6 +14,9 @@#include "qemu/accel.h" #include "qemu/queue.h" #include "sysemu/kvm.h" +#include "hw/boards.h" +#include "hw/i386/topology.h" +#include "io/channel-socket.h" typedef struct KVMSlot {
@@ -48,6 +51,34 @@ typedef struct KVMMemoryListener {#define KVM_MSI_HASHTAB_SIZE 256 +typedef struct KVMHostTopoInfo { +/* Number of package on the Host */ +unsigned int maxpkgs; +/* Number of cpus on the Host */ +unsigned int maxcpus; +/* Number of cpus on each different package */ +unsigned int *pkg_cpu_count; +/* Each package can have different maxticks */ +unsigned int *maxticks; +}KVMHostTopoInfo ; + +struct KVMMsrEnergy { +pid_t pid; +bool enable; +char *socket_path; +QIOChannelSocket *sioc; +QemuThread msr_thr; +unsigned int vcpus; +unsigned int vsockets; +X86CPUTopoInfo topo_info; +KVMHostTopoInfo host_topo; +const CPUArchIdList *cpu_list; +uint64_t *msr_value; +uint64_t msr_unit; +uint64_t msr_limit; +uint64_t msr_info; +}; +enum KVMDirtyRingReaperState {KVM_DIRTY_RING_REAPER_NONE =0 ,/* The reaper is sleeping */
@@ -114,6 +145,7 @@ struct KVMStatebool kvm_dirty_ring_with_bitmap;uint64_t kvm_eager_split_size;/* Eager Page Splitting chunk size */ struct KVMDirtyRingReaper reaper; +struct KVMMsrEnergy msr_energy;NotifyVmexitOption notify_vmexit;uint32_t notify_window;uint32_t xen_version;
target/i386/cpu.h +8 -0
@@ -400,6 +400,10 @@ typedef enum X86Seg {#define MSR_IA32_TSX_CTRL 0x122#define MSR_IA32_TSCDEADLINE 0x6e0#define MSR_IA32_PKRS 0x6e1 +#define MSR_RAPL_POWER_UNIT 0x00000606 +#define MSR_PKG_POWER_LIMIT 0x00000610 +#define MSR_PKG_ENERGY_STATUS 0x00000611 +#define MSR_PKG_POWER_INFO 0x00000614#define MSR_ARCH_LBR_CTL 0x000014ce#define MSR_ARCH_LBR_DEPTH 0x000014cf#define MSR_ARCH_LBR_FROM_0 0x00001500
@@ -1795,6 +1799,10 @@ typedef struct CPUArchState {uintptr_t retaddr; +/* RAPL MSR */ +uint64_t msr_rapl_power_unit; +uint64_t msr_pkg_energy_status; +/* Fields up to this point are cleared by a CPU reset */ struct {} end_reset_fields;
target/i386/kvm/kvm-cpu.c +9 -0
@@ -50,6 +50,15 @@ static bool kvm_cpu_realizefn(CPUState *cs, Error **errp) MSR_IA32_UCODE_REV); } } + +if (kvm_is_rapl_feat_enable (cs)) { +if (!IS_INTEL_CPU (env)) { +error_setg (errp,"RAPL feature can only be\ +enabled with Intel CPU models" ); +return false; + } + } +return host_cpu_realizefn (cs, errp); }
target/i386/kvm/kvm.c +428 -0
461 lines not on this page · raw message
target/i386/kvm/meson.build +1 -0
@@ -3,6 +3,7 @@ i386_kvm_ss = ss.source_set() i386_kvm_ss.add(files( 'kvm.c', 'kvm-cpu.c', + 'vmsr_energy.c', )) i386_kvm_ss.add(when: 'CONFIG_XEN_EMU', if_true: files('xen-emu.c'))
target/i386/kvm/vmsr_energy.c new file +335 -0
335 lines not on this page · raw message
target/i386/kvm/vmsr_energy.h new file +99 -0
99 lines not on this page · raw message
Signature · 1 line
2.44.0
Re: [PATCH v5 3/3] Add support for RAPL MSRs in KVM/Qemu
Hi Anthony,
May I ask what your usage scenario is? Is it to measure Guest's energy consumption and to charged per watt consumed? ;-)
Quoted 9 lines · Anthony Harivel
Date: Thu, 11 Apr 2024 14:14:34 +0200
From: Anthony Harivel <[email protected]>
Subject: [PATCH v5 3/3] Add support for RAPL MSRs in KVM/QemuStarting with the "Sandy Bridge" generation, Intel CPUs provide a RAPL interface (Running Average Power Limit) for advertising the accumulated energy consumption of various power domains (e.g. CPU packages, DRAM, etc.).
Quoted 4 lines
The consumption is reported via MSRs (model specific registers) like
MSR_PKG_ENERGY_STATUSfor the CPU package power domain. These MSRs are 64 bits registers that represent the accumulated energy consumption in micro Joules. They are updated by microcode every ~1ms.
What is your current target platform?
On future Xeon platforms (EMR and beyond) RAPL will support TPMI (an MMIO interface) and the TPMI based RAPL will be preferred in the future as well:
* TPMI doc: https://github.com/intel/tpmi_power_management
* TPMI based RAPL driver: drivers/powercap/intel_rapl_tpmi.c
So do you have the plan to support RAPL-TPMI?
Quoted 7 lines
For now, KVM always returns 0 when the guest requests the value of these MSRs. Use the KVM MSR filtering mechanism to allow QEMU handle these MSRs dynamically in userspace.
To limit the amount of system calls for every MSR call, create a new thread in QEMU that updates the "virtual" MSR values asynchronously.
Quoted 8 lines
Each vCPU has its own vMSR to reflect the independence of vCPUs. The thread updates the vMSR values with the ratio of energy consumed of the whole physical CPU package the vCPU thread runs on and the thread's utime and stime values.
All other non-vCPU threads are also taken into account. Their energy consumption is evenly distributed among all vCPUs threads running on the same physical CPU package.
The package energy consumption includes core part and uncore part, where uncore part consumption may not be able to be scaled based on vCPU runtime ratio.
When the uncore part consumption is small, the error in this part is small, but if it is large, then the error generated by scaling by vCPU runtime will be large.
May I ask what your usage scenario is? Is there significant uncore consumption (e.g. GPU)?
Also, I think of a generic question is whether the error in this calculation is measurable? Like comparing the RAPL status of the same workload on Guest and bare metal to check the error.
IIUC, this calculation is highly affected by native/sibling Guests, especially in cloud scenarios where there are multiple Guests, the accuracy of this algorithm needs to be checked.
Quoted 5 lines
To overcome the problem that reading the RAPL MSR requires priviliged access, a socket communication between QEMU and the qemu-vmsr-helper is mandatory. You can specified the socket path in the parameter.
This feature is activated with -accel kvm,rapl=true,path=/path/sock.sock
Based on the above comment, I suggest to rename this option as "rapl-msr" to distinguish it from rapl-tpmi.
In addition, RAPL is basically a CPU feature, I think it would be more appropriate to make it as a x86 CPU's property.
Your RAPL support actually provides a framework for assisting KVM emulation in userspace, so this informs other feature support (maybe model specific, or architectural) in the future. Enabling/disabling CPU features via -cpu looks more natural.
[snip]
Quoted 42 lines
docs/specs/rapl-msr.rst new file +41
-0+High level implementation +------------------------- + +In order to update the value of the virtual MSR, a QEMU thread is created. +The thread is basically just an infinity loop that does: + +1. Snapshot of the time metrics of all QEMU threads (Time spent scheduled in + Userspace and System) +2. Snapshot of the actual MSR_PKG_ENERGY_STATUS counter of all packages where + the QEMU threads are running on. + +3. Sleep for 1 second - During this pause the vcpu and other non-vcpu threads + will do what they have to do and so the energy counter will increase. + +4. Repeat 2. and 3. and calculate the delta of every metrics representing the + time spent scheduled for each QEMU thread *and* the energy spent by the + packages during the pause. + +5. Filter the vcpu threads and the non-vcpu threads. + +6. Retrieve the topology of the Virtual Machine. This helps identify which + vCPU is running on which virtual package. + +7. The total energy spent by the non-vcpu threads is divided by the number + of vcpu threads so that each vcpu thread will get an equal part of the + energy spent by the QEMU workers. + +8. Calculate the ratio of energy spent per vcpu threads. + +9. Calculate the energy for each virtual package. + +10. The virtual MSRs are updated for each virtual package. Each vCPU that + belongs to the same package will return the same value when accessing the + the MSR. + +11. Loop back to 1. + +Ratio calculation +----------------- + +In Linux, a process has an execution time associated with it. The scheduler is
docs/specs/rapl-msr.rst line 98
Quoted 10 lines
docs/specs/rapl-msr.rst new file +10
-0+dividing the time in clock ticks. The number of clock ticks per second can be +found by the sysconf system call. A typical value of clock ticks per second is +100. So a core can run a process at the maximum of 100 ticks per second. If a +package has 4 cores, 400 ticks maximum can be scheduled on all the cores +of the package for a period of 1 second. + +The /proc/[pid]/stat [#b]_ is a sysfs file that can give the executed time of a +process with the [pid] as the process ID. It gives the amount of ticks the +process has been scheduled in userspace (utime) and kernel space (stime). +
I understand tick would ignore frequency changes, e.g., HWP's auto-pilot or turbo boost. All these CPU frequency change would impact on core energy consumption.
I think the better way would be to use APERF counter, but unfortunately it lacks virtualization support (for Intel).
Due to such considerations, it may be more worthwhile to evaluate the accuracy of this tick-based algorithm.
[snip]
Quoted 4 lines
target/i386/kvm/kvm.c +0
-0
[snip]
Quoted 12 lines
target/i386/kvm/kvm.c +12
-0+static void *kvm_msr_energy_thread (void *data) +{ +KVMState *s = data; +struct KVMMsrEnergy *vmsr = &s->msr_energy; + + g_autofree package_energy_stat *pkg_stat = NULL; + g_autofree thread_stat *thd_stat = NULL; + g_autofree pid_t *thread_ids = NULL; + g_autofree CPUState *cpu = NULL; + g_autofree unsigned int *vpkgs_energy_stat = NULL; +unsigned int num_threads =0 ; +unsigned int tmp_num_threads =0 ;
[snip]
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2653
Quoted 2 lines
target/i386/kvm/kvm.c +2
-0+/* Sleep a short period while the other threads are working */ +usleep (MSR_ENERGY_THREAD_SLEEP_US );
Is it possible to passively read the energy status? i.e. access the Host MSR and calculate the energy consumption for the Guest when the Guest triggers the relevant exit.
I think this might make the error larger, but not sure the error would be so large as to be unacceptable.
[snip]
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2734
Quoted 11 lines
target/i386/kvm/kvm.c +11
-0+/* Retrieve the virtual package number of each vCPU */ +for (int i =0 ; i < vmsr->x86_cpu_list->len; i++) { +for (int j =0 ; j < num_threads; j++) { +if ((thd_stat[j].acpi_id == vmsr->x86_cpu_list->cpus[i].arch_id) + && (thd_stat[j].is_vcpu == true)) { +x86_topo_ids_from_apicid (thd_stat[j].acpi_id, + &vmsr->topo_info, &topo_ids); + thd_stat[j].vpkg_id = topo_ids.pkg_id; + } + } + }
We lack package instances as well as MSR topology, so we have to deal with this pain...
Similarly, my attempt at other package/die level MSRs [1] is to only allow Guest to set a package/die.
But I think that in the future with QOM-topo [2] (i.e. creating package/ die instances), MSR topology can have an easier implementation, though, looks there's a long way to go!
[1]: https://lore.kernel.org/qemu-devel/[email protected]/
[2]: https://lore.kernel.org/qemu-devel/[email protected]/
Quoted 13 lines
target/i386/kvm/kvm.c +13
-0+/* Calculate the total energy of all non-vCPU thread */ +for (int i =0 ; i < num_threads; i++) { +double temp; +if ((thd_stat[i].is_vcpu != true) && + (thd_stat[i].delta_ticks >0 )) { + temp =vmsr_get_ratio (pkg_stat[thd_stat[i].pkg_id].e_delta, + thd_stat[i].delta_ticks, + vmsr->host_topo.maxticks[thd_stat[i].pkg_id]); + pkg_stat[thd_stat[i].pkg_id].e_ratio + += (uint64_t )lround (temp); + } + } +
[snip]
Quoted 5 lines
target/i386/kvm/kvm.c +5
-0+static int kvm_msr_energy_thread_init (KVMState *s,MachineState *ms) +{ +struct KVMMsrEnergy *r = &s->msr_energy; +int ret =0 ; +
[snip]
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2832
Quoted 2 lines
target/i386/kvm/kvm.c +2
-0+/* Retrieve the number of virtual sockets */ + r->vsockets = ms->smp.sockets;
RAPL's package domain is special:
* When there's no die, it's package-scope.
* When there's die level, it's die-scope.
In SDM vol.3, char 15.10 PLATFORM SPECIFIC POWER MANAGEMENT SUPPORT, it
said: Package domain is the processor die.
So if a Guest has die level, thers MSRs should be shared in a die. Thus I guess the energy consumption status should also be distributed based on the die number.
Quoted 6 lines
target/i386/kvm/kvm.c +6
-0+/* Allocate register memory (MSR_PKG_STATUS) for each vcpu */ + r->msr_value =g_new0 (uint64_t, r->vcpus); + +/* Retrieve the CPUArchIDlist */ + r->x86_cpu_list =x86_possible_cpu_arch_ids (ms); +
[snip]
target/i386/kvm/kvm.c
Quoted 3 lines
target/i386/kvm/kvm.c +3
-0+int is_rapl_enabled (void ) +{ +const char *path ="/sys/class/powercap/intel-rapl/enabled" ;
This field does not ensure the existence of RAPL MSRs since TPMI would also enable this field. (See powercap_register_control_type() in drivers/powercap/intel_rapl_{msr,tpmi}.c)
We can read RAPL MSRs directly. If we get 0 or failure, then there's no RAPL MSRs on Host.
Quoted 14 lines
target/i386/kvm/kvm.c +14
-0+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; +}
[snip]
target/i386/kvm/kvm.c
Quoted 10 lines
target/i386/kvm/kvm.c +10
-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);
Similarly, based on the topological properties of RAPL, it is more accurate to also consider the die_id.
Currently, only CLX-AP has multiple dies. So, we either have to put a limit on the Host's CPU model or consider the topology of the die here as well. I think the latter is a more general approach.
target/i386/kvm/kvm.c
Quoted 11 lines
target/i386/kvm/kvm.c +11
-0+ +if (!g_file_get_contents (file_path, &file_contents, &length,NULL )) { + goto out; + } + + package_id =atoi (file_contents); + +out: +return package_id; +} +
Regards,
Zhao
Re: [PATCH v5 3/3] Add support for RAPL MSRs in KVM/Qemu
Quoted 15 lines · Zhao Liu
Hi Anthony,
May I ask what your usage scenario is? Is it to measure Guest's energy consumption and to charged per watt consumed? ;-)
Quoted 9 lines · Anthony Harivel
Date: Thu, 11 Apr 2024 14:14:34 +0200
From: Anthony Harivel <[email protected]>
Subject: [PATCH v5 3/3] Add support for RAPL MSRs in KVM/QemuStarting with the "Sandy Bridge" generation, Intel CPUs provide a RAPL interface (Running Average Power Limit) for advertising the accumulated energy consumption of various power domains (e.g. CPU packages, DRAM, etc.).
Quoted 6 lines
Quoted 4 lines
The consumption is reported via MSRs (model specific registers) like
MSR_PKG_ENERGY_STATUSfor the CPU package power domain. These MSRs are 64 bits registers that represent the accumulated energy consumption in micro Joules. They are updated by microcode every ~1ms.What is your current target platform?
I think we can assume /all/ future CPUs are conceptially in scope for this.
The use case is to allow guest owners to monitor the power consumption of their workloads, so they can take steps to optimize their guest VM workloads to reduce power consumed.
Quoted 3 lines
On future Xeon platforms (EMR and beyond) RAPL will support TPMI (an MMIO interface) and the TPMI based RAPL will be preferred in the future as well:
Is the MSR based interface likely to be removed in future silicon, or it will be remain for back compat ?
Quoted 3 lines
* TPMI doc: https://github.com/intel/tpmi_power_management
* TPMI based RAPL driver:drivers/powercap/intel_rapl_tpmi.c
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v5 3/3] Add support for RAPL MSRs in KVM/Qemu
Hi Daniel,
Quoted 25 lines · Daniel P. Berrangé
Date: Wed, 17 Apr 2024 13:27:03 +0100
From: "Daniel P. Berrangé" <[email protected]>
Subject: Re: [PATCH v5 3/3] Add support for RAPL MSRs in KVM/QemuQuoted 20 lines · Zhao Liu
Hi Anthony,
May I ask what your usage scenario is? Is it to measure Guest's energy consumption and to charged per watt consumed? ;-)
Quoted 13 lines · Anthony Harivel
Date: Thu, 11 Apr 2024 14:14:34 +0200
From: Anthony Harivel <[email protected]>
Subject: [PATCH v5 3/3] Add support for RAPL MSRs in KVM/QemuStarting with the "Sandy Bridge" generation, Intel CPUs provide a RAPL interface (Running Average Power Limit) for advertising the accumulated energy consumption of various power domains (e.g. CPU packages, DRAM, etc.).
The consumption is reported via MSRs (model specific registers) like
MSR_PKG_ENERGY_STATUSfor the CPU package power domain. These MSRs are 64 bits registers that represent the accumulated energy consumption in micro Joules. They are updated by microcode every ~1ms.
Quoted 8 lines
Quoted 1 line
What is your current target platform?
I think we can assume /all/ future CPUs are conceptially in scope for this.
The use case is to allow guest owners to monitor the power consumption of their workloads, so they can take steps to optimize their guest VM workloads to reduce power consumed.
Thanks for the explanation!
Quoted 6 lines
Quoted 3 lines
On future Xeon platforms (EMR and beyond) RAPL will support TPMI (an MMIO interface) and the TPMI based RAPL will be preferred in the future as well:
Is the MSR based interface likely to be removed in future silicon, or it will be remain for back compat ?
For Xeon, GNR will have both TMPI & MSR RAPL, but eventually MSR RAPL will be removed. Therefore, if RAPL support is desired for all future Xeons, then it's necessary to consider TMPI as the next plan.
Alternatively, the whole RAPL scope can be split into rapl-msr and rapl-tpmi features.
Quoted 3 lines
Quoted 3 lines
* TPMI doc: https://github.com/intel/tpmi_power_management
* TPMI based RAPL driver:drivers/powercap/intel_rapl_tpmi.c
Regards,
Zhao
Re: [PATCH v5 3/3] Add support for RAPL MSRs in KVM/Qemu
Hi Zhao, Daniel,
Zhao Liu, Apr 17, 2024 at 17:13:
Quoted 30 lines
Hi Daniel,
Quoted 27 lines · Daniel P. Berrangé
Date: Wed, 17 Apr 2024 13:27:03 +0100
From: "Daniel P. Berrangé" <[email protected]>
Subject: Re: [PATCH v5 3/3] Add support for RAPL MSRs in KVM/QemuQuoted 21 lines · Zhao Liu
Hi Anthony,
May I ask what your usage scenario is? Is it to measure Guest's energy consumption and to charged per watt consumed? ;-)
Quoted 9 lines · Anthony Harivel
Date: Thu, 11 Apr 2024 14:14:34 +0200
From: Anthony Harivel <[email protected]>
Subject: [PATCH v5 3/3] Add support for RAPL MSRs in KVM/QemuStarting with the "Sandy Bridge" generation, Intel CPUs provide a RAPL interface (Running Average Power Limit) for advertising the accumulated energy consumption of various power domains (e.g. CPU packages, DRAM, etc.).
Quoted 4 lines
The consumption is reported via MSRs (model specific registers) like
MSR_PKG_ENERGY_STATUSfor the CPU package power domain. These MSRs are 64 bits registers that represent the accumulated energy consumption in micro Joules. They are updated by microcode every ~1ms.What is your current target platform?
Quoted 9 lines
Quoted 6 lines
I think we can assume /all/ future CPUs are conceptially in scope for this.
The use case is to allow guest owners to monitor the power consumption of their workloads, so they can take steps to optimize their guest VM workloads to reduce power consumed.
Thanks for the explanation!
Thanks Daniel for stepping in on the explanation.
Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
On future Xeon platforms (EMR and beyond) RAPL will support TPMI (an MMIO interface) and the TPMI based RAPL will be preferred in the future as well:
Is the MSR based interface likely to be removed in future silicon, or it will be remain for back compat ?
Quoted 7 lines
For Xeon, GNR will have both TMPI & MSR RAPL, but eventually MSR RAPL will be removed. Therefore, if RAPL support is desired for all future Xeons, then it's necessary to consider TMPI as the next plan.
Alternatively, the whole RAPL scope can be split into rapl-msr and rapl-tpmi features.
I'm aware of the MSR/TPMI RAPL that will appear in the future, and I would like to share my perspective on that.
Firstly, we can safely assume that it will take years before all server hardware is transitioned to the new GNR (or future XEON without RAPL MSR). It may be around 2024 when these features could be integrated into QEMU. While the adoption of this feature might take some time, I'm optimistic that once it's implemented, people will finally have the tools to optimize workloads inside VMs and start reducing power consumption.
Secondly, the second-hand server market is substantial. This means that with the Virtual RAPL MSR, all XEON processors starting from Sandy Bridge (2012!) will have the potential for software optimization. Making the most of existing resources is essential for sustainability.
Lastly, when the TPMI becomes available in hardware in the future, the RAPL interface and ratio calculation will remain the same, with only the method of obtaining host values changing. This transition should be manageable.
Regards,
Anthony
Re: [PATCH v5 3/3] Add support for RAPL MSRs in KVM/Qemu
In reply to Zhao LiuHi Zhao,
Zhao Liu, Apr 17, 2024 at 12:07:
Quoted 4 lines
Hi Anthony,
May I ask what your usage scenario is? Is it to measure Guest's energy consumption and to charged per watt consumed? ;-)
See previous email from Daniel.
Quoted 17 lines · Anthony Harivel
Quoted 9 lines
Date: Thu, 11 Apr 2024 14:14:34 +0200
From: Anthony Harivel <[email protected]>
Subject: [PATCH v5 3/3] Add support for RAPL MSRs in KVM/QemuStarting with the "Sandy Bridge" generation, Intel CPUs provide a RAPL interface (Running Average Power Limit) for advertising the accumulated energy consumption of various power domains (e.g. CPU packages, DRAM, etc.).
Quoted 4 lines
The consumption is reported via MSRs (model specific registers) like
MSR_PKG_ENERGY_STATUSfor the CPU package power domain. These MSRs are 64 bits registers that represent the accumulated energy consumption in micro Joules. They are updated by microcode every ~1ms.What is your current target platform?
Quoted 7 lines
On future Xeon platforms (EMR and beyond) RAPL will support TPMI (an MMIO interface) and the TPMI based RAPL will be preferred in the future as well:
* TPMI doc: https://github.com/intel/tpmi_power_management
* TPMI based RAPL driver:drivers/powercap/intel_rapl_tpmi.cSo do you have the plan to support RAPL-TPMI?
Yes, I guess this would be inevitable in the future. But right now the lack of HW with this TPMI make hard to integrate it on day 1.
Quoted 16 lines
Quoted 7 lines
For now, KVM always returns 0 when the guest requests the value of these MSRs. Use the KVM MSR filtering mechanism to allow QEMU handle these MSRs dynamically in userspace.
To limit the amount of system calls for every MSR call, create a new thread in QEMU that updates the "virtual" MSR values asynchronously.
Quoted 8 lines
Each vCPU has its own vMSR to reflect the independence of vCPUs. The thread updates the vMSR values with the ratio of energy consumed of the whole physical CPU package the vCPU thread runs on and the thread's utime and stime values.
All other non-vCPU threads are also taken into account. Their energy consumption is evenly distributed among all vCPUs threads running on the same physical CPU package.
Quoted 8 lines
The package energy consumption includes core part and uncore part, where uncore part consumption may not be able to be scaled based on vCPU runtime ratio.
When the uncore part consumption is small, the error in this part is small, but if it is large, then the error generated by scaling by vCPU runtime will be large.
So far we can only work with what Intel is giving us i.e Package power plane and DRAM power plane on server, which is the main target of this feature. Maybe in the future, Intel will expand the core power plane and the uncore power plane to server class CPU ?
Quoted 3 lines
May I ask what your usage scenario is? Is there significant uncore consumption (e.g. GPU)?
Same answer as above: uncore/graphics power plane is only available on client class CPU.
Quoted 8 lines
Also, I think of a generic question is whether the error in this calculation is measurable? Like comparing the RAPL status of the same workload on Guest and bare metal to check the error.
IIUC, this calculation is highly affected by native/sibling Guests, especially in cloud scenarios where there are multiple Guests, the accuracy of this algorithm needs to be checked.
Indeed, depending on where your vCPUs are running within the package (on the native or sibling CPU), you might observe different power consumption levels. However, I don't consider this to be a problem, as the ratio calculation takes into account the vCPU's location.
We also need to approach the measurement differently. Due to the complexity of factors influencing power consumption, we must compare what is comparable. If you require precise power consumption data, use a power meter on the PSU of the server.It will provide the ultimate judgment. However, if you need an estimation to optimize software workloads in a guest, then this feature could be useful. All my tests have consistently shown reproducible output in terms of power consumption, which has convinced me that we can effectively work with it.
Quoted 8 lines
Quoted 5 lines
To overcome the problem that reading the RAPL MSR requires priviliged access, a socket communication between QEMU and the qemu-vmsr-helper is mandatory. You can specified the socket path in the parameter.
This feature is activated with -accel kvm,rapl=true,path=/path/sock.sock
Based on the above comment, I suggest to rename this option as "rapl-msr" to distinguish it from rapl-tpmi.
Fair enough, I can rename this like that.
Quoted 8 lines
In addition, RAPL is basically a CPU feature, I think it would be more appropriate to make it as a x86 CPU's property.
Your RAPL support actually provides a framework for assisting KVM emulation in userspace, so this informs other feature support (maybe model specific, or architectural) in the future. Enabling/disabling CPU features via -cpu looks more natural.
This is totally dependant of KVM because it used the KVM MSR Filtering to access userspace when a specific MSR is required.
I can try to find a way to use -cpu for this feature and check if KVM is activated or not.
Quoted 60 lines
[snip]
Quoted 42 lines
docs/specs/rapl-msr.rst new file +41
-0+High level implementation +------------------------- + +In order to update the value of the virtual MSR, a QEMU thread is created. +The thread is basically just an infinity loop that does: + +1. Snapshot of the time metrics of all QEMU threads (Time spent scheduled in + Userspace and System) +2. Snapshot of the actual MSR_PKG_ENERGY_STATUS counter of all packages where + the QEMU threads are running on. + +3. Sleep for 1 second - During this pause the vcpu and other non-vcpu threads + will do what they have to do and so the energy counter will increase. + +4. Repeat 2. and 3. and calculate the delta of every metrics representing the + time spent scheduled for each QEMU thread *and* the energy spent by the + packages during the pause. + +5. Filter the vcpu threads and the non-vcpu threads. + +6. Retrieve the topology of the Virtual Machine. This helps identify which + vCPU is running on which virtual package. + +7. The total energy spent by the non-vcpu threads is divided by the number + of vcpu threads so that each vcpu thread will get an equal part of the + energy spent by the QEMU workers. + +8. Calculate the ratio of energy spent per vcpu threads. + +9. Calculate the energy for each virtual package. + +10. The virtual MSRs are updated for each virtual package. Each vCPU that + belongs to the same package will return the same value when accessing the + the MSR. + +11. Loop back to 1. + +Ratio calculation +----------------- + +In Linux, a process has an execution time associated with it. The scheduler isdocs/specs/rapl-msr.rst line 98
Quoted 10 lines
docs/specs/rapl-msr.rst new file +10
-0+dividing the time in clock ticks. The number of clock ticks per second can be +found by the sysconf system call. A typical value of clock ticks per second is +100. So a core can run a process at the maximum of 100 ticks per second. If a +package has 4 cores, 400 ticks maximum can be scheduled on all the cores +of the package for a period of 1 second. + +The /proc/[pid]/stat [#b]_ is a sysfs file that can give the executed time of a +process with the [pid] as the process ID. It gives the amount of ticks the +process has been scheduled in userspace (utime) and kernel space (stime). +I understand tick would ignore frequency changes, e.g., HWP's auto-pilot or turbo boost. All these CPU frequency change would impact on core energy consumption.
Quoted 6 lines
I think the better way would be to use APERF counter, but unfortunately it lacks virtualization support (for Intel).
Due to such considerations, it may be more worthwhile to evaluate the accuracy of this tick-based algorithm.
I've evaluated such things with another tool called Kepler [1]. This tool calculate the power ratio with metrics from RAPL and uses either eBPF or the tick based systems for time metrics. The eBPF part [2] is triggered on each 'finish_task_switch' of Thread and calculate the delta of cpu cycle, cache miss, cpu time, etc. Very complex. My tests showed that the difference between using eBPF and tick based ratio is really not that important. Maybe on some special cases, using eBPF would show a way better accuracy but I'm not aware of that.
[1]: https://github.com/sustainable-computing-io/kepler
[2]: https://github.com/sustainable-computing-io/kepler/blob/main/bpfassets/libbpf/src/kepler.bpf.c
Quoted 24 lines
[snip]
Quoted 4 lines
target/i386/kvm/kvm.c +0
-0[snip]
Quoted 12 lines
target/i386/kvm/kvm.c +12
-0+static void *kvm_msr_energy_thread (void *data) +{ +KVMState *s = data; +struct KVMMsrEnergy *vmsr = &s->msr_energy; + + g_autofree package_energy_stat *pkg_stat = NULL; + g_autofree thread_stat *thd_stat = NULL; + g_autofree pid_t *thread_ids = NULL; + g_autofree CPUState *cpu = NULL; + g_autofree unsigned int *vpkgs_energy_stat = NULL; +unsigned int num_threads =0 ; +unsigned int tmp_num_threads =0 ;[snip]
Quoted 7 lines
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2653
Quoted 2 lines
target/i386/kvm/kvm.c +2
-0+/* Sleep a short period while the other threads are working */ +usleep (MSR_ENERGY_THREAD_SLEEP_US );Is it possible to passively read the energy status? i.e. access the Host MSR and calculate the energy consumption for the Guest when the Guest triggers the relevant exit.
Yes it could be possible. But what I wanted to avoid with my approach is the overhead it could take when accessing a RAPL MSR in the Guest. The value is always available and return very quickly.
I'm not sure about the overhead if we have to have to access the MSR, then do the calculation and so on.
Quoted 3 lines
I think this might make the error larger, but not sure the error would be so large as to be unacceptable.
I'm a bit concerned about the potential overflow in calculation if the time in between is too big.
Quoted 20 lines
[snip]
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2734
Quoted 11 lines
target/i386/kvm/kvm.c +11
-0+/* Retrieve the virtual package number of each vCPU */ +for (int i =0 ; i < vmsr->x86_cpu_list->len; i++) { +for (int j =0 ; j < num_threads; j++) { +if ((thd_stat[j].acpi_id == vmsr->x86_cpu_list->cpus[i].arch_id) + && (thd_stat[j].is_vcpu == true)) { +x86_topo_ids_from_apicid (thd_stat[j].acpi_id, + &vmsr->topo_info, &topo_ids); + thd_stat[j].vpkg_id = topo_ids.pkg_id; + } + } + }We lack package instances as well as MSR topology, so we have to deal with this pain...
Similarly, my attempt at other package/die level MSRs [1] is to only allow Guest to set a package/die.
Quoted 7 lines
But I think that in the future with QOM-topo [2] (i.e. creating package/ die instances), MSR topology can have an easier implementation, though, looks there's a long way to go!
[1]: https://lore.kernel.org/qemu-devel/[email protected]/
[2]: https://lore.kernel.org/qemu-devel/[email protected]/
Oh, great! Good to know!
Topology is indeed a nightmare so good luck with that!!
Quoted 31 lines
Quoted 13 lines
target/i386/kvm/kvm.c +13
-0+/* Calculate the total energy of all non-vCPU thread */ +for (int i =0 ; i < num_threads; i++) { +double temp; +if ((thd_stat[i].is_vcpu != true) && + (thd_stat[i].delta_ticks >0 )) { + temp =vmsr_get_ratio (pkg_stat[thd_stat[i].pkg_id].e_delta, + thd_stat[i].delta_ticks, + vmsr->host_topo.maxticks[thd_stat[i].pkg_id]); + pkg_stat[thd_stat[i].pkg_id].e_ratio + += (uint64_t )lround (temp); + } + } +[snip]
Quoted 5 lines
target/i386/kvm/kvm.c +5
-0+static int kvm_msr_energy_thread_init (KVMState *s,MachineState *ms) +{ +struct KVMMsrEnergy *r = &s->msr_energy; +int ret =0 ; +[snip]
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2832
Quoted 2 lines
target/i386/kvm/kvm.c +2
-0+/* Retrieve the number of virtual sockets */ + r->vsockets = ms->smp.sockets;RAPL's package domain is special:
* When there's no die, it's package-scope.
* When there's die level, it's die-scope.
Quoted 7 lines
In SDM vol.3, char 15.10 PLATFORM SPECIFIC POWER MANAGEMENT SUPPORT, it
said: Package domain is the processor die.So if a Guest has die level, thers MSRs should be shared in a die. Thus I guess the energy consumption status should also be distributed based on the die number.
Right, I've missed that totally. Thanks for let me know. I'm going to have a deeper look at this.
Quoted 9 lines
Quoted 6 lines
target/i386/kvm/kvm.c +6
-0+/* Allocate register memory (MSR_PKG_STATUS) for each vcpu */ + r->msr_value =g_new0 (uint64_t, r->vcpus); + +/* Retrieve the CPUArchIDlist */ + r->x86_cpu_list =x86_possible_cpu_arch_ids (ms); +[snip]
Quoted 8 lines
target/i386/kvm/kvm.c
Quoted 3 lines
target/i386/kvm/kvm.c +3
-0+int is_rapl_enabled (void ) +{ +const char *path ="/sys/class/powercap/intel-rapl/enabled" ;This field does not ensure the existence of RAPL MSRs since TPMI would also enable this field. (See
powercap_register_control_type()indrivers/powercap/intel_rapl_{msr,tpmi}.c)
But this is exactly what it is intended to do. Wether it is MSR of TPMI, checking this, tell me that RAPL is activated or not. If it is activated...
Quoted 3 lines
We can read RAPL MSRs directly. If we get 0 or failure, then there's no RAPL MSRs on Host.
... then it is safe to access the RAPL MSR. I would not like to access this on a XYZ cpu without knowing I can !
Quoted 31 lines
Quoted 14 lines
target/i386/kvm/kvm.c +14
-0+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; +}[snip]
target/i386/kvm/kvm.c
Quoted 10 lines
target/i386/kvm/kvm.c +10
-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);Similarly, based on the topological properties of RAPL, it is more accurate to also consider the
die_id.
Quoted 4 lines
Currently, only CLX-AP has multiple dies. So, we either have to put a limit on the Host's CPU model or consider the topology of the die here as well. I think the latter is a more general approach.
Alright. I take not on that and I will check the die approach.
Quoted 6 lines
Quoted 6 lines
target/i386/kvm/kvm.c +6
-0+ +if (!g_file_get_contents (file_path, &file_contents, &length,NULL )) { + goto out; + } + + package_id =atoi (file_contents);
Quoted 8 lines
target/i386/kvm/kvm.c
Quoted 5 lines
target/i386/kvm/kvm.c +5
-0+ +out: +return package_id; +} +Regards,
Zhao
Thanks a lot for all your feedback Zhao !
Regards,
Anthony
Re: [PATCH v5 3/3] Add support for RAPL MSRs in KVM/Qemu
Hi Anthony,
Quoted 13 lines · Anthony Harivel
Date: Thu, 18 Apr 2024 12:52:14 +0200
From: Anthony Harivel <[email protected]>
Subject: Re: [PATCH v5 3/3] Add support for RAPL MSRs in KVM/QemuQuoted 8 lines
The package energy consumption includes core part and uncore part, where uncore part consumption may not be able to be scaled based on vCPU runtime ratio.
When the uncore part consumption is small, the error in this part is small, but if it is large, then the error generated by scaling by vCPU runtime will be large.
Quoted 4 lines
So far we can only work with what Intel is giving us i.e Package power plane and DRAM power plane on server, which is the main target of this feature. Maybe in the future, Intel will expand the core power plane and the uncore power plane to server class CPU ?
Not future features, I'd like to illustrate the impact of the uncore part (iGPU/NPU on Client or various accelerators on Server) on this algorithm. Because the consumption of the uncore part is complex and not necessarily linearly related to the vCPU task running time.
It might be worth to state potential impact on accuracy of uncore parts to doc (I doubt that heavy uncore consumption will even affect the consistency of the energy trend as you said).
Anyway, clearer scenarios help this feature get used.
Quoted 6 lines
Quoted 3 lines
May I ask what your usage scenario is? Is there significant uncore consumption (e.g. GPU)?
Same answer as above: uncore/graphics power plane is only available on client class CPU.
Yes, iGPU is, but server may have other accelerators, e.g., DSA/IAA/QAT on SPR.
Quoted 14 lines
Quoted 8 lines
Also, I think of a generic question is whether the error in this calculation is measurable? Like comparing the RAPL status of the same workload on Guest and bare metal to check the error.
IIUC, this calculation is highly affected by native/sibling Guests, especially in cloud scenarios where there are multiple Guests, the accuracy of this algorithm needs to be checked.
Indeed, depending on where your vCPUs are running within the package (on the native or sibling CPU), you might observe different power consumption levels. However, I don't consider this to be a problem, as the ratio calculation takes into account the vCPU's location.
Quoted 9 lines
We also need to approach the measurement differently. Due to the complexity of factors influencing power consumption, we must compare what is comparable. If you require precise power consumption data, use a power meter on the PSU of the server.It will provide the ultimate judgment. However, if you need an estimation to optimize software workloads in a guest, then this feature could be useful. All my tests have consistently shown reproducible output in terms of power consumption, which has convinced me that we can effectively work with it.
Thanks, another mail in which you illustrated that the trend is consistent.
[snip]
Quoted 11 lines
Quoted 8 lines
In addition, RAPL is basically a CPU feature, I think it would be more appropriate to make it as a x86 CPU's property.
Your RAPL support actually provides a framework for assisting KVM emulation in userspace, so this informs other feature support (maybe model specific, or architectural) in the future. Enabling/disabling CPU features via -cpu looks more natural.
This is totally dependant of KVM because it used the KVM MSR Filtering to access userspace when a specific MSR is required.
Yes, but in other words, other KVM based features (completely hardware virtualization) are also configured by -cpu. This RAPL is still a CPU feature and just need KVM's help.
Quoted 3 lines
I can try to find a way to use -cpu for this feature and check if KVM is activated or not.
[snip]
Quoted 8 lines
Quoted 8 lines
I understand tick would ignore frequency changes, e.g., HWP's auto-pilot or turbo boost. All these CPU frequency change would impact on core energy consumption.
I think the better way would be to use APERF counter, but unfortunately it lacks virtualization support (for Intel).
Quoted 7 lines
Quoted 3 lines
Due to such considerations, it may be more worthwhile to evaluate the accuracy of this tick-based algorithm.
I've evaluated such things with another tool called Kepler [1]. This tool calculate the power ratio with metrics from RAPL and uses either eBPF or the tick based systems for time metrics.
Thanks for this information! I understand current tick based algorithm is a common approximation in the industry (like Kepler), right?
Quoted 6 lines
The eBPF part [2] is
triggered on each 'finish_task_switch' of Thread and calculate the delta of cpu cycle, cache miss, cpu time, etc. Very complex. My tests showed that the difference between using eBPF and tick based ratio is really not that important. Maybe on some special cases, using eBPF would show a way better accuracy but I'm not aware of that.
Good to know!
Just curious, so for using Kepler in Guest to optimize the task (your use case), is it only necessary that the trends are similar and there is no requirement for accuracy of the values?
Quoted 3 lines
[1]: https://github.com/sustainable-computing-io/kepler
[2]: https://github.com/sustainable-computing-io/kepler/blob/main/bpfassets/libbpf/src/kepler.bpf.c
[snip]
Quoted 15 lines
Quoted 8 lines
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2653
Quoted 3 lines
target/i386/kvm/kvm.c +2
-0+/* Sleep a short period while the other threads are working */ +usleep (MSR_ENERGY_THREAD_SLEEP_US );Is it possible to passively read the energy status? i.e. access the Host MSR and calculate the energy consumption for the Guest when the Guest triggers the relevant exit.
Yes it could be possible. But what I wanted to avoid with my approach is the overhead it could take when accessing a RAPL MSR in the Guest. The value is always available and return very quickly.
I'm not sure about the overhead if we have to have to access the MSR, then do the calculation and so on.
Quoted 6 lines
Quoted 3 lines
I think this might make the error larger, but not sure the error would be so large as to be unacceptable.
I'm a bit concerned about the potential overflow in calculation if the time in between is too big.
Okay.
[snip]
Quoted 13 lines
Quoted 8 lines
target/i386/kvm/vmsr_energy.c line 42
Quoted 3 lines
target/i386/kvm/vmsr_energy.c new file +3
-0+int is_rapl_enabled (void ) +{ +const char *path ="/sys/class/powercap/intel-rapl/enabled" ;This field does not ensure the existence of RAPL MSRs since TPMI would also enable this field. (See
powercap_register_control_type()indrivers/powercap/intel_rapl_{msr,tpmi}.c)But this is exactly what it is intended to do. Wether it is MSR of TPMI, checking this, tell me that RAPL is activated or not. If it is activated...
Quoted 6 lines
Quoted 3 lines
We can read RAPL MSRs directly. If we get 0 or failure, then there's no RAPL MSRs on Host.
... then it is safe to access the RAPL MSR. I would not like to access this on a XYZ cpu without knowing I can !
RAPL will disappear and this field can't ease your concern. ;-)
Even if there are no commercially available machines yet, the logic of the linux driver has shown that relying on this item is unreliable.
For model specific features, either use the CPU model ID to know if it is supported, or to access the relevant MSR directly. Even if the feature is not supported, the register address should not be used for other purposes, so I don't think there is information leak problem here.
[snip]
Quoted 3 lines
Thanks a lot for all your feedback Zhao !
You're welcome! I've also been thinking about how to support the emulation of other thermal features in user space in QEMU, as you did with RAPL.
Regards,
Zhao
Re: [PATCH v5 3/3] Add support for RAPL MSRs in KVM/Qemu
In reply to Anthony HarivelQuoted 38 lines · Anthony Harivel
include/sysemu/kvm.h +2
-0@@ -544,4 +544,6 @@ uint32_t kvm_dirty_ring_size(void);* reported for the VM. */ bool kvm_hwpoisoned_mem (void ); + +bool kvm_is_rapl_feat_enable (CPUState *cs);#endif include/sysemu/kvm_int.h +12
-0@@ -14,6 +14,9 @@#include "qemu/accel.h" #include "qemu/queue.h" #include "sysemu/kvm.h" +#include "hw/boards.h" +#include "hw/i386/topology.h" +#include "io/channel-socket.h" typedef struct KVMSlot {@@ -48,6 +51,34 @@ typedef struct KVMMemoryListener {#define KVM_MSI_HASHTAB_SIZE 256 +typedef struct KVMHostTopoInfo { + /* Number of package on the Host */ + unsigned int maxpkgs; + /* Number of cpus on the Host */ + unsigned int maxcpus; + /* Number of cpus on each different package */ + unsigned int *pkg_cpu_count; + /* Each package can have different maxticks */ + unsigned int *maxticks;
include/sysemu/kvm_int.h typedef struct KVMMemoryListener { · line 72
Quoted 10 lines
include/sysemu/kvm_int.h +10
-0+} KVMHostTopoInfo; + +struct KVMMsrEnergy { +pid_t pid; +bool enable; +char *socket_path; +QIOChannelSocket *sioc; +QemuThread msr_thr; +unsigned int vcpus; +unsigned int vsockets;
Add 'guest_' prefix on to these two, to make it clearer
include/sysemu/kvm_int.h typedef struct KVMMemoryListener { · line 73
Quoted 1 line
include/sysemu/kvm_int.h +1
-0+X86CPUTopoInfo topo_info;
Lets call this 'guest_topo' too, to make it more explicitly distinguished from the next 'host_topo' field.
include/sysemu/kvm_int.h typedef struct KVMMemoryListener { · line 75
Quoted 2 lines
include/sysemu/kvm_int.h +2
-0+KVMHostTopoInfo host_topo; +const CPUArchIdList *cpu_list;
This name choice has an unfortunate side effect.
cpu.h has a '#define cpu_list x86_cpu_list' for the purposes of selecting the target specific cpu_list function.
Unfortunately that macro affects your field name here, which is why the code is wierdly able to set an 'x86_cpu_list' field despite you having declared it 'cpu_list'.
I'd suggest perhaps calling this field 'guest_cpus' to avoid this confusing side-effect, and again also making clear that this is tracking guest, not host, CPUs.
Quoted 45 lines
include/sysemu/kvm_int.h +7
-0+uint64_t *msr_value; +uint64_t msr_unit; +uint64_t msr_limit; +uint64_t msr_info; +}; +enum KVMDirtyRingReaperState {KVM_DIRTY_RING_REAPER_NONE =0 ,/* The reaper is sleeping */ @@ -114,6 +145,7 @@ struct KVMStatebool kvm_dirty_ring_with_bitmap;uint64_t kvm_eager_split_size;/* Eager Page Splitting chunk size */ struct KVMDirtyRingReaper reaper; +struct KVMMsrEnergy msr_energy;NotifyVmexitOption notify_vmexit;uint32_t notify_window;uint32_t xen_version;target/i386/kvm/kvm.c +18
-0@@ -2513,6 +2565,339 @@ static void register_smram_listener(Notifier *n, void *unused) &smram_address_space, 1, "kvm-smram"); } +static void *kvm_msr_energy_thread (void *data) +{ +KVMState *s = data; +struct KVMMsrEnergy *vmsr = &s->msr_energy; + + g_autofree package_energy_stat *pkg_stat = NULL; + g_autofree thread_stat *thd_stat = NULL; + g_autofree pid_t *thread_ids = NULL; + g_autofree CPUState *cpu = NULL; + g_autofree unsigned int *vpkgs_energy_stat = NULL; +unsigned int num_threads =0 ; +unsigned int tmp_num_threads =0 ; + +X86CPUTopoIDs topo_ids; + +rcu_register_thread (); + +/* Allocate memory for each package energy status */
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2595
Quoted 10 lines
target/i386/kvm/kvm.c +10
-0+ pkg_stat =g_new0 (package_energy_stat, vmsr->host_topo.maxpkgs); + +/* Allocate memory for thread stats */ + thd_stat =g_new0 (thread_stat,1 ); + +/* Allocate memory for holding virtual package energy counter */ + vpkgs_energy_stat = g_new0(unsigned int, vmsr->vsockets); + +/* Populate the max tick of each packages */ +for (int i =0 ; i <= vmsr->host_topo.maxpkgs; i++) {
The 'maxticks' array is allocated as 'maxpkgs' in size, so this limit condition needs to be '<' not '<=', to avoid an out of bounds write.
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2608
Quoted 13 lines
target/i386/kvm/kvm.c +13
-0+/* +* Max numbers of ticks per package +* Time in second * Number of ticks/second * Number of cores/package +* ex: 100 ticks/second/CPU, 12 CPUs per Package gives 1200 ticks max +*/ + vmsr->host_topo.maxticks[i] = (MSR_ENERGY_THREAD_SLEEP_US /1000000 ) + *sysconf (_SC_CLK_TCK) + * vmsr->host_topo.pkg_cpu_count[i]; + } + +while (true) { +/* Get all qemu threads id */ + thread_ids =vmsr_get_thread_ids (vmsr->pid, &num_threads);
Since you're re-allocating thread_ids on each loop iteration, you need to free it, not overwrite the pointer. This is easily achieved by moving the earlier declaration inside the loop body. ie
g_autofree pid_t *thread_ids =
thread_ids = vmsr_get_thread_ids(vmsr->pid, &num_threads);target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2618
Quoted 10 lines
target/i386/kvm/kvm.c +10
-0+ +if (thread_ids ==NULL ) { + goto clean; + } + +if (tmp_num_threads < num_threads) { + thd_stat =g_renew (thread_stat, thd_stat, num_threads); + } + + tmp_num_threads = num_threads;
You could eliminate 'tmp_num_threads' and just unconditionally call g_renew on each loop iteration. It'll effectively be a no-op if the size hasn't changed since the last iteration, and won't hurt performance in any measurable way.
Quoted 110 lines
target/i386/kvm/kvm.c +110
-0+ +/* Populate all the thread stats */ +for (int i =0 ; i < num_threads; i++) { + thd_stat[i].utime = g_new0(unsigned long long, 2); + thd_stat[i].stime = g_new0(unsigned long long, 2); + thd_stat[i].thread_id = thread_ids[i]; +vmsr_read_thread_stat (vmsr->pid, + thd_stat[i].thread_id, + thd_stat[i].utime, + thd_stat[i].stime, + &thd_stat[i].cpu_id); + thd_stat[i].pkg_id = +vmsr_get_physical_package_id (thd_stat[i].cpu_id); + } + +/* Retrieve all packages power plane energy counter */ +for (int i =0 ; i < vmsr->host_topo.maxpkgs; i++) { +for (int j =0 ; j < num_threads; j++) { +/* +* Use the first thread we found that ran on the CPU +* of the package to read the packages energy counter +*/ +if (thd_stat[j].pkg_id == i) { + pkg_stat[i].e_start = +vmsr_read_msr (MSR_PKG_ENERGY_STATUS , + thd_stat[j].cpu_id, + thd_stat[j].thread_id, + s->msr_energy.sioc); +break ; + } + } + } + +/* Sleep a short period while the other threads are working */ +usleep (MSR_ENERGY_THREAD_SLEEP_US ); + +/* +* Retrieve all packages power plane energy counter +* Calculate the delta of all packages +*/ +for (int i =0 ; i < vmsr->host_topo.maxpkgs; i++) { +for (int j =0 ; j < num_threads; j++) { +/* +* Use the first thread we found that ran on the CPU +* of the package to read the packages energy counter +*/ +if (thd_stat[j].pkg_id == i) { + pkg_stat[i].e_end = +vmsr_read_msr (MSR_PKG_ENERGY_STATUS , + thd_stat[j].cpu_id, + thd_stat[j].thread_id, + s->msr_energy.sioc); +/* +* Prevent the case we have migrate the VM +* during the sleep period or any other cases +* were energy counter might be lower after +* the sleep period. +*/ +if (pkg_stat[i].e_end > pkg_stat[i].e_start) { + pkg_stat[i].e_delta = + pkg_stat[i].e_end - pkg_stat[i].e_start; + }else { + pkg_stat[i].e_delta =0 ; + } +break ; + } + } + } + +/* Delta of ticks spend by each thread between the sample */ +for (int i =0 ; i < num_threads; i++) { +vmsr_read_thread_stat (vmsr->pid, + thd_stat[i].thread_id, + thd_stat[i].utime, + thd_stat[i].stime, + &thd_stat[i].cpu_id); + +if (vmsr->pid <0 ) { +/* +* We don't count the dead thread +* i.e threads that existed before the sleep +* and not anymore +*/ + thd_stat[i].delta_ticks =0 ; + }else { +vmsr_delta_ticks (thd_stat, i); + } + } + +/* +* Identify the vcpu threads +* Calculate the number of vcpu per package +*/ +CPU_FOREACH (cpu) { +for (int i =0 ; i < num_threads; i++) { +if (cpu->thread_id == thd_stat[i].thread_id) { + thd_stat[i].is_vcpu = true; + thd_stat[i].vcpu_id = cpu->cpu_index; + pkg_stat[thd_stat[i].pkg_id].nb_vcpu++; + thd_stat[i].acpi_id =kvm_arch_vcpu_id (cpu); +break ; + } + } + } + +/* Retrieve the virtual package number of each vCPU */ +for (int i =0 ; i < vmsr->x86_cpu_list->len; i++) { +for (int j =0 ; j < num_threads; j++) { +if ((thd_stat[j].acpi_id == vmsr->x86_cpu_list->cpus[i].arch_id) + && (thd_stat[j].is_vcpu == true)) {
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2738
Quoted 10 lines
target/i386/kvm/kvm.c +10
-0+x86_topo_ids_from_apicid (thd_stat[j].acpi_id, + &vmsr->topo_info, &topo_ids); + thd_stat[j].vpkg_id = topo_ids.pkg_id; + } + } + } + +/* Calculate the total energy of all non-vCPU thread */ +for (int i =0 ; i < num_threads; i++) { +double temp;
Move this inside the 'if', and assign to it at time of declaration.
Quoted 13 lines
target/i386/kvm/kvm.c +13
-0+if ((thd_stat[i].is_vcpu != true) && + (thd_stat[i].delta_ticks >0 )) { + temp =vmsr_get_ratio (pkg_stat[thd_stat[i].pkg_id].e_delta, + thd_stat[i].delta_ticks, + vmsr->host_topo.maxticks[thd_stat[i].pkg_id]); + pkg_stat[thd_stat[i].pkg_id].e_ratio + += (uint64_t )lround (temp); + } + } + +/* Calculate the ratio per non-vCPU thread of each package */ +for (int i =0 ; i < vmsr->host_topo.maxpkgs; i++) { +if (pkg_stat[i].nb_vcpu >0 ) {
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2761
Quoted 10 lines
target/i386/kvm/kvm.c +10
-0+ pkg_stat[i].e_ratio = pkg_stat[i].e_ratio / pkg_stat[i].nb_vcpu; + } + } + +/* +* Calculate the energy for each Package: +* Energy Package = sum of each vCPU energy that belongs to the package +*/ +for (int i =0 ; i < num_threads; i++) { +double temp;
Aain can move inside the 'if'
Quoted 23 lines
target/i386/kvm/kvm.c +23
-0+ +if ((thd_stat[i].is_vcpu == true) && \ + (thd_stat[i].delta_ticks >0 )) { + temp =vmsr_get_ratio (pkg_stat[thd_stat[i].pkg_id].e_delta, + thd_stat[i].delta_ticks, + vmsr->host_topo.maxticks[thd_stat[i].pkg_id]); + vpkgs_energy_stat[thd_stat[i].vpkg_id] += + (uint64_t )lround (temp); + vpkgs_energy_stat[thd_stat[i].vpkg_id] += + pkg_stat[thd_stat[i].pkg_id].e_ratio; + } + } + +/* +* Finally populate the vmsr register of each vCPU with the total +* package value to emulate the real hardware where each CPU return the +* value of the package it belongs. +*/ +for (int i =0 ; i < num_threads; i++) { +if ((thd_stat[i].is_vcpu == true) && \ + (thd_stat[i].delta_ticks >0 )) { + vmsr->msr_value[thd_stat[i].vcpu_id] = \ + vpkgs_energy_stat[thd_stat[i].vpkg_id];
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2794
Quoted 10 lines
target/i386/kvm/kvm.c +10
-0+ } + } + +/* Freeing memory before zeroing the pointer */ +for (int i =0 ; i < num_threads; i++) { +g_free (thd_stat[i].utime); +g_free (thd_stat[i].stime); + } +/* Zeroing the reused memory with g_renew */ +memset (thd_stat,0 , num_threads *sizeof (thread_stat));
This memset() should be done immediately after the 'g_renew' call, since if g_renew enlarged the array, it won't have zero'd the newly added elements.
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2795
Quoted 1 line
target/i386/kvm/kvm.c +1
-0+memset (thread_ids,0 ,sizeof (pid_t));
We need to be free'ing thread_ids, not zero'ing it. This memset can just be removed entirely if you follow my earlier suggestion to rely on g_autofree and declare thread_ids inside the loop body.
Quoted 74 lines
target/i386/kvm/kvm.c +74
-0+ } + +clean: +rcu_unregister_thread (); +return NULL ; +} + +static int kvm_msr_energy_thread_init (KVMState *s,MachineState *ms) +{ +struct KVMMsrEnergy *r = &s->msr_energy; +int ret =0 ; + +/* +* Sanity check +* 1. Host cpu must be Intel cpu +* 2. RAPL must be enabled on the Host +*/ +if (is_host_cpu_intel ()) { +error_report ("The RAPL feature can only be enabled on hosts\ +with Intel CPU models" ); + ret =1 ; + goto out; + } + +if (!is_rapl_enabled ()) { + ret =1 ; + goto out; + } + +/* Retrieve the virtual topology */ +vmsr_init_topo_info (&r->topo_info, ms); + +/* Retrieve the number of vcpu */ + r->vcpus = ms->smp.cpus; + +/* Retrieve the number of virtual sockets */ + r->vsockets = ms->smp.sockets; + +/* Allocate register memory (MSR_PKG_STATUS) for each vcpu */ + r->msr_value =g_new0 (uint64_t, r->vcpus); + +/* Retrieve the CPUArchIDlist */ + r->x86_cpu_list =x86_possible_cpu_arch_ids (ms); + +/* Max number of cpus on the Host */ + r->host_topo.maxcpus =vmsr_get_maxcpus (); +if (r->host_topo.maxcpus ==0 ) { +error_report ("host max cpus = 0" ); + ret =1 ; + goto out; + } + +/* Max number of packages on the host */ + r->host_topo.maxpkgs =vmsr_get_max_physical_package (r->host_topo.maxcpus); +if (r->host_topo.maxpkgs ==0 ) { +error_report ("host max pkgs = 0" ); + ret =1 ; + goto out; + } + +/* Allocate memory for each package on the host */ + r->host_topo.pkg_cpu_count = g_new0(unsigned int, r->host_topo.maxpkgs); + r->host_topo.maxticks = g_new0(unsigned int, r->host_topo.maxpkgs); + +vmsr_count_cpus_per_package (r->host_topo.pkg_cpu_count, + r->host_topo.maxpkgs); +for (int i =0 ; i < r->host_topo.maxpkgs; i++) { +if (r->host_topo.pkg_cpu_count[i] ==0 ) { +error_report ("cpu per packages = 0 on package_%d" , i); + ret =1 ; + goto out; + } + } +
target/i386/kvm/kvm.c static void register_smram_listener(Notifier *n, void *unused) · line 2879
Quoted 10 lines
target/i386/kvm/kvm.c +10
-0+/* Get QEMU PID*/ + r->pid =getpid (); + +/* Compute the socket path if necessary */ +if (s->msr_energy.socket_path ==NULL ) { + s->msr_energy.socket_path =vmsr_compute_default_paths (); + } + +/* Open socket with vmsr helper */ + s->msr_energy.sioc =vmsr_open_socket (s->msr_energy.socket_path);
Need to detect & report error if opening the socket failed.
Quoted 49 lines
target/i386/kvm/kvm.c +21
-0+ +/* Those MSR values should not change */ + r->msr_unit =vmsr_read_msr (MSR_RAPL_POWER_UNIT ,0 , r->pid, + s->msr_energy.sioc); + r->msr_limit =vmsr_read_msr (MSR_PKG_POWER_LIMIT ,0 , r->pid, + s->msr_energy.sioc); + r->msr_info =vmsr_read_msr (MSR_PKG_POWER_INFO ,0 , r->pid, + s->msr_energy.sioc); +if (r->msr_unit ==0 || r->msr_limit ==0 || r->msr_info ==0 ) { +error_report ("can't read any virtual msr" ); + ret =1 ; + goto out; + } + +qemu_thread_create (&r->msr_thr,"kvm-msr" , + kvm_msr_energy_thread, + s,QEMU_THREAD_JOINABLE ); +out: +return ret; +} +int kvm_arch_get_default_type (MachineState *ms) {return 0 ;target/i386/kvm/vmsr_energy.c new file +18
-0@@ -0,0 +1,335 @@ +/* +* QEMU KVM support -- x86 virtual RAPL msr +* +* Copyright 2024 Red Hat, Inc. 2024 +* +* Author: +* Anthony Harivel <[email protected]> +* +* 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"
target/i386/kvm/vmsr_energy.c line 62
Quoted 10 lines
target/i386/kvm/vmsr_energy.c new file +9
-0+#include "hw/boards.h" +#include "cpu.h" +#include "host-cpu.h" + +QIOChannelSocket *vmsr_open_socket (const char *path) +{ + g_autofree char *socket_path = NULL; + + socket_path =g_strdup (path);
Assign this at time of declaring socket_path
Quoted 7 lines
target/i386/kvm/vmsr_energy.c new file +7
-0+ +SocketAddress saddr = { + .type =SOCKET_ADDRESS_TYPE_UNIX , + .u.q_unix.path = socket_path + }; + +QIOChannelSocket *sioc =qio_channel_socket_new ();
target/i386/kvm/vmsr_energy.c line 79
Quoted 10 lines
target/i386/kvm/vmsr_energy.c new file +10
-0+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));
You need a 'return NULL' here, otherwise we're returning a free'd pointer to the caller. Aso we need to report the error.
target/i386/kvm/vmsr_energy.c line 82
Quoted 3 lines
target/i386/kvm/vmsr_energy.c new file +3
-0+ } + +return sioc;
Indent misaligned.
target/i386/kvm/vmsr_energy.c line 91
Quoted 9 lines
target/i386/kvm/vmsr_energy.c new file +9
-0+} + +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 ; + g_autofree uint32_t buffer[3];
g_autofree is wrong for a stack allocated array, just remove it.
Quoted 30 lines
target/i386/kvm/vmsr_energy.c new file +29
-0+/* +* 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_all (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 cpu on the host */
target/i386/kvm/vmsr_energy.c line 183
Quoted 10 lines
target/i386/kvm/vmsr_energy.c new file +10
-0+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" );
Slightly more friendly to include the path in the error message, and also the error that glib reports.eg
g_autoptr(GError) gerr = NULL;
GDir *dir = g_dir_open(path, 0, &gerr);
if (dir == NULL) {
error_report("Unable to open cpu directory %s: %s", path, err->message);Quoted 18 lines
target/i386/kvm/vmsr_energy.c new file +18
-0+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)
target/i386/kvm/vmsr_energy.c line 211
Quoted 10 lines
target/i386/kvm/vmsr_energy.c new file +10
-0+{ + g_autofree char *file_contents = NULL; + g_autofree char *path = NULL; +gsize length; + +/* Iterate over cpus and count cpus in each package */ +for (int cpu_id =0 ; ; cpu_id++) { + path =g_build_filename ( +g_strdup_printf ("/sys/devices/system/cpu/cpu%d/" +"topology/physical_package_id" , cpu_id),NULL );
The result of 'g_strdup_printf' needs assigning to a variable marked with 'g_autofree', before being passed to g_build_filename to avoid a mem leak.
Quoted 19 lines
target/i386/kvm/vmsr_energy.c new file +18
-0+ +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 ; +} +/* Read the scheduled time for a given thread of a give pid */
target/i386/kvm/vmsr_energy.c line 261
Quoted 10 lines
target/i386/kvm/vmsr_energy.c new file +10
-0+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; + + path =g_build_filename (g_strdup_printf ("/proc/%u/task/%d/stat" , pid, \ + thread_id),NULL );
Again need to assign g_strdup_printf result to a g_autofree marked variable to avoid memleak.
Quoted 15 lines
target/i386/kvm/vmsr_energy.c new file +15
-0+ +FILE *file =fopen (path,"r" ); +if (file ==NULL ) { + pid =-1 ; +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 ) + { + pid =-1 ; +return ; + }
target/i386/kvm/vmsr_energy.c line 286
Quoted 10 lines
target/i386/kvm/vmsr_energy.c new file +10
-0+ +fclose (file); +return ; +} + +/* 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 *path = g_build_filename("/proc", +g_strdup_printf ("%d/task" , pid),NULL );
Another memleak of g_strdup_printf()result
Quoted 69 lines
target/i386/kvm/vmsr_energy.c new file +28
-0+ +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; +}target/i386/kvm/vmsr_energy.h new file +34
-0@@ -0,0 +1,99 @@ +/* +* QEMU KVM support -- x86 virtual energy-related MSR. +* +* Copyright 2024 Red Hat, Inc. 2024 +* +* Author: +* Anthony Harivel <[email protected]> +* +* This work is licensed under the terms of the GNU GPL, version 2 or later. +* See the COPYING file in the top-level directory. +* +*/ + +#ifndef VMSR_ENERGY_H +#define VMSR_ENERGY_H + +#include <stdint.h> +#include "qemu/osdep.h" +#include "io/channel-socket.h" +#include "hw/i386/topology.h" + +/* +* Define the interval time in micro seconds between 2 samples of +* energy related MSRs +*/ +#define MSR_ENERGY_THREAD_SLEEP_US 1000000.0 + +/* +* Thread statistic +* @ thread_id: TID (thread ID) +* @ is_vcpu: true if TID is vCPU thread +* @ cpu_id: CPU number last executed on +* @ pkg_id: package number of the CPU +* @ vcpu_id: vCPU ID
target/i386/kvm/vmsr_energy.h line 44
Quoted 10 lines
target/i386/kvm/vmsr_energy.h new file +10
-0+* @ vpkg: virtual package number +* @ acpi_id: APIC id of the vCPU +* @ utime: amount of clock ticks the thread +* has been scheduled in User mode +* @ stime: amount of clock ticks the thread +* has been scheduled in System mode +* @ delta_ticks: delta of utime+stime between +* the two samples (before/after sleep) +*/ +struct thread_stat {
Suggest 'vmsr_thread_stat' to better namespace
Quoted 11 lines
target/i386/kvm/vmsr_energy.h new file +11
-0+unsigned int thread_id; +bool is_vcpu; +unsigned int cpu_id; +unsigned int pkg_id; +unsigned int vpkg_id; +unsigned int vcpu_id; +unsigned long acpi_id; +unsigned long long *utime; +unsigned long long *stime; +unsigned long long delta_ticks; +};
target/i386/kvm/vmsr_energy.h line 65
Quoted 10 lines
target/i386/kvm/vmsr_energy.h new file +10
-0+ +/* +* Package statistic +* @ e_start: package energy counter before the sleep +* @ e_end: package energy counter after the sleep +* @ e_delta: delta of package energy counter +* @ e_ratio: store the energy ratio of non-vCPU thread +* @ nb_vcpu: number of vCPU running on this package +*/ +struct package_energy_stat {
And 'vmsr_package_energy_stat' too
Quoted 24 lines
target/i386/kvm/vmsr_energy.h new file +24
-0+uint64_t e_start; +uint64_t e_end; +uint64_t e_delta; +uint64_t e_ratio; +unsigned int nb_vcpu; +}; + +typedef struct thread_stat thread_stat ; +typedef struct package_energy_stat package_energy_stat ; + +char *vmsr_compute_default_paths (void ); +void vmsr_read_thread_stat (pid_t pid, +unsigned int thread_id, +unsigned long long *utime, +unsigned long long *stime, +unsigned int *cpu_id); + +QIOChannelSocket *vmsr_open_socket (const char *path); +uint64_t vmsr_read_msr (uint32_t reg,uint32_t cpu_id, +uint32_t tid,QIOChannelSocket *sioc); +void vmsr_delta_ticks (thread_stat *thd_stat,int i); +unsigned int vmsr_get_maxcpus (void ); +unsigned int vmsr_get_max_physical_package (unsigned int max_cpus); +unsigned int vmsr_count_cpus_per_package(unsigned int *package_count,
target/i386/kvm/vmsr_energy.h line 99
Quoted 10 lines
target/i386/kvm/vmsr_energy.h new file +10
-0+ unsigned int max_pkgs); +int vmsr_get_physical_package_id (int cpu_id); +pid_t *vmsr_get_thread_ids (pid_t pid,unsigned int *num_threads); +double vmsr_get_ratio (uint64_t e_delta, +unsigned long long delta_ticks, +unsigned int maxticks); +void vmsr_init_topo_info (X86CPUTopoInfo *topo_info,const MachineState *ms); +bool is_host_cpu_intel (void ); +int is_rapl_enabled (void ); +#endif /* VMSR_ENERGY_H */
Bearing in mind the other feedback from Intel about future CPUs not using MSRs for this interface, I wonder if a rename is worth doing.
eg call the file 'rapl.h', and the APIs / structs all have a 'rapl_' name prefix, instead of 'vmsr'.
Then use 'rapl_vmsr_' as a name prefix on the ones that are specifically tied to the MSR based impl. In future we can have 'rapl_tpmi' as the prefix for the new interface.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v5 3/3] Add support for RAPL MSRs in KVM/Qemu
In reply to Anthony HarivelQuoted 64 lines · Anthony Harivel
Starting with the "Sandy Bridge" generation, Intel CPUs provide a RAPL interface (Running Average Power Limit) for advertising the accumulated energy consumption of various power domains (e.g. CPU packages, DRAM, etc.).
The consumption is reported via MSRs (model specific registers) like
MSR_PKG_ENERGY_STATUSfor the CPU package power domain. These MSRs are 64 bits registers that represent the accumulated energy consumption in micro Joules. They are updated by microcode every ~1ms.For now, KVM always returns 0 when the guest requests the value of these MSRs. Use the KVM MSR filtering mechanism to allow QEMU handle these MSRs dynamically in userspace.
To limit the amount of system calls for every MSR call, create a new thread in QEMU that updates the "virtual" MSR values asynchronously.
Each vCPU has its own vMSR to reflect the independence of vCPUs. The thread updates the vMSR values with the ratio of energy consumed of the whole physical CPU package the vCPU thread runs on and the thread's utime and stime values.
All other non-vCPU threads are also taken into account. Their energy consumption is evenly distributed among all vCPUs threads running on the same physical CPU package.
To overcome the problem that reading the RAPL MSR requires priviliged access, a socket communication between QEMU and the qemu-vmsr-helper is mandatory. You can specified the socket path in the parameter.
This feature is activated with -accel kvm,rapl=true,path=/path/sock.sock
Actual limitation:
- Works only on Intel host CPU because AMD CPUs are using different MSR adresses.- Only the Package Power-Plane (
MSR_PKG_ENERGY_STATUS) is reported at the moment.Signed-off-by Anthony Harivel <[email protected]>--- accel/kvm/kvm-all.c | 27 +++ docs/specs/index.rst | 1 + docs/specs/rapl-msr.rst | 155 ++++++++++++ include/sysemu/kvm.h | 2 + include/sysemu/kvm_int.h | 32 +++ target/i386/cpu.h | 8 + target/i386/kvm/kvm-cpu.c | 9 + target/i386/kvm/kvm.c | 428 ++++++++++++++++++++++++++++++++++ target/i386/kvm/meson.build | 1 + target/i386/kvm/vmsr_energy.c | 335 ++++++++++++++++++++++++++ target/i386/kvm/vmsr_energy.h | 99 ++++++++ 11 files changed, 1097 insertions(+) create mode 100644 docs/specs/rapl-msr.rst create mode 100644 target/i386/kvm/vmsr_energy.c create mode 100644 target/i386/kvm/vmsr_energy.htarget/i386/kvm/kvm-cpu.c +0
-0@@ -50,6 +50,15 @@ static bool kvm_cpu_realizefn(CPUState *cs, Error **errp) MSR_IA32_UCODE_REV);
target/i386/kvm/kvm-cpu.c static bool kvm_cpu_realizefn(CPUState *cs, Error **errp) · line 60
Quoted 10 lines
target/i386/kvm/kvm-cpu.c +8
-0} } + +if (kvm_is_rapl_feat_enable (cs)) { +if (!IS_INTEL_CPU (env)) { +error_setg (errp,"RAPL feature can only be\ +enabled with Intel CPU models" ); +return false; + } + }
I see a crash in kvm_is_rapl_feat_enable() from this caller, when I run with this kind of command line:
$ qemu-system-x86_64 \
-kernel /lib/modules/6.6.9-100.fc38.x86_64/vmlinuz \
-initrd tiny-initrd.img -m 2000 -serial stdio -nodefaults \
-display none -accel kvm -append "console=ttyS0 quiet"#0 0x0000555555bc14b7 in kvm_is_rapl_feat_enable (cs=cs@entry=0x555557b83470) at ../target/i386/kvm/kvm.c:2531
#1 0x0000555555bc7534 in kvm_cpu_realizefn (cs=0x555557b83470, errp=0x7fffffffd2a0) at ../target/i386/kvm/kvm-cpu.c:54
#2 0x0000555555d2432a in accel_cpu_common_realize (cpu=0x555557b83470, errp=0x7fffffffd2a0) at ../accel/accel-target.c:130
#3 0x0000555555cdd955 in cpu_exec_realizefn (cpu=cpu@entry=0x555557b83470, errp=errp@entry=0x7fffffffd2a0) at ../cpu-target.c:137
#4 0x0000555555c14b89 in x86_cpu_realizefn (dev=0x555557b83470, errp=0x7fffffffd310) at ../target/i386/cpu.c:7320
#5 0x0000555555d58f4b in device_set_realized (obj=<optimized out>, value=<optimized out>, errp=0x7fffffffd390) at ../hw/core/qdev.c:510
#6 0x0000555555d5d78d in property_set_bool (obj=0x555557b83470, v=<optimized out>, name=<optimized out>, opaque=0x5555578558e0, errp=0x7fffffffd390)
at ../qom/object.c:2358
#7 0x0000555555d60b0b in object_property_set (obj=obj@entry=0x555557b83470, name=name@entry=0x55555607c799 "realized", v=v@entry=0x555557b8ccb0, errp=0x7fffffffd390,
errp@entry=0x555556e210d8 <error_fatal>) at ../qom/object.c:1472
#8 0x0000555555d6444f in object_property_set_qobject
(obj=obj@entry=0x555557b83470, name=name@entry=0x55555607c799 "realized", value=value@entry=0x555557854800, errp=errp@entry=0x555556e210d8 <error_fatal>)
at ../qom/qom-qobject.c:28
#9 0x0000555555d61174 in object_property_set_bool
(obj=0x555557b83470, name=name@entry=0x55555607c799 "realized", value=value@entry=true, errp=errp@entry=0x555556e210d8 <error_fatal>) at ../qom/object.c:1541#10 0x0000555555d59a3c in qdev_realize (dev=<optimized out>, bus=bus@entry=0x0, errp=errp@entry=0x555556e210d8 <error_fatal>) at ../hw/core/qdev.c:292 #11 0x0000555555bd51e0 in x86_cpu_new (x86ms=<optimized out>, apic_id=0, errp=0x555556e210d8 <error_fatal>) at ../hw/i386/x86.c:105
#12 0x0000555555bd52ce in x86_cpus_init (x86ms=x86ms@entry=0x555557aaed30, default_cpu_version=<optimized out>) at ../hw/i386/x86.c:156 #13 0x0000555555bdc1a7 in pc_init1 (machine=0x555557aaed30, pci_type=0x55555604aa61 "i440FX") at ../hw/i386/pc_piix.c:185
#14 0x0000555555947a11 in machine_run_board_init (machine=0x555557aaed30, mem_path=<optimized out>, errp=<optimized out>, errp@entry=0x555556e210d8 <error_fatal>)
at ../hw/core/machine.c:1547
#15 0x0000555555b020ed in qemu_init_board () at ../system/vl.c:2613
#16 qmp_x_exit_preconfig (errp=0x555556e210d8 <error_fatal>) at ../system/vl.c:2705 #17 0x0000555555b0611e in qemu_init (argc=<optimized out>, argv=<optimized out>) at ../system/vl.c:3739 #18 0x0000555555897ca9 in main (argc=<optimized out>, argv=<optimized out>) at ../system/main.c:47
The problem is that 'cs->kvm_state' is NULL here
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v5 3/3] Add support for RAPL MSRs in KVM/Qemu
Hi Daniel,
Daniel P. Berrangé, Apr 18, 2024 at 18:42:
Quoted 105 lines · Anthony Harivel
Quoted 64 lines
Starting with the "Sandy Bridge" generation, Intel CPUs provide a RAPL interface (Running Average Power Limit) for advertising the accumulated energy consumption of various power domains (e.g. CPU packages, DRAM, etc.).
The consumption is reported via MSRs (model specific registers) like
MSR_PKG_ENERGY_STATUSfor the CPU package power domain. These MSRs are 64 bits registers that represent the accumulated energy consumption in micro Joules. They are updated by microcode every ~1ms.For now, KVM always returns 0 when the guest requests the value of these MSRs. Use the KVM MSR filtering mechanism to allow QEMU handle these MSRs dynamically in userspace.
To limit the amount of system calls for every MSR call, create a new thread in QEMU that updates the "virtual" MSR values asynchronously.
Each vCPU has its own vMSR to reflect the independence of vCPUs. The thread updates the vMSR values with the ratio of energy consumed of the whole physical CPU package the vCPU thread runs on and the thread's utime and stime values.
All other non-vCPU threads are also taken into account. Their energy consumption is evenly distributed among all vCPUs threads running on the same physical CPU package.
To overcome the problem that reading the RAPL MSR requires priviliged access, a socket communication between QEMU and the qemu-vmsr-helper is mandatory. You can specified the socket path in the parameter.
This feature is activated with -accel kvm,rapl=true,path=/path/sock.sock
Actual limitation:
- Works only on Intel host CPU because AMD CPUs are using different MSR adresses.- Only the Package Power-Plane (
MSR_PKG_ENERGY_STATUS) is reported at the moment.Signed-off-by Anthony Harivel <[email protected]>--- accel/kvm/kvm-all.c | 27 +++ docs/specs/index.rst | 1 + docs/specs/rapl-msr.rst | 155 ++++++++++++ include/sysemu/kvm.h | 2 + include/sysemu/kvm_int.h | 32 +++ target/i386/cpu.h | 8 + target/i386/kvm/kvm-cpu.c | 9 + target/i386/kvm/kvm.c | 428 ++++++++++++++++++++++++++++++++++ target/i386/kvm/meson.build | 1 + target/i386/kvm/vmsr_energy.c | 335 ++++++++++++++++++++++++++ target/i386/kvm/vmsr_energy.h | 99 ++++++++ 11 files changed, 1097 insertions(+) create mode 100644 docs/specs/rapl-msr.rst create mode 100644 target/i386/kvm/vmsr_energy.c create mode 100644 target/i386/kvm/vmsr_energy.htarget/i386/kvm/kvm-cpu.c +0
-0@@ -50,6 +50,15 @@ static bool kvm_cpu_realizefn(CPUState *cs, Error **errp) MSR_IA32_UCODE_REV);target/i386/kvm/kvm-cpu.c static bool kvm_cpu_realizefn(CPUState *cs, Error **errp) · line 60
Quoted 10 lines
target/i386/kvm/kvm-cpu.c +8
-0} } + +if (kvm_is_rapl_feat_enable (cs)) { +if (!IS_INTEL_CPU (env)) { +error_setg (errp,"RAPL feature can only be\ +enabled with Intel CPU models" ); +return false; + } + }I see a crash in
kvm_is_rapl_feat_enable()from this caller, when I run with this kind of command line:$ qemu-system-x86_64 \ -kernel /lib/modules/6.6.9-100.fc38.x86_64/vmlinuz \ -initrd tiny-initrd.img -m 2000 -serial stdio -nodefaults \ -display none -accel kvm -append "console=ttyS0 quiet"#0 0x0000555555bc14b7 in kvm_is_rapl_feat_enable (cs=cs@entry=0x555557b83470) at ../target/i386/kvm/kvm.c:2531 #1 0x0000555555bc7534 in kvm_cpu_realizefn (cs=0x555557b83470, errp=0x7fffffffd2a0) at ../target/i386/kvm/kvm-cpu.c:54 #2 0x0000555555d2432a in accel_cpu_common_realize (cpu=0x555557b83470, errp=0x7fffffffd2a0) at ../accel/accel-target.c:130 #3 0x0000555555cdd955 in cpu_exec_realizefn (cpu=cpu@entry=0x555557b83470, errp=errp@entry=0x7fffffffd2a0) at ../cpu-target.c:137 #4 0x0000555555c14b89 in x86_cpu_realizefn (dev=0x555557b83470, errp=0x7fffffffd310) at ../target/i386/cpu.c:7320 #5 0x0000555555d58f4b in device_set_realized (obj=<optimized out>, value=<optimized out>, errp=0x7fffffffd390) at ../hw/core/qdev.c:510 #6 0x0000555555d5d78d in property_set_bool (obj=0x555557b83470, v=<optimized out>, name=<optimized out>, opaque=0x5555578558e0, errp=0x7fffffffd390) at ../qom/object.c:2358 #7 0x0000555555d60b0b in object_property_set (obj=obj@entry=0x555557b83470, name=name@entry=0x55555607c799 "realized", v=v@entry=0x555557b8ccb0, errp=0x7fffffffd390, errp@entry=0x555556e210d8 <error_fatal>) at ../qom/object.c:1472 #8 0x0000555555d6444f in object_property_set_qobject (obj=obj@entry=0x555557b83470, name=name@entry=0x55555607c799 "realized", value=value@entry=0x555557854800, errp=errp@entry=0x555556e210d8 <error_fatal>) at ../qom/qom-qobject.c:28 #9 0x0000555555d61174 in object_property_set_bool (obj=0x555557b83470, name=name@entry=0x55555607c799 "realized", value=value@entry=true, errp=errp@entry=0x555556e210d8 <error_fatal>) at ../qom/object.c:1541#10
0x0000555555d59a3cinqdev_realize(dev=<optimized out>, bus=bus@entry=0x0, errp=errp@entry=0x555556e210d8 <error_fatal>) at../hw/core/qdev.c:292#110x0000555555bd51e0inx86_cpu_new(x86ms=<optimized out>, apic_id=0, errp=0x555556e210d8 <error_fatal>) at../hw/i386/x86.c:105
#120x0000555555bd52ceinx86_cpus_init(x86ms=x86ms@entry=0x555557aaed30, default_cpu_version=<optimized out>) at../hw/i386/x86.c:156#130x0000555555bdc1a7inpc_init1(machine=0x555557aaed30, pci_type=0x55555604aa61 "i440FX") at../hw/i386/pc_piix.c:185
#140x0000555555947a11inmachine_run_board_init(machine=0x555557aaed30, mem_path=<optimized out>, errp=<optimized out>, errp@entry=0x555556e210d8 <error_fatal>)
Quoted 8 lines
at ../hw/core/machine.c:1547#15
0x0000555555b020edinqemu_init_board() at../system/vl.c:2613
#16qmp_x_exit_preconfig(errp=0x555556e210d8 <error_fatal>) at../system/vl.c:2705#170x0000555555b0611einqemu_init(argc=<optimized out>, argv=<optimized out>) at../system/vl.c:3739#180x0000555555897ca9in main (argc=<optimized out>, argv=<optimized out>) at../system/main.c:47The problem is that '
cs->kvm_state' is NULL here
Oh boy... Thanks for the feedback!
I'll put that on my priority list for tomorrow.
Regards,
Anthony
Quoted 6 lines
With regards,
DanielSignature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v5 3/3] Add support for RAPL MSRs in KVM/Qemu
In reply to Daniel P. BerrangéHi Daniel,
Daniel P. Berrangé, Apr 18, 2024 at 18:42:
Quoted 37 lines
target/i386/kvm/kvm-cpu.c static bool kvm_cpu_realizefn(CPUState *cs, Error **errp) · line 60
Quoted 7 lines
target/i386/kvm/kvm-cpu.c +7
-0+if (kvm_is_rapl_feat_enable (cs)) { +if (!IS_INTEL_CPU (env)) { +error_setg (errp,"RAPL feature can only be\ +enabled with Intel CPU models" ); +return false; + } + }I see a crash in
kvm_is_rapl_feat_enable()from this caller, when I run with this kind of command line:$ qemu-system-x86_64 \ -kernel /lib/modules/6.6.9-100.fc38.x86_64/vmlinuz \ -initrd tiny-initrd.img -m 2000 -serial stdio -nodefaults \ -display none -accel kvm -append "console=ttyS0 quiet"#0 0x0000555555bc14b7 in kvm_is_rapl_feat_enable (cs=cs@entry=0x555557b83470) at ../target/i386/kvm/kvm.c:2531 #1 0x0000555555bc7534 in kvm_cpu_realizefn (cs=0x555557b83470, errp=0x7fffffffd2a0) at ../target/i386/kvm/kvm-cpu.c:54 #2 0x0000555555d2432a in accel_cpu_common_realize (cpu=0x555557b83470, errp=0x7fffffffd2a0) at ../accel/accel-target.c:130 #3 0x0000555555cdd955 in cpu_exec_realizefn (cpu=cpu@entry=0x555557b83470, errp=errp@entry=0x7fffffffd2a0) at ../cpu-target.c:137 #4 0x0000555555c14b89 in x86_cpu_realizefn (dev=0x555557b83470, errp=0x7fffffffd310) at ../target/i386/cpu.c:7320 #5 0x0000555555d58f4b in device_set_realized (obj=<optimized out>, value=<optimized out>, errp=0x7fffffffd390) at ../hw/core/qdev.c:510 #6 0x0000555555d5d78d in property_set_bool (obj=0x555557b83470, v=<optimized out>, name=<optimized out>, opaque=0x5555578558e0, errp=0x7fffffffd390) at ../qom/object.c:2358 #7 0x0000555555d60b0b in object_property_set (obj=obj@entry=0x555557b83470, name=name@entry=0x55555607c799 "realized", v=v@entry=0x555557b8ccb0, errp=0x7fffffffd390, errp@entry=0x555556e210d8 <error_fatal>) at ../qom/object.c:1472 #8 0x0000555555d6444f in object_property_set_qobject (obj=obj@entry=0x555557b83470, name=name@entry=0x55555607c799 "realized", value=value@entry=0x555557854800, errp=errp@entry=0x555556e210d8 <error_fatal>) at ../qom/qom-qobject.c:28 #9 0x0000555555d61174 in object_property_set_bool (obj=0x555557b83470, name=name@entry=0x55555607c799 "realized", value=value@entry=true, errp=errp@entry=0x555556e210d8 <error_fatal>) at ../qom/object.c:1541#10
0x0000555555d59a3cinqdev_realize(dev=<optimized out>, bus=bus@entry=0x0, errp=errp@entry=0x555556e210d8 <error_fatal>) at../hw/core/qdev.c:292#110x0000555555bd51e0inx86_cpu_new(x86ms=<optimized out>, apic_id=0, errp=0x555556e210d8 <error_fatal>) at../hw/i386/x86.c:105
#120x0000555555bd52ceinx86_cpus_init(x86ms=x86ms@entry=0x555557aaed30, default_cpu_version=<optimized out>) at../hw/i386/x86.c:156#130x0000555555bdc1a7inpc_init1(machine=0x555557aaed30, pci_type=0x55555604aa61 "i440FX") at../hw/i386/pc_piix.c:185
#140x0000555555947a11inmachine_run_board_init(machine=0x555557aaed30, mem_path=<optimized out>, errp=<optimized out>, errp@entry=0x555556e210d8 <error_fatal>)
Quoted 8 lines
at ../hw/core/machine.c:1547#15
0x0000555555b020edinqemu_init_board() at../system/vl.c:2613
#16qmp_x_exit_preconfig(errp=0x555556e210d8 <error_fatal>) at../system/vl.c:2705#170x0000555555b0611einqemu_init(argc=<optimized out>, argv=<optimized out>) at../system/vl.c:3739#180x0000555555897ca9in main (argc=<optimized out>, argv=<optimized out>) at../system/main.c:47The problem is that '
cs->kvm_state' is NULL here
After some investigation it seems that kvm_state is not yet committed at this point. Shame, because GDB showed me that we have already pass the kvm_accel_instance_init() in accel/kvm/kvm-all.c that sets the value "msr_energy.enable" in kvm_state...
So should I dig more to still do the sanity check in kvm_cpu_realizefn() or should I already move the RAPL feature from -kvm to -cpu like suggested by Zhao from Intel and then access it from the CPUState ?
The last one would require more work but if I can skip a new iteration because I would need to do it anyway that would save me time in this end.
Thanks
Regards,
Anthony
Re: [PATCH v5 3/3] Add support for RAPL MSRs in KVM/Qemu
Quoted 50 lines · Anthony Harivel
Hi Daniel,
Daniel P. Berrangé, Apr 18, 2024 at 18:42:
Quoted 37 lines
target/i386/kvm/kvm-cpu.c static bool kvm_cpu_realizefn(CPUState *cs, Error **errp) · line 60
Quoted 7 lines
target/i386/kvm/kvm-cpu.c +7
-0+if (kvm_is_rapl_feat_enable (cs)) { +if (!IS_INTEL_CPU (env)) { +error_setg (errp,"RAPL feature can only be\ +enabled with Intel CPU models" ); +return false; + } + }I see a crash in
kvm_is_rapl_feat_enable()from this caller, when I run with this kind of command line:$ qemu-system-x86_64 \ -kernel /lib/modules/6.6.9-100.fc38.x86_64/vmlinuz \ -initrd tiny-initrd.img -m 2000 -serial stdio -nodefaults \ -display none -accel kvm -append "console=ttyS0 quiet"#0 0x0000555555bc14b7 in kvm_is_rapl_feat_enable (cs=cs@entry=0x555557b83470) at ../target/i386/kvm/kvm.c:2531 #1 0x0000555555bc7534 in kvm_cpu_realizefn (cs=0x555557b83470, errp=0x7fffffffd2a0) at ../target/i386/kvm/kvm-cpu.c:54 #2 0x0000555555d2432a in accel_cpu_common_realize (cpu=0x555557b83470, errp=0x7fffffffd2a0) at ../accel/accel-target.c:130 #3 0x0000555555cdd955 in cpu_exec_realizefn (cpu=cpu@entry=0x555557b83470, errp=errp@entry=0x7fffffffd2a0) at ../cpu-target.c:137 #4 0x0000555555c14b89 in x86_cpu_realizefn (dev=0x555557b83470, errp=0x7fffffffd310) at ../target/i386/cpu.c:7320 #5 0x0000555555d58f4b in device_set_realized (obj=<optimized out>, value=<optimized out>, errp=0x7fffffffd390) at ../hw/core/qdev.c:510 #6 0x0000555555d5d78d in property_set_bool (obj=0x555557b83470, v=<optimized out>, name=<optimized out>, opaque=0x5555578558e0, errp=0x7fffffffd390) at ../qom/object.c:2358 #7 0x0000555555d60b0b in object_property_set (obj=obj@entry=0x555557b83470, name=name@entry=0x55555607c799 "realized", v=v@entry=0x555557b8ccb0, errp=0x7fffffffd390, errp@entry=0x555556e210d8 <error_fatal>) at ../qom/object.c:1472 #8 0x0000555555d6444f in object_property_set_qobject (obj=obj@entry=0x555557b83470, name=name@entry=0x55555607c799 "realized", value=value@entry=0x555557854800, errp=errp@entry=0x555556e210d8 <error_fatal>) at ../qom/qom-qobject.c:28 #9 0x0000555555d61174 in object_property_set_bool (obj=0x555557b83470, name=name@entry=0x55555607c799 "realized", value=value@entry=true, errp=errp@entry=0x555556e210d8 <error_fatal>) at ../qom/object.c:1541#10
0x0000555555d59a3cinqdev_realize(dev=<optimized out>, bus=bus@entry=0x0, errp=errp@entry=0x555556e210d8 <error_fatal>) at../hw/core/qdev.c:292#110x0000555555bd51e0inx86_cpu_new(x86ms=<optimized out>, apic_id=0, errp=0x555556e210d8 <error_fatal>) at../hw/i386/x86.c:105
#120x0000555555bd52ceinx86_cpus_init(x86ms=x86ms@entry=0x555557aaed30, default_cpu_version=<optimized out>) at../hw/i386/x86.c:156#130x0000555555bdc1a7inpc_init1(machine=0x555557aaed30, pci_type=0x55555604aa61 "i440FX") at../hw/i386/pc_piix.c:185
#140x0000555555947a11inmachine_run_board_init(machine=0x555557aaed30, mem_path=<optimized out>, errp=<optimized out>, errp@entry=0x555556e210d8 <error_fatal>)Quoted 8 lines
at ../hw/core/machine.c:1547#15
0x0000555555b020edinqemu_init_board() at../system/vl.c:2613
#16qmp_x_exit_preconfig(errp=0x555556e210d8 <error_fatal>) at../system/vl.c:2705#170x0000555555b0611einqemu_init(argc=<optimized out>, argv=<optimized out>) at../system/vl.c:3739#180x0000555555897ca9in main (argc=<optimized out>, argv=<optimized out>) at../system/main.c:47The problem is that '
cs->kvm_state' is NULL here
Quoted 8 lines
After some investigation it seems that
kvm_stateis not yet committed at this point. Shame, because GDB showed me that we have already pass thekvm_accel_instance_init()inaccel/kvm/kvm-all.cthat sets the value "msr_energy.enable" inkvm_state...So should I dig more to still do the sanity check in
kvm_cpu_realizefn()or should I already move the RAPL feature from -kvm to -cpu like suggested by Zhao from Intel and then access it from the CPUState ?
I'm not so sure about that question. I think Paolo is best placed to suggest which is better, as the KVM maintainer.
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Re: [PATCH v5 3/3] Add support for RAPL MSRs in KVM/Qemu
Hi Paolo,
Daniel P. Berrangé, Apr 25, 2024 at 17:42:
Quoted 56 lines · Anthony Harivel
Quoted 55 lines
Hi Daniel,
Daniel P. Berrangé, Apr 18, 2024 at 18:42:
Quoted 37 lines
target/i386/kvm/kvm-cpu.c static bool kvm_cpu_realizefn(CPUState *cs, Error **errp) · line 60
Quoted 7 lines
target/i386/kvm/kvm-cpu.c +7
-0+if (kvm_is_rapl_feat_enable (cs)) { +if (!IS_INTEL_CPU (env)) { +error_setg (errp,"RAPL feature can only be\ +enabled with Intel CPU models" ); +return false; + } + }I see a crash in
kvm_is_rapl_feat_enable()from this caller, when I run with this kind of command line:$ qemu-system-x86_64 \ -kernel /lib/modules/6.6.9-100.fc38.x86_64/vmlinuz \ -initrd tiny-initrd.img -m 2000 -serial stdio -nodefaults \ -display none -accel kvm -append "console=ttyS0 quiet"#0 0x0000555555bc14b7 in kvm_is_rapl_feat_enable (cs=cs@entry=0x555557b83470) at ../target/i386/kvm/kvm.c:2531 #1 0x0000555555bc7534 in kvm_cpu_realizefn (cs=0x555557b83470, errp=0x7fffffffd2a0) at ../target/i386/kvm/kvm-cpu.c:54 #2 0x0000555555d2432a in accel_cpu_common_realize (cpu=0x555557b83470, errp=0x7fffffffd2a0) at ../accel/accel-target.c:130 #3 0x0000555555cdd955 in cpu_exec_realizefn (cpu=cpu@entry=0x555557b83470, errp=errp@entry=0x7fffffffd2a0) at ../cpu-target.c:137 #4 0x0000555555c14b89 in x86_cpu_realizefn (dev=0x555557b83470, errp=0x7fffffffd310) at ../target/i386/cpu.c:7320 #5 0x0000555555d58f4b in device_set_realized (obj=<optimized out>, value=<optimized out>, errp=0x7fffffffd390) at ../hw/core/qdev.c:510 #6 0x0000555555d5d78d in property_set_bool (obj=0x555557b83470, v=<optimized out>, name=<optimized out>, opaque=0x5555578558e0, errp=0x7fffffffd390) at ../qom/object.c:2358 #7 0x0000555555d60b0b in object_property_set (obj=obj@entry=0x555557b83470, name=name@entry=0x55555607c799 "realized", v=v@entry=0x555557b8ccb0, errp=0x7fffffffd390, errp@entry=0x555556e210d8 <error_fatal>) at ../qom/object.c:1472 #8 0x0000555555d6444f in object_property_set_qobject (obj=obj@entry=0x555557b83470, name=name@entry=0x55555607c799 "realized", value=value@entry=0x555557854800, errp=errp@entry=0x555556e210d8 <error_fatal>) at ../qom/qom-qobject.c:28 #9 0x0000555555d61174 in object_property_set_bool (obj=0x555557b83470, name=name@entry=0x55555607c799 "realized", value=value@entry=true, errp=errp@entry=0x555556e210d8 <error_fatal>) at ../qom/object.c:1541#10
0x0000555555d59a3cinqdev_realize(dev=<optimized out>, bus=bus@entry=0x0, errp=errp@entry=0x555556e210d8 <error_fatal>) at../hw/core/qdev.c:292#110x0000555555bd51e0inx86_cpu_new(x86ms=<optimized out>, apic_id=0, errp=0x555556e210d8 <error_fatal>) at../hw/i386/x86.c:105
#120x0000555555bd52ceinx86_cpus_init(x86ms=x86ms@entry=0x555557aaed30, default_cpu_version=<optimized out>) at../hw/i386/x86.c:156#130x0000555555bdc1a7inpc_init1(machine=0x555557aaed30, pci_type=0x55555604aa61 "i440FX") at../hw/i386/pc_piix.c:185
#140x0000555555947a11inmachine_run_board_init(machine=0x555557aaed30, mem_path=<optimized out>, errp=<optimized out>, errp@entry=0x555556e210d8 <error_fatal>)Quoted 8 lines
at ../hw/core/machine.c:1547#15
0x0000555555b020edinqemu_init_board() at../system/vl.c:2613
#16qmp_x_exit_preconfig(errp=0x555556e210d8 <error_fatal>) at../system/vl.c:2705#170x0000555555b0611einqemu_init(argc=<optimized out>, argv=<optimized out>) at../system/vl.c:3739#180x0000555555897ca9in main (argc=<optimized out>, argv=<optimized out>) at../system/main.c:47The problem is that '
cs->kvm_state' is NULL hereAfter some investigation it seems that
kvm_stateis not yet committed at this point. Shame, because GDB showed me that we have already pass thekvm_accel_instance_init()inaccel/kvm/kvm-all.cthat sets the value "msr_energy.enable" inkvm_state...
Quoted 7 lines
Quoted 3 lines
So should I dig more to still do the sanity check in
kvm_cpu_realizefn()or should I already move the RAPL feature from -kvm to -cpu like suggested by Zhao from Intel and then access it from the CPUState ?I'm not so sure about that question. I think Paolo is best placed to suggest which is better, as the KVM maintainer.
I'm facing an issue that either require a simple change or a more complex one depending on the decision.
TL;DR: Should I move from -kvm to -cpu the rapl feature ?
Zhao from Intel suggested me that enabling the rapl feature looks more natural than through -kvm feature because it is indeed a cpu feature.
From the point of view of the QEMU architecture, it's more easier to enable the feature with -kvm because it is kvm dependent; but maybe from the point of view of the user, it's more natural to enable this at -cpu level.
The issue I'm facing above is from a suggestion from Daniel to do the sanity check at kvm_cpu_realizefn() level. However GDB showed me that kvm_state is not yet populated, even if we have passed the init instance kvm_accel_instance_init().
So either I'm moving from -kvm to -cpu and change the location of the sanity check (checking if rapl feat is activated on Host / if Intel CPU) or I keep the feature on -kvm and either find a solution of the above issue or change the location of the sanity check.
Thanks in advance for your guidance.
Regards,
Anthony
Re: [PATCH v5 3/3] Add support for RAPL MSRs in KVM/Qemu
Anthony Harivel, Apr 26, 2024 at 10:36:
Quoted 91 lines
Hi Paolo,
Daniel P. Berrangé, Apr 25, 2024 at 17:42:
Quoted 56 lines · Anthony Harivel
Quoted 55 lines
Hi Daniel,
Daniel P. Berrangé, Apr 18, 2024 at 18:42:
Quoted 37 lines
target/i386/kvm/kvm-cpu.c static bool kvm_cpu_realizefn(CPUState *cs, Error **errp) · line 60
Quoted 7 lines
target/i386/kvm/kvm-cpu.c +7
-0+if (kvm_is_rapl_feat_enable (cs)) { +if (!IS_INTEL_CPU (env)) { +error_setg (errp,"RAPL feature can only be\ +enabled with Intel CPU models" ); +return false; + } + }I see a crash in
kvm_is_rapl_feat_enable()from this caller, when I run with this kind of command line:$ qemu-system-x86_64 \ -kernel /lib/modules/6.6.9-100.fc38.x86_64/vmlinuz \ -initrd tiny-initrd.img -m 2000 -serial stdio -nodefaults \ -display none -accel kvm -append "console=ttyS0 quiet"#0 0x0000555555bc14b7 in kvm_is_rapl_feat_enable (cs=cs@entry=0x555557b83470) at ../target/i386/kvm/kvm.c:2531 #1 0x0000555555bc7534 in kvm_cpu_realizefn (cs=0x555557b83470, errp=0x7fffffffd2a0) at ../target/i386/kvm/kvm-cpu.c:54 #2 0x0000555555d2432a in accel_cpu_common_realize (cpu=0x555557b83470, errp=0x7fffffffd2a0) at ../accel/accel-target.c:130 #3 0x0000555555cdd955 in cpu_exec_realizefn (cpu=cpu@entry=0x555557b83470, errp=errp@entry=0x7fffffffd2a0) at ../cpu-target.c:137 #4 0x0000555555c14b89 in x86_cpu_realizefn (dev=0x555557b83470, errp=0x7fffffffd310) at ../target/i386/cpu.c:7320 #5 0x0000555555d58f4b in device_set_realized (obj=<optimized out>, value=<optimized out>, errp=0x7fffffffd390) at ../hw/core/qdev.c:510 #6 0x0000555555d5d78d in property_set_bool (obj=0x555557b83470, v=<optimized out>, name=<optimized out>, opaque=0x5555578558e0, errp=0x7fffffffd390) at ../qom/object.c:2358 #7 0x0000555555d60b0b in object_property_set (obj=obj@entry=0x555557b83470, name=name@entry=0x55555607c799 "realized", v=v@entry=0x555557b8ccb0, errp=0x7fffffffd390, errp@entry=0x555556e210d8 <error_fatal>) at ../qom/object.c:1472 #8 0x0000555555d6444f in object_property_set_qobject (obj=obj@entry=0x555557b83470, name=name@entry=0x55555607c799 "realized", value=value@entry=0x555557854800, errp=errp@entry=0x555556e210d8 <error_fatal>) at ../qom/qom-qobject.c:28 #9 0x0000555555d61174 in object_property_set_bool (obj=0x555557b83470, name=name@entry=0x55555607c799 "realized", value=value@entry=true, errp=errp@entry=0x555556e210d8 <error_fatal>) at ../qom/object.c:1541#10
0x0000555555d59a3cinqdev_realize(dev=<optimized out>, bus=bus@entry=0x0, errp=errp@entry=0x555556e210d8 <error_fatal>) at../hw/core/qdev.c:292#110x0000555555bd51e0inx86_cpu_new(x86ms=<optimized out>, apic_id=0, errp=0x555556e210d8 <error_fatal>) at../hw/i386/x86.c:105
#120x0000555555bd52ceinx86_cpus_init(x86ms=x86ms@entry=0x555557aaed30, default_cpu_version=<optimized out>) at../hw/i386/x86.c:156#130x0000555555bdc1a7inpc_init1(machine=0x555557aaed30, pci_type=0x55555604aa61 "i440FX") at../hw/i386/pc_piix.c:185
#140x0000555555947a11inmachine_run_board_init(machine=0x555557aaed30, mem_path=<optimized out>, errp=<optimized out>, errp@entry=0x555556e210d8 <error_fatal>)Quoted 8 lines
at ../hw/core/machine.c:1547#15
0x0000555555b020edinqemu_init_board() at../system/vl.c:2613
#16qmp_x_exit_preconfig(errp=0x555556e210d8 <error_fatal>) at../system/vl.c:2705#170x0000555555b0611einqemu_init(argc=<optimized out>, argv=<optimized out>) at../system/vl.c:3739#180x0000555555897ca9in main (argc=<optimized out>, argv=<optimized out>) at../system/main.c:47The problem is that '
cs->kvm_state' is NULL hereAfter some investigation it seems that
kvm_stateis not yet committed at this point. Shame, because GDB showed me that we have already pass thekvm_accel_instance_init()inaccel/kvm/kvm-all.cthat sets the value "msr_energy.enable" inkvm_state...Quoted 7 lines
Quoted 3 lines
So should I dig more to still do the sanity check in
kvm_cpu_realizefn()or should I already move the RAPL feature from -kvm to -cpu like suggested by Zhao from Intel and then access it from the CPUState ?I'm not so sure about that question. I think Paolo is best placed to suggest which is better, as the KVM maintainer.
I'm facing an issue that either require a simple change or a more complex one depending on the decision.
TL;DR: Should I move from -kvm to -cpu the rapl feature ?
Zhao from Intel suggested me that enabling the rapl feature looks more natural than through -kvm feature because it is indeed a cpu feature.
From the point of view of the QEMU architecture, it's more easier to enable the feature with -kvm because it is kvm dependent; but maybe from the point of view of the user, it's more natural to enable this at -cpu level.
The issue I'm facing above is from a suggestion from Daniel to do the sanity check at
kvm_cpu_realizefn()level. However GDB showed me thatkvm_stateis not yet populated, even if we have passed the init instancekvm_accel_instance_init().So either I'm moving from -kvm to -cpu and change the location of the sanity check (checking if rapl feat is activated on Host / if Intel CPU) or I keep the feature on -kvm and either find a solution of the above issue or change the location of the sanity check.
Quoted 4 lines
Thanks in advance for your guidance.
Regards,
Anthony
Hi Paolo,
Just a gentle ping for the above question that would unlock me for the next iteration.
Thanks a lot.
Regards,
Anthony
Quoted 6 lines · Anthony Harivel
Dear maintainers,
First of all, thank you very much for your review of my patch [1].
Quoted 7 lines
In this version (v5), I have attempted to address all the problems addressed by Daniel during the last review. I've been more careful with all the remarks made.
However, one question remains unanswered pointing the issue with the location of "
/var/local/run/qemu-vmsr-helper.sock", created bycompute_default_paths(). QEMU is not allowed to reach the socket here.
If I understand correctly the question, that is expected. This is a privileged functionality and therefore it requires manual intervention to change the owner of the socket and allow QEMU to access it.
Paolo
Quoted 97 lines
Thank you again for your continued guidance.
v4 -> v5
--------- correct
qio_channel_get_peerpid: return pid = -1 in case of error
-Vmsr_helper: compile only for x86
-Vmsr_helper: use qio_channel_read/write_all
-Vmsr_helper: abandon user/group
-Vmsr_energy.c: correct allerror_report
- Vmsr thread: compute default socket path only once
- Vmsr thread: open socket only once
- Pass relevant QEMU CIv3 -> v4
--------- Correct memory leaks with AddressSanitizer
- Add sanity check for QEMU and qemu-vmsr-helper for checking if host is INTEL and if RAPL is activated.
- Rename poor variables naming for easier comprehension
- Move code that checks Host before creating the VMSR thread
- Get rid of libnuma: create function that read sysfs for reading the Host topology insteadv2 -> v3
--------- Move all memory allocations from Clib to Glib
- Compile on *BSD (working on Linux only)
- No more limitation on the virtual package: each vCPU that belongs to the same virtual package is giving the same results like expected on a real CPU.
This has been tested topology like:-smp 4,sockets=2 -smp 16,sockets=4,cores=2,threads=2v1 -> v2
--------- To overcome the CVE-2020-8694 a socket communication is created to a priviliged helper
- Add the priviliged helper (qemu-vmsr-helper)
- AddSO_PEERCREDin qio channel socketRFC -> v1
---------- Add vmsr_* in front of all vmsr specific function
- Change malloc()/calloc()... with all glib equivalent
- Pre-allocate all dynamic memories when possible
- Add a Documentation of implementation, limitation and usageBest regards,
Anthony[1]: https://lists.gnu.org/archive/html/qemu-devel/2024-03/msg04417.html
Anthony Harivel (3):
qio: add support forSO_PEERCREDfor socket channel
tools: build qemu-vmsr-helper
Add support for RAPL MSRs in KVM/Qemuaccel/kvm/kvm-all.c | 27 ++ contrib/systemd/qemu-vmsr-helper.service | 15 + contrib/systemd/qemu-vmsr-helper.socket | 9 + docs/specs/index.rst | 1 + docs/specs/rapl-msr.rst | 155 +++++++ docs/tools/index.rst | 1 + docs/tools/qemu-vmsr-helper.rst | 89 ++++ include/io/channel.h | 21 + include/sysemu/kvm.h | 2 + include/sysemu/kvm_int.h | 32 ++ io/channel-socket.c | 28 ++ io/channel.c | 13 + meson.build | 7 + target/i386/cpu.h | 8 + target/i386/kvm/kvm-cpu.c | 9 + target/i386/kvm/kvm.c | 428 ++++++++++++++++++ target/i386/kvm/meson.build | 1 + target/i386/kvm/vmsr_energy.c | 335 ++++++++++++++ target/i386/kvm/vmsr_energy.h | 99 +++++ tools/i386/qemu-vmsr-helper.c | 529 +++++++++++++++++++++++ tools/i386/rapl-msr-index.h | 28 ++ 21 files changed, 1837 insertions(+) create mode 100644 contrib/systemd/qemu-vmsr-helper.service create mode 100644 contrib/systemd/qemu-vmsr-helper.socket create mode 100644 docs/specs/rapl-msr.rst create mode 100644 docs/tools/qemu-vmsr-helper.rst create mode 100644 target/i386/kvm/vmsr_energy.c create mode 100644 target/i386/kvm/vmsr_energy.h create mode 100644 tools/i386/qemu-vmsr-helper.c create mode 100644 tools/i386/rapl-msr-index.h--
2.44.0
Quoted 11 lines · Paolo Bonzini
Quoted 10 lines · Anthony Harivel
Dear maintainers,
First of all, thank you very much for your review of my patch [1].
In this version (v5), I have attempted to address all the problems addressed by Daniel during the last review. I've been more careful with all the remarks made.
Quoted 7 lines
Quoted 3 lines
However, one question remains unanswered pointing the issue with the location of "
/var/local/run/qemu-vmsr-helper.sock", created bycompute_default_paths(). QEMU is not allowed to reach the socket here.If I understand correctly the question, that is expected. This is a privileged functionality and therefore it requires manual intervention to change the owner of the socket and allow QEMU to access it.
In the systemd case, it will set the owner and mode, but in the non-system case, I wonder if it worth making this helper program have "--socket-owner" and "--socket-mode" args, so it can create the socket with the right mode/owner immediately, rather than expecting the admin to manuall chmod+chown after start the helper
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Quoted 8 lines · Daniel P. Berrangé
Quoted 7 lines
Quoted 3 lines
However, one question remains unanswered pointing the issue with the location of "
/var/local/run/qemu-vmsr-helper.sock", created bycompute_default_paths(). QEMU is not allowed to reach the socket here.If I understand correctly the question, that is expected. This is a privileged functionality and therefore it requires manual intervention to change the owner of the socket and allow QEMU to access it.
Quoted 6 lines
In the systemd case, it will set the owner and mode, but in the non-system case, I wonder if it worth making this helper program have "--socket-owner" and "--socket-mode" args, so it can create the socket with the right mode/owner immediately, rather than expecting the admin to manuall chmod+chown after start the helper
I think a better idea would be to contribute them to systemd-socket-activate, and just launch the helper that way. It's mostly a testing tool, but tbh if you're not using systemd you're on your own. If you write an init script for example, that would be the place where you put the chmod/chown.
Paolo
Quoted 8 lines · Anthony Harivel
Dear maintainers,
First of all, thank you very much for your review of my patch [1].
In this version (v5), I have attempted to address all the problems addressed by Daniel during the last review. I've been more careful with all the remarks made.
I'm wondering if you had tips for testing this functionality ?
Is there any nice app to run in the host/guest to report the power usage, to see that it is working as desired ?
With regards,
Daniel
Signature · 3 lines
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
Hi Daniel,
Daniel P. Berrangé, Apr 17, 2024 at 19:23:
Quoted 6 lines · Anthony Harivel
Quoted 5 lines
Dear maintainers,
First of all, thank you very much for your review of my patch [1].
Quoted 9 lines
Quoted 3 lines
In this version (v5), I have attempted to address all the problems addressed by Daniel during the last review. I've been more careful with all the remarks made.
I'm wondering if you had tips for testing this functionality ?
Is there any nice app to run in the host/guest to report the power usage, to see that it is working as desired ?
Great question. Unfortunately, there isn't an easy-to-use, out-of-the-box app that can assist you.
The 'cpupower' tool in linux/tools/power/ or 'turbostat' in linux/tools/power/x86/ require some modifications as they fail the sanity check inside a VM. It is on my agenda to work on a proposal patch for these tools if the vmsr patch lands in QEMU. These are the excellent apps that everyone should use IMO.
So how do I test my patch ?
I'm using a slightly more complex tool called Kepler [1]. Since a patch that has been merged [2], it can also report VM consumption. The installation is easy on RPM based distribution [3].
But indeed, this tools is a Prometheus exporter so you need a Prometheus/Grafana stack for the observation which make the test more complex than the 2 previous tools mentioned.
Last month, I conducted a test with Kepler tools on both a host and within VMs. I was pleased to observe that the power graph trends were identical both outside and inside the VMs, albeit with a slight variation in terms of 1:1 Watt comparison.
If Kepler isn't the tool you're looking for, I'm open to any suggestions regarding cpupower/turbostat. I can work on a temporary patch that would enable us to utilize them.
Regards,
Anthony
[1]: https://sustainable-computing.io/
[2]: https://github.com/sustainable-computing-io/kepler/pull/931
[3]: https://sustainable-computing.io/installation/kepler-rpm/