diff mbox series

[3/3,v4] KVM: x86: Add a new VM statistic to show number of VCPUs created in a given VM

Message ID 20210609180340.104248-4-krish.sadhukhan@oracle.com (mailing list archive)
State New, archived
Headers show
Series KVM: nVMX: nSVM: Add more statistics to KVM debugfs | expand

Commit Message

Krish Sadhukhan June 9, 2021, 6:03 p.m. UTC
'struct kvm' already has a member for tracking the number of VCPUs created
in a given VM. Add this as a new VM statistic to KVM debugfs. This statistic
can be a useful metric to track the usage of VCPUs on a host running
customer VMs.

Reported-by: kernel test robot <lkp@intel.com>
Signed-off-by: Krish Sadhukhan <Krish.Sadhukhan@oracle.com>
---
 arch/x86/include/asm/kvm_host.h | 1 +
 arch/x86/kvm/x86.c              | 1 +
 virt/kvm/kvm_main.c             | 6 ++++++
 3 files changed, 8 insertions(+)

Comments

Paolo Bonzini June 10, 2021, 2:27 p.m. UTC | #1
On 09/06/21 20:03, Krish Sadhukhan wrote:
> 'struct kvm' already has a member for tracking the number of VCPUs created
> in a given VM. Add this as a new VM statistic to KVM debugfs. This statistic
> can be a useful metric to track the usage of VCPUs on a host running
> customer VMs.
> 
> Reported-by: kernel test robot <lkp@intel.com>

Not sure why this "Reported-by", you can remove it.

Please add the statistic to all architectures, in order to avoid the #ifdef.

Paolo
diff mbox series

Patch

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index f6d5387bb88f..8f61a3fc3d39 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -1138,6 +1138,7 @@  struct kvm_vm_stat {
 	ulong lpages;
 	ulong nx_lpage_splits;
 	ulong max_mmu_page_hash_collisions;
+	ulong vcpus;
 };
 
 struct kvm_vcpu_stat {
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index baa953757911..7a1ff3052488 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -258,6 +258,7 @@  struct kvm_stats_debugfs_item debugfs_entries[] = {
 	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),
+	VM_STAT("vcpus", vcpus),
 	{ NULL }
 };
 
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 6b4feb92dc79..a129a6734965 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -3318,6 +3318,9 @@  static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
 	}
 
 	kvm->created_vcpus++;
+#ifdef CONFIG_X86
+	kvm->stat.vcpus++;
+#endif
 	mutex_unlock(&kvm->lock);
 
 	r = kvm_arch_vcpu_precreate(kvm, id);
@@ -3394,6 +3397,9 @@  static int kvm_vm_ioctl_create_vcpu(struct kvm *kvm, u32 id)
 vcpu_decrement:
 	mutex_lock(&kvm->lock);
 	kvm->created_vcpus--;
+#ifdef CONFIG_X86
+	kvm->stat.vcpus--;
+#endif
 	mutex_unlock(&kvm->lock);
 	return r;
 }