From: Manali Shukla <manali.shukla@amd.com>
To: <kvm@vger.kernel.org>, <linux-kselftest@vger.kernel.org>
Cc: <pbonzini@redhat.com>, <seanjc@google.com>, <shuah@kernel.org>,
<nikunj@amd.com>, <thomas.lendacky@amd.com>,
<vkuznets@redhat.com>, <manali.shukla@amd.com>, <bp@alien8.de>,
<ajones@ventanamicro.com>
Subject: [PATCH v3 4/5] KVM: selftests: Add an interface to read the data of named vcpu stat
Date: Tue, 28 May 2024 04:19:25 +0000 [thread overview]
Message-ID: <20240528041926.3989-5-manali.shukla@amd.com> (raw)
In-Reply-To: <20240528041926.3989-1-manali.shukla@amd.com>
From: Manali Shukla <Manali.Shukla@amd.com>
The interface is used to read the data values of a specified vcpu stat
from the currenly available binary stats interface.
Signed-off-by: Manali Shukla <Manali.Shukla@amd.com>
---
.../kvm/include/kvm_arch_vcpu_states.h | 49 +++++++++++++++++++
.../testing/selftests/kvm/include/kvm_util.h | 34 +++++++++++++
tools/testing/selftests/kvm/lib/kvm_util.c | 32 ++++++++++++
3 files changed, 115 insertions(+)
create mode 100644 tools/testing/selftests/kvm/include/kvm_arch_vcpu_states.h
diff --git a/tools/testing/selftests/kvm/include/kvm_arch_vcpu_states.h b/tools/testing/selftests/kvm/include/kvm_arch_vcpu_states.h
new file mode 100644
index 000000000000..755ff7de53d9
--- /dev/null
+++ b/tools/testing/selftests/kvm/include/kvm_arch_vcpu_states.h
@@ -0,0 +1,49 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+/*
+ * Arch-specific stats are added to the kvm_arch_vcpu_states.h. Sequence
+ * of arch-specific vcpu_stat_type should be same as they are declared in
+ * arch-specific kvm_vcpu_stat.
+ */
+#ifdef __x86_64__
+#define KVM_X86_VCPU_STATE(x) KVM_VCPU_STATE(x)
+
+KVM_X86_VCPU_STATE(PF_TAKEN)
+KVM_X86_VCPU_STATE(PF_FIXED)
+KVM_X86_VCPU_STATE(PF_EMULATE)
+KVM_X86_VCPU_STATE(PF_SPURIOUS)
+KVM_X86_VCPU_STATE(PF_FAST)
+KVM_X86_VCPU_STATE(PF_MMIO_SPTE_CREATED)
+KVM_X86_VCPU_STATE(PF_GUEST)
+KVM_X86_VCPU_STATE(TLB_FLUSH)
+KVM_X86_VCPU_STATE(INVLPG)
+KVM_X86_VCPU_STATE(EXITS)
+KVM_X86_VCPU_STATE(IO_EXITS)
+KVM_X86_VCPU_STATE(MMIO_EXITS)
+KVM_X86_VCPU_STATE(SIGNAL_EXITS)
+KVM_X86_VCPU_STATE(IRQ_WINDOW_EXITS)
+KVM_X86_VCPU_STATE(NMI_WINDOW_EXITS)
+KVM_X86_VCPU_STATE(LD_FLUSH)
+KVM_X86_VCPU_STATE(HALT_EXITS)
+KVM_X86_VCPU_STATE(REQUEST_IRQ_EXITS)
+KVM_X86_VCPU_STATE(IRQ_EXITS)
+KVM_X86_VCPU_STATE(HOST_STATE_RELOAD)
+KVM_X86_VCPU_STATE(FPU_RELOAD)
+KVM_X86_VCPU_STATE(INSN_EMULATION)
+KVM_X86_VCPU_STATE(INSN_EMULATION_FAIL)
+KVM_X86_VCPU_STATE(HYPERCALLS)
+KVM_X86_VCPU_STATE(IRQ_INJECTIONS)
+KVM_X86_VCPU_STATE(NMI_INJECTIONS)
+KVM_X86_VCPU_STATE(REQ_EVENT)
+KVM_X86_VCPU_STATE(NESTED_RUN)
+KVM_X86_VCPU_STATE(DIRECTED_YIELD_ATTEMPTED)
+KVM_X86_VCPU_STATE(DIRECTED_YIELD_SUCCESSFUL)
+KVM_X86_VCPU_STATE(PREEMPTION_REPORTED)
+KVM_X86_VCPU_STATE(PREEMPTION_OTHER)
+KVM_X86_VCPU_STATE(GUEST_MODE)
+KVM_X86_VCPU_STATE(NOTIFY_WINDOW_EXITS)
+
+#undef KVM_X86_VCPU_STATE
+#undef KVM_VCPU_STATE
+#endif
+
diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h
index 63c2aaae51f3..fe66b1736060 100644
--- a/tools/testing/selftests/kvm/include/kvm_util.h
+++ b/tools/testing/selftests/kvm/include/kvm_util.h
@@ -518,6 +518,40 @@ static inline uint64_t vm_get_stat(struct kvm_vm *vm, const char *stat_name)
return data;
}
+/*
+ * Ensure that the sequence of the enum vcpu_stat_types matches the order of
+ * kvm_vcpu_stats_desc[]. Otherwise, vcpu_get_stat() may return incorrect data
+ * because __vcpu_get_stat() uses the enum type as an index to get the
+ * descriptor for a given stat and then uses read_stat_data() to get the stats
+ * from the descriptor.
+ */
+enum vcpu_stat_types {
+ HALT_SUCCESSFUL_POLL,
+ HALT_ATTEMPTED_POLL,
+ HALT_POLL_INVALID,
+ HALT_WAKEUP,
+ HALT_POLL_SUCCESS_NS,
+ HALT_POLL_FAIL_NS,
+ HALT_WAIT_NS,
+ HALT_POLL_SUCCESS_HIST,
+ HALT_POLL_FAIL_HIST,
+ HALT_WAIT_HIST,
+ BLOCKING,
+#define KVM_VCPU_STATE(x) x,
+#include "kvm_arch_vcpu_states.h"
+};
+
+void __vcpu_get_stat(struct kvm_vcpu *vcpu, enum vcpu_stat_types type, uint64_t *data,
+ size_t max_elements);
+
+static inline uint64_t vcpu_get_stat(struct kvm_vcpu *vcpu, enum vcpu_stat_types type)
+{
+ uint64_t data;
+
+ __vcpu_get_stat(vcpu, type, &data, 1);
+ return data;
+}
+
void vm_create_irqchip(struct kvm_vm *vm);
static inline int __vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size,
diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index ad00e4761886..d4b6a352c1ae 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -2264,6 +2264,38 @@ void read_stat_data(int stats_fd, struct kvm_stats_header *header,
desc->name, size, ret);
}
+/*
+ * Read the data of the named vcpu stat
+ *
+ * Input Args:
+ * vcpu - the vcpu for which the stat should be read
+ * stat_name - the name of the stat to read
+ * max_elements - the maximum number of 8-byte values to read into data
+ *
+ * Output Args:
+ * data - the buffer into which stat data should be read
+ *
+ * Read the data values of a specified stat from the binary stats interface.
+ */
+void __vcpu_get_stat(struct kvm_vcpu *vcpu, enum vcpu_stat_types type, uint64_t *data,
+ size_t max_elements)
+{
+ int vcpu_stats_fd;
+ struct kvm_stats_header header;
+ struct kvm_stats_desc *desc, *t_desc;
+ size_t size_desc;
+
+ vcpu_stats_fd = vcpu_get_stats_fd(vcpu);
+ read_stats_header(vcpu_stats_fd, &header);
+
+ desc = read_stats_descriptors(vcpu_stats_fd, &header);
+ size_desc = get_stats_descriptor_size(&header);
+
+ t_desc = (void *)desc + (type * size_desc);
+ read_stat_data(vcpu_stats_fd, &header, t_desc,
+ data, max_elements);
+}
+
/*
* Read the data of the named stat
*
--
2.34.1
next prev parent reply other threads:[~2024-05-28 4:19 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-28 4:19 [PATCH v3 0/5] Add support for the Idle HLT intercept feature Manali Shukla
2024-05-28 4:19 ` [PATCH v3 1/5] x86/cpufeatures: Add CPUID feature bit for Idle HLT intercept Manali Shukla
2024-05-28 7:42 ` Borislav Petkov
2024-05-28 4:19 ` [PATCH v3 2/5] KVM: SVM: Add Idle HLT intercept support Manali Shukla
2024-05-28 4:19 ` [PATCH v3 3/5] KVM: selftests: Add safe_halt() and cli() helpers to common code Manali Shukla
2024-05-28 4:19 ` Manali Shukla [this message]
2024-08-13 16:37 ` [PATCH v3 4/5] KVM: selftests: Add an interface to read the data of named vcpu stat Sean Christopherson
2024-10-22 5:49 ` Manali Shukla
2024-05-28 4:19 ` [PATCH v3 5/5] KVM: selftests: KVM: SVM: Add Idle HLT intercept test Manali Shukla
2024-05-28 7:46 ` Chao Gao
2024-05-30 13:19 ` Manali Shukla
2024-05-31 6:49 ` Chao Gao
2024-06-19 17:09 ` Manali Shukla
2024-08-13 15:38 ` Sean Christopherson
2024-08-13 16:03 ` Sean Christopherson
2024-05-28 10:22 ` [PATCH v3 0/5] Add support for the Idle HLT intercept feature Paolo Bonzini
2024-06-04 0:47 ` Sean Christopherson
2024-06-04 13:21 ` Manali Shukla
2024-06-04 12:23 ` Manali Shukla
2024-08-13 16:19 ` Sean Christopherson
2024-10-22 10:35 ` Manali Shukla
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240528041926.3989-5-manali.shukla@amd.com \
--to=manali.shukla@amd.com \
--cc=ajones@ventanamicro.com \
--cc=bp@alien8.de \
--cc=kvm@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=nikunj@amd.com \
--cc=pbonzini@redhat.com \
--cc=seanjc@google.com \
--cc=shuah@kernel.org \
--cc=thomas.lendacky@amd.com \
--cc=vkuznets@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox