Skip to contentSkip to conversation

i386 Vmi timer patch

RFC PATCH In review Zachary Amsden 1 participant 1 message Mar 13, 2006

lore
Share & tools
lore
#0 lore raw

RFC PATCH 23/24 i386 Vmi timer patch

In a virtualized environment, virtual machines will time share the system with each other and with other processes running on the host system. Therefore, a VM's virtual CPUs (VCPUs) will be executing on the host's physical CPUs (pcpus) for only some portion of time.

The VMI exposes a paravirtual view of time to the guest operating systems so that they may operate more effectively in a virtual environment. The interface also provides a way for the VCPUs to set alarms in this paravirtual view of time.

A VCPU is always in one of three states: running, halted, or ready. The VCPU is in the 'running' state if it is executing. When the VCPU executes the Halt interface, the VCPU enters the 'halted' state and remains halted until there is some work pending for the VCPU (e.g. an alarm expires, host I/O completes on behalf of virtual I/O). At this point, the VCPU enters the 'ready' state (waiting for the hypervisor to reschedule it). Finally, at any time when the VCPU is not in the 'running' state nor the 'halted' state, it is in the 'ready' state.

The VMI provides cycle counters for three time domains: real time, available time and stolen time. Real time progresses when the VCPU is in the 'halted', 'ready' and 'running' states. Stolen time is defined per VCPU to progress at the rate of real time when the VCPU is in the 'ready' state, and does not progress otherwise. Available time is defined per VCPU to progress at the rate of real time when the VCPU is in the 'running' and 'halted' states, and does not progress when the VCPU is in the 'ready' state. Additionally, the wallclock time is provided by the VMI. See the VMI specification for more details.

This patch provides a Linux time source, the VMI Time device, that is programmed against the cycle counters and alarms provided by the VMI. It is used as the time source when the kernel detects the presence of a hypervisor.

Some noteworthy characteristics of the VMI Time device code are:

1) Cycle counter frequency is indicated by the VMI rather than needing to be calculated by comparing the counter rate to a known interrupt rate.

On native hardware, the TSC frequency is calculated by comparing the rate at which the TSC advances with the (known) rate of a timer interrupt. This method for determining a cycle counter frequency is unreliable on virtual hardware since the VCPU may be preempted by the hypervisor during this calculation.

Instead, the VMI Time device determines the frequency of the VMI cycle counters by querying the VMI.

2) The timer_irq_works() algorithm is problematic in a virtual environment for various reasons, and is avoided. When running on a hypervisor, the kernel can assume that the alarm will be wired up correctly to the interrupt controller and this logic is avoided altogether.

3) Rather than keeping kernel time state (e.g. jiffies, xtime, time slices) up to date by counting interrupts, the state is updated based on the values of the VMI cycle counters.

Therefore, a VCPU only needs to receive timer interrupts while it is running. This leads to better scaling of the number of virtual machines that can be run on a single host.

4) As a consequence of #3, the interrupt rate of the alarm can be dropped lower than HZ. It may be beneficial for the guest to request a lower alarm rate to decrease the overhead of delivering virtual interrupts. With #3, this is possible without changing the HZ constant (which is important in order to allow for transparency with native hardware using the normal HZ rate).

5) System wide time state (e.g. jiffies, xtime) is updated by all VCPUs.

With the native x86 timer interrupts, only the boot processor's PIT interrupt updates jiffies and xtime. With the VMI Time device, jiffies and xtime are updated by all the VCPUs of a VM. This is important since the VCPUs may be independently scheduled. Also, it allows for a simpler NO_IDLE_HZ implementation.

6) The xtime counter is kept up with the wallclock time.

7) The jiffies counter is kept up with the real time cycle counter.

8) Scheduler time slices and sched_clock() are kept up with the available time cycle counter.

When using the VMI Time device as the time source, the scheduler runs in available time. Otherwise, processes would be charged for time stolen from the VCPU (time in which the VCPU isn't even running). The scheduler_tick() interface is called only for every tick of available time. Also, sched_clock() is implemented using the available time counter so that the scheduler's calculations of a process' run-time does not charge stolen time.

9) Stolen time is computed by the hypervisor, not the Linux guest.

