Skip to contentSkip to conversation

scheduler hints

PATCH In review Robert Love 6 participants 9 messages Jun 4, 2002 – Jun 12, 2002 Scheduler

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scheduler hints

So I went ahead and implemented scheduler hints on top of the O(1) scheduler.

I tried to find a decent paper on the web covering scheduler hints (sometimes referred to as hint-based scheduling) but could not find anything worthwhile. Solaris, for example, implements scheduler hints so perhaps the "Solaris Internals" book has some information.

Basically, 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 subsequently making better scheduling decisions. After all, who knows better than the application what it is about to do?

For example, consider a group of SCHED_RR threads that share a semaphore. Before one of the threads were to acquire the semaphore, it could give a "hint" to the scheduler to increase its remaining timeslice in order to ensure it could complete its work and drop the semaphore before being preempted. Since, if it were preempted, it would just end up being rescheduled as the other real-time threads would eventually block on the held semaphore.

Other hints could be "I am interactive" or "I am a batch (i.e. cpu hog) task" or "I am cache hot: try to keep me on this CPU".

The scheduler tries hard to figure out the three qualities and it is usually right, although perhaps it can react quicker to these hints than it can figure things out on its own. If nothing else, this serves as a useful tool for determining just how accurate our O(1) scheduler is.

I am not necessarily suggesting this for inclusion; it is more of a "just for fun" thing that turned into something with what I am actually seeing improvements, so I post it here for others to see.

You use scheduler hints in a program by doing something like,

sched_hint(hint)

