Skip to contentSkip to conversation

2.5: fair scheduler hints

PATCH In review Robert Love 2 participants 2 messages Jul 2, 2002 – Jul 3, 2002 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 2.5: fair scheduler hints

Here is an updated version of scheduler hints (often called hint-based scheduling) for 2.5.24. Scheduler hints are a way for a program to give a "hint" to the scheduler about its present behavior in the hopes of the scheduler consequently making better scheduling decisions. After all, who knows better than the application what it is about to do?

In this version, I have only implement the Solaris-style "gimme timeslice" hint. However, I have also implemented a fairness mechanism, although it is currently voluntary.

To enforce fairness even from malicious applications will require a small bit of code in schedule (although Pavel can probably think of a way to stick it in the slow-path in ptrace! 8). Since this is more a "does it actually show benefit" than a "we need more stuff in the kernel" patch I am hesitant to add this code.

Fairness is provided simply by another hint which tells the scheduler you are finished with your extra timeslice. The scheduler will calculate what your timeslice would of been had you not made the hint and set your new timeslice to that. If it is negative, you are put on the expired list. Thus the hint is more of a safety net to prevent preemption while holding a critical resource than a method to achieve arbitrarily long timeslices. This is what Solaris does.

Typical use would be:

	sched_hint(HINT_START);
	/* acquire critical resource */
	...
	/* release critical resource */
	sched_hint(HINT_STOP);

Even with the fairness implement I am still seeing ~5% improvement with a hand full of threads contending over a resource. Interestingly, I can measure an improvement even if only _one_ thread does the hint. In other words, it is fair.

Robert Love

P.S. I broke out the code to expire a task and move it to the expired array and put it in its own inline function. This is a bit cleaner as both sys_sched_hint and the scheduler_tick use this logic.

arch/i386/kernel/entry.S +1 -0
diff -urN linux-2.5.24/arch/i386/kernel/entry.S linux/arch/i386/kernel/entry.S
@@ -764,6 +764,7 @@
 	.long sys_futex		/* 240 */
 	.long sys_sched_setaffinity
 	.long sys_sched_getaffinity
+	.long sys_sched_hint
 
 	.rept NR_syscalls-(.-sys_call_table)/4
 		.long sys_ni_syscall
include/asm-i386/unistd.h +1 -0
diff -urN linux-2.5.24/include/asm-i386/unistd.h linux/include/asm-i386/unistd.h
@@ -247,6 +247,7 @@
 #define __NR_futex		240
 #define __NR_sched_setaffinity	241
 #define __NR_sched_getaffinity	242
+#define __NR_sched_hint		243
 
 /* user-visible error numbers are in the range -1 - -124: see <asm-i386/errno.h> */
 
include/linux/sched.h +9 -0
diff -urN linux-2.5.24/include/linux/sched.h linux/include/linux/sched.h
@@ -115,6 +115,12 @@
 #endif
 
 /*
+ * Scheduling Hints
+ */
+#define HINT_START		1	/* increase remaining timeslice */
+#define HINT_STOP		2	/* restore timeslice */
+
+/*
  * Scheduling policies
  */
 #define SCHED_OTHER		0
@@ -274,6 +280,9 @@
 
 	unsigned int allocation_order, nr_local_pages;
 
+	unsigned long hint_timestamp;
+	unsigned int hint_timeslice;
+
 /* task state */
 	struct linux_binfmt *binfmt;
 	int exit_code, exit_signal;
kernel/sched.c +90 -12

121 lines not on this page · raw message

#1 lore raw
Quoted 4 lines · Robert Love

Even with the fairness implement I am still seeing ~5% improvement with a hand full of threads contending over a resource. Interestingly, I can measure an improvement even if only _one_ thread does the hint. In other words, it is fair.

could you post the testcode as well?

Ingo