Skip to contentSkip to conversation

i386 Vmi no idle hz

RFC PATCH In review Zachary Amsden 3 participants 3 messages Mar 13, 2006 – Mar 15, 2006

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 24/24 i386 Vmi no idle hz

A NO_IDLE_HZ implementation is provided for i386 VMI builds.

When a VCPU enters its idle loop, it disables its periodic alarm and sets up a one shot alarm for the next time event. That way, it does not become ready to run just to service the periodic alarm interrupt. Instead, it can remain halted until there is some real work pending for it. This allows the hypervisor to use the physical resources more effectively since idle VCPUs will have lower overhead.

arch/i386/Kconfig +17 -0
Index: linux-2.6.16-rc6/arch/i386/Kconfig
===================================================================
@@ -193,6 +193,23 @@ config VMI_REQUIRE_HYPERVISOR
           This option forces the kernel to run with a hypervisor present.
           The kernel will panic if booted on native hardware.
 
+config NO_IDLE_HZ
+	bool "No HZ timer ticks in idle"
+	default y
+	help
+	  Switches the regular HZ timer off when the system is going idle.
+          This reduces the overhead of an idle system.  Also, this causes 
+          the guest to voluntarily block until it has some useful work to do,
+          rather than running in order to just handle timer interrupts.
+
+	  The HZ timer can be switched on/off via /proc/sys/kernel/hz_timer.
+	  hz_timer=0 means HZ timer is disabled. hz_timer=1 means HZ
+	  timer is active.
+
+          In order for this option to take effect, the kernel must be running
+          on a hypervisor.  This option has no effect when running on native 
+          hardware.
+
 choice 
 	prompt "VMI alarm rate"
 	default VMI_ALARM_HZ_100
arch/i386/kernel/apic.c +4 -0
Index: linux-2.6.16-rc6/arch/i386/kernel/apic.c
===================================================================
@@ -40,6 +40,7 @@
 #include <mach_apic.h>
 #include <mach_ipi.h>
 #include <mach_apictimer.h>
+#include <mach_idletimer.h>
 
 #include "io_ports.h"
 
@@ -1193,6 +1194,7 @@ fastcall void smp_apic_timer_interrupt(s
 	 * interrupt lock, which is the WrongThing (tm) to do.
 	 */
 	irq_enter();
+	restart_hz_timer(regs);
 	smp_local_timer_interrupt(regs);
 	irq_exit();
 }
