KVM Steal time, new submission
Subsystems (6)
PATCH v2 1/6 KVM-HDR: KVM Steal time implementation
In reply to Glauber CostaTo implement steal time, we need the hypervisor to pass the guest information about how much time was spent running other processes outside the VM. This is per-vcpu, and using the kvmclock structure for that is an abuse we decided not to make.
In this patchset, I am introducing a new msr, KVM_MSR_STEAL_TIME, that holds the memory area address containing information about steal time
This patch contains the headers for it. I am keeping it separate to facilitate backports to people who wants to backport the kernel part but not the hypervisor, or the other way around.
--- arch/x86/include/asm/kvm_para.h | 9 +++++++++ 1 files changed, 9 insertions(+), 0 deletions(-)
arch/x86/include/asm/kvm_para.h +9 -0
@@ -21,6 +21,7 @@*/ #define KVM_FEATURE_CLOCKSOURCE2 3#define KVM_FEATURE_ASYNC_PF 4 +#define KVM_FEATURE_STEAL_TIME 5/* The last 8 bits are used to indicate how to interpret the flags field * in pvclock structure. If no bits are set, all flags are ignored.
@@ -34,6 +35,14 @@#define MSR_KVM_WALL_CLOCK_NEW 0x4b564d00#define MSR_KVM_SYSTEM_TIME_NEW 0x4b564d01#define MSR_KVM_ASYNC_PF_EN 0x4b564d02 +#define MSR_KVM_STEAL_TIME 0x4b564d03 + +struct kvm_steal_time { +__u64 steal; +__u32 version; +__u32 flags; +__u32 pad[6 ]; +};#define KVM_MAX_MMU_OP_BATCH 32
Signature · 1 line
1.7.2.3
PATCH v2 6/6 Describe KVM_MSR_STEAL_TIME
In reply to Glauber CostaThis patch adds documentation about usage of the newly introduced KVM_MSR_STEAL_TIME.
--- Documentation/kvm/msr.txt | 32 ++++++++++++++++++++++++++++++++ 1 files changed, 32 insertions(+), 0 deletions(-)
Documentation/kvm/msr.txt +32 -0
@@ -185,3 +185,35 @@ MSR_KVM_ASYNC_PF_EN: 0x4b564d02 Currently type 2 APF will be always delivered on the same vcpu as type 1 was, but guest should not rely on that. + +MSR_KVM_STEAL_TIME: 0x4b564d03 + + data: 4-byte alignment physical address of a memory area which must be + in guest RAM, plus an enable bit in bit 0. This memory is expected to + hold a copy of the following structure: + + struct kvm_steal_time { + __u64 steal; + __u32 version; + __u32 flags; + __u32 pad[6]; + } + + whose data will be filled in by the hypervisor periodically. Only one + write, or registration, is needed for each VCPU. The interval between + updates of this structure is arbitrary and implementation-dependent. + The hypervisor may update this structure at any time it sees fit until + anything with bit0 == 0 is written to it. + + Fields have the following meanings: + + version: guest has to check version before and after grabbing + time information and check that they are both equal and even. + An odd version indicates an in-progress update. + + flags: At this point, always zero. May be used to indicate + changes in this structure in the future. + + steal: the amount of time in which this vCPU did not run, in + nanoseconds. +
Signature · 1 line
1.7.2.3
v2 cover KVM Steal time, new submission
Gentlemen,
If I may please steal a little bit of your time, for the revision of this patchset, this is appreciated. This version only includes the steal time part, using a new MSR+cpuid to register it.
It also includes some documentation, describing the ABI. Mostly inspired by the kvmclock one.
Comments welcome
Glauber Costa (6):
KVM-HDR: KVM Steal time implementation
KVM-HV: KVM Steal time implementation
KVM-GST: KVM Steal time accounting
KVM-GST: KVM Steal time registration
KVM-GST: adjust scheduler cpu power
Describe KVM_MSR_STEAL_TIME
Documentation/kvm/msr.txt | 32 ++++++++++ arch/x86/Kconfig | 12 ++++ arch/x86/include/asm/kvm_host.h | 6 ++ arch/x86/include/asm/kvm_para.h | 10 +++ arch/x86/kernel/kvm.c | 61 +++++++++++++++++++ arch/x86/kernel/kvmclock.c | 2 + arch/x86/kvm/x86.c | 48 +++++++++++++-- include/linux/kvm.h | 1 + include/linux/sched.h | 1 + kernel/sched.c | 126 +++++++++++++++++++++++++++++++++++---- kernel/sched_features.h | 4 +- 11 files changed, 283 insertions(+), 20 deletions(-)
Signature · 1 line
1.7.2.3
PATCH v2 3/6 KVM-GST: KVM Steal time accounting
This patch accounts steal time time in kernel/sched.
I kept it from last proposal, because I still see advantages in it: Doing it here will give us easier access from scheduler variables such as the cpu rq. The next patch shows an example of usage for it.
Since functions like account_idle_time() can be called from multiple places, not only account_process_tick(), steal time grabbing is repeated in each account function separatedely.
--- include/linux/sched.h | 1 + kernel/sched.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 0 deletions(-)
include/linux/sched.h +1 -0
@@ -302,6 +302,7 @@ long io_schedule_timeout(long timeout);extern void cpu_init (void );extern void trap_init (void );extern void update_process_times (int user); +extern u64 (*hypervisor_steal_time)(void );extern void scheduler_tick (void );extern void sched_show_task (struct task_struct *p);
kernel/sched.c +41 -0
65 lines not on this page · raw message
Signature · 1 line
1.7.2.3
PATCH v2 2/6 KVM-HV: KVM Steal time implementation
In reply to Glauber CostaTo implement steal time, we need the hypervisor to pass the guest information about how much time was spent running other processes outside the VM. This is per-vcpu, and using the kvmclock structure for that is an abuse we decided not to make.
In this patchset, I am introducing a new msr, KVM_MSR_STEAL_TIME, that holds the memory area address containing information about steal time
This patch contains the hypervisor part for it. I am keeping it separate from the headers to facilitate backports to people who wants to backport the kernel part but not the hypervisor, or the other way around.
--- arch/x86/include/asm/kvm_host.h | 6 +++++ arch/x86/kvm/x86.c | 48 ++++++++++++++++++++++++++++++++++----- include/linux/kvm.h | 1 + 3 files changed, 49 insertions(+), 6 deletions(-)
arch/x86/include/asm/kvm_host.h +6 -0
@@ -377,6 +377,12 @@ struct kvm_vcpu_arch {unsigned int hw_tsc_khz;unsigned int time_offset;struct page *time_page; + +gpa_t stime; +u32 sversion; +u64 time_out; +u64 this_time_out; +u64 last_host_tsc;u64 last_guest_tsc;u64 last_kernel_ns;
arch/x86/kvm/x86.c +42 -6
106 lines not on this page · raw message
include/linux/kvm.h +1 -0
@@ -541,6 +541,7 @@ struct kvm_ppc_pvinfo {#define KVM_CAP_PPC_GET_PVINFO 57#define KVM_CAP_PPC_IRQ_LEVEL 58#define KVM_CAP_ASYNC_PF 59 +#define KVM_CAP_STEAL_TIME 60#ifdef KVM_CAP_IRQ_ROUTING
Signature · 1 line
1.7.2.3
PATCH v2 4/6 KVM-GST: KVM Steal time registration
In reply to Glauber CostaRegister steal time within KVM. Everytime we sample the steal time information, we update a local variable that tells what was the last time read. We then account the difference.
--- arch/x86/include/asm/kvm_para.h | 1 + arch/x86/kernel/kvm.c | 61 +++++++++++++++++++++++++++++++++++++++ arch/x86/kernel/kvmclock.c | 2 + 3 files changed, 64 insertions(+), 0 deletions(-)
arch/x86/include/asm/kvm_para.h +1 -0
@@ -89,6 +89,7 @@ struct kvm_vcpu_pv_apf_data {extern void kvmclock_init (void );extern int kvm_register_clock (char *txt); +extern int kvm_register_steal_time (void );/* This instruction is vmcall. On non-VT architectures, it will generate a
arch/x86/kernel/kvm.c +61 -0
97 lines not on this page · raw message
arch/x86/kernel/kvmclock.c +2 -0
@@ -164,6 +164,7 @@ static void __cpuinit kvm_setup_secondary_clock(void)static void kvm_crash_shutdown (struct pt_regs *regs) {native_write_msr (msr_kvm_system_time,0 ,0 ); +native_write_msr (MSR_KVM_STEAL_TIME ,0 ,0 );native_machine_crash_shutdown (regs); }#endif
@@ -171,6 +172,7 @@ static void kvm_crash_shutdown(struct pt_regs *regs)static void kvm_shutdown (void ) {native_write_msr (msr_kvm_system_time,0 ,0 ); +native_write_msr (MSR_KVM_STEAL_TIME ,0 ,0 );native_machine_shutdown (); }
Signature · 1 line
1.7.2.3
PATCH v2 5/6 KVM-GST: adjust scheduler cpu power
In reply to Glauber CostaThis is a first proposal for using steal time information to influence the scheduler. There are a lot of optimizations and fine grained adjustments to be done, but it is working reasonably so far for me (mostly)
With this patch (and some host pinnings to demonstrate the situation), two vcpus with very different steal time (Say 80 % vs 1 %) will not get an even distribution of processes. This is a situation that can naturally arise, specially in overcommited scenarios. Previosly, the guest scheduler would wrongly think that all cpus have the same ability to run processes, lowering the overall throughput.
--- arch/x86/Kconfig | 12 +++++++ kernel/sched.c | 85 ++++++++++++++++++++++++++++++++++++++++------- kernel/sched_features.h | 4 +- 3 files changed, 87 insertions(+), 14 deletions(-)
arch/x86/Kconfig +12 -0
@@ -515,6 +515,18 @@ menuconfig PARAVIRT_GUEST if PARAVIRT_GUEST +config PARAVIRT_TIME_ACCOUNTING + bool "Paravirtual steal time accounting" + select PARAVIRT + default n + ---help--- + Select this option to enable fine granularity task steal time + accounting. Time spent executing other tasks in parallel with + the current vCPU is discounted from the vCPU power. To account for + that, there can be a small performance impact. + + If in doubt, say N here. + source "arch/x86/xen/Kconfig" config KVM_CLOCK
kernel/sched.c +72 -11
120 lines not on this page · raw message
kernel/sched_features.h +2 -2
@@ -61,6 +61,6 @@ SCHED_FEAT(LB_BIAS, 1)SCHED_FEAT (OWNER_SPIN ,1 )/* -* Decrement CPU power based on irq activity +* Decrement CPU power based on time not spent running tasks */ -SCHED_FEAT (NONIRQ_POWER ,1 ) +SCHED_FEAT (NONTASK_POWER ,1 )
Signature · 1 line
1.7.2.3
Re: [PATCH v2 3/6] KVM-GST: KVM Steal time accounting
In reply to Glauber CostaQuoted 5 lines · Glauber Costa
This patch accounts steal time time in kernel/sched.
I kept it from last proposal, because I still see advantages in it: Doing it here will give us easier access from scheduler variables such as the cpu rq. The next patch shows an example of usage for it.
Is this used on the host or guest side?
J
Quoted 99 lines
Since functions like
account_idle_time()can be called from multiple places, not onlyaccount_process_tick(), steal time grabbing is repeated in each account function separatedely.Signed-off-by Glauber Costa <[email protected]>CC Rik van Riel <[email protected]>CC Jeremy Fitzhardinge <[email protected]>CC Peter Zijlstra <[email protected]>CC Avi Kivity <[email protected]>--- include/linux/sched.h | 1 + kernel/sched.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 0 deletions(-)include/linux/sched.h +1
-0@@ -302,6 +302,7 @@ long io_schedule_timeout(long timeout);extern void cpu_init (void );extern void trap_init (void );extern void update_process_times (int user); +extern u64 (*hypervisor_steal_time)(void );extern void scheduler_tick (void );extern void sched_show_task (struct task_struct *p);kernel/sched.c +41
-0@@ -3508,6 +3508,39 @@ unsigned long long thread_group_sched_runtime(struct task_struct *p)return ns; } +u64 (*hypervisor_steal_time)(void) =NULL ; + +/* +* We have to at flush steal time information every time something else +* is accounted. Since the accounting functions are all visible to the rest +* of the kernel, it gets tricky to do them in one place. This helper function +* helps us. +* +* When the system is idle, the concept of steal time does not apply. We just +* tell the underlying hypervisor that we grabbed the data, but skip steal time +* accounting +*/ +static int touch_steal_time (int is_idle) +{ +u64 steal, st; + +if (!hypervisor_steal_time) +return 0 ; + + steal =hypervisor_steal_time (); + + st =usecs_to_cputime (steal /1000 ); + +if (is_idle) +return 0 ; + +if (st) { +account_steal_time (st); +return 1 ; + } +return 0 ; +} +/* * Account user cpu time to a process. * @p: the process that the cpu time gets accounted to @@ -3520,6 +3553,9 @@ void account_user_time(struct task_struct *p, cputime_t cputime,struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;cputime64_t tmp; +if (touch_steal_time (0 )) +return ; +/* Add user time to process. */ p->utime =cputime_add (p->utime, cputime); p->utimescaled =cputime_add (p->utimescaled, cputime_scaled);@@ -3580,6 +3616,9 @@ void account_system_time(struct task_struct *p, int hardirq_offset,struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;cputime64_t tmp; +if (touch_steal_time (0 )) +return ; +if ((p->flags &PF_VCPU ) && (irq_count () - hardirq_offset ==0 )) {account_guest_time (p, cputime, cputime_scaled);return ;@@ -3627,6 +3666,8 @@ void account_idle_time(cputime_t cputime)cputime64_t cputime64 =cputime_to_cputime64 (cputime);struct rq *rq =this_rq (); +touch_steal_time (1 ); +if (atomic_read (&rq->nr_iowait) >0 ) cpustat->iowait =cputime64_add (cpustat->iowait, cputime64);else
Re: [PATCH v2 3/6] KVM-GST: KVM Steal time accounting
Quoted 8 lines · Jeremy Fitzhardinge
Quoted 5 lines · Glauber Costa
This patch accounts steal time time in kernel/sched.
I kept it from last proposal, because I still see advantages in it: Doing it here will give us easier access from scheduler variables such as the cpu rq. The next patch shows an example of usage for it.Is this used on the host or guest side?
Guest.
Quoted 102 lines
J
Quoted 99 lines
Since functions like
account_idle_time()can be called from multiple places, not onlyaccount_process_tick(), steal time grabbing is repeated in each account function separatedely.Signed-off-by Glauber Costa <[email protected]>CC Rik van Riel <[email protected]>CC Jeremy Fitzhardinge <[email protected]>CC Peter Zijlstra <[email protected]>CC Avi Kivity <[email protected]>--- include/linux/sched.h | 1 + kernel/sched.c | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 0 deletions(-)include/linux/sched.h +1
-0@@ -302,6 +302,7 @@ long io_schedule_timeout(long timeout);extern void cpu_init (void );extern void trap_init (void );extern void update_process_times (int user); +extern u64 (*hypervisor_steal_time)(void );extern void scheduler_tick (void );extern void sched_show_task (struct task_struct *p);kernel/sched.c +41
-0@@ -3508,6 +3508,39 @@ unsigned long long thread_group_sched_runtime(struct task_struct *p)return ns; } +u64 (*hypervisor_steal_time)(void) =NULL ; + +/* +* We have to at flush steal time information every time something else +* is accounted. Since the accounting functions are all visible to the rest +* of the kernel, it gets tricky to do them in one place. This helper function +* helps us. +* +* When the system is idle, the concept of steal time does not apply. We just +* tell the underlying hypervisor that we grabbed the data, but skip steal time +* accounting +*/ +static int touch_steal_time (int is_idle) +{ +u64 steal, st; + +if (!hypervisor_steal_time) +return 0 ; + + steal =hypervisor_steal_time (); + + st =usecs_to_cputime (steal /1000 ); + +if (is_idle) +return 0 ; + +if (st) { +account_steal_time (st); +return 1 ; + } +return 0 ; +} +/* * Account user cpu time to a process. * @p: the process that the cpu time gets accounted to @@ -3520,6 +3553,9 @@ void account_user_time(struct task_struct *p, cputime_t cputime,struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;cputime64_t tmp; +if (touch_steal_time (0 )) +return ; +/* Add user time to process. */ p->utime =cputime_add (p->utime, cputime); p->utimescaled =cputime_add (p->utimescaled, cputime_scaled);@@ -3580,6 +3616,9 @@ void account_system_time(struct task_struct *p, int hardirq_offset,struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;cputime64_t tmp; +if (touch_steal_time (0 )) +return ; +if ((p->flags &PF_VCPU ) && (irq_count () - hardirq_offset ==0 )) {account_guest_time (p, cputime, cputime_scaled);return ;@@ -3627,6 +3666,8 @@ void account_idle_time(cputime_t cputime)cputime64_t cputime64 =cputime_to_cputime64 (cputime);struct rq *rq =this_rq (); +touch_steal_time (1 ); +if (atomic_read (&rq->nr_iowait) >0 ) cpustat->iowait =cputime64_add (cpustat->iowait, cputime64);else
Re: [PATCH v2 1/6] KVM-HDR: KVM Steal time implementation
In reply to Glauber CostaQuoted 12 lines · Glauber Costa
To implement steal time, we need the hypervisor to pass the guest information about how much time was spent running other processes outside the VM. This is per-vcpu, and using the kvmclock structure for that is an abuse we decided not to make.
In this patchset, I am introducing a new msr,
KVM_MSR_STEAL_TIME, that holds the memory area address containing information about steal timeThis patch contains the headers for it. I am keeping it separate to facilitate backports to people who wants to backport the kernel part but not the hypervisor, or the other way around.
Quoted 5 lines
Signed-off-by Glauber Costa<[email protected]>CC Rik van Riel<[email protected]>CC Jeremy Fitzhardinge<[email protected]>CC Peter Zijlstra<[email protected]>CC Avi Kivity<[email protected]>
Signature · 1 line
All rights reversed
Re: [PATCH v2 2/6] KVM-HV: KVM Steal time implementation
In reply to Glauber CostaQuoted 12 lines · Glauber Costa
To implement steal time, we need the hypervisor to pass the guest information about how much time was spent running other processes outside the VM. This is per-vcpu, and using the kvmclock structure for that is an abuse we decided not to make.
In this patchset, I am introducing a new msr,
KVM_MSR_STEAL_TIME, that holds the memory area address containing information about steal timeThis patch contains the hypervisor part for it. I am keeping it separate from the headers to facilitate backports to people who wants to backport the kernel part but not the hypervisor, or the other way around.
Quoted 5 lines
Signed-off-by Glauber Costa<[email protected]>CC Rik van Riel<[email protected]>CC Jeremy Fitzhardinge<[email protected]>CC Peter Zijlstra<[email protected]>CC Avi Kivity<[email protected]>
Signature · 1 line
All rights reversed
Re: [PATCH v2 3/6] KVM-GST: KVM Steal time accounting
In reply to Glauber CostaQuoted 10 lines · Glauber Costa
This patch accounts steal time time in kernel/sched.
I kept it from last proposal, because I still see advantages in it: Doing it here will give us easier access from scheduler variables such as the cpu rq. The next patch shows an example of usage for it.Since functions like
account_idle_time()can be called from multiple places, not onlyaccount_process_tick(), steal time grabbing is repeated in each account function separatedely.
Quoted 5 lines
Signed-off-by Glauber Costa<[email protected]>CC Rik van Riel<[email protected]>CC Jeremy Fitzhardinge<[email protected]>CC Peter Zijlstra<[email protected]>CC Avi Kivity<[email protected]>
Not the traditional way of doing steal time, but a lot cleaner than the legacy code that's left over from when each clocksource had its own interrupt function.
I like it.
Signature · 1 line
All rights reversed
Re: [PATCH v2 4/6] KVM-GST: KVM Steal time registration
In reply to Glauber CostaQuoted 9 lines · Glauber Costa
Register steal time within KVM. Everytime we sample the steal time information, we update a local variable that tells what was the last time read. We then account the difference.
Signed-off-by Glauber Costa<[email protected]>CC Rik van Riel<[email protected]>CC Jeremy Fitzhardinge<[email protected]>CC Peter Zijlstra<[email protected]>CC Avi Kivity<[email protected]>
Signature · 1 line
All rights reversed
Re: [PATCH v2 2/6] KVM-HV: KVM Steal time implementation
In reply to Glauber CostaQuoted 14 lines · Glauber Costa
To implement steal time, we need the hypervisor to pass the guest information about how much time was spent running other processes outside the VM. This is per-vcpu, and using the kvmclock structure for that is an abuse we decided not to make.
In this patchset, I am introducing a new msr,
KVM_MSR_STEAL_TIME, that holds the memory area address containing information about steal timeThis patch contains the hypervisor part for it. I am keeping it separate from the headers to facilitate backports to people who wants to backport the kernel part but not the hypervisor, or the other way around.
arch/x86/kvm/x86.c int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1532
Quoted 9 lines
arch/x86/kvm/x86.c +0
-4@@ -1528,16 +1528,23 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) vcpu->arch.time_page =gfn_to_page (vcpu->kvm, data>>PAGE_SHIFT ); -if (is_error_page (vcpu->arch.time_page)) { -kvm_release_page_clean (vcpu->arch.time_page); - vcpu->arch.time_page =NULL ; - }break ; }
Unrelated?
Quoted 4 lines
arch/x86/kvm/x86.c +0
-0@@ -2106,6 +2120,25 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)kvm_migrate_timers (vcpu); vcpu->cpu = cpu; }
arch/x86/kvm/x86.c void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 2132
Quoted 10 lines
arch/x86/kvm/x86.c +10
-0+ +if (vcpu->arch.this_time_out) { +u64 to = (get_kernel_ns () - vcpu->arch.this_time_out); +/* +* using nanoseconds introduces noise, which accumulates easily +* leading to big steal time values. We want, however, to keep the +* interface nanosecond-based for future-proofness. +*/ + to /=NSEC_PER_USEC ; + to *=NSEC_PER_USEC ;
Seems there is a real problem and that this is just papering it over. I'd like to understand the root cause.
arch/x86/kvm/x86.c void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 2135
Quoted 3 lines
arch/x86/kvm/x86.c +3
-0+ vcpu->arch.time_out += to; +kvm_write_guest (vcpu->kvm, (gpa_t)&st->steal, + &vcpu->arch.time_out,sizeof (st->steal));
Error check.
arch/x86/kvm/x86.c void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 2136
Quoted 1 line
arch/x86/kvm/x86.c +1
-0+ vcpu->arch.sversion +=2 ;
Doesn't survive live migration. You need to use the version from the guest area.
Quoted 7 lines
arch/x86/kvm/x86.c +5
-0+kvm_write_guest (vcpu->kvm, (gpa_t)&st->version, + &vcpu->arch.sversion,sizeof (st->version)); +/* is it possible to have 2 loads in sequence? */ + vcpu->arch.this_time_out =0 ; + } }
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v2 4/6] KVM-GST: KVM Steal time registration
In reply to Glauber Costaarch/x86/kernel/kvm.c static void __init kvm_smp_prepare_boot_cpu(void) · line 510
Quoted 12 lines · Glauber Costa
Register steal time within KVM. Everytime we sample the steal time information, we update a local variable that tells what was the last time read. We then account the difference.
static void kvm_guest_cpu_offline(void *dummy) { kvm_pv_disable_apf(NULL);arch/x86/kernel/kvm.c +1
-0+native_write_msr (MSR_KVM_STEAL_TIME ,0 ,0 );apf_task_wake_all (); }
Don't use the native_ versions, they override the pvops implementation. It doesn't matter for kvm, but we're not supposed to know this.
arch/x86/kernel/kvm.c static void __init kvm_apf_trap_init(void) · line 573
Quoted 8 lines
arch/x86/kernel/kvm.c +8
-0+/* +* using nanoseconds introduces noise, which accumulates easily +* leading to big steal time values. We want, however, to keep the +* interface nanosecond-based for future-proofness. The hypervisor may +* adopt a similar strategy, but we can't rely on that. +*/ + delta /=NSEC_PER_MSEC ; + delta *=NSEC_PER_MSEC ;
You're working around this problem both in the guest and host. So even if we fix it in one, it will still be broken in the other.
Quoted 4 lines
arch/x86/kernel/kvm.c +4
-0+ +return delta; +} +
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v2 6/6] Describe KVM_MSR_STEAL_TIME
In reply to Glauber CostaDocumentation/kvm/msr.txt MSR_KVM_ASYNC_PF_EN: 0x4b564d02
Quoted 9 lines · Glauber Costa
This patch adds documentation about usage of the newly introduced
KVM_MSR_STEAL_TIME.Documentation/kvm/msr.txt +5
-0+ +MSR_KVM_STEAL_TIME: 0x4b564d03 + + data: 4-byte alignment physical address of a memory area which must be + in guest RAM, plus an enable bit in bit 0.
64-byte aligned:
- avoids wrapping around a page boundary, which may let us optimize things later on (see kvm_write_guest_cached()).
- gives us 5 more unused bits to enable more options
Quoted 18 lines
This memory is expected to
Documentation/kvm/msr.txt +17
-0+ hold a copy of the following structure: + + struct kvm_steal_time { + __u64 steal; + __u32 version; + __u32 flags; + __u32 pad[6]; + } + + whose data will be filled in by the hypervisor periodically. Only one + write, or registration, is needed for each VCPU. The interval between + updates of this structure is arbitrary and implementation-dependent. + The hypervisor may update this structure at any time it sees fit until + anything with bit0 == 0 is written to it. + + Fields have the following meanings: +
Documentation/kvm/msr.txt MSR_KVM_ASYNC_PF_EN: 0x4b564d02 · line 219
Quoted 10 lines
Documentation/kvm/msr.txt +10
-0+ version: guest has to check version before and after grabbing + time information and check that they are both equal and even. + An odd version indicates an in-progress update. + + flags: At this point, always zero. May be used to indicate + changes in this structure in the future. + + steal: the amount of time in which this vCPU did not run, in + nanoseconds. +
The guest must initialize the entire 64-byte structure to zero before enabling the feature.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v2 3/6] KVM-GST: KVM Steal time accounting
In reply to Glauber CostaQuoted 10 lines · Glauber Costa
This patch accounts steal time time in kernel/sched.
I kept it from last proposal, because I still see advantages in it: Doing it here will give us easier access from scheduler variables such as the cpu rq. The next patch shows an example of usage for it.Since functions like
account_idle_time()can be called from multiple places, not onlyaccount_process_tick(), steal time grabbing is repeated in each account function separatedely.
I accept that steal time is worthwhile, but do you have some way to demonstrate that the implementation actually works and is beneficial?
Perhaps run two cpu-bound compute processes on one vcpu, overcommit that vcpu, and see what happens to the processing rate with and without steal time accounting. I'd expect a fairer response with steal time accounting.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v2 3/6] KVM-GST: KVM Steal time accounting
I think we can use performance counter.
use unhalted core cycles event, in the nmi callback funcation, count which process is running .
if the vm exit is caused by nmi,discard it.
the system time of qemu process is the time steal by kvm.
2011/1/30 Avi Kivity <[email protected]>:
Quoted 28 lines · Glauber Costa
Quoted 11 lines
This patch accounts steal time time in kernel/sched.
I kept it from last proposal, because I still see advantages in it: Doing it here will give us easier access from scheduler variables such as the cpu rq. The next patch shows an example of usage for it.Since functions like
account_idle_time()can be called from multiple places, not onlyaccount_process_tick(), steal time grabbing is repeated in each account function separatedely.I accept that steal time is worthwhile, but do you have some way to demonstrate that the implementation actually works and is beneficial?
Perhaps run two cpu-bound compute processes on one vcpu, overcommit that vcpu, and see what happens to the processing rate with and without steal time accounting. I'd expect a fairer response with steal time accounting.
--
error compilingcommittee.c: too many arguments to function--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Peter Zijlstra
maintainerRe: [PATCH v2 2/6] KVM-HV: KVM Steal time implementation
In reply to Glauber Costaarch/x86/kvm/x86.c void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 2132
Quoted 8 lines · Glauber Costa
arch/x86/kvm/x86.c +8
-0+u64 to = (get_kernel_ns () - vcpu->arch.this_time_out); +/* +* using nanoseconds introduces noise, which accumulates easily +* leading to big steal time values. We want, however, to keep the +* interface nanosecond-based for future-proofness. +*/ + to /=NSEC_PER_USEC ; + to *=NSEC_PER_USEC ;
This just doesn't make any sense at all, you use the most expensive and accurate kernel interface to get ns resolution timestamps (ktime_get), and then you truncate the stuff to usec for some totally weird and unexplained reason.
If ktime_get is wrong all our time-keeping is out the window.
Peter Zijlstra
maintainerRe: [PATCH v2 4/6] KVM-GST: KVM Steal time registration
In reply to Glauber Costaarch/x86/kernel/kvm.c static void __init kvm_apf_trap_init(void) · line 573
Quoted 8 lines · Glauber Costa
arch/x86/kernel/kvm.c +8
-0+/* +* using nanoseconds introduces noise, which accumulates easily +* leading to big steal time values. We want, however, to keep the +* interface nanosecond-based for future-proofness. The hypervisor may +* adopt a similar strategy, but we can't rely on that. +*/ + delta /=NSEC_PER_MSEC ; + delta *=NSEC_PER_MSEC ;
I really doubt that claim..
Peter Zijlstra
maintainerRe: [PATCH v2 5/6] KVM-GST: adjust scheduler cpu power
In reply to Glauber CostaQuoted 47 lines · Glauber Costa
kernel/sched.c +43
-0+#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING +static DEFINE_PER_CPU(u64, cpu_steal_time); + +#ifndef CONFIG_64BIT +static DEFINE_PER_CPU(seqcount_t, steal_time_seq); + +static inline void steal_time_write_begin (void ) +{ +__this_cpu_inc (steal_time_seq.sequence); +smp_wmb (); +} + +static inline void steal_time_write_end (void ) +{ +smp_wmb (); +__this_cpu_inc (steal_time_seq.sequence); +} + +static inline u64 steal_time_read (int cpu) +{ +u64 steal_time; +unsigned seq; + +do { + seq =read_seqcount_begin (&per_cpu (steal_time_seq, cpu)); + steal_time =per_cpu (cpu_steal_time, cpu); + }while (read_seqcount_retry (&per_cpu (steal_time_seq, cpu), seq)); + +return steal_time; +} +#else /* CONFIG_64BIT */ +static inline void steal_time_write_begin (void ) +{ +} + +static inline void steal_time_write_end (void ) +{ +} + +static inline u64 steal_time_read (int cpu) +{ +return per_cpu (cpu_steal_time, cpu); +}@@ -3536,6 +3592,11 @@ static int touch_steal_time(int is_idle)
kernel/sched.c static int touch_steal_time(int is_idle) · line 3602
Quoted 10 lines
kernel/sched.c +5
-0if (st) {account_steal_time (st); +#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING +steal_time_write_begin (); +__this_cpu_add (cpu_steal_time, steal); +steal_time_write_end (); +#endif return 1 ; }return 0 ;
Why replicate all logic you've already got in patch 4? That too is reading steal time in a loop in kvm_account_steal_time(), why not extend that interface to take a cpu argument and be done with it?
Peter Zijlstra
maintainerRe: [PATCH v2 5/6] KVM-GST: adjust scheduler cpu power
Quoted 49 lines · Peter Zijlstra
Quoted 47 lines · Glauber Costa
kernel/sched.c +43
-0+#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING +static DEFINE_PER_CPU(u64, cpu_steal_time); + +#ifndef CONFIG_64BIT +static DEFINE_PER_CPU(seqcount_t, steal_time_seq); + +static inline void steal_time_write_begin (void ) +{ +__this_cpu_inc (steal_time_seq.sequence); +smp_wmb (); +} + +static inline void steal_time_write_end (void ) +{ +smp_wmb (); +__this_cpu_inc (steal_time_seq.sequence); +} + +static inline u64 steal_time_read (int cpu) +{ +u64 steal_time; +unsigned seq; + +do { + seq =read_seqcount_begin (&per_cpu (steal_time_seq, cpu)); + steal_time =per_cpu (cpu_steal_time, cpu); + }while (read_seqcount_retry (&per_cpu (steal_time_seq, cpu), seq)); + +return steal_time; +} +#else /* CONFIG_64BIT */ +static inline void steal_time_write_begin (void ) +{ +} + +static inline void steal_time_write_end (void ) +{ +} + +static inline u64 steal_time_read (int cpu) +{ +return per_cpu (cpu_steal_time, cpu); +}@@ -3536,6 +3592,11 @@ static int touch_steal_time(int is_idle)
Quoted 15 lines
kernel/sched.c static int touch_steal_time(int is_idle) · line 3602
Quoted 10 lines
kernel/sched.c +5
-0if (st) {account_steal_time (st); +#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING +steal_time_write_begin (); +__this_cpu_add (cpu_steal_time, steal); +steal_time_write_end (); +#endif return 1 ; }return 0 ;Why replicate all logic you've already got in patch 4? That too is reading steal time in a loop in
kvm_account_steal_time(), why not extend that interface to take a cpu argument and be done with it?
Also, this relies on touch_steal_time() to have ran, not at all something that is tied to calling update_rq_clock().
We can call update_rq_clock() many many times between ticks, as can the stealtime increase because the vcpu can schedule many many times between ticks.
Re: [PATCH v2 2/6] KVM-HV: KVM Steal time implementation
In reply to Avi KivityQuoted 18 lines · Avi Kivity
Quoted 17 lines · Glauber Costa
To implement steal time, we need the hypervisor to pass the guest information about how much time was spent running other processes outside the VM. This is per-vcpu, and using the kvmclock structure for that is an abuse we decided not to make.
In this patchset, I am introducing a new msr,
KVM_MSR_STEAL_TIME, that holds the memory area address containing information about steal timeThis patch contains the hypervisor part for it. I am keeping it separate from the headers to facilitate backports to people who wants to backport the kernel part but not the hypervisor, or the other way around.
arch/x86/kvm/x86.c +0
-0@@ -1528,16 +1528,23 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) vcpu->arch.time_page =gfn_to_page (vcpu->kvm, data>>PAGE_SHIFT );
Quoted 8 lines
arch/x86/kvm/x86.c int kvm_set_msr_common(struct kvm_vcpu *vcpu, u32 msr, u64 data) · line 1532
Quoted 6 lines
arch/x86/kvm/x86.c +0
-4-if (is_error_page (vcpu->arch.time_page)) { -kvm_release_page_clean (vcpu->arch.time_page); - vcpu->arch.time_page =NULL ; - }break ; }Unrelated?
Indeed, leftover from another patch. Thanks
Quoted 9 lines
Quoted 9 lines
arch/x86/kvm/x86.c +5
-0@@ -2106,6 +2120,25 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)kvm_migrate_timers (vcpu); vcpu->cpu = cpu; } + +if (vcpu->arch.this_time_out) { +u64 to = (get_kernel_ns () - vcpu->arch.this_time_out); +/* +* using nanoseconds introduces noise, which accumulates easily
Quoted 8 lines
arch/x86/kvm/x86.c void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 2132
Quoted 5 lines
arch/x86/kvm/x86.c +5
-0+* leading to big steal time values. We want, however, to keep the +* interface nanosecond-based for future-proofness. +*/ + to /=NSEC_PER_USEC ; + to *=NSEC_PER_USEC ;Seems there is a real problem and that this is just papering it over. I'd like to understand the root cause.
Okay, my self-explanation seemed reasonable to me, but since both you and Peter dislike it, I think it is important enough to get a more thorough investigation before a second round. But in this case, I keep that using nanoseconds may then not be the best approach here. We also have to keep in mind that the host and guest clocks may be running at different resolutions.
Quoted 6 lines
arch/x86/kvm/x86.c void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 2135
Quoted 4 lines
arch/x86/kvm/x86.c +3
-0+ vcpu->arch.time_out += to; +kvm_write_guest (vcpu->kvm, (gpa_t)&st->steal, + &vcpu->arch.time_out,sizeof (st->steal));Error check.
Fair.
Quoted 4 lines
arch/x86/kvm/x86.c void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) · line 2136
Quoted 1 line
arch/x86/kvm/x86.c +1
-0+ vcpu->arch.sversion +=2 ;Doesn't survive live migration. You need to use the version from the guest area.
Why not? Who said versions need to always increase? If current version is 102324, and we live migrate and it becomes 0, what is the problem? next update will make it two, that will at most force another read loop in the guest.
The only properties we have to maintain are:
1) Value is even (checked)
2) Value does not change between a read (checked).
Reading the value from the guest just slows it down, imho.
Re: [PATCH v2 4/6] KVM-GST: KVM Steal time registration
In reply to Avi KivityQuoted 7 lines · Avi Kivity
Quoted 6 lines · Glauber Costa
Register steal time within KVM. Everytime we sample the steal time information, we update a local variable that tells what was the last time read. We then account the difference.
Quoted 9 lines
arch/x86/kernel/kvm.c static void __init kvm_smp_prepare_boot_cpu(void) · line 510
Quoted 6 lines
static void kvm_guest_cpu_offline(void *dummy) { kvm_pv_disable_apf(NULL);arch/x86/kernel/kvm.c +1
-0+native_write_msr (MSR_KVM_STEAL_TIME ,0 ,0 );apf_task_wake_all (); }Don't use the
native_versions, they override the pvops implementation. It doesn't matter for kvm, but we're not supposed to know this.
fair.
Quoted 11 lines
arch/x86/kernel/kvm.c static void __init kvm_apf_trap_init(void) · line 573
Quoted 8 lines
arch/x86/kernel/kvm.c +8
-0+/* +* using nanoseconds introduces noise, which accumulates easily +* leading to big steal time values. We want, however, to keep the +* interface nanosecond-based for future-proofness. The hypervisor may +* adopt a similar strategy, but we can't rely on that. +*/ + delta /=NSEC_PER_MSEC ; + delta *=NSEC_PER_MSEC ;You're working around this problem both in the guest and host. So even if we fix it in one, it will still be broken in the other.
And if you notice, in two different ways:
I am (was) forcing to usecs in the host, and msecs in the guest. One of the problems here, is that if we account steal time, we refrain from accounting user / system time. Reason being, that if we account it, we'll end up with more than HZ ticks per HZ, since we'll account ticks as both steal and real.
And since the granularity of the cpu accounting is too coarse, we end up with much more steal time than we should, because things that are less than 1 unity of cputime, are often rounded up to 1 unity of cputime.
Now, I've already said that I will investigate further, and I'm ready to back of from all of this. But assuming my analysis is right so far, what if we keep things in nsecs or msecs, and only convert to cputime in the time of read? This would allow us to just subtract steal time from user/system time, in a more fine grained way.
Re: [PATCH v2 6/6] Describe KVM_MSR_STEAL_TIME
In reply to Avi KivityQuoted 39 lines · Avi Kivity
Documentation/kvm/msr.txt MSR_KVM_ASYNC_PF_EN: 0x4b564d02
Quoted 9 lines · Glauber Costa
This patch adds documentation about usage of the newly introduced
KVM_MSR_STEAL_TIME.Documentation/kvm/msr.txt +5
-0+ +MSR_KVM_STEAL_TIME: 0x4b564d03 + + data: 4-byte alignment physical address of a memory area which must be + in guest RAM, plus an enable bit in bit 0.64-byte aligned:
- avoids wrapping around a page boundary, which may let us optimize things later on (seekvm_write_guest_cached()).
- gives us 5 more unused bits to enable more optionsQuoted 23 lines
This memory is expected to
Documentation/kvm/msr.txt +22
-0+ hold a copy of the following structure: + + struct kvm_steal_time { + __u64 steal; + __u32 version; + __u32 flags; + __u32 pad[6]; + } + + whose data will be filled in by the hypervisor periodically. Only one + write, or registration, is needed for each VCPU. The interval between + updates of this structure is arbitrary and implementation-dependent. + The hypervisor may update this structure at any time it sees fit until + anything with bit0 == 0 is written to it. + + Fields have the following meanings: + + version: guest has to check version before and after grabbing + time information and check that they are both equal and even. + An odd version indicates an in-progress update. + + flags: At this point, always zero. May be used to indicate
Quoted 8 lines
Documentation/kvm/msr.txt MSR_KVM_ASYNC_PF_EN: 0x4b564d02 · line 219
Quoted 5 lines
Documentation/kvm/msr.txt +5
-0+ changes in this structure in the future. + + steal: the amount of time in which this vCPU did not run, in + nanoseconds. +The guest must initialize the entire 64-byte structure to zero before enabling the feature.
I honestly don't see why. But I also don't see why not... Will update it.
Re: [PATCH v2 3/6] KVM-GST: KVM Steal time accounting
In reply to Avi KivityQuoted 12 lines · Avi Kivity
Quoted 10 lines · Glauber Costa
This patch accounts steal time time in kernel/sched.
I kept it from last proposal, because I still see advantages in it: Doing it here will give us easier access from scheduler variables such as the cpu rq. The next patch shows an example of usage for it.Since functions like
account_idle_time()can be called from multiple places, not onlyaccount_process_tick(), steal time grabbing is repeated in each account function separatedely.
Quoted 6 lines
I accept that steal time is worthwhile, but do you have some way to demonstrate that the implementation actually works and is beneficial?
Perhaps run two cpu-bound compute processes on one vcpu, overcommit that vcpu, and see what happens to the processing rate with and without steal time accounting. I'd expect a fairer response with steal time accounting.
Avi,
There are two things here:
One of them, which is solely the accounting of steal time, (patches 1 to
4) has absolutely nothing to do with what you said. Its sole purpose is to provide the user with information about "why is my process slow if I am using 100 % of my cpu?")
The last patch is the only one that actually tries to rebalance cpus according to steal time information. For that, I do have some experiments I did here to see if it is working, will try to provide more precise data in the next submission.
Re: [PATCH v2 3/6] KVM-GST: KVM Steal time accounting
In reply to lidong chenQuoted 5 lines · lidong chen
I think we can use performance counter.
use unhalted core cycles event, in the nmi callback funcation, count which process is running .
if the vm exit is caused by nmi,discard it.
the system time of qemu process is the time steal by kvm.
Performance counters are a scarce resource, so I'd rather not use them, since it will mean forcing a context switch from whoever is using it at the moment. Which is also an expensive operation anyway.
So even though it can be possible, in theory, I don't see why use it in this particular case.
Quoted 31 lines
2011/1/30 Avi Kivity <[email protected]>:
Quoted 28 lines · Glauber Costa
Quoted 11 lines
This patch accounts steal time time in kernel/sched.
I kept it from last proposal, because I still see advantages in it: Doing it here will give us easier access from scheduler variables such as the cpu rq. The next patch shows an example of usage for it.Since functions like
account_idle_time()can be called from multiple places, not onlyaccount_process_tick(), steal time grabbing is repeated in each account function separatedely.I accept that steal time is worthwhile, but do you have some way to demonstrate that the implementation actually works and is beneficial?
Perhaps run two cpu-bound compute processes on one vcpu, overcommit that vcpu, and see what happens to the processing rate with and without steal time accounting. I'd expect a fairer response with steal time accounting.
--
error compilingcommittee.c: too many arguments to function--
To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH v2 5/6] KVM-GST: adjust scheduler cpu power
In reply to Peter ZijlstraQuoted 49 lines · Peter Zijlstra
Quoted 47 lines · Glauber Costa
kernel/sched.c +43
-0+#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING +static DEFINE_PER_CPU(u64, cpu_steal_time); + +#ifndef CONFIG_64BIT +static DEFINE_PER_CPU(seqcount_t, steal_time_seq); + +static inline void steal_time_write_begin (void ) +{ +__this_cpu_inc (steal_time_seq.sequence); +smp_wmb (); +} + +static inline void steal_time_write_end (void ) +{ +smp_wmb (); +__this_cpu_inc (steal_time_seq.sequence); +} + +static inline u64 steal_time_read (int cpu) +{ +u64 steal_time; +unsigned seq; + +do { + seq =read_seqcount_begin (&per_cpu (steal_time_seq, cpu)); + steal_time =per_cpu (cpu_steal_time, cpu); + }while (read_seqcount_retry (&per_cpu (steal_time_seq, cpu), seq)); + +return steal_time; +} +#else /* CONFIG_64BIT */ +static inline void steal_time_write_begin (void ) +{ +} + +static inline void steal_time_write_end (void ) +{ +} + +static inline u64 steal_time_read (int cpu) +{ +return per_cpu (cpu_steal_time, cpu); +}@@ -3536,6 +3592,11 @@ static int touch_steal_time(int is_idle)
Quoted 15 lines
kernel/sched.c static int touch_steal_time(int is_idle) · line 3602
Quoted 10 lines
kernel/sched.c +5
-0if (st) {account_steal_time (st); +#ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING +steal_time_write_begin (); +__this_cpu_add (cpu_steal_time, steal); +steal_time_write_end (); +#endif return 1 ; }return 0 ;Why replicate all logic you've already got in patch 4? That too is reading steal time in a loop in
kvm_account_steal_time(), why not extend that interface to take a cpu argument and be done with it?
Because that part is kvm-specific, and this is scheduler general. It seemed cleaner to me to do it this way. But I can do it differently, certainly.
Quoted 1 line
Peter Zijlstra
maintainerRe: [PATCH v2 4/6] KVM-GST: KVM Steal time registration
In reply to Glauber CostaQuoted 4 lines · Glauber Costa
And since the granularity of the cpu accounting is too coarse, we end up with much more steal time than we should, because things that are less than 1 unity of cputime, are often rounded up to 1 unity of cputime.
See, that! is the problem, don't round up like that.
What you can do is: steal_ticks = steal_time_clock() / TICK_NSEC, or simply keep a steal time delta and every time it overflows cputime_one_jiffy insert a steal-time tick.
Venki might have created some infrastructure for doing this with the IRQ_TIME accounting mess, but irqtime_account_process_tick() still gives me a head-ache.
Peter Zijlstra
maintainerRe: [PATCH v2 5/6] KVM-GST: adjust scheduler cpu power
In reply to Glauber CostaQuoted 4 lines · Glauber Costa
Because that part is kvm-specific, and this is scheduler general. It seemed cleaner to me to do it this way. But I can do it differently, certainly.
Well, any steal time clock will be hypervisor specific, but if we agree that anything that enables CONFIG_PARAVIRT_TIME_ACCOUNTING provides a u64 steal_time_clock(int cpu) function then all should be well, right?
The bit you have in kvm is almost that, except it assumes cpu == this_cpu.
You simply cannot rely on the silly tick accounting to drive any clock, its archaic.
Re: [PATCH v2 5/6] KVM-GST: adjust scheduler cpu power
Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Glauber Costa
Because that part is kvm-specific, and this is scheduler general. It seemed cleaner to me to do it this way. But I can do it differently, certainly.
Well, any steal time clock will be hypervisor specific, but if we agree that anything that enables
CONFIG_PARAVIRT_TIME_ACCOUNTINGprovides a u64steal_time_clock(int cpu)function then all should be well, right?
Once the hypervisor provided the data, it can all be generic, and have large parts of it that are generic, living in sched.c.
Quoted 5 lines
The bit you have in kvm is almost that, except it assumes cpu ==
this_cpu.You simply cannot rely on the silly tick accounting to drive any clock, its archaic.
Which tick accounting? In your other e-mail , you pointed that this only runs in touch_steal_time, which is fine, will change. But all the rest here, that is behind the hypervisor specific vs generic code has nothing to do with ticks at all.
Quoted 2 lines
Re: [PATCH v2 4/6] KVM-GST: KVM Steal time registration
In reply to Peter ZijlstraQuoted 3 lines
See, that! is the problem, don't round up like that.
Yeah, I was using usecs_to_cputime(), believing this was the standard interface. By the way, one of the things that also led to better results were just forcing it to 0 every time we had steal == 1 in the end. But
*that* was a real hack =)
Quoted 3 lines
What you can do is:
steal_ticks=steal_time_clock()/TICK_NSEC, or simply keep a steal time delta and every time it overflowscputime_one_jiffyinsert a steal-time tick.
What do you think about keeping accounting in msec/usec resolution and (thus allowing us to compute half a tick to user/system, other half to steal time) only change it to cputime in the last minute?
Re: [PATCH v2 2/6] KVM-HV: KVM Steal time implementation
In reply to Glauber CostaQuoted 15 lines · Glauber Costa
Quoted 15 lines
Quoted 14 lines
@@ -2106,6 +2120,25 @@ void
kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)kvm_migrate_timers(vcpu); vcpu->cpu = cpu; }+
+ if (vcpu->arch.this_time_out) {
+ u64 to = (get_kernel_ns()-vcpu->arch.this_time_out);
+ /*
+ * using nanoseconds introduces noise, which accumulates easily
+ * leading to big steal time values. We want, however, to keep the
+ * interface nanosecond-based for future-proofness.
+ */
+ to /=NSEC_PER_USEC;
+ to *=NSEC_PER_USEC;
Quoted 5 lines
Quoted 2 lines
Seems there is a real problem and that this is just papering it over. I'd like to understand the root cause.
Okay, my self-explanation seemed reasonable to me, but since both you and Peter dislike it, I think it is important enough to get a more thorough investigation before a second round.
Yes please.
Quoted 4 lines
But in this case,
I keep that using nanoseconds may then not be the best approach here. We also have to keep in mind that the host and guest clocks may be running at different resolutions.
We need to choose a resolution for the clock (or negotiate one), an nanoseconds seems as good as any from a range and precision considerations, and is convenient for the host and Linux guests. So why not pick it?
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
+
vcpu->arch.sversion+= 2;Doesn't survive live migration. You need to use the version from the guest area.
Why not? Who said versions need to always increase? If current version is 102324, and we live migrate and it becomes 0, what is the problem?
Guest reads version (result: 2)
Guest starts reading data
Live migration; vcpu->arch.sversion is zeroed Steal time update; vcpu->arch.sversion += 2; write to guest Guest continues reading data
Guest reads version (result: 2)
So the guest is unaware that an update has occurred while it was reading the data.
Signature · 1 line
error compiling committee.c: too many arguments to function
Peter Zijlstra
maintainerRe: [PATCH v2 4/6] KVM-GST: KVM Steal time registration
In reply to Glauber CostaQuoted 8 lines · Glauber Costa
Quoted 4 lines
What you can do is:
steal_ticks=steal_time_clock()/TICK_NSEC, or simply keep a steal time delta and every time it overflowscputime_one_jiffyinsert a steal-time tick.What do you think about keeping accounting in msec/usec resolution and (thus allowing us to compute half a tick to user/system, other half to steal time) only change it to cputime in the last minute?
its only accounting full tick..
Peter Zijlstra
maintainerRe: [PATCH v2 5/6] KVM-GST: adjust scheduler cpu power
In reply to Glauber CostaQuoted 4 lines · Glauber Costa
Which tick accounting? In your other e-mail , you pointed that this only runs in
touch_steal_time, which is fine, will change.
That tick ;-), all the account_foo muck is per tick.
Quoted 3 lines
But all the rest
here, that is behind the hypervisor specific vs generic code has nothing to do with ticks at all.
But I don't get it, there is no generic code needed, all that's needed is u64 steal_time_clock(int cpu), and the first part of your kvm_account_steal_time() function is exactly that if you add the cpu argument.
+static u64 steal_time_clock(int cpu)
+{
+ u64 steal_time; + struct kvm_steal_time *src; + int version;
+
+ preempt_disable();
+ src = &per_cpu_ptr(steal_time, cpu);
+ do {
+ version = src->version; + rmb(); + steal_time = src->steal; + rmb();
+ } while ((src->version & 1) || (version != src->version));
+ preempt_enable();
+
+ return steal_time
+}
And you're done.. no need to for any of that steal_time_{read,write} business.
Re: [PATCH v2 5/6] KVM-GST: adjust scheduler cpu power
Quoted 36 lines · Peter Zijlstra
Quoted 4 lines · Glauber Costa
Which tick accounting? In your other e-mail , you pointed that this only runs in
touch_steal_time, which is fine, will change.That tick ;-), all the
account_foomuck is per tick.Quoted 3 lines
But all the rest
here, that is behind the hypervisor specific vs generic code has nothing to do with ticks at all.But I don't get it, there is no generic code needed, all that's needed is u64
steal_time_clock(int cpu), and the first part of yourkvm_account_steal_time()function is exactly that if you add the cpu argument.(quoted diff) +18
-0+static u64 steal_time_clock(int cpu) +{ + u64 steal_time; + struct kvm_steal_time *src; + int version; + + preempt_disable(); + src = &per_cpu_ptr(steal_time, cpu); + do { + version = src->version; + rmb(); + steal_time = src->steal; + rmb(); + } while ((src->version & 1) || (version != src->version)); + preempt_enable(); + + return steal_time +}
Quoted 1 line
And you're done.. no need to for any of that steal_time_{read,write} business.
update_rq_clock_task still have to keep track of what was the last steal time value we saw, in the same way it does for irq. One option is to call update_rq_clock_task from inside kvm-code, but I don't really like it very much.
But okay, there are many ways to work around it, I'll cook something.
Re: [PATCH v2 2/6] KVM-HV: KVM Steal time implementation
In reply to Avi KivityQuoted 48 lines · Avi Kivity
Quoted 15 lines · Glauber Costa
Quoted 15 lines
Quoted 14 lines
@@ -2106,6 +2120,25 @@ void
kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)kvm_migrate_timers(vcpu); vcpu->cpu = cpu; }+
+ if (vcpu->arch.this_time_out) {
+ u64 to = (get_kernel_ns()-vcpu->arch.this_time_out);
+ /*
+ * using nanoseconds introduces noise, which accumulates easily
+ * leading to big steal time values. We want, however, to keep the
+ * interface nanosecond-based for future-proofness.
+ */
+ to /=NSEC_PER_USEC;
+ to *=NSEC_PER_USEC;Quoted 5 lines
Quoted 2 lines
Seems there is a real problem and that this is just papering it over. I'd like to understand the root cause.
Okay, my self-explanation seemed reasonable to me, but since both you and Peter dislike it, I think it is important enough to get a more thorough investigation before a second round.
Yes please.
Quoted 4 lines
But in this case,
I keep that using nanoseconds may then not be the best approach here. We also have to keep in mind that the host and guest clocks may be running at different resolutions.We need to choose a resolution for the clock (or negotiate one), an nanoseconds seems as good as any from a range and precision considerations, and is convenient for the host and Linux guests. So why not pick it?
Quoted 6 lines
Quoted 4 lines
Quoted 1 line
+
vcpu->arch.sversion+= 2;Doesn't survive live migration. You need to use the version from the guest area.
Why not? Who said versions need to always increase? If current version is 102324, and we live migrate and it becomes 0, what is the problem?
Guest reads version (result: 2)
Guest starts reading data
Live migration;vcpu->arch.sversionis zeroed Steal time update;vcpu->arch.sversion+= 2; write to guest Guest continues reading data
Guest reads version (result: 2)
Quoted 2 lines
So the guest is unaware that an update has occurred while it was reading the data.
Ok, fair.
By the way, kvmclock have the same problem, then
Quoted 1 line
Peter Zijlstra
maintainerRe: [PATCH v2 5/6] KVM-GST: adjust scheduler cpu power
In reply to Glauber CostaQuoted 3 lines · Glauber Costa
update_rq_clock_taskstill have to keep track of what was the last steal time value we saw, in the same way it does for irq.
Right, the CONFIG_SCHED_PARAVIRT patch I sent earlier adds a prev_steal_time member to struct rq for this purpose.
Quoted 3 lines
One option is to
callupdate_rq_clock_taskfrom inside kvm-code, but I don't really like it very much.
Why would you need to call anything from the kvm code?
Simply make u64 steal_time_clock(int cpu) a paravirt function with u64 native_steal_time_clock(int cpu) { return 0ULL; }. Possibly avoid the whole CONFIG_SCHED_PARAVIRT block in update_rq_clock_task() for ! paravirt guests.
Re: [PATCH v2 4/6] KVM-GST: KVM Steal time registration
In reply to Peter ZijlstraQuoted 6 lines · Peter Zijlstra
Quoted 5 lines · Glauber Costa
Quoted 4 lines
What you can do is:
steal_ticks=steal_time_clock()/TICK_NSEC, or simply keep a steal time delta and every time it overflowscputime_one_jiffyinsert a steal-time tick.
Quoted 6 lines
Quoted 3 lines
What do you think about keeping accounting in msec/usec resolution and (thus allowing us to compute half a tick to user/system, other half to steal time) only change it to cputime in the last minute?
its only accounting full tick..
Yes. The way we ended up dealing with this in irq time case is track it in fine granularity and accumulate over time (internally) but account it (make it visible externally) only in terms of ticks, only when the value being accumulated crosses the tick boundary. This does has a hole when we use 99% of time on tick on irq and use 1% just before the tick on some system, then whole tick will be system and on next tick if there is 1% irq and 99% system then that will be accounted as irq as our accumulated value crosses the tick boundary then. But, such holes on avg should not be worse than not having fine granularity.
Thanks,
Venki
Re: [PATCH v2 2/6] KVM-HV: KVM Steal time implementation
In reply to Glauber CostaQuoted 8 lines · Glauber Costa
Quoted 8 lines
Guest reads version (result: 2)
Guest starts reading data
Live migration;vcpu->arch.sversionis zeroed Steal time update;vcpu->arch.sversion+= 2; write to guest Guest continues reading data
Guest reads version (result: 2)
Quoted 6 lines
Quoted 2 lines
So the guest is unaware that an update has occurred while it was reading the data.
Ok, fair.
By the way, kvmclock have the same problem, then
It needs the same fix then.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v2 3/6] KVM-GST: KVM Steal time accounting
In reply to Glauber CostaQuoted 20 lines · Glauber Costa
Quoted 12 lines · Avi Kivity
Quoted 10 lines · Glauber Costa
This patch accounts steal time time in kernel/sched.
I kept it from last proposal, because I still see advantages in it: Doing it here will give us easier access from scheduler variables such as the cpu rq. The next patch shows an example of usage for it.Since functions like
account_idle_time()can be called from multiple places, not onlyaccount_process_tick(), steal time grabbing is repeated in each account function separatedely.Quoted 6 lines
I accept that steal time is worthwhile, but do you have some way to demonstrate that the implementation actually works and is beneficial?
Perhaps run two cpu-bound compute processes on one vcpu, overcommit that vcpu, and see what happens to the processing rate with and without steal time accounting. I'd expect a fairer response with steal time accounting.
Quoted 7 lines
Avi,
There are two things here:
One of them, which is solely the accounting of steal time, (patches 1 to
4) has absolutely nothing to do with what you said. Its sole purpose is to provide the user with information about "why is my process slow if I am using 100 % of my cpu?")
Right. Like irq and softirq time, we need to report this to the user, as it's potentially much higher.
Quoted 5 lines
The last patch is the only one that actually tries to rebalance cpus according to steal time information. For that, I do have some experiments I did here to see if it is working, will try to provide more precise data in the next submission.
Thanks.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v2 6/6] Describe KVM_MSR_STEAL_TIME
In reply to Glauber CostaQuoted 6 lines · Glauber Costa
Quoted 3 lines
The guest must initialize the entire 64-byte structure to zero before enabling the feature.
I honestly don't see why. But I also don't see why not... Will update it.
Requiring the guest to initialize it to zero allows us to later assign meanings to various fields, as long as a zero value is backwards compatible.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v2 3/6] KVM-GST: KVM Steal time accounting
In reply to Avi KivityQuoted 26 lines · Avi Kivity
Quoted 25 lines · Glauber Costa
Quoted 12 lines · Avi Kivity
Quoted 10 lines · Glauber Costa
This patch accounts steal time time in kernel/sched.
I kept it from last proposal, because I still see advantages in it: Doing it here will give us easier access from scheduler variables such as the cpu rq. The next patch shows an example of usage for it.Since functions like
account_idle_time()can be called from multiple places, not onlyaccount_process_tick(), steal time grabbing is repeated in each account function separatedely.Quoted 4 lines
I accept that steal time is worthwhile, but do you have some way to demonstrate that the implementation actually works and is beneficial?
Perhaps run two cpu-bound compute processes on one vcpu,
overcommit that
Quoted 1 line
vcpu, and see what happens to the processing rate with and without
steal
Quoted 1 line
time accounting. I'd expect a fairer response with steal time
accounting.
Avi,
Quoted 7 lines
Quoted 5 lines
There are two things here:
One of them, which is solely the accounting of steal time, (patches 1 to
4) has absolutely nothing to do with what you said. Its sole purpose is to provide the user with information about "why is my process slow if I am using 100 % of my cpu?")Right. Like irq and softirq time, we need to report this to the user, as it's potentially much higher.
Of course, it's not enough to just account for this time, you also have to expose it somewhere, and update tools like top(1) to display it.
Signature · 1 line
error compiling committee.c: too many arguments to function
Re: [PATCH v2 3/6] KVM-GST: KVM Steal time accounting
Quoted 33 lines · Avi Kivity
Quoted 32 lines · Avi Kivity
Quoted 30 lines · Glauber Costa
Quoted 12 lines · Avi Kivity
Quoted 10 lines · Glauber Costa
This patch accounts steal time time in kernel/sched.
I kept it from last proposal, because I still see advantages in it: Doing it here will give us easier access from scheduler variables such as the cpu rq. The next patch shows an example of usage for it.Since functions like
account_idle_time()can be called from multiple places, not onlyaccount_process_tick(), steal time grabbing is repeated in each account function separatedely.Quoted 4 lines
I accept that steal time is worthwhile, but do you have some way to demonstrate that the implementation actually works and is beneficial?
Perhaps run two cpu-bound compute processes on one vcpu,
overcommit that
Quoted 1 line
vcpu, and see what happens to the processing rate with and without
steal
Quoted 1 line
time accounting. I'd expect a fairer response with steal time
accounting.
Avi,
There are two things here:
One of them, which is solely the accounting of steal time, (patches 1 to
4) has absolutely nothing to do with what you said. Its sole purpose is to provide the user with information about "why is my process slow if I am using 100 % of my cpu?")
Quoted 5 lines
Quoted 2 lines
Right. Like irq and softirq time, we need to report this to the user, as it's potentially much higher.
Of course, it's not enough to just account for this time, you also have to expose it somewhere, and update tools like top(1) to display it.
Yes, what I meant is that just the accounting will just expose it to the tools, won't affect the scheduler in any sense.