Skip to content

Commit 1b870fa

Browse files
committed
kvm: stats: tell userspace which values are boolean
Some of the statistics values exported by KVM are always only 0 or 1. It can be useful to export this fact to userspace so that it can track them specially (for example by polling the value every now and then to compute a % of time spent in a specific state). Therefore, add "boolean value" as a new "unit". While it is not exactly a unit, it walks and quacks like one. In particular, using the type would be wrong because boolean values could be instantaneous or peak values (e.g. "is the rmap allocated?") or even two-bucket histograms (e.g. "number of posted vs. non-posted interrupt injections"). Suggested-by: Amneesh Singh <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 84e7051 commit 1b870fa

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

Documentation/virt/kvm/api.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5657,6 +5657,7 @@ by a string of size ``name_size``.
56575657
#define KVM_STATS_UNIT_BYTES (0x1 << KVM_STATS_UNIT_SHIFT)
56585658
#define KVM_STATS_UNIT_SECONDS (0x2 << KVM_STATS_UNIT_SHIFT)
56595659
#define KVM_STATS_UNIT_CYCLES (0x3 << KVM_STATS_UNIT_SHIFT)
5660+
#define KVM_STATS_UNIT_BOOLEAN (0x4 << KVM_STATS_UNIT_SHIFT)
56605661
#define KVM_STATS_UNIT_MAX KVM_STATS_UNIT_CYCLES
56615662

56625663
#define KVM_STATS_BASE_SHIFT 8
@@ -5724,6 +5725,11 @@ Bits 4-7 of ``flags`` encode the unit:
57245725
It indicates that the statistics data is used to measure time or latency.
57255726
* ``KVM_STATS_UNIT_CYCLES``
57265727
It indicates that the statistics data is used to measure CPU clock cycles.
5728+
* ``KVM_STATS_UNIT_BOOLEAN``
5729+
It indicates that the statistic will always be either 0 or 1. Boolean
5730+
statistics of "peak" type will never go back from 1 to 0. Boolean
5731+
statistics can be linear histograms (with two buckets) but not logarithmic
5732+
histograms.
57275733

57285734
Bits 8-11 of ``flags``, together with ``exponent``, encode the scale of the
57295735
unit:

arch/x86/kvm/x86.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ const struct _kvm_stats_desc kvm_vcpu_stats_desc[] = {
298298
STATS_DESC_COUNTER(VCPU, directed_yield_successful),
299299
STATS_DESC_COUNTER(VCPU, preemption_reported),
300300
STATS_DESC_COUNTER(VCPU, preemption_other),
301-
STATS_DESC_ICOUNTER(VCPU, guest_mode)
301+
STATS_DESC_IBOOLEAN(VCPU, guest_mode)
302302
};
303303

304304
const struct kvm_stats_header kvm_vcpu_stats_header = {

include/linux/kvm_host.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1822,6 +1822,15 @@ struct _kvm_stats_desc {
18221822
STATS_DESC_PEAK(SCOPE, name, KVM_STATS_UNIT_NONE, \
18231823
KVM_STATS_BASE_POW10, 0)
18241824

1825+
/* Instantaneous boolean value, read only */
1826+
#define STATS_DESC_IBOOLEAN(SCOPE, name) \
1827+
STATS_DESC_INSTANT(SCOPE, name, KVM_STATS_UNIT_BOOLEAN, \
1828+
KVM_STATS_BASE_POW10, 0)
1829+
/* Peak (sticky) boolean value, read/write */
1830+
#define STATS_DESC_PBOOLEAN(SCOPE, name) \
1831+
STATS_DESC_PEAK(SCOPE, name, KVM_STATS_UNIT_BOOLEAN, \
1832+
KVM_STATS_BASE_POW10, 0)
1833+
18251834
/* Cumulative time in nanosecond */
18261835
#define STATS_DESC_TIME_NSEC(SCOPE, name) \
18271836
STATS_DESC_CUMULATIVE(SCOPE, name, KVM_STATS_UNIT_SECONDS, \
@@ -1853,7 +1862,7 @@ struct _kvm_stats_desc {
18531862
HALT_POLL_HIST_COUNT), \
18541863
STATS_DESC_LOGHIST_TIME_NSEC(VCPU_GENERIC, halt_wait_hist, \
18551864
HALT_POLL_HIST_COUNT), \
1856-
STATS_DESC_ICOUNTER(VCPU_GENERIC, blocking)
1865+
STATS_DESC_IBOOLEAN(VCPU_GENERIC, blocking)
18571866

18581867
extern struct dentry *kvm_debugfs_dir;
18591868

include/uapi/linux/kvm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,6 +2083,7 @@ struct kvm_stats_header {
20832083
#define KVM_STATS_UNIT_BYTES (0x1 << KVM_STATS_UNIT_SHIFT)
20842084
#define KVM_STATS_UNIT_SECONDS (0x2 << KVM_STATS_UNIT_SHIFT)
20852085
#define KVM_STATS_UNIT_CYCLES (0x3 << KVM_STATS_UNIT_SHIFT)
2086+
#define KVM_STATS_UNIT_BOOLEAN (0x4 << KVM_STATS_UNIT_SHIFT)
20862087
#define KVM_STATS_UNIT_MAX KVM_STATS_UNIT_CYCLES
20872088

20882089
#define KVM_STATS_BASE_SHIFT 8

0 commit comments

Comments
 (0)