@@ -1242,6 +1244,7 @@ fastcall void smp_spurious_interrupt(str
 	unsigned long v;
 
 	irq_enter();
+	restart_hz_timer(regs);
 	/*
 	 * Check if this really is a spurious interrupt and ACK it
 	 * if it is a vectored one.  Just in case...
@@ -1266,6 +1269,7 @@ fastcall void smp_error_interrupt(struct
 	unsigned long v, v1;
 
 	irq_enter();
+	restart_hz_timer(regs);
 	/* First tickle the hardware, only then report what went on. -- REW */
 	v = apic_read(APIC_ESR);
 	apic_write(APIC_ESR, 0);
arch/i386/kernel/irq.c +3 -0
Index: linux-2.6.16-rc6/arch/i386/kernel/irq.c
===================================================================
@@ -18,6 +18,7 @@
 #include <linux/notifier.h>
 #include <linux/cpu.h>
 #include <linux/delay.h>
+#include <mach_idletimer.h>
 
 DEFINE_PER_CPU(irq_cpustat_t, irq_stat) ____cacheline_internodealigned_in_smp;
 EXPORT_PER_CPU_SYMBOL(irq_stat);
@@ -76,6 +77,8 @@ fastcall unsigned int do_IRQ(struct pt_r
 	}
 #endif
 
+	restart_hz_timer(regs);
+
 #ifdef CONFIG_4KSTACKS
 
 	curctx = (union irq_ctx *) current_thread_info();
arch/i386/kernel/process.c +5 -2
Index: linux-2.6.16-rc6/arch/i386/kernel/process.c
===================================================================
@@ -57,6 +57,8 @@
 
 #include <asm/tlbflush.h>
 #include <asm/cpu.h>
+#include <mach_idletimer.h>
+
 
 asmlinkage void ret_from_fork(void) __asm__("ret_from_fork");
 
@@ -107,9 +109,10 @@ void default_idle(void)
 		smp_mb__after_clear_bit();
 		while (!need_resched()) {
 			local_irq_disable();
-			if (!need_resched())
+			if (!need_resched()) {
+				stop_hz_timer();
 				safe_halt();
-			else
+			} else
 				local_irq_enable();
 		}
 		set_thread_flag(TIF_POLLING_NRFLAG);
arch/i386/kernel/smp.c +8 -0
Index: linux-2.6.16-rc6/arch/i386/kernel/smp.c
===================================================================
@@ -24,6 +24,7 @@
 #include <asm/mtrr.h>
 #include <asm/tlbflush.h>
 #include <mach_apic.h>
+#include <mach_idletimer.h>
 
 /*
  *	Some notes on x86 processor bugs affecting SMP operation:
@@ -313,6 +314,8 @@ fastcall void smp_invalidate_interrupt(s
 {
 	unsigned long cpu;
 
+	restart_hz_timer(regs);
+
 	cpu = get_cpu();
 
 	if (!cpu_isset(cpu, flush_cpumask))
@@ -601,6 +604,8 @@ void smp_send_stop(void)
 fastcall void smp_reschedule_interrupt(struct pt_regs *regs)
 {
 	ack_APIC_irq();
+
+	restart_hz_timer(regs);
 }
 
 fastcall void smp_call_function_interrupt(struct pt_regs *regs)
@@ -610,6 +615,9 @@ fastcall void smp_call_function_interrup
 	int wait = call_data->wait;
 
 	ack_APIC_irq();
+
+	restart_hz_timer(regs);
+
 	/*
 	 * Notify initiating CPU that I've grabbed the data and am
 	 * about to execute the function
arch/i386/kernel/timers/timer_vmi.c +119 -0

155 lines not on this page · raw message

arch/i386/mach-vmi/proc.c +20 -0
Index: linux-2.6.16-rc6/arch/i386/mach-vmi/proc.c
===================================================================
@@ -49,6 +49,11 @@ struct pnp_header {
 	unsigned short product_offset;
 } __attribute__((packed));
 
+#ifdef CONFIG_NO_IDLE_HZ
+extern DEFINE_PER_CPU(unsigned long, vmi_idle_no_hz_irqs);
+extern DEFINE_PER_CPU(unsigned long, vmi_idle_no_hz_jiffies);
+#endif
+
 /* XXX This hack can only be used with passthrough TSC */
 static inline unsigned long long get_tsc(void)
 {
@@ -123,6 +128,21 @@ static int proc_vmi_info_show(struct seq
 	time_page_fault();
 	seq_printf(m, "Kernel #PF cycle count: %lld\n", page_fault_cycles);
 
+#ifdef CONFIG_NO_IDLE_HZ 
+ {
+	int i;
+	seq_printf(m, "Idle HZ:");
+	for_each_cpu(i) {
+		/* Shouldn't go idle now, so no extra synchronization needed. */
+		unsigned int irqs_per_ks = 0;
+		if (per_cpu(vmi_idle_no_hz_jiffies, i)) 
+			irqs_per_ks = 1000 * HZ * per_cpu(vmi_idle_no_hz_irqs, i) /
+				per_cpu(vmi_idle_no_hz_jiffies, i);
+		seq_printf(m, " %u.%03u", irqs_per_ks/1000, irqs_per_ks%1000);
+	}
+	seq_printf(m, "\n");
+ }
+#endif
 	return 0;
 }
 
kernel/sysctl.c +11 -10
Index: linux-2.6.16-rc6/kernel/sysctl.c
===================================================================
@@ -560,16 +560,6 @@ static ctl_table kern_table[] = {
 		.proc_handler	= &proc_dointvec,
 	},
 #endif
-#ifdef CONFIG_NO_IDLE_HZ
-	{
-		.ctl_name       = KERN_HZ_TIMER,
-		.procname       = "hz_timer",
-		.data           = &sysctl_hz_timer,
-		.maxlen         = sizeof(int),
-		.mode           = 0644,
-		.proc_handler   = &proc_dointvec,
-	},
-#endif
 	{
 		.ctl_name	= KERN_S390_USER_DEBUG_LOGGING,
 		.procname	= "userprocess_debug",
@@ -579,6 +569,17 @@ static ctl_table kern_table[] = {
 		.proc_handler	= &proc_dointvec,
 	},
 #endif
+#if defined(CONFIG_NO_IDLE_HZ) && (defined(CONFIG_ARCH_S390) || \
+				   defined(CONFIG_X86) && defined(CONFIG_X86_VMI))
+	{
+		.ctl_name       = KERN_HZ_TIMER,
+		.procname       = "hz_timer",
+		.data           = &sysctl_hz_timer,
+		.maxlen         = sizeof(int),
+		.mode           = 0644,
+		.proc_handler   = &proc_dointvec,
+	},
+#endif
 	{
 		.ctl_name	= KERN_PIDMAX,
 		.procname	= "pid_max",
kernel/timer.c +22 -6
Index: linux-2.6.16-rc6/kernel/timer.c
===================================================================
@@ -823,6 +823,17 @@ static void update_wall_time(unsigned lo
 	} while (ticks);
 }
 
+static inline void after_process_times_updated(int user_tick, struct task_struct *p)
+{
+	int cpu = smp_processor_id();
+
+	run_local_timers();
+	if (rcu_pending(cpu))
+		rcu_check_callbacks(cpu, user_tick);
+	scheduler_tick();
+ 	run_posix_cpu_timers(p);
+}
+
 /*
  * Called from the timer interrupt handler to charge one tick to the current 
  * process.  user_tick is 1 if the tick is user time, 0 for system.
@@ -830,18 +841,23 @@ static void update_wall_time(unsigned lo
 void update_process_times(int user_tick)
 {
 	struct task_struct *p = current;
-	int cpu = smp_processor_id();
 
 	/* Note: this timer irq context must be accounted for as well. */
 	if (user_tick)
 		account_user_time(p, jiffies_to_cputime(1));
 	else
 		account_system_time(p, HARDIRQ_OFFSET, jiffies_to_cputime(1));
-	run_local_timers();
-	if (rcu_pending(cpu))
-		rcu_check_callbacks(cpu, user_tick);
-	scheduler_tick();
- 	run_posix_cpu_timers(p);
+
+	after_process_times_updated(user_tick, p);
+}
+
+void update_idle_times(cputime_t idle_time)
+{
+	/* current should be the idle task. */
+	struct task_struct *p = current;
+
+	account_system_time(p, HARDIRQ_OFFSET, idle_time);
+	after_process_times_updated(0, p);
 }
 
 /*
include/asm-i386/mach-vmi/mach_idletimer.h +69 -0

69 lines not on this page · raw message

include/asm-i386/mach-default/mach_idletimer.h +19 -0
Index: linux-2.6.16-rc6/include/asm-i386/mach-default/mach_idletimer.h
===================================================================
@@ -0,0 +1,19 @@
+
+/*
+ * NO_IDLE_HZ callbacks.
+ */
+
+#ifndef __ASM_MACH_IDLETIMER_H
+#define __ASM_MACH_IDLETIMER_H
+
+static inline void stop_hz_timer(void) 
+{
+
+}
+
+static inline void restart_hz_timer(struct pt_regs *regs)
+{
+
+}
+
+#endif /* __ASM_MACH_IDLETIMER_H */
include/linux/sched.h +1 -0
Index: linux-2.6.16-rc6/include/linux/sched.h
===================================================================
@@ -203,6 +203,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 void update_idle_times(cputime_t idle_time);
 extern void scheduler_tick(void);
 
 #ifdef CONFIG_DETECT_SOFTLOCKUP
#1 lore raw

Hi!

Quoted 7 lines

When a VCPU enters its idle loop, it disables its periodic alarm and sets up a one shot alarm for the next time event. That way, it does not become ready to run just to service the periodic alarm interrupt. Instead, it can remain halted until there is some real work pending for it. This allows the hypervisor to use the physical resources more effectively since idle VCPUs will have lower overhead.

Does this NO_IDLE_HZ work only on VMI-enabled runs or globally? We are trying to get NO_IDLE_HZ working to save some power on notebooks; how is it related to this?

Quoted 8 lines
kernel/sysctl.c +4 -0
@@ -579,6 +569,17 @@ static ctl_table kern_table[] = {
 		.proc_handler	= &proc_dointvec,
 	},
 #endif
+#if defined(CONFIG_NO_IDLE_HZ) && (defined(CONFIG_ARCH_S390) || \
+				   defined(CONFIG_X86) && defined(CONFIG_X86_VMI))
+	{
+		.ctl_name       = KERN_HZ_TIMER,

kernel/sysctl.c static ctl_table kern_table[] = { · line 585

Quoted 10 lines
kernel/sysctl.c +7 -0
+		.procname       = "hz_timer",
+		.data           = &sysctl_hz_timer,
+		.maxlen         = sizeof(int),
+		.mode           = 0644,
+		.proc_handler   = &proc_dointvec,
+	},
+#endif
 	{
 		.ctl_name	= KERN_PIDMAX,
 		.procname	= "pid_max",

But this seems to disable it for non-VMI machines :-(.

Quoted 14 lines

Index: linux-2.6.16-rc6/include/asm-i386/mach-default/mach_idletimer.h
===================================================================

include/asm-i386/mach-default/mach_idletimer.h +9 -0
@@ -0,0 +1,19 @@
+
+/*
+ * NO_IDLE_HZ callbacks.
+ */
+
+#ifndef __ASM_MACH_IDLETIMER_H
+#define __ASM_MACH_IDLETIMER_H
+
+static inline void stop_hz_timer(void) 

include/asm-i386/mach-default/mach_idletimer.h line 19

Quoted 10 lines
include/asm-i386/mach-default/mach_idletimer.h +10 -0
+{
+
+}
+
+static inline void restart_hz_timer(struct pt_regs *regs)
+{
+
+}
+
+#endif /* __ASM_MACH_IDLETIMER_H */

And I guess these would need to be implemented.

Can you use NO_IDLE_HZ patches that are already floating around?

Pavel

Signature · 1 line
180:        alg = Rijndael.Create();
#2 lore raw
Quoted 10 lines · Pavel Machek

Hi!

Quoted 7 lines

When a VCPU enters its idle loop, it disables its periodic alarm and sets up a one shot alarm for the next time event. That way, it does not become ready to run just to service the periodic alarm interrupt. Instead, it can remain halted until there is some real work pending for it. This allows the hypervisor to use the physical resources more effectively since idle VCPUs will have lower overhead.

Quoted 4 lines

Does this NO_IDLE_HZ work only on VMI-enabled runs or globally? We are trying to get NO_IDLE_HZ working to save some power on notebooks; how is it related to this?

The NO_IDLE_HZ implementation provided here is enabled when the VMI-Timer device is used as the timer interrupt source. The VMI-Timer device is only present on paravirtual hardware.

However, the hooks introduced into the kernel (stop_hz_timer, restart_hz_timer) with these patches will be approximately the same (perhaps a subset) for implementing NO_IDLE_HZ on systems that use other interrupt sources.

Quoted 2 lines

Can you use NO_IDLE_HZ patches that are already floating around?

Certainly we plan to merge our NO_IDLE_HZ implementation with those patches once they are stabilized in the kernel. Presently, they seem to be too fast of a moving target to be worth merging at this point, though. Additionally, the VMI-Timer NO_IDLE_HZ implementation does not need all the machinery provided with the NO_IDLE_HZ patches that are floating around because the VMI-Timer code does not track time by counting interrupts. This leads to a fairly simplistic NO_IDLE_HZ implementation.

Dan