Re: [PATCH/RFC 4/4, second shot]KVM uses "account_guest_time()"

From: Laurent Vivier
Date: Fri Aug 17 2007 - 07:54:58 EST


KVM updates vtime in task_struct to allow account_guest_time() to modify user,
system and guest time in cpustat accordingly.
Index: kvm/drivers/kvm/Kconfig
===================================================================
--- kvm.orig/drivers/kvm/Kconfig 2007-08-17 10:24:46.000000000 +0200
+++ kvm/drivers/kvm/Kconfig 2007-08-17 10:25:25.000000000 +0200
@@ -41,4 +41,10 @@
Provides support for KVM on AMD processors equipped with the AMD-V
(SVM) extensions.

+config GUEST_ACCOUNTING
+ bool "Virtual Machine accounting support"
+ depends on KVM
+ ---help---
+ Allows to account CPU time used by the Virtual Machines.
+
endif # VIRTUALIZATION
Index: kvm/drivers/kvm/kvm.h
===================================================================
--- kvm.orig/drivers/kvm/kvm.h 2007-08-17 10:19:30.000000000 +0200
+++ kvm/drivers/kvm/kvm.h 2007-08-17 10:21:56.000000000 +0200
@@ -589,6 +589,19 @@

int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run);

+#ifdef CONFIG_GUEST_ACCOUNTING
+static inline ktime_t kvm_guest_enter(void)
+{
+ return ktime_get();
+}
+
+static inline void kvm_guest_exit(ktime_t enter)
+{
+ ktime_t delta = ktime_sub(ktime_get(), enter);
+ current->vtime = ktime_add(current->vtime, delta);
+}
+#endif
+
static inline int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t gva,
u32 error_code)
{
Index: kvm/drivers/kvm/svm.c
===================================================================
--- kvm.orig/drivers/kvm/svm.c 2007-08-17 10:22:07.000000000 +0200
+++ kvm/drivers/kvm/svm.c 2007-08-17 10:23:32.000000000 +0200
@@ -1392,6 +1392,9 @@
u16 gs_selector;
u16 ldt_selector;
int r;
+#ifdef CONFIG_GUEST_ACCOUNTING
+ ktime_t now;
+#endif

again:
r = kvm_mmu_reload(vcpu);
@@ -1404,6 +1407,9 @@
clgi();

vcpu->guest_mode = 1;
+#ifdef CONFIG_GUEST_ACCOUNTING
+ now = kvm_guest_enter();
+#endif
if (vcpu->requests)
if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests))
svm_flush_tlb(vcpu);
@@ -1536,6 +1542,9 @@
#endif
: "cc", "memory" );

+#ifdef CONFIG_GUEST_ACCOUNTING
+ kvm_guest_exit(now);
+#endif
vcpu->guest_mode = 0;

if (vcpu->fpu_active) {
Index: kvm/drivers/kvm/vmx.c
===================================================================
--- kvm.orig/drivers/kvm/vmx.c 2007-08-17 10:23:36.000000000 +0200
+++ kvm/drivers/kvm/vmx.c 2007-08-17 10:24:37.000000000 +0200
@@ -2052,6 +2052,9 @@
struct vcpu_vmx *vmx = to_vmx(vcpu);
u8 fail;
int r;
+#ifdef CONFIG_GUEST_ACCOUNTING
+ ktime_t now;
+#endif

preempted:
if (vcpu->guest_debug.enabled)
@@ -2078,6 +2081,9 @@
local_irq_disable();

vcpu->guest_mode = 1;
+#ifdef CONFIG_GUEST_ACCOUNTING
+ now = kvm_guest_enter();
+#endif
if (vcpu->requests)
if (test_and_clear_bit(KVM_TLB_FLUSH, &vcpu->requests))
vmx_flush_tlb(vcpu);
@@ -2198,6 +2204,9 @@
[cr2]"i"(offsetof(struct kvm_vcpu, cr2))
: "cc", "memory" );

+#ifdef CONFIG_GUEST_ACCOUNTING
+ kvm_guest_exit(now);
+#endif
vcpu->guest_mode = 0;
local_irq_enable();