Laurent Vivier
2007-Aug-16 08:58 UTC
[PATCH/RFC 4/4]Modify KVM to use the "account modifiers"
[PATCH 4/4] Modify KVM to use the "account modifiers". KVM can now measure time consumed by a Virtual Machine on a per-cpu basis and modify kernel statistics to report this value. Signed-off-by: Laurent Vivier <Laurent.Vivier@bull.net> -- ------------- Laurent.Vivier@bull.net -------------- "Software is hard" - Donald Knuth -------------- next part -------------- Index: kvm/drivers/kvm/kvm.h =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- kvm.orig/drivers/kvm/kvm.h 2007-08-16 16:21:42.000000000 +0200 +++ kvm/drivers/kvm/kvm.h 2007-08-16 16:27:00.000000000 +0200 @@ -408,6 +408,10 @@ struct file *filp; struct kvm_io_bus mmio_bus; struct kvm_io_bus pio_bus; +#ifdef CONFIG_ACCOUNT_MODIFIERS + struct account_modifier account_modifier; + ktime_t vtime[NR_CPUS]; +#endif }; struct descriptor_table { @@ -589,6 +593,20 @@ int kvm_hypercall(struct kvm_vcpu *vcpu, struct kvm_run *run); +#ifdef CONFIG_ACCOUNT_MODIFIERS +static inline ktime_t kvm_guest_enter(void) +{ + return ktime_get(); +} + +static inline void kvm_guest_exit(struct kvm *kvm, ktime_t enter) +{ + ktime_t delta =3D ktime_sub(ktime_get(), enter); + kvm->vtime[smp_processor_id()] =3D + ktime_add(kvm->vtime[smp_processor_id()], delta); +} +#endif + static inline int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gva_t gva, u32 error_code) { Index: kvm/drivers/kvm/kvm_main.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- kvm.orig/drivers/kvm/kvm_main.c 2007-08-16 16:21:42.000000000 +0200 +++ kvm/drivers/kvm/kvm_main.c 2007-08-16 16:40:28.000000000 +0200 @@ -33,6 +33,7 @@ #include <linux/file.h> #include <linux/sysdev.h> #include <linux/cpu.h> +#include <linux/kernel_stat.h> #include <linux/sched.h> #include <linux/cpumask.h> #include <linux/smp.h> @@ -57,6 +58,9 @@ EXPORT_SYMBOL_GPL(kvm_vcpu_cache); static __read_mostly struct preempt_ops kvm_preempt_ops; +#ifdef CONFIG_ACCOUNT_MODIFIERS +static __read_mostly struct account_ops kvm_account_ops; +#endif #define STAT_OFFSET(x) offsetof(struct kvm_vcpu, stat.x) @@ -285,6 +289,42 @@ } EXPORT_SYMBOL_GPL(kvm_vcpu_uninit); +#ifdef CONFIG_ACCOUNT_MODIFIERS +static inline +struct kvm *account_modifier_to_kvm(struct account_modifier *am) +{ + return container_of(am, struct kvm, account_modifier); +} + +static cputime_t kvm_account_system_modifier(struct account_modifier *modifier, + struct task_struct *curr, + int hardirq_offset, + cputime_t cputime) +{ + struct kvm *kvm =3D account_modifier_to_kvm(modifier); + ktime_t kmsec =3D ktime_set(0, NSEC_PER_MSEC); + cputime_t cmsec =3D msecs_to_cputime(1); + ktime_t vtime =3D kvm->vtime[smp_processor_id()]; + + while ((ktime_to_ns(vtime) >=3D NSEC_PER_MSEC) && + (cputime_to_msecs(cputime) >=3D 1)) { + kvm->vtime[smp_processor_id()] =3D ktime_sub(vtime, kmsec); + + curr->utime =3D cputime_add(curr->utime, cmsec); + curr->gtime =3D cputime_add(curr->gtime, cmsec); + + kstat_this_cpu.cpustat.guest =3D cputime64_add(kstat_this_cpu.cpustat.guest, + cputime_to_cputime64(cmsec)); + kstat_this_cpu.cpustat.user =3D cputime64_add(kstat_this_cpu.cpustat.user, + cputime_to_cputime64(cmsec)); + + cputime =3D cputime_sub(cputime, cmsec); + } + + return cputime; +} +#endif + static struct kvm *kvm_create_vm(void) { struct kvm *kvm =3D kzalloc(sizeof(struct kvm), GFP_KERNEL); @@ -299,6 +339,15 @@ spin_lock(&kvm_lock); list_add(&kvm->vm_list, &vm_list); spin_unlock(&kvm_lock); + +#ifdef CONFIG_ACCOUNT_MODIFIERS + kvm_account_ops.user_time =3D NULL; + kvm_account_ops.system_time =3D kvm_account_system_modifier; + + account_modifier_init(&kvm->account_modifier, &kvm_account_ops); + account_modifier_register(&kvm->account_modifier); +#endif + return kvm; } @@ -376,6 +425,9 @@ spin_lock(&kvm_lock); list_del(&kvm->vm_list); spin_unlock(&kvm_lock); +#ifdef CONFIG_ACCOUNT_MODIFIERS + account_modifier_unregister(&kvm->account_modifier); +#endif kvm_io_bus_destroy(&kvm->pio_bus); kvm_io_bus_destroy(&kvm->mmio_bus); kvm_free_vcpus(kvm); Index: kvm/drivers/kvm/svm.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- kvm.orig/drivers/kvm/svm.c 2007-08-16 16:21:42.000000000 +0200 +++ kvm/drivers/kvm/svm.c 2007-08-16 16:29:41.000000000 +0200 @@ -1392,6 +1392,9 @@ u16 gs_selector; u16 ldt_selector; int r; +#ifdef CONFIG_ACCOUNT_MODIFIERS + ktime_t now; +#endif again: r =3D kvm_mmu_reload(vcpu); @@ -1404,6 +1407,9 @@ clgi(); vcpu->guest_mode =3D 1; +#ifdef CONFIG_ACCOUNT_MODIFIERS + now =3D 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_ACCOUNT_MODIFIERS + kvm_guest_exit(vcpu->kvm, now); +#endif vcpu->guest_mode =3D 0; if (vcpu->fpu_active) { Index: kvm/drivers/kvm/vmx.c =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- kvm.orig/drivers/kvm/vmx.c 2007-08-16 16:21:42.000000000 +0200 +++ kvm/drivers/kvm/vmx.c 2007-08-16 16:30:24.000000000 +0200 @@ -2052,6 +2052,9 @@ struct vcpu_vmx *vmx =3D to_vmx(vcpu); u8 fail; int r; +#ifdef CONFIG_ACCOUNT_MODIFIERS + ktime_t now; +#endif preempted: if (vcpu->guest_debug.enabled) @@ -2078,6 +2081,9 @@ local_irq_disable(); vcpu->guest_mode =3D 1; +#ifdef CONFIG_ACCOUNT_MODIFIERS + now =3D 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_ACCOUNT_MODIFIERS + kvm_guest_exit(vcpu->kvm, now); +#endif vcpu->guest_mode =3D 0; local_irq_enable(); Index: kvm/drivers/kvm/Kconfig =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- kvm.orig/drivers/kvm/Kconfig 2007-08-16 16:21:42.000000000 +0200 +++ kvm/drivers/kvm/Kconfig 2007-08-16 16:21:44.000000000 +0200 @@ -41,4 +41,10 @@ Provides support for KVM on AMD processors equipped with the AMD-V (SVM) extensions. +config ACCOUNT_MODIFIERS + bool "Virtual Machine accounting support" + depends on KVM + ---help--- + Allows to account CPU time used by the Virtual Machines. + endif # VIRTUALIZATION