History

KVM: stats: Add fd-based API to read binary stats data

This commit defines the API for userspace and prepare the common functionalities to support per VM/VCPU binary stats data readings. The KVM stats now is only accessible by debugfs, which has some shortcomings this change series are supposed to fix: 1. The current debugfs stats solution in KVM could be disabled when kernel Lockdown mode is enabled, which is a potential rick for production. 2. The current debugfs stats solution in KVM is organized as "one stats per file", it is good for debugging, but not efficient for production. 3. The stats read/clear in current debugfs solution in KVM are protected by the global kvm_lock. Besides that, there are some other benefits with this change: 1. All KVM VM/VCPU stats can be read out in a bulk by one copy to userspace. 2. A schema is used to describe KVM statistics. From userspace's perspective, the KVM statistics are self-describing. 3. With the fd-based solution, a separate telemetry would be able to read KVM stats in a less privileged environment. 4. After the initial setup by reading in stats descriptors, a telemetry only needs to read the stats data itself, no more parsing or setup is needed.

Jing Zhang committed 5 years ago

Commit cb082bf1 parent 0193cc99 files · +307 −7Browse files

Showing the changes to include/uapi (1 of 9 files). Show all

Unified · Split

include/uapi/linux/kvm.h+73−0
@@ -1087,6 +1087,7 @@ struct kvm_ppc_resize_hpt { · 1086 unmodified lines
10871087 #define KVM_CAP_SREGS2 200
10881088 #define KVM_CAP_EXIT_HYPERCALL 201
10891089 #define KVM_CAP_PPC_RPT_INVALIDATE 202
1090+#define KVM_CAP_BINARY_STATS_FD 203
10901091
10911092 #ifdef KVM_CAP_IRQ_ROUTING
10921093
@@ -1906,4 +1907,76 @@ struct kvm_dirty_gfn { · 813 unmodified lines
19061907 #define KVM_BUS_LOCK_DETECTION_OFF (1 << 0)
19071908 #define KVM_BUS_LOCK_DETECTION_EXIT (1 << 1)
19081909
1910+/**
1911+ * struct kvm_stats_header - Header of per vm/vcpu binary statistics data.
1912+ * @flags: Some extra information for header, always 0 for now.
1913+ * @name_size: The size in bytes of the memory which contains statistics
1914+ * name string including trailing '\0'. The memory is allocated
1915+ * at the send of statistics descriptor.
1916+ * @num_desc: The number of statistics the vm or vcpu has.
1917+ * @id_offset: The offset of the vm/vcpu stats' id string in the file pointed
1918+ * by vm/vcpu stats fd.
1919+ * @desc_offset: The offset of the vm/vcpu stats' descriptor block in the file
1920+ * pointd by vm/vcpu stats fd.
1921+ * @data_offset: The offset of the vm/vcpu stats' data block in the file
1922+ * pointed by vm/vcpu stats fd.
1923+ *
1924+ * This is the header userspace needs to read from stats fd before any other
1925+ * readings. It is used by userspace to discover all the information about the
1926+ * vm/vcpu's binary statistics.
1927+ * Userspace reads this header from the start of the vm/vcpu's stats fd.
1928+ */
1929+struct kvm_stats_header {
1930+ __u32 flags;
1931+ __u32 name_size;
1932+ __u32 num_desc;
1933+ __u32 id_offset;
1934+ __u32 desc_offset;
1935+ __u32 data_offset;
1936+};
1937+
1938+#define KVM_STATS_TYPE_SHIFT 0
1939+#define KVM_STATS_TYPE_MASK (0xF << KVM_STATS_TYPE_SHIFT)
1940+#define KVM_STATS_TYPE_CUMULATIVE (0x0 << KVM_STATS_TYPE_SHIFT)
1941+#define KVM_STATS_TYPE_INSTANT (0x1 << KVM_STATS_TYPE_SHIFT)
1942+#define KVM_STATS_TYPE_PEAK (0x2 << KVM_STATS_TYPE_SHIFT)
1943+#define KVM_STATS_TYPE_MAX KVM_STATS_TYPE_PEAK
1944+
1945+#define KVM_STATS_UNIT_SHIFT 4
1946+#define KVM_STATS_UNIT_MASK (0xF << KVM_STATS_UNIT_SHIFT)
1947+#define KVM_STATS_UNIT_NONE (0x0 << KVM_STATS_UNIT_SHIFT)
1948+#define KVM_STATS_UNIT_BYTES (0x1 << KVM_STATS_UNIT_SHIFT)
1949+#define KVM_STATS_UNIT_SECONDS (0x2 << KVM_STATS_UNIT_SHIFT)
1950+#define KVM_STATS_UNIT_CYCLES (0x3 << KVM_STATS_UNIT_SHIFT)
1951+#define KVM_STATS_UNIT_MAX KVM_STATS_UNIT_CYCLES
1952+
1953+#define KVM_STATS_BASE_SHIFT 8
1954+#define KVM_STATS_BASE_MASK (0xF << KVM_STATS_BASE_SHIFT)
1955+#define KVM_STATS_BASE_POW10 (0x0 << KVM_STATS_BASE_SHIFT)
1956+#define KVM_STATS_BASE_POW2 (0x1 << KVM_STATS_BASE_SHIFT)
1957+#define KVM_STATS_BASE_MAX KVM_STATS_BASE_POW2
1958+
1959+/**
1960+ * struct kvm_stats_desc - Descriptor of a KVM statistics.
1961+ * @flags: Annotations of the stats, like type, unit, etc.
1962+ * @exponent: Used together with @flags to determine the unit.
1963+ * @size: The number of data items for this stats.
1964+ * Every data item is of type __u64.
1965+ * @offset: The offset of the stats to the start of stat structure in
1966+ * struture kvm or kvm_vcpu.
1967+ * @unused: Unused field for future usage. Always 0 for now.
1968+ * @name: The name string for the stats. Its size is indicated by the
1969+ * &kvm_stats_header->name_size.
1970+ */
1971+struct kvm_stats_desc {
1972+ __u32 flags;
1973+ __s16 exponent;
1974+ __u16 size;
1975+ __u32 offset;
1976+ __u32 unused;
1977+ char name[];
1978+};
1979+
1980+#define KVM_GET_STATS_FD _IO(KVMIO, 0xce)
1981+
19091982 #endif /* __LINUX_KVM_H */