KVM statistics data fd-based binary interface
Subsystems (13)
v9 cover KVM statistics data fd-based binary interface
This patchset provides a file descriptor for every VM and VCPU to read KVM statistics data in binary format.
It is meant to provide a lightweight, flexible, scalable and efficient lock-free solution for user space telemetry applications to pull the statistics data periodically for large scale systems. The pulling frequency could be as high as a few times per second.
In this patchset, every statistics data are treated to have some attributes as below:
* architecture dependent or generic
* VM statistics data or VCPU statistics data
* type: cumulative, instantaneous, peak
* unit: none for simple counter, nanosecond, microsecond, millisecond, second, Byte, KiByte, MiByte, GiByte. Clock Cycles
Since no lock/synchronization is used, the consistency between all the statistics data is not guaranteed. That means not all statistics data are read out at the exact same time, since the statistics date are still being updated by KVM subsystems while they are read out.
---
* v8 -> v9
- Rebase to commit 8331a2bc0898
(KVM: X86: Introduce KVM_HC_MAP_GPA_RANGE hypercall)
- Reduce code duplication between binary and debugfs interface
- Add field "offset" in stats descriptor to let us define stats descriptors in any order (not necessary in the order of stats defined in vm/vcpu stats structures)
- Add static check to make sure the number of stats descriptors is the same as the number of stats defined in vm/vcpu stats structures
- Fix missing/mismatched stats descriptor definition caused by rebase
* v7 -> v8
- Rebase to kvm/queue, commit c1dc20e254b4 ("KVM: switch per-VM stats to u64")
- Revise code to reflect the per-VM stats type from ulong to u64
- Addressed some other nits
* v6 -> v7
- Improve file descriptor allocation function by Krish suggestion
- Use "generic stats" instead of "common stats" as Krish suggested
- Addressed some other nits from Krish and David Matlack
* v5 -> v6
- Use designated initializers for STATS_DESC
- Change KVM_STATS_SCALE... to KVM_STATS_BASE...
- Use a common function for kvm_[vm|vcpu]_stats_read
- Fix some documentation errors/missings
- Use TEST_ASSERT in selftest
- Use a common function for [vm|vcpu]_stats_test in selftest
* v4 -> v5
- Rebase to kvm/queue, commit a4345a7cecfb ("Merge tag
'kvmarm-fixes-5.13-1'")
- Change maximum stats name length to 48
- Replace VM_STATS_COMMON/VCPU_STATS_COMMON macros with stats descriptor definition macros.
- Fixed some errors/warnings reported by checkpatch.pl
* v3 -> v4
- Rebase to kvm/queue, commit 9f242010c3b4 ("KVM: avoid "deadlock" between install_new_memslots and MMU notifier")
- Use C-stype comments in the whole patch
- Fix wrong count for x86 VCPU stats descriptors
- Fix KVM stats data size counting and validity check in selftest
* v2 -> v3
- Rebase to kvm/queue, commit edf408f5257b ("KVM: avoid "deadlock" between install_new_memslots and MMU notifier")
- Resolve some nitpicks about format
* v1 -> v2
- Use ARRAY_SIZE to count the number of stats descriptors
- Fix missing size field initialization in macro STATS_DESC
[1] https://lore.kernel.org/kvm/[email protected]
[2] https://lore.kernel.org/kvm/[email protected]
[3] https://lore.kernel.org/kvm/[email protected]
[4] https://lore.kernel.org/kvm/[email protected]
[5] https://lore.kernel.org/kvm/[email protected]
[6] https://lore.kernel.org/kvm/[email protected]
[7] https://lore.kernel.org/kvm/[email protected]
[8] https://lore.kernel.org/kvm/[email protected]
---
Jing Zhang (5):
KVM: stats: Separate generic stats from architecture specific ones
KVM: stats: Add fd-based API to read binary stats data
KVM: stats: Add documentation for statistics data binary interface
KVM: selftests: Add selftest for KVM statistics data binary interface
KVM: stats: Remove code duplication for binary and debugfs stats
Documentation/virt/kvm/api.rst | 177 +++++++++++- arch/arm64/include/asm/kvm_host.h | 9 +- arch/arm64/kvm/guest.c | 50 +++- arch/mips/include/asm/kvm_host.h | 9 +- arch/mips/kvm/mips.c | 92 +++--- arch/powerpc/include/asm/kvm_host.h | 9 +- arch/powerpc/kvm/book3s.c | 93 ++++--- arch/powerpc/kvm/book3s_hv.c | 12 +- arch/powerpc/kvm/book3s_pr.c | 2 +- arch/powerpc/kvm/book3s_pr_papr.c | 2 +- arch/powerpc/kvm/booke.c | 78 ++++-- arch/s390/include/asm/kvm_host.h | 9 +- arch/s390/kvm/kvm-s390.c | 234 +++++++++------- arch/x86/include/asm/kvm_host.h | 9 +- arch/x86/kvm/x86.c | 111 +++++--- include/linux/kvm_host.h | 178 +++++++++++- include/linux/kvm_types.h | 12 + include/uapi/linux/kvm.h | 48 ++++ tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 3 + .../testing/selftests/kvm/include/kvm_util.h | 3 + .../selftests/kvm/kvm_binary_stats_test.c | 225 +++++++++++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 12 + virt/kvm/kvm_main.c | 261 +++++++++++++++--- 24 files changed, 1293 insertions(+), 346 deletions(-) create mode 100644 tools/testing/selftests/kvm/kvm_binary_stats_test.c
base-commit: 8331a2bc089881d7fd2fc9a6658f39780817e4e0
Signature · 1 line
2.32.0.272.g935e593368-goog
PATCH v9 1/5 KVM: stats: Separate generic stats from architecture specific ones
Put all generic statistics in a separate structure to ease statistics handling for the incoming new statistics API.
No functional change intended.
--- arch/arm64/include/asm/kvm_host.h | 9 ++------- arch/arm64/kvm/guest.c | 12 ++++++------ arch/mips/include/asm/kvm_host.h | 9 ++------- arch/mips/kvm/mips.c | 12 ++++++------ arch/powerpc/include/asm/kvm_host.h | 9 ++------- arch/powerpc/kvm/book3s.c | 12 ++++++------ arch/powerpc/kvm/book3s_hv.c | 12 ++++++------ arch/powerpc/kvm/book3s_pr.c | 2 +- arch/powerpc/kvm/book3s_pr_papr.c | 2 +- arch/powerpc/kvm/booke.c | 14 +++++++------- arch/s390/include/asm/kvm_host.h | 9 ++------- arch/s390/kvm/kvm-s390.c | 12 ++++++------ arch/x86/include/asm/kvm_host.h | 9 ++------- arch/x86/kvm/x86.c | 14 +++++++------- include/linux/kvm_host.h | 9 +++++++-- include/linux/kvm_types.h | 12 ++++++++++++ virt/kvm/kvm_main.c | 14 +++++++------- 17 files changed, 82 insertions(+), 90 deletions(-)
arch/arm64/include/asm/kvm_host.h +2 -7
@@ -556,16 +556,11 @@ static inline bool __vcpu_write_sys_reg_to_cpu(u64 val, int reg) }struct kvm_vm_stat { -u64 remote_tlb_flush; +struct kvm_vm_stat_generic generic; };struct kvm_vcpu_stat { -u64 halt_successful_poll; -u64 halt_attempted_poll; -u64 halt_poll_success_ns; -u64 halt_poll_fail_ns; -u64 halt_poll_invalid; -u64 halt_wakeup; +struct kvm_vcpu_stat_generic generic;u64 hvc_exit_stat;u64 wfe_exit_stat;u64 wfi_exit_stat;
arch/arm64/kvm/guest.c +6 -6
@@ -29,18 +29,18 @@#include "trace.h" struct kvm_stats_debugfs_item debugfs_entries[] = { -VCPU_STAT ("halt_successful_poll" , halt_successful_poll), -VCPU_STAT ("halt_attempted_poll" , halt_attempted_poll), -VCPU_STAT ("halt_poll_invalid" , halt_poll_invalid), -VCPU_STAT ("halt_wakeup" , halt_wakeup), +VCPU_STAT_GENERIC ("halt_successful_poll" , halt_successful_poll), +VCPU_STAT_GENERIC ("halt_attempted_poll" , halt_attempted_poll), +VCPU_STAT_GENERIC ("halt_poll_invalid" , halt_poll_invalid), +VCPU_STAT_GENERIC ("halt_wakeup" , halt_wakeup),VCPU_STAT ("hvc_exit_stat" , hvc_exit_stat),VCPU_STAT ("wfe_exit_stat" , wfe_exit_stat),VCPU_STAT ("wfi_exit_stat" , wfi_exit_stat),VCPU_STAT ("mmio_exit_user" , mmio_exit_user),VCPU_STAT ("mmio_exit_kernel" , mmio_exit_kernel),VCPU_STAT ("exits" , exits), -VCPU_STAT ("halt_poll_success_ns" , halt_poll_success_ns), -VCPU_STAT ("halt_poll_fail_ns" , halt_poll_fail_ns), +VCPU_STAT_GENERIC ("halt_poll_success_ns" , halt_poll_success_ns), +VCPU_STAT_GENERIC ("halt_poll_fail_ns" , halt_poll_fail_ns), {NULL } };
arch/mips/include/asm/kvm_host.h +2 -7
@@ -109,10 +109,11 @@ static inline bool kvm_is_error_hva(unsigned long addr) }struct kvm_vm_stat { -u64 remote_tlb_flush; +struct kvm_vm_stat_generic generic; };struct kvm_vcpu_stat { +struct kvm_vcpu_stat_generic generic;u64 wait_exits;u64 cache_exits;u64 signal_exits;
@@ -142,12 +143,6 @@ struct kvm_vcpu_stat {#ifdef CONFIG_CPU_LOONGSON64 u64 vz_cpucfg_exits;#endif -u64 halt_successful_poll; -u64 halt_attempted_poll; -u64 halt_poll_success_ns; -u64 halt_poll_fail_ns; -u64 halt_poll_invalid; -u64 halt_wakeup; };struct kvm_arch_memory_slot {
arch/mips/kvm/mips.c +6 -6
@@ -68,12 +68,12 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {#ifdef CONFIG_CPU_LOONGSON64 VCPU_STAT ("vz_cpucfg" , vz_cpucfg_exits), #endif -VCPU_STAT ("halt_successful_poll" , halt_successful_poll), -VCPU_STAT ("halt_attempted_poll" , halt_attempted_poll), -VCPU_STAT ("halt_poll_invalid" , halt_poll_invalid), -VCPU_STAT ("halt_wakeup" , halt_wakeup), -VCPU_STAT ("halt_poll_success_ns" , halt_poll_success_ns), -VCPU_STAT ("halt_poll_fail_ns" , halt_poll_fail_ns), +VCPU_STAT_GENERIC ("halt_successful_poll" , halt_successful_poll), +VCPU_STAT_GENERIC ("halt_attempted_poll" , halt_attempted_poll), +VCPU_STAT_GENERIC ("halt_poll_invalid" , halt_poll_invalid), +VCPU_STAT_GENERIC ("halt_wakeup" , halt_wakeup), +VCPU_STAT_GENERIC ("halt_poll_success_ns" , halt_poll_success_ns), +VCPU_STAT_GENERIC ("halt_poll_fail_ns" , halt_poll_fail_ns), {NULL} };
arch/powerpc/include/asm/kvm_host.h +2 -7
@@ -81,12 +81,13 @@ struct kvmppc_book3s_shadow_vcpu;struct kvm_nested_guest ;struct kvm_vm_stat { -u64 remote_tlb_flush; +struct kvm_vm_stat_generic generic;u64 num_2M_pages;u64 num_1G_pages; };struct kvm_vcpu_stat { +struct kvm_vcpu_stat_generic generic;u64 sum_exits;u64 mmio_exits;u64 signal_exits;
@@ -102,14 +103,8 @@ struct kvm_vcpu_stat {u64 emulated_inst_exits;u64 dec_exits;u64 ext_intr_exits; -u64 halt_poll_success_ns; -u64 halt_poll_fail_ns;u64 halt_wait_ns; -u64 halt_successful_poll; -u64 halt_attempted_poll;u64 halt_successful_wait; -u64 halt_poll_invalid; -u64 halt_wakeup;u64 dbell_exits;u64 gdbell_exits;u64 ld;
arch/powerpc/kvm/book3s.c +6 -6
@@ -47,14 +47,14 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {VCPU_STAT ("dec" , dec_exits),VCPU_STAT ("ext_intr" , ext_intr_exits),VCPU_STAT ("queue_intr" , queue_intr), -VCPU_STAT ("halt_poll_success_ns" , halt_poll_success_ns), -VCPU_STAT ("halt_poll_fail_ns" , halt_poll_fail_ns), +VCPU_STAT_GENERIC ("halt_poll_success_ns" , halt_poll_success_ns), +VCPU_STAT_GENERIC ("halt_poll_fail_ns" , halt_poll_fail_ns),VCPU_STAT ("halt_wait_ns" , halt_wait_ns), -VCPU_STAT ("halt_successful_poll" , halt_successful_poll), -VCPU_STAT ("halt_attempted_poll" , halt_attempted_poll), +VCPU_STAT_GENERIC ("halt_successful_poll" , halt_successful_poll), +VCPU_STAT_GENERIC ("halt_attempted_poll" , halt_attempted_poll),VCPU_STAT ("halt_successful_wait" , halt_successful_wait), -VCPU_STAT ("halt_poll_invalid" , halt_poll_invalid), -VCPU_STAT ("halt_wakeup" , halt_wakeup), +VCPU_STAT_GENERIC ("halt_poll_invalid" , halt_poll_invalid), +VCPU_STAT_GENERIC ("halt_wakeup" , halt_wakeup),VCPU_STAT ("pf_storage" , pf_storage),VCPU_STAT ("sp_storage" , sp_storage), VCPU_STAT("pf_instruc", pf_instruc),
arch/powerpc/kvm/book3s_hv.c +4 -6
@@ -236,7 +236,7 @@ static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu) waitp =kvm_arch_vcpu_get_wait (vcpu);if (rcuwait_wake_up (waitp)) - ++vcpu->stat.halt_wakeup; + ++vcpu->stat.generic.halt_wakeup; cpu =READ_ONCE (vcpu->arch.thread_cpu);if (cpu >=0 &&kvmppc_ipi_thread (cpu))
@@ -3925,7 +3925,7 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc) cur = start_poll =ktime_get ();if (vc->halt_poll_ns) {ktime_t stop =ktime_add_ns (start_poll, vc->halt_poll_ns); - ++vc->runner->stat.halt_attempted_poll; + ++vc->runner->stat.generic.halt_attempted_poll; vc->vcore_state =VCORE_POLLING ;spin_unlock (&vc->lock);
@@ -3942,7 +3942,7 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc) vc->vcore_state =VCORE_INACTIVE ;if (!do_sleep) { - ++vc->runner->stat.halt_successful_poll; + ++vc->runner->stat.generic.halt_successful_poll; goto out; } }
@@ -3954,7 +3954,7 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc) do_sleep =0 ;/* If we polled, count this as a successful poll */ if (vc->halt_poll_ns) - ++vc->runner->stat.halt_successful_poll; + ++vc->runner->stat.generic.halt_successful_poll; goto out; }
@@ -3981,13 +3981,13 @@ static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)ktime_to_ns (cur) -ktime_to_ns (start_wait);/* Attribute failed poll time */ if (vc->halt_poll_ns) - vc->runner->stat.halt_poll_fail_ns ++ vc->runner->stat.generic.halt_poll_fail_ns + ktime_to_ns(start_wait) -ktime_to_ns (start_poll); }else {/* Attribute successful poll time */ if (vc->halt_poll_ns) - vc->runner->stat.halt_poll_success_ns ++ vc->runner->stat.generic.halt_poll_success_ns + ktime_to_ns(cur) -ktime_to_ns (start_poll); }
arch/powerpc/kvm/book3s_pr.c +1 -1
@@ -493,7 +493,7 @@ static void kvmppc_set_msr_pr(struct kvm_vcpu *vcpu, u64 msr)if (!vcpu->arch.pending_exceptions) {kvm_vcpu_block (vcpu);kvm_clear_request (KVM_REQ_UNHALT , vcpu); - vcpu->stat.halt_wakeup++; + vcpu->stat.generic.halt_wakeup++;/* Unset POW bit after we woke up */ msr &= ~MSR_POW ;
arch/powerpc/kvm/book3s_pr_papr.c +1 -1
@@ -378,7 +378,7 @@ int kvmppc_h_pr(struct kvm_vcpu *vcpu, unsigned long cmd)kvmppc_set_msr_fast (vcpu,kvmppc_get_msr (vcpu) |MSR_EE );kvm_vcpu_block (vcpu);kvm_clear_request (KVM_REQ_UNHALT , vcpu); - vcpu->stat.halt_wakeup++; + vcpu->stat.generic.halt_wakeup++;return EMULATE_DONE ;case H_LOGICAL_CI_LOAD :return kvmppc_h_pr_logical_ci_load (vcpu);
arch/powerpc/kvm/booke.c +7 -7
@@ -49,15 +49,15 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {VCPU_STAT ("inst_emu" , emulated_inst_exits),VCPU_STAT ("dec" , dec_exits),VCPU_STAT ("ext_intr" , ext_intr_exits), -VCPU_STAT ("halt_successful_poll" , halt_successful_poll), -VCPU_STAT ("halt_attempted_poll" , halt_attempted_poll), -VCPU_STAT ("halt_poll_invalid" , halt_poll_invalid), -VCPU_STAT ("halt_wakeup" , halt_wakeup), +VCPU_STAT_GENERIC ("halt_successful_poll" , halt_successful_poll), +VCPU_STAT_GENERIC ("halt_attempted_poll" , halt_attempted_poll), +VCPU_STAT_GENERIC ("halt_poll_invalid" , halt_poll_invalid), +VCPU_STAT_GENERIC ("halt_wakeup" , halt_wakeup),VCPU_STAT ("doorbell" , dbell_exits),VCPU_STAT ("guest doorbell" , gdbell_exits), -VCPU_STAT ("halt_poll_success_ns" , halt_poll_success_ns), -VCPU_STAT ("halt_poll_fail_ns" , halt_poll_fail_ns), -VM_STAT ("remote_tlb_flush" , remote_tlb_flush), +VCPU_STAT_GENERIC ("halt_poll_success_ns" , halt_poll_success_ns), +VCPU_STAT_GENERIC ("halt_poll_fail_ns" , halt_poll_fail_ns), +VM_STAT_GENERIC ("remote_tlb_flush" , remote_tlb_flush), { NULL } };
arch/s390/include/asm/kvm_host.h +2 -7
@@ -361,6 +361,7 @@ struct sie_page { };struct kvm_vcpu_stat { +struct kvm_vcpu_stat_generic generic;u64 exit_userspace;u64 exit_null;u64 exit_external_request;
@@ -370,13 +371,7 @@ struct kvm_vcpu_stat {u64 exit_validity;u64 exit_instruction;u64 exit_pei; -u64 halt_successful_poll; -u64 halt_attempted_poll; -u64 halt_poll_invalid;u64 halt_no_poll_steal; -u64 halt_wakeup; -u64 halt_poll_success_ns; -u64 halt_poll_fail_ns;u64 instruction_lctl;u64 instruction_lctlg;u64 instruction_stctl;
@@ -755,12 +750,12 @@ struct kvm_vcpu_arch { };struct kvm_vm_stat { +struct kvm_vm_stat_generic generic;u64 inject_io;u64 inject_float_mchk;u64 inject_pfault_done;u64 inject_service_signal;u64 inject_virtio; -u64 remote_tlb_flush; };struct kvm_arch_memory_slot {
arch/s390/kvm/kvm-s390.c +6 -6
@@ -72,13 +72,13 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {VCPU_STAT ("exit_program_interruption" , exit_program_interruption),VCPU_STAT ("exit_instr_and_program_int" , exit_instr_and_program),VCPU_STAT ("exit_operation_exception" , exit_operation_exception), -VCPU_STAT ("halt_successful_poll" , halt_successful_poll), -VCPU_STAT ("halt_attempted_poll" , halt_attempted_poll), -VCPU_STAT ("halt_poll_invalid" , halt_poll_invalid), +VCPU_STAT_GENERIC ("halt_successful_poll" , halt_successful_poll), +VCPU_STAT_GENERIC ("halt_attempted_poll" , halt_attempted_poll), +VCPU_STAT_GENERIC ("halt_poll_invalid" , halt_poll_invalid),VCPU_STAT ("halt_no_poll_steal" , halt_no_poll_steal), -VCPU_STAT ("halt_wakeup" , halt_wakeup), -VCPU_STAT ("halt_poll_success_ns" , halt_poll_success_ns), -VCPU_STAT ("halt_poll_fail_ns" , halt_poll_fail_ns), +VCPU_STAT_GENERIC ("halt_wakeup" , halt_wakeup), +VCPU_STAT_GENERIC ("halt_poll_success_ns" , halt_poll_success_ns), +VCPU_STAT_GENERIC ("halt_poll_fail_ns" , halt_poll_fail_ns),VCPU_STAT ("instruction_lctlg" , instruction_lctlg),VCPU_STAT ("instruction_lctl" , instruction_lctl), VCPU_STAT("instruction_stctl", instruction_stctl),
arch/x86/include/asm/kvm_host.h +2 -7
@@ -1167,6 +1167,7 @@ struct kvm_arch { };struct kvm_vm_stat { +struct kvm_vm_stat_generic generic;u64 mmu_shadow_zapped;u64 mmu_pte_write;u64 mmu_pde_zapped;
@@ -1174,13 +1175,13 @@ struct kvm_vm_stat {u64 mmu_recycled;u64 mmu_cache_miss;u64 mmu_unsync; -u64 remote_tlb_flush;u64 lpages;u64 nx_lpage_splits;u64 max_mmu_page_hash_collisions; };struct kvm_vcpu_stat { +struct kvm_vcpu_stat_generic generic;u64 pf_fixed;u64 pf_guest;u64 tlb_flush;
@@ -1194,10 +1195,6 @@ struct kvm_vcpu_stat {u64 nmi_window_exits;u64 l1d_flush;u64 halt_exits; -u64 halt_successful_poll; -u64 halt_attempted_poll; -u64 halt_poll_invalid; -u64 halt_wakeup;u64 request_irq_exits;u64 irq_exits;u64 host_state_reload;
@@ -1208,8 +1205,6 @@ struct kvm_vcpu_stat {u64 irq_injections;u64 nmi_injections;u64 req_event; -u64 halt_poll_success_ns; -u64 halt_poll_fail_ns;u64 nested_run;u64 directed_yield_attempted;u64 directed_yield_successful;
arch/x86/kvm/x86.c +7 -7
@@ -235,10 +235,10 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {VCPU_STAT ("irq_window" , irq_window_exits),VCPU_STAT ("nmi_window" , nmi_window_exits),VCPU_STAT ("halt_exits" , halt_exits), -VCPU_STAT ("halt_successful_poll" , halt_successful_poll), -VCPU_STAT ("halt_attempted_poll" , halt_attempted_poll), -VCPU_STAT ("halt_poll_invalid" , halt_poll_invalid), -VCPU_STAT ("halt_wakeup" , halt_wakeup), +VCPU_STAT_GENERIC ("halt_successful_poll" , halt_successful_poll), +VCPU_STAT_GENERIC ("halt_attempted_poll" , halt_attempted_poll), +VCPU_STAT_GENERIC ("halt_poll_invalid" , halt_poll_invalid), +VCPU_STAT_GENERIC ("halt_wakeup" , halt_wakeup),VCPU_STAT ("hypercalls" , hypercalls),VCPU_STAT ("request_irq" , request_irq_exits), VCPU_STAT("irq_exits", irq_exits),
@@ -250,8 +250,8 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {VCPU_STAT ("nmi_injections" , nmi_injections),VCPU_STAT ("req_event" , req_event),VCPU_STAT ("l1d_flush" , l1d_flush), -VCPU_STAT ("halt_poll_success_ns" , halt_poll_success_ns), -VCPU_STAT ("halt_poll_fail_ns" , halt_poll_fail_ns), +VCPU_STAT_GENERIC ("halt_poll_success_ns" , halt_poll_success_ns), +VCPU_STAT_GENERIC ("halt_poll_fail_ns" , halt_poll_fail_ns),VCPU_STAT ("nested_run" , nested_run),VCPU_STAT ("directed_yield_attempted" , directed_yield_attempted), VCPU_STAT("directed_yield_successful", directed_yield_successful),
@@ -263,7 +263,7 @@ struct kvm_stats_debugfs_item debugfs_entries[] = {VM_STAT ("mmu_recycled" , mmu_recycled),VM_STAT ("mmu_cache_miss" , mmu_cache_miss),VM_STAT ("mmu_unsync" , mmu_unsync), -VM_STAT ("remote_tlb_flush" , remote_tlb_flush), +VM_STAT_GENERIC ("remote_tlb_flush" , remote_tlb_flush), VM_STAT("largepages", lpages, .mode = 0444), VM_STAT("nx_largepages_splitted", nx_lpage_splits, .mode = 0444), VM_STAT("max_mmu_page_hash_collisions", max_mmu_page_hash_collisions),
include/linux/kvm_host.h +7 -2
@@ -1275,10 +1275,15 @@ struct kvm_stats_debugfs_item {#define KVM_DBGFS_GET_MODE (dbgfs_item) \ ((dbgfs_item)->mode ? (dbgfs_item)->mode : 0644) -#define VM_STAT (n, x, ...) \ +#define VM_STAT (n, x, ...) \ { n, offsetof(struct kvm, stat.x), KVM_STAT_VM, ## __VA_ARGS__ } -#define VCPU_STAT (n, x, ...) \ +#define VCPU_STAT (n, x, ...) \ { n, offsetof(struct kvm_vcpu, stat.x), KVM_STAT_VCPU, ## __VA_ARGS__ } +#define VM_STAT_GENERIC (n, x, ...) \ + { n, offsetof(struct kvm, stat.generic.x), KVM_STAT_VM, ## __VA_ARGS__ } +#define VCPU_STAT_GENERIC (n, x, ...) \ + { n, offsetof(struct kvm_vcpu, stat.generic.x), \ + KVM_STAT_VCPU, ## __VA_ARGS__ }extern struct kvm_stats_debugfs_item debugfs_entries[];extern struct dentry *kvm_debugfs_dir;
include/linux/kvm_types.h +12 -0
@@ -76,5 +76,17 @@ struct kvm_mmu_memory_cache { };#endif +struct kvm_vm_stat_generic { +u64 remote_tlb_flush; +}; + +struct kvm_vcpu_stat_generic { +u64 halt_successful_poll; +u64 halt_attempted_poll; +u64 halt_poll_invalid; +u64 halt_wakeup; +u64 halt_poll_success_ns; +u64 halt_poll_fail_ns; +};#endif /* __KVM_TYPES_H__ */
virt/kvm/kvm_main.c +7 -7
@@ -332,7 +332,7 @@ void kvm_flush_remote_tlbs(struct kvm *kvm)*/ if (!kvm_arch_flush_remote_tlb (kvm) ||kvm_make_all_cpus_request (kvm,KVM_REQ_TLB_FLUSH )) - ++kvm->stat.remote_tlb_flush; + ++kvm->stat.generic.remote_tlb_flush;cmpxchg (&kvm->tlbs_dirty, dirty_count,0 ); }EXPORT_SYMBOL_GPL (kvm_flush_remote_tlbs);
@@ -3029,9 +3029,9 @@ static inline void update_halt_poll_stats(struct kvm_vcpu *vcpu, u64 poll_ns, bool waited) { if (waited) - vcpu->stat.halt_poll_fail_ns += poll_ns; + vcpu->stat.generic.halt_poll_fail_ns += poll_ns; else - vcpu->stat.halt_poll_success_ns += poll_ns; + vcpu->stat.generic.halt_poll_success_ns += poll_ns; }/*
@@ -3049,16 +3049,16 @@ void kvm_vcpu_block(struct kvm_vcpu *vcpu)if (vcpu->halt_poll_ns && !kvm_arch_no_poll (vcpu)) {ktime_t stop =ktime_add_ns (ktime_get (), vcpu->halt_poll_ns); - ++vcpu->stat.halt_attempted_poll; + ++vcpu->stat.generic.halt_attempted_poll; do { /* * This sets KVM_REQ_UNHALT if an interrupt * arrives. */ if (kvm_vcpu_check_block(vcpu) < 0) { - ++vcpu->stat.halt_successful_poll; + ++vcpu->stat.generic.halt_successful_poll; if (!vcpu_valid_wakeup(vcpu)) - ++vcpu->stat.halt_poll_invalid; + ++vcpu->stat.generic.halt_poll_invalid; goto out; } poll_end = cur = ktime_get();
@@ -3115,7 +3115,7 @@ bool kvm_vcpu_wake_up(struct kvm_vcpu *vcpu) waitp =kvm_arch_vcpu_get_wait (vcpu);if (rcuwait_wake_up (waitp)) {WRITE_ONCE (vcpu->ready, true); - ++vcpu->stat.halt_wakeup; + ++vcpu->stat.generic.halt_wakeup;return true; }
Signature · 1 line
2.32.0.272.g935e593368-goog
PATCH v9 2/5 KVM: stats: Add fd-based API to read binary stats data
In reply to Jing ZhangProvides a file descriptor per VM to read VM stats info/data. Provides a file descriptor per vCPU to read vCPU stats info/data.
--- arch/arm64/kvm/guest.c | 38 +++++++++ arch/mips/kvm/mips.c | 61 ++++++++++++++ arch/powerpc/kvm/book3s.c | 64 +++++++++++++++ arch/powerpc/kvm/booke.c | 57 ++++++++++++++ arch/s390/kvm/kvm-s390.c | 130 ++++++++++++++++++++++++++++++ arch/x86/kvm/x86.c | 68 ++++++++++++++++ include/linux/kvm_host.h | 162 ++++++++++++++++++++++++++++++++++++++ include/uapi/linux/kvm.h | 46 +++++++++++ virt/kvm/kvm_main.c | 143 +++++++++++++++++++++++++++++++++ 9 files changed, 769 insertions(+)
arch/arm64/kvm/guest.c +38 -0
@@ -28,6 +28,44 @@#include "trace.h" +struct _kvm_stats_desc kvm_vm_stats_desc[] = { +KVM_GENERIC_VM_STATS () +}; +static_assert (ARRAY_SIZE (kvm_vm_stats_desc) = +sizeof (struct kvm_vm_stat ) /sizeof (u64)); + +struct _kvm_stats_header kvm_vm_stats_header = { + { + .name_size =KVM_STATS_NAME_LEN , + .count =ARRAY_SIZE (kvm_vm_stats_desc), + .desc_offset =sizeof (struct _kvm_stats_header ), + .data_offset =sizeof (struct _kvm_stats_header ) + +sizeof (kvm_vm_stats_desc), + } +}; + +struct _kvm_stats_desc kvm_vcpu_stats_desc[] = { +KVM_GENERIC_VCPU_STATS (), +STATS_DESC_COUNTER (VCPU , hvc_exit_stat), +STATS_DESC_COUNTER (VCPU , wfe_exit_stat), +STATS_DESC_COUNTER (VCPU , wfi_exit_stat), +STATS_DESC_COUNTER (VCPU , mmio_exit_user), +STATS_DESC_COUNTER (VCPU , mmio_exit_kernel), +STATS_DESC_COUNTER (VCPU , exits) +}; +static_assert (ARRAY_SIZE (kvm_vcpu_stats_desc) = +sizeof (struct kvm_vcpu_stat ) /sizeof (u64)); + +struct _kvm_stats_header kvm_vcpu_stats_header = { + { + .name_size =KVM_STATS_NAME_LEN , + .count =ARRAY_SIZE (kvm_vcpu_stats_desc), + .desc_offset =sizeof (struct _kvm_stats_header ), + .data_offset =sizeof (struct _kvm_stats_header ) + +sizeof (kvm_vcpu_stats_desc), + } +}; +struct kvm_stats_debugfs_item debugfs_entries[] = {VCPU_STAT_GENERIC ("halt_successful_poll" , halt_successful_poll),VCPU_STAT_GENERIC ("halt_attempted_poll" , halt_attempted_poll),
arch/mips/kvm/mips.c +61 -0
67 lines not on this page · raw message
arch/powerpc/kvm/book3s.c +64 -0
70 lines not on this page · raw message
arch/powerpc/kvm/booke.c +57 -0
63 lines not on this page · raw message
arch/s390/kvm/kvm-s390.c +130 -0
136 lines not on this page · raw message
arch/x86/kvm/x86.c +68 -0
74 lines not on this page · raw message
include/linux/kvm_host.h +162 -0
176 lines not on this page · raw message
include/uapi/linux/kvm.h +46 -0
56 lines not on this page · raw message
virt/kvm/kvm_main.c +143 -0
173 lines not on this page · raw message
Signature · 1 line
2.32.0.272.g935e593368-goog
Paolo Bonzini
maintainerRe: [PATCH v9 2/5] KVM: stats: Add fd-based API to read binary stats data
virt/kvm/kvm_main.c static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) · line 3551
Quoted 11 lines · Jing Zhang
virt/kvm/kvm_main.c +11
-0+/* Copy kvm stats values */ + copylen = header->header.data_offset + size_stats - pos; + copylen =min (copylen, remain); +if (copylen >0 ) { + src = stats + pos - header->header.data_offset; +if (copy_to_user (dest, src, copylen)) +return -EFAULT ; + remain -= copylen; + pos += copylen; + dest += copylen; + }
Hi Jing,
this code is causing usercopy warnings because the statistics are not part of the vcpu slab's usercopy region. You need to move struct kvm_vcpu_stat next to struct kvm_vcpu_arch, and adjust the call to kmem_cache_create_usercopy in kvm_init.
Can you post a new version of the series, and while you are at it explain the rationale for binary stats in the commit message for this patch? This should include:
- the problem statement (e.g. frequency of the accesses)
- what are the benefits compared to debugfs
- why the schema is included in the file descriptor as well
You can probably find a lot or all of the information in my emails from the last couple days, but you might also have other breadcrumbs from Google's internal implementation of binary stats.
Thanks,
Paolo
Re: [PATCH v9 2/5] KVM: stats: Add fd-based API to read binary stats data
Quoted 31 lines · Paolo Bonzini
virt/kvm/kvm_main.c static int kvm_vcpu_ioctl_set_sigmask(struct kvm_vcpu *vcpu, sigset_t *sigset) · line 3551
Quoted 11 lines · Jing Zhang
virt/kvm/kvm_main.c +11
-0+/* Copy kvm stats values */ + copylen = header->header.data_offset + size_stats - pos; + copylen =min (copylen, remain); +if (copylen >0 ) { + src = stats + pos - header->header.data_offset; +if (copy_to_user (dest, src, copylen)) +return -EFAULT ; + remain -= copylen; + pos += copylen; + dest += copylen; + }Hi Jing,
this code is causing usercopy warnings because the statistics are not part of the vcpu slab's usercopy region. You need to move struct
kvm_vcpu_statnext to structkvm_vcpu_arch, and adjust the call tokmem_cache_create_usercopyinkvm_init.Can you post a new version of the series, and while you are at it explain the rationale for binary stats in the commit message for this patch? This should include:
- the problem statement (e.g. frequency of the accesses)
- what are the benefits compared to debugfs
- why the schema is included in the file descriptor as well
Quoted 8 lines
You can probably find a lot or all of the information in my emails from the last couple days, but you might also have other breadcrumbs from Google's internal implementation of binary stats.
Thanks,
Paolo
Hi Paolo
Will fix the usercopy warnings, add more explanations and post another version. And thanks for all the information in your emails.
Jing
PATCH v9 3/5 KVM: stats: Add documentation for statistics data binary interface
In reply to Jing ZhangUpdate KVM API documentation for binary statistics.
--- Documentation/virt/kvm/api.rst | 177 ++++++++++++++++++++++++++++++++- 1 file changed, 176 insertions(+), 1 deletion(-)
Documentation/virt/kvm/api.rst +176 -1
@@ -5034,7 +5034,6 @@ see KVM_XEN_VCPU_SET_ATTR above. The KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST type may not be used with the KVM_XEN_VCPU_GET_ATTR ioctl. - 4.131 KVM_GET_SREGS2 ------------------
@@ -5081,6 +5080,174 @@ Writes special registers into the vcpu. See KVM_GET_SREGS2 for the data structures. This ioctl (when supported) replaces the KVM_SET_SREGS. +4.133 KVM_GET_STATS_FD +---------------------- + +:Capability: KVM_CAP_STATS_BINARY_FD +:Architectures: all +:Type: vm ioctl, vcpu ioctl +:Parameters: none +:Returns: statistics file descriptor on success, < 0 on error + +Errors: + + === =========================== + ENOMEM if the fd could not be created due to lack of memory + EMFILE if the number of opened files exceeds the limit + === =========================== + +The file descriptor can be used to read VM/vCPU statistics data in binary +format. The file data is organized into three blocks as below: ++-------------+ +| Header | ++-------------+ +| Descriptors | ++-------------+ +| Stats Data | ++-------------+ + +The Header block is always at the start of the file. It is only needed to be +read one time for the lifetime of the file descriptor. +It is in the form of ``struct kvm_stats_header`` as below:: + + #define KVM_STATS_ID_MAXLEN 64 + + struct kvm_stats_header { + __u32 name_size; + __u32 count; + __u32 desc_offset; + __u32 data_offset; + char id[0]; + }; + +The ``id`` field is identification for the corresponding KVM statistics. For +VM statistics, it is in the form of "kvm-{kvm pid}", like "kvm-12345". For +VCPU statistics, it is in the form of "kvm-{kvm pid}/vcpu-{vcpu id}", like +"kvm-12345/vcpu-12". + +The ``name_size`` field is the size (byte) of the statistics name string +(including trailing '\0') appended to the end of every statistics descriptor. + +The ``count`` field is the number of statistics. + +The ``desc_offset`` field is the offset of the Descriptors block from the start +of the file indicated by the file descriptor. + +The ``data_offset`` field is the offset of the Stats Data block from the start +of the file indicated by the file descriptor. + +The Descriptors block is only needed to be read once for the lifetime of the +file descriptor. It is an array of ``struct kvm_stats_desc`` as shown in +below code block:: + + #define KVM_STATS_TYPE_SHIFT 0 + #define KVM_STATS_TYPE_MASK (0xF << KVM_STATS_TYPE_SHIFT) + #define KVM_STATS_TYPE_CUMULATIVE (0x0 << KVM_STATS_TYPE_SHIFT) + #define KVM_STATS_TYPE_INSTANT (0x1 << KVM_STATS_TYPE_SHIFT) + #define KVM_STATS_TYPE_MAX KVM_STATS_TYPE_INSTANT + + #define KVM_STATS_UNIT_SHIFT 4 + #define KVM_STATS_UNIT_MASK (0xF << KVM_STATS_UNIT_SHIFT) + #define KVM_STATS_UNIT_NONE (0x0 << KVM_STATS_UNIT_SHIFT) + #define KVM_STATS_UNIT_BYTES (0x1 << KVM_STATS_UNIT_SHIFT) + #define KVM_STATS_UNIT_SECONDS (0x2 << KVM_STATS_UNIT_SHIFT) + #define KVM_STATS_UNIT_CYCLES (0x3 << KVM_STATS_UNIT_SHIFT) + #define KVM_STATS_UNIT_MAX KVM_STATS_UNIT_CYCLES + + #define KVM_STATS_BASE_SHIFT 8 + #define KVM_STATS_BASE_MASK (0xF << KVM_STATS_BASE_SHIFT) + #define KVM_STATS_BASE_POW10 (0x0 << KVM_STATS_BASE_SHIFT) + #define KVM_STATS_BASE_POW2 (0x1 << KVM_STATS_BASE_SHIFT) + #define KVM_STATS_BASE_MAX KVM_STATS_BASE_POW2 + + struct kvm_stats_desc { + __u32 flags; + __s16 exponent; + __u16 size; + __u32 offset; + __u32 unused; + char name[0]; + }; + +The ``flags`` field contains the type and unit of the statistics data described +by this descriptor. The following flags are supported: + +Bits 0-3 of ``flags`` encode the type: + * ``KVM_STATS_TYPE_CUMULATIVE`` + The statistics data is cumulative. The value of data can only be increased. + Most of the counters used in KVM are of this type. + The corresponding ``count`` field for this type is always 1. + * ``KVM_STATS_TYPE_INSTANT`` + The statistics data is instantaneous. Its value can be increased or + decreased. This type is usually used as a measurement of some resources, + like the number of dirty pages, the number of large pages, etc. + The corresponding ``count`` field for this type is always 1. + +Bits 4-7 of ``flags`` encode the unit: + * ``KVM_STATS_UNIT_NONE`` + There is no unit for the value of statistics data. This usually means that + the value is a simple counter of an event. + * ``KVM_STATS_UNIT_BYTES`` + It indicates that the statistics data is used to measure memory size, in the + unit of Byte, KiByte, MiByte, GiByte, etc. The unit of the data is + determined by the ``exponent`` field in the descriptor. The + ``KVM_STATS_BASE_POW2`` flag is valid in this case. The unit of the data is + determined by ``pow(2, exponent)``. For example, if value is 10, + ``exponent`` is 20, which means the unit of statistics data is MiByte, we + can get the statistics data in the unit of Byte by + ``value * pow(2, exponent) = 10 * pow(2, 20) = 10 MiByte`` which is + 10 * 1024 * 1024 Bytes. + * ``KVM_STATS_UNIT_SECONDS`` + It indicates that the statistics data is used to measure time/latency, in + the unit of nanosecond, microsecond, millisecond and second. The unit of the + data is determined by the ``exponent`` field in the descriptor. The + ``KVM_STATS_BASE_POW10`` flag is valid in this case. The unit of the data + is determined by ``pow(10, exponent)``. For example, if value is 2000000, + ``exponent`` is -6, which means the unit of statistics data is microsecond, + we can get the statistics data in the unit of second by + ``value * pow(10, exponent) = 2000000 * pow(10, -6) = 2 seconds``. + * ``KVM_STATS_UNIT_CYCLES`` + It indicates that the statistics data is used to measure CPU clock cycles. + The ``KVM_STATS_BASE_POW10`` flag is valid in this case. For example, if + value is 200, ``exponent`` is 4, we can get the number of CPU clock cycles + by ``value * pow(10, exponent) = 200 * pow(10, 4) = 2000000``. + +Bits 8-11 of ``flags`` encode the base: + * ``KVM_STATS_BASE_POW10`` + The scale is based on power of 10. It is used for measurement of time and + CPU clock cycles. + * ``KVM_STATS_BASE_POW2`` + The scale is based on power of 2. It is used for measurement of memory size. + +The ``exponent`` field is the scale of corresponding statistics data. For +example, if the unit is ``KVM_STATS_UNIT_BYTES``, the base is +``KVM_STATS_BASE_POW2``, the ``exponent`` is 10, then we know that the real +unit of the statistics data is KBytes a.k.a pow(2, 10) = 1024 bytes. + +The ``size`` field is the number of values (u64) of this statistics data. Its +value is usually 1 for most of simple statistics. + +The ``offset`` field is the offset from the start of Data Block to the start of +the corresponding statistics data. + +The ``unused`` fields are reserved for future support for other types of +statistics data, like log/linear histogram. + +The ``name`` field points to the name string of the statistics data. The name +string starts at the end of ``struct kvm_stats_desc``. +The maximum length (including trailing '\0') is indicated by ``name_size`` +in ``struct kvm_stats_header``. + +The Stats Data block contains an array of data values of type ``struct +kvm_vm_stats_data`` or ``struct kvm_vcpu_stats_data``. It would be read by +userspace periodically to pull statistics data. +The order of data value in Stats Data block is the same as the order of +descriptors in Descriptors block. + * Statistics data for VM/VCPU:: + + struct kvm_stats_data { + __u64 value[0]; + }; 5. The kvm_run structure ============
@@ -6969,3 +7136,11 @@ The argument to KVM_ENABLE_CAP is also a bitmask, and must be a subset of the result of KVM_CHECK_EXTENSION. KVM will forward to userspace the hypercalls whose corresponding bit is in the argument, and return ENOSYS for the others. + +8.35 KVM_CAP_STATS_BINARY_FD +---------------------------- + +:Architectures: all + +This capability indicates the feature that userspace can create get a file +descriptor for every VM and VCPU to read statistics data in binary format.
Signature · 1 line
2.32.0.272.g935e593368-goog
Re: [PATCH v9 3/5] KVM: stats: Add documentation for statistics data binary interface
Quoted 1 line · Jing Zhang
Update KVM API documentation for binary statistics.
You should write more here. See my comment at the bottom...
Quoted 184 lines
Reviewed-by David Matlack <[email protected]>Reviewed-by Ricardo Koller <[email protected]>Reviewed-by Krish Sadhukhan <[email protected]>Reviewed-by Fuad Tabba <[email protected]>Signed-off-by Jing Zhang <[email protected]>--- Documentation/virt/kvm/api.rst | 177 ++++++++++++++++++++++++++++++++- 1 file changed, 176 insertions(+), 1 deletion(-)Documentation/virt/kvm/api.rst +158
-1@@ -5034,7 +5034,6 @@ see KVM_XEN_VCPU_SET_ATTR above. The KVM_XEN_VCPU_ATTR_TYPE_RUNSTATE_ADJUST type may not be used with the KVM_XEN_VCPU_GET_ATTR ioctl. - 4.131 KVM_GET_SREGS2 ------------------@@ -5081,6 +5080,174 @@ Writes special registers into the vcpu. See KVM_GET_SREGS2 for the data structures. This ioctl (when supported) replaces the KVM_SET_SREGS. +4.133 KVM_GET_STATS_FD +---------------------- + +:Capability: KVM_CAP_STATS_BINARY_FD +:Architectures: all +:Type: vm ioctl, vcpu ioctl +:Parameters: none +:Returns: statistics file descriptor on success, < 0 on error + +Errors: + + === =========================== + ENOMEM if the fd could not be created due to lack of memory + EMFILE if the number of opened files exceeds the limit + === =========================== + +The file descriptor can be used to read VM/vCPU statistics data in binary +format. The file data is organized into three blocks as below: ++-------------+ +| Header | ++-------------+ +| Descriptors | ++-------------+ +| Stats Data | ++-------------+ + +The Header block is always at the start of the file. It is only needed to be +read one time for the lifetime of the file descriptor. +It is in the form of ``struct kvm_stats_header`` as below:: + + #define KVM_STATS_ID_MAXLEN 64 + + struct kvm_stats_header { + __u32 name_size; + __u32 count; + __u32 desc_offset; + __u32 data_offset; + char id[0]; + }; + +The ``id`` field is identification for the corresponding KVM statistics. For +VM statistics, it is in the form of "kvm-{kvm pid}", like "kvm-12345". For +VCPU statistics, it is in the form of "kvm-{kvm pid}/vcpu-{vcpu id}", like +"kvm-12345/vcpu-12". + +The ``name_size`` field is the size (byte) of the statistics name string +(including trailing '\0') appended to the end of every statistics descriptor. + +The ``count`` field is the number of statistics. + +The ``desc_offset`` field is the offset of the Descriptors block from the start +of the file indicated by the file descriptor. + +The ``data_offset`` field is the offset of the Stats Data block from the start +of the file indicated by the file descriptor. + +The Descriptors block is only needed to be read once for the lifetime of the +file descriptor. It is an array of ``struct kvm_stats_desc`` as shown in +below code block:: + + #define KVM_STATS_TYPE_SHIFT 0 + #define KVM_STATS_TYPE_MASK (0xF << KVM_STATS_TYPE_SHIFT) + #define KVM_STATS_TYPE_CUMULATIVE (0x0 << KVM_STATS_TYPE_SHIFT) + #define KVM_STATS_TYPE_INSTANT (0x1 << KVM_STATS_TYPE_SHIFT) + #define KVM_STATS_TYPE_MAX KVM_STATS_TYPE_INSTANT + + #define KVM_STATS_UNIT_SHIFT 4 + #define KVM_STATS_UNIT_MASK (0xF << KVM_STATS_UNIT_SHIFT) + #define KVM_STATS_UNIT_NONE (0x0 << KVM_STATS_UNIT_SHIFT) + #define KVM_STATS_UNIT_BYTES (0x1 << KVM_STATS_UNIT_SHIFT) + #define KVM_STATS_UNIT_SECONDS (0x2 << KVM_STATS_UNIT_SHIFT) + #define KVM_STATS_UNIT_CYCLES (0x3 << KVM_STATS_UNIT_SHIFT) + #define KVM_STATS_UNIT_MAX KVM_STATS_UNIT_CYCLES + + #define KVM_STATS_BASE_SHIFT 8 + #define KVM_STATS_BASE_MASK (0xF << KVM_STATS_BASE_SHIFT) + #define KVM_STATS_BASE_POW10 (0x0 << KVM_STATS_BASE_SHIFT) + #define KVM_STATS_BASE_POW2 (0x1 << KVM_STATS_BASE_SHIFT) + #define KVM_STATS_BASE_MAX KVM_STATS_BASE_POW2 + + struct kvm_stats_desc { + __u32 flags; + __s16 exponent; + __u16 size; + __u32 offset; + __u32 unused; + char name[0]; + }; + +The ``flags`` field contains the type and unit of the statistics data described +by this descriptor. The following flags are supported: + +Bits 0-3 of ``flags`` encode the type: + * ``KVM_STATS_TYPE_CUMULATIVE`` + The statistics data is cumulative. The value of data can only be increased. + Most of the counters used in KVM are of this type. + The corresponding ``count`` field for this type is always 1. + * ``KVM_STATS_TYPE_INSTANT`` + The statistics data is instantaneous. Its value can be increased or + decreased. This type is usually used as a measurement of some resources, + like the number of dirty pages, the number of large pages, etc. + The corresponding ``count`` field for this type is always 1. + +Bits 4-7 of ``flags`` encode the unit: + * ``KVM_STATS_UNIT_NONE`` + There is no unit for the value of statistics data. This usually means that + the value is a simple counter of an event. + * ``KVM_STATS_UNIT_BYTES`` + It indicates that the statistics data is used to measure memory size, in the + unit of Byte, KiByte, MiByte, GiByte, etc. The unit of the data is + determined by the ``exponent`` field in the descriptor. The + ``KVM_STATS_BASE_POW2`` flag is valid in this case. The unit of the data is + determined by ``pow(2, exponent)``. For example, if value is 10, + ``exponent`` is 20, which means the unit of statistics data is MiByte, we + can get the statistics data in the unit of Byte by + ``value * pow(2, exponent) = 10 * pow(2, 20) = 10 MiByte`` which is + 10 * 1024 * 1024 Bytes. + * ``KVM_STATS_UNIT_SECONDS`` + It indicates that the statistics data is used to measure time/latency, in + the unit of nanosecond, microsecond, millisecond and second. The unit of the + data is determined by the ``exponent`` field in the descriptor. The + ``KVM_STATS_BASE_POW10`` flag is valid in this case. The unit of the data + is determined by ``pow(10, exponent)``. For example, if value is 2000000, + ``exponent`` is -6, which means the unit of statistics data is microsecond, + we can get the statistics data in the unit of second by + ``value * pow(10, exponent) = 2000000 * pow(10, -6) = 2 seconds``. + * ``KVM_STATS_UNIT_CYCLES`` + It indicates that the statistics data is used to measure CPU clock cycles. + The ``KVM_STATS_BASE_POW10`` flag is valid in this case. For example, if + value is 200, ``exponent`` is 4, we can get the number of CPU clock cycles + by ``value * pow(10, exponent) = 200 * pow(10, 4) = 2000000``. + +Bits 8-11 of ``flags`` encode the base: + * ``KVM_STATS_BASE_POW10`` + The scale is based on power of 10. It is used for measurement of time and + CPU clock cycles. + * ``KVM_STATS_BASE_POW2`` + The scale is based on power of 2. It is used for measurement of memory size. + +The ``exponent`` field is the scale of corresponding statistics data. For +example, if the unit is ``KVM_STATS_UNIT_BYTES``, the base is +``KVM_STATS_BASE_POW2``, the ``exponent`` is 10, then we know that the real +unit of the statistics data is KBytes a.k.a pow(2, 10) = 1024 bytes. + +The ``size`` field is the number of values (u64) of this statistics data. Its +value is usually 1 for most of simple statistics. + +The ``offset`` field is the offset from the start of Data Block to the start of +the corresponding statistics data. + +The ``unused`` fields are reserved for future support for other types of +statistics data, like log/linear histogram. + +The ``name`` field points to the name string of the statistics data. The name +string starts at the end of ``struct kvm_stats_desc``. +The maximum length (including trailing '\0') is indicated by ``name_size`` +in ``struct kvm_stats_header``. +
Documentation/virt/kvm/api.rst Writes special registers into the vcpu. · line 5250
Quoted 10 lines
Documentation/virt/kvm/api.rst +10
-0+The Stats Data block contains an array of data values of type ``struct +kvm_vm_stats_data`` or ``struct kvm_vcpu_stats_data``. It would be read by +userspace periodically to pull statistics data. +The order of data value in Stats Data block is the same as the order of +descriptors in Descriptors block. + * Statistics data for VM/VCPU:: + + struct kvm_stats_data { + __u64 value[0]; + };
I forgot to comment on this one, sorry for the delay.
Why are you "inventing" your own schema format here for this? Why not use a well-known or at least well-designed/implemented one that we have in userspace already?
There are a few that I would love to see in the kernel, varlink being the best example. We have kernel examples of this and I would consider using that as a transport for sysfs-like data in the future, but never got around to it.
So again, why reinvent the wheel to create a custom api when you could use an existing one?
thanks,
greg k-h
Paolo Bonzini
maintainerRe: [PATCH v9 3/5] KVM: stats: Add documentation for statistics data binary interface
Quoted 10 lines · Greg KH
I forgot to comment on this one, sorry for the delay.
Why are you "inventing" your own schema format here for this? Why not use a well-known or at least well-designed/implemented one that we have in userspace already?
There are a few that I would love to see in the kernel, varlink being the best example. We have kernel examples of this and I would consider using that as a transport for sysfs-like data in the future, but never got around to it.
Thanks, that's a good observation, and it's a problem that the rationale and the design process didn't end up in either the documentation or the commit message (only the outcome did). In order to fix that, this is going to be quite a long message.
Varlink comprises both a schema and an encoding, and each has its own problems. For the encoding of the data, varlink is really just using JSON and it is really just the wrong tool here. The first few problems that come to mind are:
- varlink structs are encoded as JSON dictionaries. Therefore, every time userspace reads the fields, the kernel has to include the field names as JSON dictionary keys. This means that a lot of time is spent writing buffers, and on the receiving side parsing them.
- because numeric data has to be converted to ASCII the output doesn't have fixed offsets, so it is not possible to make an efficient implementation of pread.
- even though Varlink specifies that int is "usually int64", a little-known gem is that JSON behavior for numbers not representable as a double (i.e. exceeding 2^53) is implementation-defined; implementations can and will mess up values outside that range. This problem is not specific to this KVM stats usecase; varlink's schema specification really is at odds with its encoding specification.
For the schema, there are some specific problems with varlink, but also a more generic issue. The specific problems are:
- the schema doesn't include the length of arrays. This makes it hard to compute in advance lengths and offsets of fields (even ignoring the fact that data is not binary, which I'll get to later)
- the schema also is not extensible with user annotations. In our case that would be mostly the unit in which the value is expressed.
The main problem with the various available serialization formats is that their schema languages are designed to be compiled with a code generator. Here instead the schema is transmitted from the kernel to userspace. Userspace is not really supposed to know what a value means or even that if it exists. Userspace takes care of collecting the data from the kernel, but ultimately there will be a human that knows what e.g. "io_exits" or "req_event" means, and they will ask for the current value, or a plot over time, of a specific statistic.
Now, unlike most other schema languages, varlink does not require precompiling the schema in its C bindings. However, this was mostly just the authors not bothering to write a C code generator---they did write one for Rust, for example. When using the "official" varlink C bindings, you hardly use the schema at all.
Something similar to the above issues is quite common in other formats.
For example, FlatBuffers[2]'s schema language[1] does have annotations
(which it calls metadata), but then we would still have to invent some standard annotations and teach programs about them. However, the deal breaker is that again there is no way to transmit the schema from the server to the client, and that users are supposed to precompile the schema using a code generator.
All that said, what we _could_ do is serialize the schema as JSON instead of using a binary format, like so:
[{
"name": "exits",
"kind": "cumulative",
"count": 1
}, {
"name": "halt_poll_fail_ns",
"kind": "cumulative",
"unit": "seconds",
"scale": {"base": "pow2", exponent: -9},
"count": 1
}, ...]while keeping the actual statistics as an array of u64 values just like in these patches. The JSON representation of the schema would be always the same, so it could be treated as fixed-size and an efficient implementation of pread would be possible. And once the schema is JSON, there could be a *meta*-schema expressed using the varlink language:
# compare to struct kvm_stats_desc, included after my sig
# for reference
type StatsDescriptor {
name: string,
kind: (cumulative, instant, peak),
unit: ?(bytes, seconds, cycles),
scale: ?(base: (pow2, pow10), exponent: int),
count: int
} type StatsSchema {
name: string,
stats: []StatsDescriptor
}Varlink would *not* be used in the kernel, because Varlink isreally just JSON. The above ten lines (plus comments) would be dropped in Documentation/ as a .varlink file; they would be a convenient way to describe (to either a human or a program) how to parse the JSON schema.
There would then be another problem, namely how to include the varlink schema in the generated documentation, since right now there are for example no sphinx bindings for varlink. Nevertheless, if you think it's better to have the schema as JSON instead of binary, according to the above varlink meta-schema, then we can look into it.
Thanks,
Paolo
Quoted 8 lines
Quoted 8 lines
Documentation/virt/kvm/api.rst +8
-0+ struct kvm_stats_desc { + __u32 flags; + __s16 exponent; + __u16 size; + __u32 offset; + __u32 unused; + char name[0]; + };
Re: [PATCH v9 3/5] KVM: stats: Add documentation for statistics data binary interface
Quoted 22 lines · Paolo Bonzini
Quoted 10 lines · Greg KH
I forgot to comment on this one, sorry for the delay.
Why are you "inventing" your own schema format here for this? Why not use a well-known or at least well-designed/implemented one that we have in userspace already?
There are a few that I would love to see in the kernel, varlink being the best example. We have kernel examples of this and I would consider using that as a transport for sysfs-like data in the future, but never got around to it.
Thanks, that's a good observation, and it's a problem that the rationale and the design process didn't end up in either the documentation or the commit message (only the outcome did). In order to fix that, this is going to be quite a long message.
Varlink comprises both a schema and an encoding, and each has its own problems. For the encoding of the data, varlink is really just using JSON and it is really just the wrong tool here. The first few problems that come to mind are:
Quoted 4 lines
- varlink structs are encoded as JSON dictionaries. Therefore, every time userspace reads the fields, the kernel has to include the field names as JSON dictionary keys. This means that a lot of time is spent writing buffers, and on the receiving side parsing them.
Has this been measured? Years ago when I messed with this, it was in the noise as JSON parsing is really fast these days.
Quoted 3 lines
- because numeric data has to be converted to ASCII the output doesn't have fixed offsets, so it is not possible to make an efficient implementation of pread.
efficient where? In the kernel?
Quoted 6 lines
- even though Varlink specifies that int is "usually int64", a little-known gem is that JSON behavior for numbers not representable as a double (i.e. exceeding 2^53) is implementation-defined; implementations can and will mess up values outside that range. This problem is not specific to this KVM stats usecase; varlink's schema specification really is at odds with its encoding specification.
That's interesting, do the varlink developers know this? And we can say "for the kernel int will be int64" and be done with it, so this shouldn't be that big of an issue.
Quoted 6 lines
For the schema, there are some specific problems with varlink, but also a more generic issue. The specific problems are:
- the schema doesn't include the length of arrays. This makes it hard to compute in advance lengths and offsets of fields (even ignoring the fact that data is not binary, which I'll get to later)
Do you care in advance?
Quoted 2 lines
- the schema also is not extensible with user annotations. In our case that would be mostly the unit in which the value is expressed.
units would be nice, I agree.
Quoted 9 lines
The main problem with the various available serialization formats is that their schema languages are designed to be compiled with a code generator. Here instead the schema is transmitted from the kernel to userspace. Userspace is not really supposed to know what a value means or even that if it exists. Userspace takes care of collecting the data from the kernel, but ultimately there will be a human that knows what e.g. "
io_exits" or "req_event" means, and they will ask for the current value, or a plot over time, of a specific statistic.
Quoted 5 lines
Now, unlike most other schema languages, varlink does not require precompiling the schema in its C bindings. However, this was mostly just the authors not bothering to write a C code generator---they did write one for Rust, for example. When using the "official" varlink C bindings, you hardly use the schema at all.
I don't think the kernel implementation needs this, you can just specify the schema "on the fly" and it should work. Or maybe I'm wrong, it's been a few years.
Quoted 7 lines
Something similar to the above issues is quite common in other formats. For example, FlatBuffers[2]'s schema language[1] does have annotations (which it calls metadata), but then we would still have to invent some standard annotations and teach programs about them. However, the deal breaker is that again there is no way to transmit the schema from the server to the client, and that users are supposed to precompile the schema using a code generator.
Again, I didn't think this was an issue with the kernel implementation in that the userspace side could determine the schema by the data coming from the kernel, it wouldn't have to "know" about it ahead of time.
But I could be wrong.
Quoted 41 lines
All that said, what we _could_ do is serialize the schema as JSON instead of using a binary format, like so:
[{ "name": "exits", "kind": "cumulative", "count": 1 }, { "name": "halt_poll_fail_ns", "kind": "cumulative", "unit": "seconds", "scale": {"base": "pow2", exponent: -9}, "count": 1 }, ...]while keeping the actual statistics as an array of u64 values just like in these patches. The JSON representation of the schema would be always the same, so it could be treated as fixed-size and an efficient implementation of pread would be possible. And once the schema is JSON, there could be a *meta*-schema expressed using the varlink language:
# compare to struct kvm_stats_desc, included after my sig # for reference type StatsDescriptor { name: string, kind: (cumulative, instant, peak), unit: ?(bytes, seconds, cycles), scale: ?(base: (pow2, pow10), exponent: int), count: int }type StatsSchema { name: string, stats: []StatsDescriptor }Varlink would *not* be used in the kernel, because Varlink isreally just JSON. The above ten lines (plus comments) would be dropped in
Documentation/as a .varlink file; they would be a convenient way to describe (to either a human or a program) how to parse the JSON schema.
Quoted 5 lines
There would then be another problem, namely how to include the varlink schema in the generated documentation, since right now there are for example no sphinx bindings for varlink. Nevertheless, if you think it's better to have the schema as JSON instead of binary, according to the above varlink meta-schema, then we can look into it.
It should be in some standard format. I'm not wed to varlink, but I thought it solved some of your issues here with userspace not knowing the schema ahead of time which is the key as it will change over time and the kernel should be 'self-describing'.
If not, and it sounds like you have looked into it, or at least the userspace side, then that's fine.
But you should write up a justification somewhere why you didn't use an existing format (what about the netlink format?) in at least the changelog, like you did here. As you will be stuck with this api for the next 20+ years, it's good to be confident in it :)
maybe someday we can get varlink into the tree, as I still think it would be better than the ad-hoc implementations we currently have.
thanks,
greg k-h
Paolo Bonzini
maintainerRe: [PATCH v9 3/5] KVM: stats: Add documentation for statistics data binary interface
Quoted 8 lines · Greg KH
Quoted 4 lines · Paolo Bonzini
- varlink structs are encoded as JSON dictionaries. Therefore, every time userspace reads the fields, the kernel has to include the field names as JSON dictionary keys. This means that a lot of time is spent writing buffers, and on the receiving side parsing them.
Has this been measured? Years ago when I messed with this, it was in the noise as JSON parsing is really fast these days.
Yes, parsing is really fast. However, the work doesn't end at building an in-memory representation. An efficient representation (and a schema that is negotiated in advance) makes it possible to do this work as late and as little as possible, instead of doing it on every fetch of the statistics.
For cloud vendors running virtual machines, they want to consolidate housekeeping tasks on as few CPUs as possible (because housekeeping CPUs cannot be billed to the customers), and every millisecond really counts. ASCII is inviting, but things like Protobufs, FlatBuffers, Cap'n'Proto are all binary because all those hidden costs do exist.
Quoted 5 lines
Quoted 3 lines
- because numeric data has to be converted to ASCII the output doesn't have fixed offsets, so it is not possible to make an efficient implementation of pread.
efficient where? In the kernel?
Yes, Jing's patches can just do a quick copy_to_user if pread does not access the schema. And it's very simple code too.
Quoted 7 lines
Quoted 3 lines
- even though Varlink specifies that int is "usually int64", a little-known gem is that JSON behavior for numbers not representable as a double (i.e. exceeding 2^53) is implementation-defined
That's interesting, do the varlink developers know this? And we can say "for the kernel int will be int64" and be done with it, so this shouldn't be that big of an issue.
Well yeah, but there's still the problem of what the other side thinks.
In the end varlink's interesting because it's just JSON, meaning
there's plenty of parsers available---but they all too often don't separate int vs. double. We had this issue with projects talking to QEMU (which has been using JSON the same way as varlink for ten years or so) and JSON parsers returning an overflow for 2^64-1 (because it rounds to 2^64) or an incorrect value. I'm not saying it's a showstopper, it's just an unavoidable ugliness if you pick JSON.
Quoted 8 lines
Quoted 6 lines
For the schema, there are some specific problems with varlink, but also a more generic issue. The specific problems are:
- the schema doesn't include the length of arrays. This makes it hard to compute in advance lengths and offsets of fields (even ignoring the fact that data is not binary, which I'll get to later)
Do you care in advance?
Yes, once we add for example histograms we would like to include in the schema the size and number of the buckets.
Quoted 4 lines
Again, I didn't think this was an issue with the kernel implementation in that the userspace side could determine the schema by the data coming from the kernel, it wouldn't have to "know" about it ahead of time. But I could be wrong.
No, you're right. The C implementations are really just very thin wrappers over JSON. There's very little "Varlink"ness in them.
However the interesting part of the schema are the metadata--the unit, whether something is an instant vs. a cumulative value, the bucket size when we add histograms. These things are obviously not included in the data and must be communicated separately. Userspace tools could also use a schema to validate user requests ("record the halt_poll_fail_ns every second").
Quoted 7 lines
Quoted 2 lines
All that said, what we _could_ do is serialize the schema as JSON instead of using a binary format
It should be in some standard format. If not, and it sounds like you have looked into it, or at least the userspace side, then that's fine. But you should write up a justification somewhere why you didn't use an existing format (what about the netlink format?)
I guess you're talking about NETLINK_GENERIC, that also has the issue that the schema (the attributes) is not dynamic but rather part of the uAPI. We explicitly don't want them to be stable, they're like tracepoints in that respect and that's why we took ideas from trace-cmd.
Anyway, as a start Jing will summarize all these discussions in v10.
Thanks,
Paolo
PATCH v9 4/5 KVM: selftests: Add selftest for KVM statistics data binary interface
In reply to Jing ZhangAdd selftest to check KVM stats descriptors validity.
--- tools/testing/selftests/kvm/.gitignore | 1 + tools/testing/selftests/kvm/Makefile | 3 + .../testing/selftests/kvm/include/kvm_util.h | 3 + .../selftests/kvm/kvm_binary_stats_test.c | 225 ++++++++++++++++++ tools/testing/selftests/kvm/lib/kvm_util.c | 12 + 5 files changed, 244 insertions(+) create mode 100644 tools/testing/selftests/kvm/kvm_binary_stats_test.c
tools/testing/selftests/kvm/.gitignore +1 -0
@@ -46,3 +46,4 @@ /memslot_perf_test /set_memory_region_test /steal_time +/kvm_binary_stats_test
tools/testing/selftests/kvm/Makefile +3 -0
@@ -79,6 +79,7 @@ TEST_GEN_PROGS_x86_64 += memslot_modification_stress_test TEST_GEN_PROGS_x86_64 += memslot_perf_test TEST_GEN_PROGS_x86_64 += set_memory_region_test TEST_GEN_PROGS_x86_64 += steal_time +TEST_GEN_PROGS_x86_64 += kvm_binary_stats_test TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list TEST_GEN_PROGS_aarch64 += aarch64/get-reg-list-sve
@@ -90,6 +91,7 @@ TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus TEST_GEN_PROGS_aarch64 += kvm_page_table_test TEST_GEN_PROGS_aarch64 += set_memory_region_test TEST_GEN_PROGS_aarch64 += steal_time +TEST_GEN_PROGS_aarch64 += kvm_binary_stats_test TEST_GEN_PROGS_s390x = s390x/memop TEST_GEN_PROGS_s390x += s390x/resets
@@ -99,6 +101,7 @@ TEST_GEN_PROGS_s390x += dirty_log_test TEST_GEN_PROGS_s390x += kvm_create_max_vcpus TEST_GEN_PROGS_s390x += kvm_page_table_test TEST_GEN_PROGS_s390x += set_memory_region_test +TEST_GEN_PROGS_s390x += kvm_binary_stats_test TEST_GEN_PROGS += $(TEST_GEN_PROGS_$(UNAME_M)) LIBKVM += $(LIBKVM_$(UNAME_M))
tools/testing/selftests/kvm/include/kvm_util.h +3 -0
@@ -391,4 +391,7 @@ uint64_t get_ucall(struct kvm_vm *vm, uint32_t vcpu_id, struct ucall *uc);#define GUEST_ASSERT_4 (_condition, arg1, arg2, arg3, arg4) \ __GUEST_ASSERT((_condition), 4, (arg1), (arg2), (arg3), (arg4)) +int vm_get_stats_fd (struct kvm_vm *vm); +int vcpu_get_stats_fd (struct kvm_vm *vm,uint32_t vcpuid); +#endif /* SELFTEST_KVM_UTIL_H */
tools/testing/selftests/kvm/kvm_binary_stats_test.c new file +224 -0
224 lines not on this page · raw message
tools/testing/selftests/kvm/lib/kvm_util.c +12 -0
@@ -2286,3 +2286,15 @@ unsigned int vm_calc_num_guest_pages(enum vm_guest_mode mode, size_t size) n =DIV_ROUND_UP (size, vm_guest_mode_params[mode].page_size);return vm_adjust_num_guest_pages (mode, n); } + +int vm_get_stats_fd (struct kvm_vm *vm) +{ +return ioctl (vm->fd,KVM_GET_STATS_FD ,NULL ); +} + +int vcpu_get_stats_fd (struct kvm_vm *vm,uint32_t vcpuid) +{ +struct vcpu *vcpu =vcpu_find (vm, vcpuid); + +return ioctl (vcpu->fd,KVM_GET_STATS_FD ,NULL ); +}
Signature · 1 line
2.32.0.272.g935e593368-goog
Re: [PATCH v9 4/5] KVM: selftests: Add selftest for KVM statistics data binary interface
Hi Jing,
Quoted 11 lines
tools/testing/selftests/kvm/kvm_binary_stats_test.c new file +11
-0+int main (int argc,char *argv[]) +{ +int max_vm =DEFAULT_NUM_VM , max_vcpu =DEFAULT_NUM_VCPU , ret, i, j; +struct kvm_vm **vms; + +/* Get the number of VMs and VCPUs that would be created for testing. */ +if (argc >1 ) { + max_vm =strtol (argv[1 ],NULL ,0 ); +if (max_vm <=0 ) + max_vm =DEFAULT_NUM_VM ; + }
tools/testing/selftests/kvm/kvm_binary_stats_test.c line 201
Quoted 10 lines
tools/testing/selftests/kvm/kvm_binary_stats_test.c new file +10
-0+if (argc >2 ) { + max_vcpu =strtol (argv[2 ],NULL ,0 ); +if (max_vcpu <=0 ) + max_vcpu =DEFAULT_NUM_VCPU ; + } + +/* Check the extension for binary stats */ + ret =kvm_check_cap (KVM_CAP_BINARY_STATS_FD ); +TEST_ASSERT (ret >=0 , +"Binary form statistics interface is not supported" );
kvm_check_cap returns the value of KVM_CHECK_EXTENSION, which is 0 if unsupported (-ERROR on an error). The assertion should be for ret > 0.
Made that change locally, and tested it with various configurations (vhe, nvhe), as well as kernel versions (with and without KVM_CAP_BINARY_STATS_FD), and it passes (or fails as expected). Without that fix and with a kernel that doesn't support KVM_CAP_BINARY_STATS_FD, it passes that assertion, but fails later at vcpu_stats_test().
With that fixed:
Cheers,
/fuad
Quoted 46 lines
tools/testing/selftests/kvm/kvm_binary_stats_test.c new file +23
-0+ +/* Create VMs and VCPUs */ + vms =malloc (sizeof (vms[0 ]) * max_vm); +TEST_ASSERT (vms,"Allocate memory for storing VM pointers" ); +for (i =0 ; i < max_vm; ++i) { + vms[i] =vm_create (VM_MODE_DEFAULT , +DEFAULT_GUEST_PHY_PAGES ,O_RDWR ); +for (j =0 ; j < max_vcpu; ++j) +vm_vcpu_add (vms[i], j); + } + +/* Check stats read for every VM and VCPU */ +for (i =0 ; i < max_vm; ++i) { +vm_stats_test (vms[i]); +for (j =0 ; j < max_vcpu; ++j) +vcpu_stats_test (vms[i], j); + } + +for (i =0 ; i < max_vm; ++i) +kvm_vm_free (vms[i]); +free (vms); +return 0 ; +}tools/testing/selftests/kvm/lib/kvm_util.c +12
-1@@ -2286,3 +2286,15 @@ unsigned int vm_calc_num_guest_pages(enum vm_guest_mode mode, size_t size) n =DIV_ROUND_UP (size, vm_guest_mode_params[mode].page_size);return vm_adjust_num_guest_pages (mode, n); } + +int vm_get_stats_fd (struct kvm_vm *vm) +{ +return ioctl (vm->fd,KVM_GET_STATS_FD ,NULL ); +} + +int vcpu_get_stats_fd (struct kvm_vm *vm,uint32_t vcpuid) +{ +struct vcpu *vcpu =vcpu_find (vm, vcpuid); + +return ioctl (vcpu->fd,KVM_GET_STATS_FD ,NULL ); +} --2.32.0.272.g935e593368-goog
Re: [PATCH v9 4/5] KVM: selftests: Add selftest for KVM statistics data binary interface
Hi Fuad,
Quoted 35 lines · Fuad Tabba
Hi Jing,
Quoted 11 lines
tools/testing/selftests/kvm/kvm_binary_stats_test.c new file +11
-0+int main (int argc,char *argv[]) +{ +int max_vm =DEFAULT_NUM_VM , max_vcpu =DEFAULT_NUM_VCPU , ret, i, j; +struct kvm_vm **vms; + +/* Get the number of VMs and VCPUs that would be created for testing. */ +if (argc >1 ) { + max_vm =strtol (argv[1 ],NULL ,0 ); +if (max_vm <=0 ) + max_vm =DEFAULT_NUM_VM ; + }tools/testing/selftests/kvm/kvm_binary_stats_test.c line 201
Quoted 10 lines
tools/testing/selftests/kvm/kvm_binary_stats_test.c new file +10
-0+if (argc >2 ) { + max_vcpu =strtol (argv[2 ],NULL ,0 ); +if (max_vcpu <=0 ) + max_vcpu =DEFAULT_NUM_VCPU ; + } + +/* Check the extension for binary stats */ + ret =kvm_check_cap (KVM_CAP_BINARY_STATS_FD ); +TEST_ASSERT (ret >=0 , +"Binary form statistics interface is not supported" );
kvm_check_capreturns the value ofKVM_CHECK_EXTENSION, which is 0 if unsupported (-ERRORon an error). The assertion should be for ret > 0.Made that change locally, and tested it with various configurations (vhe, nvhe), as well as kernel versions (with and without
KVM_CAP_BINARY_STATS_FD), and it passes (or fails as expected). Without that fix and with a kernel that doesn't supportKVM_CAP_BINARY_STATS_FD, it passes that assertion, but fails later atvcpu_stats_test().
Quoted 7 lines
With that fixed:
Tested-by Fuad Tabba <[email protected]> #arm64Cheers,
/fuad
Thanks for the review and testing. Will fix it.
Quoted 38 lines
Quoted 38 lines
tools/testing/selftests/kvm/kvm_binary_stats_test.c new file +23
-0+ +/* Create VMs and VCPUs */ + vms =malloc (sizeof (vms[0 ]) * max_vm); +TEST_ASSERT (vms,"Allocate memory for storing VM pointers" ); +for (i =0 ; i < max_vm; ++i) { + vms[i] =vm_create (VM_MODE_DEFAULT , +DEFAULT_GUEST_PHY_PAGES ,O_RDWR ); +for (j =0 ; j < max_vcpu; ++j) +vm_vcpu_add (vms[i], j); + } + +/* Check stats read for every VM and VCPU */ +for (i =0 ; i < max_vm; ++i) { +vm_stats_test (vms[i]); +for (j =0 ; j < max_vcpu; ++j) +vcpu_stats_test (vms[i], j); + } + +for (i =0 ; i < max_vm; ++i) +kvm_vm_free (vms[i]); +free (vms); +return 0 ; +}tools/testing/selftests/kvm/lib/kvm_util.c +7
-0@@ -2286,3 +2286,15 @@ unsigned int vm_calc_num_guest_pages(enum vm_guest_mode mode, size_t size) n =DIV_ROUND_UP (size, vm_guest_mode_params[mode].page_size);return vm_adjust_num_guest_pages (mode, n); } + +int vm_get_stats_fd (struct kvm_vm *vm) +{ +return ioctl (vm->fd,KVM_GET_STATS_FD ,NULL ); +} + +int vcpu_get_stats_fd (struct kvm_vm *vm,uint32_t vcpuid)
Quoted 8 lines
Quoted 8 lines
tools/testing/selftests/kvm/lib/kvm_util.c +5
-1+{ +struct vcpu *vcpu =vcpu_find (vm, vcpuid); + +return ioctl (vcpu->fd,KVM_GET_STATS_FD ,NULL ); +} --2.32.0.272.g935e593368-goog
Thank,
Jing
PATCH v9 5/5 KVM: stats: Remove code duplication for binary and debugfs stats
In reply to Jing ZhangTo remove code duplication, use the binary stats descriptors for debugfs interface for KVM stats. Then we only have one stats definitions for both binary and debugfs interface.
--- arch/arm64/kvm/guest.c | 16 ------ arch/mips/kvm/mips.c | 39 -------------- arch/powerpc/kvm/book3s.c | 33 ------------ arch/powerpc/kvm/booke.c | 25 --------- arch/s390/kvm/kvm-s390.c | 108 -------------------------------------- arch/x86/kvm/x86.c | 49 +---------------- include/linux/kvm_host.h | 43 +++++---------- include/uapi/linux/kvm.h | 4 +- virt/kvm/kvm_main.c | 104 ++++++++++++++++++++++++++---------- 9 files changed, 94 insertions(+), 327 deletions(-)
arch/arm64/kvm/guest.c +0 -16
@@ -66,22 +66,6 @@ struct _kvm_stats_header kvm_vcpu_stats_header = { } }; -struct kvm_stats_debugfs_item debugfs_entries[] = { -VCPU_STAT_GENERIC ("halt_successful_poll" , halt_successful_poll), -VCPU_STAT_GENERIC ("halt_attempted_poll" , halt_attempted_poll), -VCPU_STAT_GENERIC ("halt_poll_invalid" , halt_poll_invalid), -VCPU_STAT_GENERIC ("halt_wakeup" , halt_wakeup), -VCPU_STAT ("hvc_exit_stat" , hvc_exit_stat), -VCPU_STAT ("wfe_exit_stat" , wfe_exit_stat), -VCPU_STAT ("wfi_exit_stat" , wfi_exit_stat), -VCPU_STAT ("mmio_exit_user" , mmio_exit_user), -VCPU_STAT ("mmio_exit_kernel" , mmio_exit_kernel), -VCPU_STAT ("exits" , exits), -VCPU_STAT_GENERIC ("halt_poll_success_ns" , halt_poll_success_ns), -VCPU_STAT_GENERIC ("halt_poll_fail_ns" , halt_poll_fail_ns), - {NULL } -}; -static bool core_reg_offset_is_vreg (u64 off) { return off >= KVM_REG_ARM_CORE_REG(fp_regs.vregs) &&
arch/mips/kvm/mips.c +0 -39
@@ -99,45 +99,6 @@ struct _kvm_stats_header kvm_vcpu_stats_header = { } }; -struct kvm_stats_debugfs_item debugfs_entries[] = { -VCPU_STAT ("wait" , wait_exits), -VCPU_STAT ("cache" , cache_exits), -VCPU_STAT ("signal" , signal_exits), -VCPU_STAT ("interrupt" , int_exits), -VCPU_STAT ("cop_unusable" , cop_unusable_exits), -VCPU_STAT ("tlbmod" , tlbmod_exits), -VCPU_STAT ("tlbmiss_ld" , tlbmiss_ld_exits), -VCPU_STAT ("tlbmiss_st" , tlbmiss_st_exits), -VCPU_STAT ("addrerr_st" , addrerr_st_exits), -VCPU_STAT ("addrerr_ld" , addrerr_ld_exits), -VCPU_STAT ("syscall" , syscall_exits), -VCPU_STAT ("resvd_inst" , resvd_inst_exits), -VCPU_STAT ("break_inst" , break_inst_exits), -VCPU_STAT ("trap_inst" , trap_inst_exits), -VCPU_STAT ("msa_fpe" , msa_fpe_exits), -VCPU_STAT ("fpe" , fpe_exits), -VCPU_STAT ("msa_disabled" , msa_disabled_exits), -VCPU_STAT ("flush_dcache" , flush_dcache_exits), -VCPU_STAT ("vz_gpsi" , vz_gpsi_exits), -VCPU_STAT ("vz_gsfc" , vz_gsfc_exits), -VCPU_STAT ("vz_hc" , vz_hc_exits), -VCPU_STAT ("vz_grr" , vz_grr_exits), -VCPU_STAT ("vz_gva" , vz_gva_exits), -VCPU_STAT ("vz_ghfc" , vz_ghfc_exits), -VCPU_STAT ("vz_gpa" , vz_gpa_exits), -VCPU_STAT ("vz_resvd" , vz_resvd_exits), -#ifdef CONFIG_CPU_LOONGSON64 -VCPU_STAT ("vz_cpucfg" , vz_cpucfg_exits), -#endif -VCPU_STAT_GENERIC ("halt_successful_poll" , halt_successful_poll), -VCPU_STAT_GENERIC ("halt_attempted_poll" , halt_attempted_poll), -VCPU_STAT_GENERIC ("halt_poll_invalid" , halt_poll_invalid), -VCPU_STAT_GENERIC ("halt_wakeup" , halt_wakeup), -VCPU_STAT_GENERIC ("halt_poll_success_ns" , halt_poll_success_ns), -VCPU_STAT_GENERIC ("halt_poll_fail_ns" , halt_poll_fail_ns), - {NULL } -}; -bool kvm_trace_guest_mode_change;int kvm_guest_mode_change_trace_reg (void )
arch/powerpc/kvm/book3s.c +0 -33
@@ -102,39 +102,6 @@ struct _kvm_stats_header kvm_vcpu_stats_header = { } }; -struct kvm_stats_debugfs_item debugfs_entries[] = { -VCPU_STAT ("exits" , sum_exits), -VCPU_STAT ("mmio" , mmio_exits), -VCPU_STAT ("sig" , signal_exits), -VCPU_STAT ("sysc" , syscall_exits), -VCPU_STAT ("inst_emu" , emulated_inst_exits), -VCPU_STAT ("dec" , dec_exits), -VCPU_STAT ("ext_intr" , ext_intr_exits), -VCPU_STAT ("queue_intr" , queue_intr), -VCPU_STAT_GENERIC ("halt_poll_success_ns" , halt_poll_success_ns), -VCPU_STAT_GENERIC ("halt_poll_fail_ns" , halt_poll_fail_ns), -VCPU_STAT ("halt_wait_ns" , halt_wait_ns), -VCPU_STAT_GENERIC ("halt_successful_poll" , halt_successful_poll), -VCPU_STAT_GENERIC ("halt_attempted_poll" , halt_attempted_poll), -VCPU_STAT ("halt_successful_wait" , halt_successful_wait), -VCPU_STAT_GENERIC ("halt_poll_invalid" , halt_poll_invalid), -VCPU_STAT_GENERIC ("halt_wakeup" , halt_wakeup), -VCPU_STAT ("pf_storage" , pf_storage), -VCPU_STAT ("sp_storage" , sp_storage), -VCPU_STAT ("pf_instruc" , pf_instruc), -VCPU_STAT ("sp_instruc" , sp_instruc), -VCPU_STAT ("ld" , ld), -VCPU_STAT ("ld_slow" , ld_slow), -VCPU_STAT ("st" , st), -VCPU_STAT ("st_slow" , st_slow), -VCPU_STAT ("pthru_all" , pthru_all), -VCPU_STAT ("pthru_host" , pthru_host), -VCPU_STAT ("pthru_bad_aff" , pthru_bad_aff), - VM_STAT("largepages_2M", num_2M_pages, .mode = 0444), - VM_STAT("largepages_1G", num_1G_pages, .mode = 0444), - {NULL } -}; -static inline void kvmppc_update_int_pending (struct kvm_vcpu *vcpu,unsigned long pending_now,unsigned long old_pending) {
arch/powerpc/kvm/booke.c +0 -25
@@ -93,31 +93,6 @@ struct _kvm_stats_header kvm_vcpu_stats_header = { } }; -struct kvm_stats_debugfs_item debugfs_entries[] = { -VCPU_STAT ("mmio" , mmio_exits), -VCPU_STAT ("sig" , signal_exits), -VCPU_STAT ("itlb_r" , itlb_real_miss_exits), -VCPU_STAT ("itlb_v" , itlb_virt_miss_exits), -VCPU_STAT ("dtlb_r" , dtlb_real_miss_exits), -VCPU_STAT ("dtlb_v" , dtlb_virt_miss_exits), -VCPU_STAT ("sysc" , syscall_exits), -VCPU_STAT ("isi" , isi_exits), -VCPU_STAT ("dsi" , dsi_exits), -VCPU_STAT ("inst_emu" , emulated_inst_exits), -VCPU_STAT ("dec" , dec_exits), -VCPU_STAT ("ext_intr" , ext_intr_exits), -VCPU_STAT_GENERIC ("halt_successful_poll" , halt_successful_poll), -VCPU_STAT_GENERIC ("halt_attempted_poll" , halt_attempted_poll), -VCPU_STAT_GENERIC ("halt_poll_invalid" , halt_poll_invalid), -VCPU_STAT_GENERIC ("halt_wakeup" , halt_wakeup), -VCPU_STAT ("doorbell" , dbell_exits), -VCPU_STAT ("guest doorbell" , gdbell_exits), -VCPU_STAT_GENERIC ("halt_poll_success_ns" , halt_poll_success_ns), -VCPU_STAT_GENERIC ("halt_poll_fail_ns" , halt_poll_fail_ns), -VM_STAT_GENERIC ("remote_tlb_flush" , remote_tlb_flush), - {NULL } -}; -/* TODO: use vcpu_printf() */ void kvmppc_dump_vcpu (struct kvm_vcpu *vcpu) {
arch/s390/kvm/kvm-s390.c +0 -108
114 lines not on this page · raw message
arch/x86/kvm/x86.c +1 -48
61 lines not on this page · raw message
include/linux/kvm_host.h +14 -29
78 lines not on this page · raw message
include/uapi/linux/kvm.h +3 -1
@@ -1920,7 +1920,9 @@ struct kvm_stats_header {#define KVM_STATS_TYPE_MASK (0xF << KVM_STATS_TYPE_SHIFT)#define KVM_STATS_TYPE_CUMULATIVE (0x0 << KVM_STATS_TYPE_SHIFT)#define KVM_STATS_TYPE_INSTANT (0x1 << KVM_STATS_TYPE_SHIFT) -#define KVM_STATS_TYPE_MAX KVM_STATS_TYPE_INSTANT +/* Peak type is used as read/write instant counters */ +#define KVM_STATS_TYPE_PEAK (0x2 << KVM_STATS_TYPE_SHIFT) +#define KVM_STATS_TYPE_MAX KVM_STATS_TYPE_PEAK#define KVM_STATS_UNIT_SHIFT 4#define KVM_STATS_UNIT_MASK (0xF << KVM_STATS_UNIT_SHIFT)
virt/kvm/kvm_main.c +76 -28
209 lines not on this page · raw message
Signature · 1 line
2.32.0.272.g935e593368-goog
Quoted 9 lines · Jing Zhang
This patchset provides a file descriptor for every VM and VCPU to read KVM statistics data in binary format.
It is meant to provide a lightweight, flexible, scalable and efficient lock-free solution for user space telemetry applications to pull the statistics data periodically for large scale systems. The pulling frequency could be as high as a few times per second.
In this patchset, every statistics data are treated to have some attributes as below:
* architecture dependent or generic
Quoted 8 lines
* VM statistics data or VCPU statistics data
* type: cumulative, instantaneous, peak
* unit: none for simple counter, nanosecond, microsecond, millisecond, second, Byte, KiByte, MiByte, GiByte. Clock Cycles
Since no lock/synchronization is used, the consistency between all the statistics data is not guaranteed. That means not all statistics data are read out at the exact same time, since the statistics date are still being updated by KVM subsystems while they are read out.
Sorry for my naive questions, but how does telemetry get statistics for hypervisors? Why is KVM different from hypervisors or NIC's statistics or any other high speed devices (RDMA) that generate tons of data?
Thanks
Paolo Bonzini
maintainerQuoted 3 lines · Leon Romanovsky
Sorry for my naive questions, but how does telemetry get statistics for hypervisors? Why is KVM different from hypervisors or NIC's statistics or any other high speed devices (RDMA) that generate tons of data?
Right now, the only way is debugfs but it's slow, and it's disabled when using lockdown mode; this series is a way to fix this.
I sense that there is another question in there; are you wondering if another mechanism should be used, for example netlink? The main issue there is how to identify a VM, since KVM file descriptors don't have a name. Using a pid works (sort of) for debugfs, but pids are not appropriate for a stable API. Using a file descriptor as in this series requires collaboration from the userspace program; howver, once the file descriptor has been transmitted via SCM_RIGHTS, telemetry can read it forever without further IPC, and there is proper privilege separation.
Paolo
Quoted 8 lines · Paolo Bonzini
Quoted 3 lines · Leon Romanovsky
Sorry for my naive questions, but how does telemetry get statistics for hypervisors? Why is KVM different from hypervisors or NIC's statistics or any other high speed devices (RDMA) that generate tons of data?
Right now, the only way is debugfs but it's slow, and it's disabled when using lockdown mode; this series is a way to fix this.
Quoted 8 lines
I sense that there is another question in there; are you wondering if another mechanism should be used, for example netlink? The main issue there is how to identify a VM, since KVM file descriptors don't have a name. Using a pid works (sort of) for debugfs, but pids are not appropriate for a stable API. Using a file descriptor as in this series requires collaboration from the userspace program; howver, once the file descriptor has been transmitted via
SCM_RIGHTS, telemetry can read it forever without further IPC, and there is proper privilege separation.
Yeah, sorry for mixing different questions into one.
So the answer to the question "why KVM is different" is that it doesn't have any stable identification except file descriptor. While hypervisors have stable names, NICs and RDMA devices have interface indexes e.t.c. Did I get it right?
And this was second part of my question, the first part was my attempt to get on answer why current statistics like process info (/proc/xxx/*), NICs (netlink) and RDMA (sysfs) are not using binary format.
Thanks
Quoted 3 lines
Paolo
Paolo Bonzini
maintainerQuoted 8 lines · Leon Romanovsky
Quoted 3 lines
Sorry for my naive questions, but how does telemetry get statistics for hypervisors? Why is KVM different from hypervisors or NIC's statistics or any other high speed devices (RDMA) that generate tons of data?
So the answer to the question "why KVM is different" is that it doesn't have any stable identification except file descriptor. While hypervisors have stable names, NICs and RDMA devices have interface indexes etc. Did I get it right?
Right.
Quoted 3 lines
And this was second part of my question, the first part was my attempt to get on answer why current statistics like process info (
/proc/xxx/*), NICs (netlink) and RDMA (sysfs) are not using binary format.
NICs are using binary format (partly in struct ethtool_stats, partly in an array of u64). For KVM we decided to put the schema and the stats in the same file (though you can use pread to get only the stats) to have a single interface and avoid ioctls, unlike having both ETH_GSTRINGS and ETH_GSTATS.
I wouldn't say processes are using any specific format. There's a mix of "one value per file" (e.g. cpuset), human-readable tabular format (e.g. limits, sched), human- and machine-readable tabular format (e.g. status), and files that are ASCII but not human-readable (e.g. stat).
Paolo
Quoted 21 lines · Paolo Bonzini
Quoted 8 lines · Leon Romanovsky
Quoted 3 lines
Sorry for my naive questions, but how does telemetry get statistics for hypervisors? Why is KVM different from hypervisors or NIC's statistics or any other high speed devices (RDMA) that generate tons of data?
So the answer to the question "why KVM is different" is that it doesn't have any stable identification except file descriptor. While hypervisors have stable names, NICs and RDMA devices have interface indexes etc. Did I get it right?
Right.
Quoted 3 lines
And this was second part of my question, the first part was my attempt to get on answer why current statistics like process info (
/proc/xxx/*), NICs (netlink) and RDMA (sysfs) are not using binary format.NICs are using binary format (partly in struct
ethtool_stats, partly in an array of u64). For KVM we decided to put the schema and the stats in the same file (though you can use pread to get only the stats) to have a single interface and avoid ioctls, unlike having bothETH_GSTRINGSandETH_GSTATS.
Quoted 4 lines
I wouldn't say processes are using any specific format. There's a mix of "one value per file" (e.g. cpuset), human-readable tabular format (e.g. limits, sched), human- and machine-readable tabular format (e.g. status), and files that are ASCII but not human-readable (e.g. stat).
I see, your explanation to Enrico cleared the mud.
Thanks
Quoted 3 lines
Paolo
On 14.06.21 23:21, Jing Zhang wrote:
Hi,
Quoted 2 lines
This patchset provides a file descriptor for every VM and VCPU to read KVM statistics data in binary format.
I've missed the discussions of previous versions, so please forgive my stupid questions:
* why is it binary instead of text ? is it so very high volume that it really matters ?
* how will possible future extensions of the telemetry packets work ?
* aren't there other means to get this fd instead of an ioctl() on the VM fd ? something more from the outside (eg. sysfs/procfs)
* how will that relate to other hypervisors ?
Some notes from the operating perspective:
In typical datacenters we've got various monitoring tools that are able to catch up lots of data from different sources (especially files). If an operator e.g. is interested in something in happening in some file (e.g. in /proc of /sys), it's quite trivial - just configure yet another probe (maybe some regex for parsing) and done. Automatically fed in his $monitoring_solution (e.g. nagios, ELK, Splunk, whatsnot)
With your approach, it's not that simple: now the operator needs to create (and deploy and manage) a separate agent that somehow receives that fd from the VMM, reads and parses that specific binary stream and finally pushes it into the monitoring infrastructure. Or the VMM writes it into some file, where some monitoring agent can pick it up. In any case, not actually trivial from ops perspective.
In general I tend to like the fd approach (even though I don't like ioctls very much - I'd rather like to have it more Plan9-like ;-)). But it has the drawback of acquiring those fd's by separate processes isn't entirely easy and needs a lot of coordinated interaction.
That issue would be much easier if we had the ability to publish existing fd's into the file system (like Plan9's srvfs does), but we don't have that yet. (actually, I've hacked up some srvfs for Linux, but ... well ... it's just a hack, nowhere near to production).
Why not putting this into sysfs ?
I see two options:
a) if it's really kvm-specific (and no chance of using the same interface for other hypervisors), we could put it under the kvm device (/sys/class/misc/kvm).
b) have a generic VMM stats interface that theroretically could work with any hypervisor.
--mtx
Signature · 8 lines
--- Hinweis: unverschlüsselte E-Mails können leicht abgehört und manipuliert werden ! Für eine vertrauliche Kommunikation senden Sie bitte ihren GPG/PGP-Schlüssel zu. --- Enrico Weigelt, metux IT consult Free software and Linux embedded engineering [email protected] -- +49-151-27565287
Quoted 1 line · metux IT consult
Why not putting this into sysfs ?
Because sysfs is "one value per file".
Quoted 5 lines
I see two options:
a) if it's really kvm-specific (and no chance of using the same interface for other hypervisors), we could put it under the kvm device (
/sys/class/misc/kvm).
Again, that is NOT what sysfs is for.
Quoted 2 lines
b) have a generic VMM stats interface that theroretically could work with any hypervisor.
What other hypervisor matters?
greg k-h
Paolo Bonzini
maintainerQuoted 2 lines · metux IT consult
* why is it binary instead of text ? is it so very high volume that it really matters ?
The main reason to have a binary format is not the high volume actually (though it also has its part). Rather, we would really like to include the schema to make the statistics self-describing. This includes stuff like whether the unit of measure of a statistic is clock cycles, nanoseconds, pages or whatnot; having this kind of information in text leads to awkwardness in the parsers. trace-cmd is another example where the data consists of a schema followed by binary data.
Text format could certainly be added if there's a usecase, but for developer use debugfs is usually a suitable replacement.
Last year we tried the opposite direction: we built a one-value-per-file filesystem with a common API that any subsystem could use (e.g. providing ethtool stats, /proc/interrupts, etc. in addition to KVM stats). We started with text, similar to sysfs, with the plan of extending it to a binary format later. However, other subsystems expressed very little interest in this, so instead we decided to go with something that is designed around KVM needs.
Still, the binary format that KVM uses is designed not to be KVM-specific. If other subsystems want to publish high-volume, self-describing statistic information, they are welcome to share the binary format and the code. Perhaps it may make sense in some cases to have them in sysfs, even (e.g. /sys/kernel/slab/*/.stats). As Greg said sysfs is currently one value per file, but perhaps that could be changed if the binary format is an additional way to access the information and not the only one (not that I'm planning to do it).
Quoted 1 line
* how will possible future extensions of the telemetry packets work ?
The format includes a schema, so it's possible to add more statistics in the future. The exact list of statistics varies per architecture and is not part of the userspace API (obvious caveat: https://xkcd.com/1172/).
Quoted 2 lines
* aren't there other means to get this fd instead of an
ioctl()on the VM fd ? something more from the outside (eg. sysfs/procfs)
Not yet, but if there's a need it can be added. It'd be plausible to publish system-wide statistics via a ioctl on /dev/kvm, for example. We'd have to check how this compares with stuff that is world-readable in procfs and sysfs, but I don't think there are security concerns in exposing that.
There's also pidfd_getfd(2) which can be used to pull a VM file descriptor from another running process. That can be used to avoid the issue of KVM file descriptors being unnamed.
Quoted 1 line
* how will that relate to other hypervisors ?
Other hypervisors do not run as part of the Linux kernel (at least they are not upstream). These statistics only apply to Linux *hosts*, not guests.
As far as I know, there is no standard that Xen or the proprietary hypervisors use to communicate their telemetry info to monitoring tools, and also no standard binary format used by exporters to talk to monitoring tools. If this format will be adopted by other hypervisors or any random software, I will be happy.
Quoted 8 lines
Some notes from the operating perspective:
In typical datacenters we've got various monitoring tools that are able to catch up lots of data from different sources (especially files). If an operator e.g. is interested in something in happening in some file (e.g. in /proc of /sys), it's quite trivial - just configure yet another probe (maybe some regex for parsing) and done. Automatically fed in his $monitoring_solution (e.g. nagios, ELK, Splunk, whatsnot)
... but in practice what you do is you have prebuilt exporters that talks to $monitoring_solution. Monitoring individual files is the exception, not the rule. But indeed Libvirt already has I/O and network statistics and there is an exporter for Prometheus, so we should add support for this new method as well to both QEMU (exporting the file descriptor) and Libvirt.
I hope this helps clarifying your doubts!
Paolo
Quoted 6 lines
With your approach, it's not that simple: now the operator needs to create (and deploy and manage) a separate agent that somehow receives that fd from the VMM, reads and parses that specific binary stream and finally pushes it into the monitoring infrastructure. Or the VMM writes it into some file, where some monitoring agent can pick it up. In any case, not actually trivial from ops perspective.