Only the hypervisor can accurately compute stolen time. On some hypervisors, stolen time is computed by the guest by subtracting the amount of time a VCPU has run on a PCPU from the amount of real time that has passed. Then, the routine account_steal_time() is used to charge this time difference to
a) steal time, if the current process is not the idle process, or
b) idle time, if the current process is the idle process.

The problem with that calculation is that when a VCPU's halt ends, it will not necessarily run immediately. Instead, it will be transitioned from the 'halted' state to the 'ready' state (i.e. added to the hypervisor's runqueue) until the hypervisor chooses to run it. The time the VCPU is 'ready' is not idle time, but instead stolen time. But, implementations using account_steal_time() will account all of this time to idle time, rather than partitioning the time such that the time spent in the 'halted' state is idle time and the time spent in the 'ready' state is stolen time.

Only the hypervisor can determine when the transition from 'halted' to 'ready' occurred (due to work becoming pending for the VCPU). So, the VMI Time device queries the stolen time from the VMI rather than using the faulty account_steal_time() algorithm.

arch/i386/Kconfig +31 -0
Index: linux-2.6.16-rc6/arch/i386/Kconfig
===================================================================
@@ -193,6 +193,37 @@ config VMI_REQUIRE_HYPERVISOR
           This option forces the kernel to run with a hypervisor present.
           The kernel will panic if booted on native hardware.
 
+choice 
+	prompt "VMI alarm rate"
+	default VMI_ALARM_HZ_100
+
+config VMI_ALARM_HZ_100
+	bool "Use 100 hz"
+        help
+          Sets the interrupt rate for the VMI-timer alarm, which is the
+          timer device that is used when running on a hypervisor.
+
+          Sets the alarm rate to 100 interrupts per second.
+
+          This option has no effect when running on native hardware.
+
+config VMI_ALARM_HZ_HZ
+	bool "Use HZ"
+	help
+          Sets the interrupt rate for the VMI-timer alarm, which is the
+          timer device that is used when running on a hypervisor.
+
+          Sets the alarm rate to HZ interrupts per second.
+
+          This option has no effect when running on native hardware.
+
+endchoice
+
+config VMI_ALARM_HZ
+        int
+        default 100 if VMI_ALARM_HZ_100
+        default HZ if VMI_ALARM_HZ_HZ
+
 config VMI_DEBUG
 	bool "VMI debugging"
 	default n
arch/i386/kernel/timers/Makefile +1 -0
Index: linux-2.6.16-rc6/arch/i386/kernel/timers/Makefile
===================================================================
@@ -7,3 +7,4 @@ obj-y := timer.o timer_none.o timer_tsc.
 obj-$(CONFIG_X86_CYCLONE_TIMER)	+= timer_cyclone.o
 obj-$(CONFIG_HPET_TIMER)	+= timer_hpet.o
 obj-$(CONFIG_X86_PM_TIMER)	+= timer_pm.o