where `hint' is currently one or more of:

HINT_TIME - task needs some more quanta, boost remaining timeslice

HINT_INTERACTIVE - task is interactive, give it a small priority bonus to help.

HINT_BATCH - task is a batch-processed task, give it a small priority penalty to be fair.

Right now the code makes no attempt to be fair - a program giving HINT_TIME now will not receive some sort of penalty later. Thus sched_hint requires CAP_SYS_NICE. This really is not what we want; we need any arbitrary task to be able to give these hints. Since the current solution is not fair, however, we cannot sanely do that.

A patch for 2.5.20 is attached. A patch for 2.4 + O(1) scheduler as well as a lame example program can be had at:

ftp://ftp.kernel.org/pub/linux/kernel/people/rml/sched/scheduler-hints

Any comments or suggestions are welcome. Thanks,

Robert Love

arch/i386/kernel/entry.S +1 -0
diff -urN linux-2.5.20/arch/i386/kernel/entry.S linux/arch/i386/kernel/entry.S
@@ -785,6 +785,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.20/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 +7 -0
diff -urN linux-2.5.20/include/linux/sched.h linux/include/linux/sched.h
@@ -116,6 +116,13 @@
 #endif
 
 /*
+ * Scheduling Hints
+ */
+#define HINT_TIME		1	/* increase remaining timeslice */
+#define HINT_INTERACTIVE	2	/* interactive task: prio bonus */
+#define HINT_BATCH		4	/* batch task: prio penalty */
+
+/*
  * Scheduling policies
  */
 #define SCHED_OTHER		0
kernel/sched.c +60 -1

79 lines not on this page · raw message

#1 lore raw
Quoted 4 lines · Robert Love

I tried to find a decent paper on the web covering scheduler hints (sometimes referred to as hint-based scheduling) but could not find anything worthwhile. Solaris, for example, implements scheduler hints so perhaps the "Solaris Internals" book has some information.

Hi Robert,
This isn't my thing but my flatmate had left a copy of solaris internals on the table ;)

This is briefly mentioned around about page 384 and appears to be targetted at userspace processes for exactly the cases you're suggesting (holding global resources).

A good entry point into the sun online documentation for this stuff is schedctl_init() -
http://docs.sun.com/db?q=schedctl_init&p=/doc/816-0216/6m6ngupm0&a=view

-Simon

Simon Trimmer <[email protected]> VERITAS R&D Watford, UK

#2 lore raw
Quoted 7 lines · Simon Trimmer

Hi Robert,
This isn't my thing but my flatmate had left a copy of solaris internals on the table ;)

This is briefly mentioned around about page 384 and appears to be targetted at userspace processes for exactly the cases you're suggesting (holding global resources).

I knew I read it there ;) My copy of "Solaris Internals" is elsewhere so I could not confirm.

Quoted 3 lines

A good entry point into the sun online documentation for this stuff is schedctl_init() -
http://docs.sun.com/db?q=schedctl_init&p=/doc/816-0216/6m6ngupm0&a=view

Hm, what they export is a bit different. I wonder what the internal kernel interface is like (i.e. how close to sched_hint it is)?

Since they have a start_hint and stop_hint, that is where they are able to enforce their fairness. When you call stop, I suspect they penalize your timeslice by some amount similar to the duration from start to stop. If you don't call stop before you reschedule, then you probably forfeit a large chunk of your timeslice.

This would be doable with our scheduler - and perhaps even with minimal impact (which is my goal). However, since I wrote this more as an exercise in fun than something to merge, I do not know if it is worth it to make a whole infrastructure around this. Those who really see benefit (scientific computing or real-time or whatever) could just grab the patch, remove the permission check, and code their applications to fit -- they trust their application base.

Anyhow, to pique interest, here are some benchmark numbers. I have 5 pthreads contesting over a single semaphore. They loop, doing some busy looping, down the semaphore, busy loop, and then up the semaphore. Thus they use a lot of their timeslice and spend the rest of the time blocking on the semaphore. I let them loop a fixed number of times before exiting.

(These are average of ~10 runs)

With a call to sched_hint(HINT_TIME) after successfully downing the semaphore the avg total duration is 7233459 us. Without the sched_hint, the avg total duration is 7683220 us.

That is an improvement of 6% - with only 5 threads.

A quick glance shows a reduction in context switches, but what really matters is if we are entering schedule and neither (a) rescheduling the same task, or (b) running another thread that quickly blocks on the semaphore.

It is all academic anyhow...

Robert Love

#3 lore raw
In reply to Robert Love

Robert Love wrote:
[...]

Quoted 5 lines

Basically, 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 subsequently making better scheduling decisions. After all, who knows better than the application what it is about to do?

Quoted 8 lines

For example, consider a group of SCHED_RR threads that share a semaphore. Before one of the threads were to acquire the semaphore, it could give a "hint" to the scheduler to increase its remaining timeslice in order to ensure it could complete its work and drop the semaphore before being preempted. Since, if it were preempted, it would just end up being rescheduled as the other real-time threads would eventually block on the held semaphore.

Seems to me this particular case is covered by increasing priority when grabbing the semaphore and normalizing priority when releasing.

Only root can do that - but only root does real-time anyway. And I guess only rood should be able to increase its timeslice too...

Quoted 1 line

Other hints could be "I am interactive"

Already shows up as a thread who always ends its timeslice blocking for io. Such threads do get an priority boost for the next timeslice.

Quoted 1 line

or "I am a batch (i.e. cpu hog)

shows up as a thread who spends its entire timeslice - these don't get the above mentioned boost as it is assumed it gets "enough cpu" while the interactive threads blocks.

Quoted 1 line

task" or "I am cache hot: try to keep me on this CPU".

Perhaps that might be useful.

Quoted 4 lines

The scheduler tries hard to figure out the three qualities and it is usually right, although perhaps it can react quicker to these hints than it can figure things out on its own. If nothing else, this serves as a useful tool for determining just how accurate our O(1) scheduler is.

Well, hog/interactive is determined in one timeslice already...

The problem is that this may be abused. Someone nasty could write a cpu hog that drops a lot of hints about being interactive, starving real interactive programs.

Generally, it degenerates into application programmers using _all_ the hints to get performance, so they can beat some competitor in benchmarks. And all other programs just get penalized.

Helge Hafting

#5 lore raw
In reply to Robert Love
Quoted 8 lines · Helge Hafting

The problem is that this may be abused. Someone nasty could write a cpu hog that drops a lot of hints about being interactive, starving real interactive programs.

Generally, it degenerates into application programmers using _all_ the hints to get performance, so they can beat some competitor in benchmarks. And all other programs just get penalized.

I can see how that would be a problem.
If I didn't have the source to $programs.

Dave

Signature · 2 lines
| Dave Jones.        http://www.codemonkey.org.uk
| SuSE Labs
#4 lore raw
In reply to Helge Hafting
Quoted 7 lines · Helge Hafting

Seems to me this particular case is covered by increasing priority when grabbing the semaphore and normalizing priority when releasing.

Only root can do that - but only root does real-time anyway. And I guess only rood should be able to increase its timeslice too...

Increasing its priority has no bearing on whether it runs out of timeslice, however. The idea here is to help the task complete its critical section (and thus not block other tasks) before being preempted. Only way to achieve that is boost its timeslice.

Boosting its priority will assure there is no priority inversion and that, eventually, the task will run - but it does nothing to avoid the nasty "grab resource, be preempted, reschedule a bunch, finally find yourself running again since everyone else blocked" issue.

And I don't think only root should be able to do this. If we later punish the task (take back the timeslice we gave it) then this is fair.

Quoted 6 lines
Quoted 1 line

Other hints could be "I am interactive"

Already shows up as a thread who always ends its timeslice blocking for io. Such threads do get an priority boost for the next timeslice.

Quoted 7 lines
Quoted 1 line

or "I am a batch (i.e. cpu hog)

shows up as a thread who spends its entire timeslice - these don't get the above mentioned boost as it is assumed it gets "enough cpu" while the interactive threads blocks.

Well, hog/interactive is determined in one timeslice already...

In the O(1) scheduler it is determined based on a running sleep average, not timeslice used (this is effectively the same thing - although we turn it into a heuristic so it is more accurate over time).

The problem is it takes time to figure these out. One whole schedule of the app to determine anything, and then a series of schedules to perfect it. My idea here was let the app tell the system what it is to give the system a head start. The scheduler will slowly readjust whatever it is told, based on the task's behavior, anyhow.

Giving a hint at the start of an interactive task, for example, skips the second or two of low priority where the task is not receiving its full boost.

Quoted 3 lines

The problem is that this may be abused. Someone nasty could write a cpu hog that drops a lot of hints about being interactive, starving real interactive programs.

Agreed. The code does require CAP_SYS_NICE and the comments explain the issue... One thing worth saying is I don't think this is as useful as the HINT_TIME hint anyhow.

Quoted 4 lines

Generally, it degenerates into application programmers using _all_ the hints to get performance, so they can beat some competitor in benchmarks. And all other programs just get penalized.

Well they can already nice themselves or make themselves real-time, so we have to trust them in numerous ways already not to cheat.

Robert Love

#6 lore raw
In reply to Robert Love

Hi!

Quoted 13 lines
Quoted 7 lines

Seems to me this particular case is covered by increasing priority when grabbing the semaphore and normalizing priority when releasing.

Only root can do that - but only root does real-time anyway. And I guess only rood should be able to increase its timeslice too...

Increasing its priority has no bearing on whether it runs out of timeslice, however. The idea here is to help the task complete its critical section (and thus not block other tasks) before being preempted. Only way to achieve that is boost its timeslice.

Quoted 8 lines

Boosting its priority will assure there is no priority inversion and that, eventually, the task will run - but it does nothing to avoid the nasty "grab resource, be preempted, reschedule a bunch, finally find yourself running again since everyone else blocked" issue.

And I don't think only root should be able to do this. If we later punish the task (take back the timeslice we gave it) then this is fair.

Another possibility might be to allow it to *steal* time from another processes... Of course only processes of same UID ;-).
Pavel

Signature · 2 lines
(about SSSCA) "I don't say this lightly.  However, I really think that the U.S.
no longer is classifiable as a democracy, but rather as a plutocracy." --hpa
#7 lore raw
In reply to Robert Love
Quoted 5 lines · Pavel Machek
Quoted 5 lines

Boosting its priority will assure there is no priority inversion and that, eventually, the task will run - but it does nothing to avoid the nasty "grab resource, be preempted, reschedule a bunch, finally find yourself running again since everyone else blocked" issue.

Quoted 7 lines
Quoted 3 lines

And I don't think only root should be able to do this. If we later punish the task (take back the timeslice we gave it) then this is fair.

Another possibility might be to allow it to *steal* time from another processes... Of course only processes of same UID ;-).
Pavel

Good idea!

And I would say SID instead of UID and give up, if no task in the same SID is runnable.

One could provide different policies here, which the user can choose/combine.

That way we aren't at least unfair to other users on our remote machine.

Regards

Ingo Oeser

Signature · 1 line
Science is what we can tell a computer. Art is everything else. --- D.E.Knuth
#8 lore raw
Quoted 10 lines · Ingo Oeser

Good idea!

And I would say SID instead of UID and give up, if no task in the same SID is runnable.

One could provide different policies here, which the user can choose/combine.

That way we aren't at least unfair to other users on our remote machine.

The solution I am working on now is to just take the timeslice away later. I.e., now it is more beneficial to me than later, so give me some of my future timeslice now. This is per-process.

It is enforced right now only by a call to sched_hint() with a hint saying "I am done". The timeslice used is calculated and your timeslice is adjusted and if applicable you are removed from the active runqueue.

Next I need to add a check to schedule() to check for processes who are scheduling off and have not explicitly given the "I am done" hint. I am weary of what to do here as I do not want to adversely affect the fastpath.

That is what I am playing with now, anyhow... but I have been a bit busy of late and not put enough cycles into it.

Robert Love