+obj-$(CONFIG_X86_VMI)           += timer_vmi.o
arch/i386/kernel/timers/timer.c +3 -0
Index: linux-2.6.16-rc6/arch/i386/kernel/timers/timer.c
===================================================================
@@ -13,6 +13,9 @@
 #endif
 /* list of timers, ordered by preference, NULL terminated */
 static struct init_timer_opts* __initdata timers[] = {
+#ifdef CONFIG_X86_VMI
+	&timer_vmi_init,
+#endif
 #ifdef CONFIG_X86_CYCLONE_TIMER
 	&timer_cyclone_init,
 #endif
arch/i386/kernel/timers/timer_tsc.c +5 -8
Index: linux-2.6.16-rc6/arch/i386/kernel/timers/timer_tsc.c
===================================================================
@@ -22,6 +22,7 @@
 
 #include "io_ports.h"
 #include "mach_timer.h"
+#include "mach_schedclock.h"
 
 #include <asm/hpet.h>
 #include <asm/i8253.h>
@@ -36,6 +37,7 @@ static inline void cpufreq_delayed_get(v
 
 int tsc_disable __devinitdata = 0;
 
+int use_sched_clock_cycles;
 static int use_tsc;
 /* Number of usecs that the last interrupt was delayed */
 static int delay_at_last_interrupt;
@@ -116,23 +118,17 @@ static unsigned long long monotonic_cloc
  */
 unsigned long long sched_clock(void)
 {
-	unsigned long long this_offset;
-
 	/*
 	 * In the NUMA case we dont use the TSC as they are not
 	 * synchronized across all CPUs.
 	 */
 #ifndef CONFIG_NUMA
-	if (!use_tsc)
+	if (!use_sched_clock_cycles)
 #endif
 		/* no locking but a rare wrong value is not a big deal */
 		return jiffies_64 * (1000000000 / HZ);
-
-	/* Read the Time Stamp Counter */
-	rdtscll(this_offset);
-
 	/* return the value in ns */
-	return cycles_2_ns(this_offset);
+	return cycles_2_ns(sched_clock_cycles());
 }
 
 static void delay_tsc(unsigned long loops)
@@ -510,6 +506,7 @@ static int __init init_tsc(char* overrid
 			init_xtime_from_cmos();
 			fast_gettimeoffset_quotient = tsc_quotient;
 			use_tsc = 1;
+			use_sched_clock_cycles = 1;
 			/*
 			 *	We could be more selective here I suspect
 			 *	and just enable this for the next intel chips ?
arch/i386/kernel/timers/timer_vmi.c +421 -0

421 lines not on this page · raw message

arch/i386/mach-vmi/setup.c +18 -2
Index: linux-2.6.16-rc6/arch/i386/mach-vmi/setup.c
===================================================================
@@ -41,12 +41,16 @@
 #include <asm/pgtable.h>
 #include <vmi.h>
 
+extern irqreturn_t vmi_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs);
+extern fastcall void apic_vmi_timer_interrupt(void);
+
 extern char __VMI_END;
 extern char __VMI_START;
 extern char __VMI_SHARED;
 VROMHeader *vmi_rom = NULL;
 
 VMI_UINT8 hypervisor_found;
+VMI_UINT8 hypervisor_timer_found;
 
 /* Convenient macro for calling VMI functions indirectly in the ROM */
 typedef VMI_UINT32 __attribute__((regparm(1))) (VROMFUNC)(void);
@@ -201,6 +205,11 @@ void __init intr_init_hook(void)
 {
 #ifdef CONFIG_X86_LOCAL_APIC
 	apic_intr_init();
+
+	/* if the VMI-timer is used, redirect the local APIC timer interrupt
+	 * gate to point to the vmi interrupt handler. */
+	if (vmi_timer_used())
+		set_intr_gate(LOCAL_TIMER_VECTOR, apic_vmi_timer_interrupt);
 #endif
 
 	setup_irq(2, &irq2);
@@ -275,8 +284,10 @@ void __init vmi_init(void)
 		       " - falling back to native mode\n",
 		       VMI_API_REV_MAJOR, MIN_VMI_API_REV_MINOR);
 
-	if (hypervisor_found)
+	if (hypervisor_found) {
 		pm_power_off = vmi_power_off;
+		probe_vmi_timer();
+	}
 }
 
 
@@ -306,6 +317,8 @@ void __init trap_init_hook(void)
 
 static struct irqaction irq0  = { timer_interrupt, SA_INTERRUPT, CPU_MASK_NONE, "timer", NULL, NULL};
 
+static struct irqaction vmi_irq0  = { vmi_timer_interrupt, SA_INTERRUPT, CPU_MASK_NONE, "VMI-alarm", NULL, NULL};
+
 /**
  * time_init_hook - do any specific initialisations for the system timer.
  *
@@ -315,5 +328,8 @@ static struct irqaction irq0  = { timer_
  **/
 void __init time_init_hook(void)
 {
-	setup_irq(0, &irq0);
+	if (vmi_timer_used())
+		setup_irq(0, &vmi_irq0);
+	else
+		setup_irq(0, &irq0);
 }
include/asm-i386/timer.h +11 -0
Index: linux-2.6.16-rc6/include/asm-i386/timer.h
===================================================================
@@ -48,6 +48,8 @@ extern int pit_latch_buggy;
 extern struct timer_opts *cur_timer;
 extern int timer_ack;
 
+extern int use_sched_clock_cycles;
+
 /* list of externed timers */
 extern struct timer_opts timer_none;
 extern struct timer_opts timer_pit;
@@ -70,9 +72,18 @@ extern unsigned long calibrate_tsc_hpet(
 extern struct init_timer_opts timer_pmtmr_init;
 #endif
 
+#ifdef CONFIG_X86_VMI
+extern struct init_timer_opts timer_vmi_init;
+void setup_vmi_timer(void);
+#endif
+
 static inline void setup_system_timer(void) 
 {
+#ifdef CONFIG_X86_VMI
+	setup_vmi_timer();
+#else
 	setup_pit_timer();
+#endif
 }
 
 /* convert from cycles(64bits) => nanoseconds (64bits)
include/asm-i386/mach-vmi/mach_apictimer.h +58 -0

58 lines not on this page · raw message

include/asm-i386/mach-vmi/mach_schedclock.h +43 -0

43 lines not on this page · raw message

include/asm-i386/mach-vmi/entry_arch.h +1 -0
Index: linux-2.6.16-rc6/include/asm-i386/mach-vmi/entry_arch.h
===================================================================
@@ -24,6 +24,7 @@ BUILD_INTERRUPT(call_function_interrupt,
  */
 #ifdef CONFIG_X86_LOCAL_APIC
 BUILD_INTERRUPT(apic_timer_interrupt,LOCAL_TIMER_VECTOR)
+BUILD_INTERRUPT(apic_vmi_timer_interrupt,LOCAL_TIMER_VECTOR)
 BUILD_INTERRUPT(error_interrupt,ERROR_APIC_VECTOR)
 BUILD_INTERRUPT(spurious_interrupt,SPURIOUS_APIC_VECTOR)
 
include/asm-i386/mach-vmi/vmi_time.h +142 -0

142 lines not on this page · raw message

include/asm-i386/mach-default/mach_schedclock.h +18 -0
Index: linux-2.6.16-rc6/include/asm-i386/mach-default/mach_schedclock.h
===================================================================
@@ -0,0 +1,18 @@
+
+/*
+ * Machine specific sched_clock routines.
+ */
+
+#ifndef __ASM_MACH_SCHEDCLOCK_H
+#define __ASM_MACH_SCHEDCLOCK_H
+
+#include <mach_msr.h>
+
+static inline unsigned long long sched_clock_cycles(void)
+{
+	unsigned long long cycles;
+	rdtscll(cycles);
+	return cycles;
+}
+
+#endif /* __ASM_MACH_SCHEDCLOCK_H */
include/linux/kernel_stat.h +1 -0
Index: linux-2.6.16-rc6/include/linux/kernel_stat.h
===================================================================
@@ -55,5 +55,6 @@ static inline int kstat_irqs(int irq)
 extern void account_user_time(struct task_struct *, cputime_t);
 extern void account_system_time(struct task_struct *, int, cputime_t);
 extern void account_steal_time(struct task_struct *, cputime_t);
+extern void account_cpu_steal_time(cputime_t);
 
 #endif /* _LINUX_KERNEL_STAT_H */
kernel/sched.c +18 -3
Index: linux-2.6.16-rc6/kernel/sched.c
===================================================================
@@ -2535,18 +2535,33 @@ void account_system_time(struct task_str
  */
 void account_steal_time(struct task_struct *p, cputime_t steal)
 {
-	struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
-	cputime64_t tmp = cputime_to_cputime64(steal);
+	struct cpu_usage_stat *cpustat;
+	cputime64_t tmp;
 	runqueue_t *rq = this_rq();
 
 	if (p == rq->idle) {
+		cpustat = &kstat_this_cpu.cpustat;
+		tmp = cputime_to_cputime64(steal);
+		
 		p->stime = cputime_add(p->stime, steal);
 		if (atomic_read(&rq->nr_iowait) > 0)
 			cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
 		else
 			cpustat->idle = cputime64_add(cpustat->idle, tmp);
 	} else
-		cpustat->steal = cputime64_add(cpustat->steal, tmp);
+		account_cpu_steal_time(steal);
+}
+
+/*
+ * Account for involuntary wait time for this cpu.
+ * @steal: the cpu time spent in involuntary wait
+ */
+void account_cpu_steal_time(cputime_t steal)
+{
+	struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
+	cputime64_t tmp = cputime_to_cputime64(steal);
+
+	cpustat->steal = cputime64_add(cpustat->steal, tmp);
 }
 
 /*