Skip to contentSkip to conversation

scheduler nice 19 versus 'idle' behavior / static low-priority scheduling

DISCUSSION Nathanael Hoyle 8 participants 31 messages Jan 30, 2009 – Feb 9, 2009

lore
Share & tools
lore
#0 lore raw

All (though perhaps of special interest to a few such as Ingo, Peter, and David),

I am posting regarding an issue I have been dealing with recently, though this post is not really a request for troubleshooting. Instead I'd like to ramble for just a moment about my understanding of the current 2.6 scheduler, describe the behavior I'm seeing, and discuss a couple of the architectural solutions I've considered, as well as pose the question whether anyone else views this as a general-case problem worthy of being addressed, or whether this is something that gets ignored by and large. It is my hope that this is not too off-topic for this group.

First, let me explain the issue I encountered. I am running a relatively powerful system for a home desktop, an Intel Core 2 Quad Q9450 with 4 GB of RAM. If it matters for the discussion, it also has 4 drives in an mdraid raid-5 array, and decent I/O throughput. In normal circumstances it is quite responsive as a desktop (kde 3.5.4 atm). It is further a very carefully configured kernel build, including only those things which I truly need, and excluding everything else. I often use it to watch DVD movies, and have had no trouble with performance in general.

Recently I installed the Folding@Home client, which many of you may be familiar with, intended to utilize spare CPU cycles to perform protein folding simulations in order to further medical research. It is not a multi-threaded client at this point, so it simply runs four instances on my system, since it has four cores. It is configured to run at nice-level 19.

Because it is heavily optimized, and needs little external data to perform its work, it spends almost all of its time cpu-bound, with little to no io-wait or blocking on network calls, etc. I had been using it for about a week with no real difficulty until I went to watch another DVD and found that the video was slightly stuttery/jerky so long as foldingathome was running in the background. Once I shut it down, the video playback resumed its normal smooth form.

There are a couple simple solutions to this:

Substantially boosting the process priority of the mplayer process also returns the video to smooth playback, but this is undesirable in that it requires manual intervention each time, and root privileges. It fails to achieve what I want, which is for the foldingathome computation to not interfere with anything else I may try to do. I want my compiles to be as *exactly* as fast as they were without it as possible, etc.

Stopping foldingathome before I do something performance sensitive is also possible, but again smacks of workaround rather than solution. The scheduler should be able to resolve the goal without me stopping the other work.

I have done a bit of research on how the kernel scheduler works, and why I am seeing this behavior. I had previously, apparently ignorantly, equated 'nice 19' with being akin to Microsoft Windows' 'idle' thread priority, and assumed it would never steal CPU cycles from a process with a higher(lower, depending on nomenclature) priority.

It is my current understanding that when mplayer is running (also typically CPU bound, occassionally it becomes I/O bound briefly), one of the instances of foldingathome, which is sharing the CPU (core) with mplayer starts getting starved, and the scheduler dynamically rewards it with up to four additional priority levels based on the time remaining in its quantum which it was not allowed to execute for.

At this point, when mplayer blocks for just a moment, say to page in the data for the next video frame, foldingathome gets scheduled again, and gets to run for at least MIN_TIMESLICE (plus, due to the lack of kernel pre-emptibility, possibly longer). It appears that it takes too long to switch back to mplayer and the result is the stuttering picture I observe.

I have tried adjusting CONFIG_HZ_xxx from 300 (where I had it) to 1000, and noted some improvement, but not complete remedy.

In my prior searching on this, I found only one poster with the same essential problem (from 2004, and regarding distributed.net in the background, which is essentially the same problem). The only technical answer given him was to perhaps try tuning the MIN_TIMESLICE value downward. It is my understanding that this parameter is relatively important in order to avoid cache thrashing, and I do not wish to alter it and have not so far.

Given all of the above, I am unconvinced that I see a good overall solution. However, one thing that seems to me a glaring weakness of the scheduler is that only realtime priority threads can be given static priorities. What I really want for foldingathome, and similar tasks, is static, low priority. Something that would not boost up, no matter how well behaved it was or how much it had been starved, or how close to the same memory segments the needed code was.

I think that there are probably (at least) three approaches here. One I consider unnacceptable at the outset, which is to alter the semantics of nice 19 such that it does not boost. Since this would break existing assumptions and code, I do not think it is feasible.

Secondly, one could add additional nice levels which would correspond to new static priorities below the bottom of the current user ones. This should not interfere with the O(1) scheduler implementation as I understand it, because current I believe 5 32-bit words are used to flag the queue usage, and 140 priorities leaves 20 more bits available for new priorities. This has its own problems however, in that existing tools which examine process priorities could break on priorities outside the known 'nice' range of -20 to 19.

Finally, new scheduling classes could be introduced, together with new system calls so that applications could select a different scheduling class at startup. In this way, applications could volunteer to use a scheduling class which never received dynamic 'reward' boosts that would raise their priorities. I believe Solaris has done this since Solaris 9, with the 'FX' scheduling class.

Stepping back:

1) Is my problem 'expected' based on others' understanding of the current design of the scheduler, or do I have a one-off problem to troubleshoot here?

2) Am I overlooking obvious alternative (but clean) fixes?

3) Does anyone else see the need for static, but low process priorities?

4) What is the view of introducing a new scheduler class to handle this?

I welcome any further feedback on this. I will try to follow replies on-list, but would appreciate being CC'd off-list as well. Please make the obvious substitution to my email address in order to bypass the spam-killer.

Thanks,
Nathanael Hoyle

#1 lore raw
Quoted 8 lines · Nathanael Hoyle

I have done a bit of research on how the kernel scheduler works, and why I am seeing this behavior. I had previously, apparently ignorantly, equated 'nice 19' with being akin to Microsoft Windows' 'idle' thread priority, and assumed it would never steal CPU cycles from a process with a higher(lower, depending on nomenclature) priority. [...]

Quoted 4 lines

One[...] is to alter the semantics of nice 19 such that it does not boost. Since this would break existing assumptions and code, I do not think it is feasible. [...] Finally, new scheduling classes could be introduced[...]

Surprise. There is already SCHED_BATCH (intended for computing tasks as I gathered) and SCHED_IDLE (for idle stuff).

Quoted 2 lines

Please make the obvious substitution to my email address in order to bypass the spam-killer.

(Obviously this is not obvious... there are no 'nospam' keywords or similar in it that could be removed.)

#17 lore raw
In reply to Nathanael Hoyle
Quoted 7 lines · Nathanael Hoyle

Recently I installed the Folding@Home client, which many of you may be familiar with, intended to utilize spare CPU cycles to perform protein folding simulations in order to further medical research. It is not a multi-threaded client at this point, so it simply runs four instances on my system, since it has four cores. It is configured to run at nice-level 19.

Quoted 7 lines

Because it is heavily optimized, and needs little external data to perform its work, it spends almost all of its time cpu-bound, with little to no io-wait or blocking on network calls, etc. I had been using it for about a week with no real difficulty until I went to watch another DVD and found that the video was slightly stuttery/jerky so long as foldingathome was running in the background. Once I shut it down, the video playback resumed its normal smooth form.

Sounds like a problem was recently fixed. Can you try 2.6.29-rc3 or 2.6.28.2?

-Mike

#2 lore raw
In reply to Jan Engelhardt
Quoted 9 lines · Jan Engelhardt
Quoted 8 lines · Nathanael Hoyle

I have done a bit of research on how the kernel scheduler works, and why I am seeing this behavior. I had previously, apparently ignorantly, equated 'nice 19' with being akin to Microsoft Windows' 'idle' thread priority, and assumed it would never steal CPU cycles from a process with a higher(lower, depending on nomenclature) priority. [...]

Quoted 8 lines
Quoted 4 lines

One[...] is to alter the semantics of nice 19 such that it does not boost. Since this would break existing assumptions and code, I do not think it is feasible. [...] Finally, new scheduling classes could be introduced[...]

Surprise. There is already SCHED_BATCH (intended for computing tasks as I gathered) and SCHED_IDLE (for idle stuff).

The one discussion I saw referencing SCHED_BATCH seemed to imply that it was a non-standard kernel patch by Con Kolivas in one of his -ck variants that never made it into mainline and is not being maintained. Is this inaccurate?

I was unfamiliar with SCHED_IDLE. Having done a little Googling now, I finally find reference to the man page for sched_setscheduler(2). This appears that it is likely what I wanted.

I think the information I had been able to find was somehwat out of date. It had indicated that the only static priority levels were the realtime ones.

Is there currently a standardized userspace tool to use to run a command in order to alter its scheduling class? Obviously writing one would be trivial, but didn't know if something like:

$ runidle ./foldingathome

would be available.

Thanks for your helpful reply.

Quoted 7 lines
Quoted 4 lines

Please make the obvious substitution to my email address in order to bypass the spam-killer.

(Obviously this is not obvious... there are no 'nospam' keywords or similar in it that could be removed.)

I made a failed attempt to post earlier in the evening, which included the address '[email protected]'. When that one didn't make it to the list (though I'm unsure it had to do with the address I used) I retried with the clean address. I forgot to remove the note at the bottom of the posting.

Sincerely,
Nathanael Hoyle

#21 lore raw
In reply to V.Radhakrishnan
Quoted 11 lines · V.Radhakrishnan

Clear description of a "problem" which may not be directly "solvable" unless one becomes philosophical.

The linux scheduler by default is a "FAIR" scheduler which means that every runnable process ready to occupy CPU time will at some point RUN and never be denied cpu time. This is possible by boosting the dynamic priority of runnable processes so that one day they rule the roost.

However, the kernel also supports SCHED_FIFO and SCHED_RR which supports Real Time capabilities, albeit as root.

Quoted 7 lines

The DVD player is a soft real time application where the display gets jittery whenever the frame display rate is not achievable.

If you wish 100% smooth display, you could make it run as SCHED_FIFO which means that your foldingathome would wait quietly for the movie to get completed fully. What's "wrong" with that aproach, which is essentially what you want ?

My view of what's "wrong" with that approach is that it requires root privileges to boost the scheduling priority of each and every process (although in this case, mplayer is the issue) which I want to not be affected by foldingathome's CPU usage. While I happen to be root on this system, since it is my desktop, I would imagine there are instances where the root user/administrator of a system wanted to be able to run items which had no impact on other users, including allowing them to run fast and responsive applications. Aside from that, it's a PITA to start mplayer playing, go renice -19 it and resume watching my movie every time.

Quoted 4 lines

The key question is whether YOU want the foldingathome application to run in parallel with the dvd player or not.

I want it to be able to run in the background without my having to intervene, but I want the core which shares mplayer to starve foldingathome until my movie is over :-).

Quoted 144 lines

Lets wait for more light from the gurus...!!

V. Radhakrishnan

Quoted 137 lines · Nathanael Hoyle

All (though perhaps of special interest to a few such as Ingo, Peter, and David),

I am posting regarding an issue I have been dealing with recently, though this post is not really a request for troubleshooting. Instead I'd like to ramble for just a moment about my understanding of the current 2.6 scheduler, describe the behavior I'm seeing, and discuss a couple of the architectural solutions I've considered, as well as pose the question whether anyone else views this as a general-case problem worthy of being addressed, or whether this is something that gets ignored by and large. It is my hope that this is not too off-topic for this group.

First, let me explain the issue I encountered. I am running a relatively powerful system for a home desktop, an Intel Core 2 Quad Q9450 with 4 GB of RAM. If it matters for the discussion, it also has 4 drives in an mdraid raid-5 array, and decent I/O throughput. In normal circumstances it is quite responsive as a desktop (kde 3.5.4 atm). It is further a very carefully configured kernel build, including only those things which I truly need, and excluding everything else. I often use it to watch DVD movies, and have had no trouble with performance in general.

Recently I installed the Folding@Home client, which many of you may be familiar with, intended to utilize spare CPU cycles to perform protein folding simulations in order to further medical research. It is not a multi-threaded client at this point, so it simply runs four instances on my system, since it has four cores. It is configured to run at nice-level 19.

Because it is heavily optimized, and needs little external data to perform its work, it spends almost all of its time cpu-bound, with little to no io-wait or blocking on network calls, etc. I had been using it for about a week with no real difficulty until I went to watch another DVD and found that the video was slightly stuttery/jerky so long as foldingathome was running in the background. Once I shut it down, the video playback resumed its normal smooth form.

There are a couple simple solutions to this:

Substantially boosting the process priority of the mplayer process also returns the video to smooth playback, but this is undesirable in that it requires manual intervention each time, and root privileges. It fails to achieve what I want, which is for the foldingathome computation to not interfere with anything else I may try to do. I want my compiles to be as *exactly* as fast as they were without it as possible, etc.

Stopping foldingathome before I do something performance sensitive is also possible, but again smacks of workaround rather than solution. The scheduler should be able to resolve the goal without me stopping the other work.

I have done a bit of research on how the kernel scheduler works, and why I am seeing this behavior. I had previously, apparently ignorantly, equated 'nice 19' with being akin to Microsoft Windows' 'idle' thread priority, and assumed it would never steal CPU cycles from a process with a higher(lower, depending on nomenclature) priority.

It is my current understanding that when mplayer is running (also typically CPU bound, occassionally it becomes I/O bound briefly), one of the instances of foldingathome, which is sharing the CPU (core) with mplayer starts getting starved, and the scheduler dynamically rewards it with up to four additional priority levels based on the time remaining in its quantum which it was not allowed to execute for.

At this point, when mplayer blocks for just a moment, say to page in the data for the next video frame, foldingathome gets scheduled again, and gets to run for at least MIN_TIMESLICE (plus, due to the lack of kernel pre-emptibility, possibly longer). It appears that it takes too long to switch back to mplayer and the result is the stuttering picture I observe.

I have tried adjusting CONFIG_HZ_xxx from 300 (where I had it) to 1000, and noted some improvement, but not complete remedy.

In my prior searching on this, I found only one poster with the same essential problem (from 2004, and regarding distributed.net in the background, which is essentially the same problem). The only technical answer given him was to perhaps try tuning the MIN_TIMESLICE value downward. It is my understanding that this parameter is relatively important in order to avoid cache thrashing, and I do not wish to alter it and have not so far.

Given all of the above, I am unconvinced that I see a good overall solution. However, one thing that seems to me a glaring weakness of the scheduler is that only realtime priority threads can be given static priorities. What I really want for foldingathome, and similar tasks, is static, low priority. Something that would not boost up, no matter how well behaved it was or how much it had been starved, or how close to the same memory segments the needed code was.

I think that there are probably (at least) three approaches here. One I consider unnacceptable at the outset, which is to alter the semantics of nice 19 such that it does not boost. Since this would break existing assumptions and code, I do not think it is feasible.

Secondly, one could add additional nice levels which would correspond to new static priorities below the bottom of the current user ones. This should not interfere with the O(1) scheduler implementation as I understand it, because current I believe 5 32-bit words are used to flag the queue usage, and 140 priorities leaves 20 more bits available for new priorities. This has its own problems however, in that existing tools which examine process priorities could break on priorities outside the known 'nice' range of -20 to 19.

Finally, new scheduling classes could be introduced, together with new system calls so that applications could select a different scheduling class at startup. In this way, applications could volunteer to use a scheduling class which never received dynamic 'reward' boosts that would raise their priorities. I believe Solaris has done this since Solaris 9, with the 'FX' scheduling class.

Stepping back:

1) Is my problem 'expected' based on others' understanding of the current design of the scheduler, or do I have a one-off problem to troubleshoot here?

2) Am I overlooking obvious alternative (but clean) fixes?

3) Does anyone else see the need for static, but low process priorities?

4) What is the view of introducing a new scheduler class to handle this?

I welcome any further feedback on this. I will try to follow replies on-list, but would appreciate being CC'd off-list as well. Please make the obvious substitution to my email address in order to bypass the spam-killer.

Thanks,
Nathanael Hoyle

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#18 lore raw
In reply to Mike Galbraith
Quoted 17 lines · Mike Galbraith
Quoted 7 lines · Nathanael Hoyle

Recently I installed the Folding@Home client, which many of you may be familiar with, intended to utilize spare CPU cycles to perform protein folding simulations in order to further medical research. It is not a multi-threaded client at this point, so it simply runs four instances on my system, since it has four cores. It is configured to run at nice-level 19.

Quoted 7 lines

Because it is heavily optimized, and needs little external data to perform its work, it spends almost all of its time cpu-bound, with little to no io-wait or blocking on network calls, etc. I had been using it for about a week with no real difficulty until I went to watch another DVD and found that the video was slightly stuttery/jerky so long as foldingathome was running in the background. Once I shut it down, the video playback resumed its normal smooth form.

Quoted 5 lines

Sounds like a problem was recently fixed. Can you try 2.6.29-rc3 or 2.6.28.2?

-Mike

I will try to do so as soon as I get the chance. Do you have any specific info on the problem that you believe was fixed and/or the fix applied?

Thanks,
-Nathanael

Tested-by Acked-by
#19 lore raw
Quoted 9 lines · Nathanael Hoyle
Quoted 3 lines · Mike Galbraith

Sounds like a problem was recently fixed. Can you try 2.6.29-rc3 or 2.6.28.2?

I will try to do so as soon as I get the chance. Do you have any specific info on the problem that you believe was fixed and/or the fix applied.

The commit text below also applies to +nice tasks.

commit 046e7f77d734778a3b2e7d51ce63da3dbe7a8168

Author: Peter Zijlstra <[email protected]>
Date:   Thu Jan 15 14:53:39 2009 +0100

sched: fix update_min_vruntime

commit e17036dac189dd034c092a91df56aa740db7146d upstream.

Impact: fix SCHED_IDLE latency problems

OK, so we have 1 running task A (which is obviously curr and the tree is equally obviously empty).

'A' nicely chugs along, doing its thing, carrying min_vruntime along as it goes.

Then some whacko speed freak SCHED_IDLE task gets inserted due to SMP balancing, which is very likely far right, in that case

update_curr
update_min_vruntime
cfs_rq->rb_leftmost := true (the crazy task sitting in a tree)
vruntime = se->vruntime

and voila, min_vruntime is waaay right of where it ought to be.

OK, so why did I write it like that to begin with...

Aah, yes.

Say we've just dequeued current

schedule
deactivate_task(prev)
dequeue_entity
update_min_vruntime

Then we'll set

vruntime = cfs_rq->min_vruntime;

we find !cfs_rq->curr, but do find someone in the tree. Then we _must_ do vruntime = se->vruntime, because

vruntime = min_vruntime(vruntime := cfs_rq->min_vruntime, se->vruntime)

will not advance vruntime, and cause lags the other way around (which we fixed with that initial patch: 1af5f730fc1bf7c62ec9fb2d307206e18bf40a69 (sched: more accurate min_vruntime accounting).

kernel/sched_fair.c +1 -1
index 98345e4..06a68c4 100644
@@ -283,7 +283,7 @@ static void update_min_vruntime(struct cfs_rq *cfs_rq)
 						   struct sched_entity,
 						   run_node);
 
-		if (vruntime == cfs_rq->min_vruntime)
+		if (!cfs_rq->curr)
 			vruntime = se->vruntime;
 		else
 			vruntime = min_vruntime(vruntime, se->vruntime);
#20 lore raw
In reply to Nathanael Hoyle

Clear description of a "problem" which may not be directly "solvable" unless one becomes philosophical.

The linux scheduler by default is a "FAIR" scheduler which means that every runnable process ready to occupy CPU time will at some point RUN and never be denied cpu time. This is possible by boosting the dynamic priority of runnable processes so that one day they rule the roost.

However, the kernel also supports SCHED_FIFO and SCHED_RR which supports Real Time capabilities, albeit as root.

The DVD player is a soft real time application where the display gets jittery whenever the frame display rate is not achievable.

If you wish 100% smooth display, you could make it run as SCHED_FIFO which means that your foldingathome would wait quietly for the movie to get completed fully. What's "wrong" with that aproach, which is essentially what you want ?

The key question is whether YOU want the foldingathome application to run in parallel with the dvd player or not.

Lets wait for more light from the gurus...!!

V. Radhakrishnan

Quoted 137 lines · Nathanael Hoyle

All (though perhaps of special interest to a few such as Ingo, Peter, and David),

I am posting regarding an issue I have been dealing with recently, though this post is not really a request for troubleshooting. Instead I'd like to ramble for just a moment about my understanding of the current 2.6 scheduler, describe the behavior I'm seeing, and discuss a couple of the architectural solutions I've considered, as well as pose the question whether anyone else views this as a general-case problem worthy of being addressed, or whether this is something that gets ignored by and large. It is my hope that this is not too off-topic for this group.

First, let me explain the issue I encountered. I am running a relatively powerful system for a home desktop, an Intel Core 2 Quad Q9450 with 4 GB of RAM. If it matters for the discussion, it also has 4 drives in an mdraid raid-5 array, and decent I/O throughput. In normal circumstances it is quite responsive as a desktop (kde 3.5.4 atm). It is further a very carefully configured kernel build, including only those things which I truly need, and excluding everything else. I often use it to watch DVD movies, and have had no trouble with performance in general.

Recently I installed the Folding@Home client, which many of you may be familiar with, intended to utilize spare CPU cycles to perform protein folding simulations in order to further medical research. It is not a multi-threaded client at this point, so it simply runs four instances on my system, since it has four cores. It is configured to run at nice-level 19.

Because it is heavily optimized, and needs little external data to perform its work, it spends almost all of its time cpu-bound, with little to no io-wait or blocking on network calls, etc. I had been using it for about a week with no real difficulty until I went to watch another DVD and found that the video was slightly stuttery/jerky so long as foldingathome was running in the background. Once I shut it down, the video playback resumed its normal smooth form.

There are a couple simple solutions to this:

Substantially boosting the process priority of the mplayer process also returns the video to smooth playback, but this is undesirable in that it requires manual intervention each time, and root privileges. It fails to achieve what I want, which is for the foldingathome computation to not interfere with anything else I may try to do. I want my compiles to be as *exactly* as fast as they were without it as possible, etc.

Stopping foldingathome before I do something performance sensitive is also possible, but again smacks of workaround rather than solution. The scheduler should be able to resolve the goal without me stopping the other work.

I have done a bit of research on how the kernel scheduler works, and why I am seeing this behavior. I had previously, apparently ignorantly, equated 'nice 19' with being akin to Microsoft Windows' 'idle' thread priority, and assumed it would never steal CPU cycles from a process with a higher(lower, depending on nomenclature) priority.

It is my current understanding that when mplayer is running (also typically CPU bound, occassionally it becomes I/O bound briefly), one of the instances of foldingathome, which is sharing the CPU (core) with mplayer starts getting starved, and the scheduler dynamically rewards it with up to four additional priority levels based on the time remaining in its quantum which it was not allowed to execute for.

At this point, when mplayer blocks for just a moment, say to page in the data for the next video frame, foldingathome gets scheduled again, and gets to run for at least MIN_TIMESLICE (plus, due to the lack of kernel pre-emptibility, possibly longer). It appears that it takes too long to switch back to mplayer and the result is the stuttering picture I observe.

I have tried adjusting CONFIG_HZ_xxx from 300 (where I had it) to 1000, and noted some improvement, but not complete remedy.

In my prior searching on this, I found only one poster with the same essential problem (from 2004, and regarding distributed.net in the background, which is essentially the same problem). The only technical answer given him was to perhaps try tuning the MIN_TIMESLICE value downward. It is my understanding that this parameter is relatively important in order to avoid cache thrashing, and I do not wish to alter it and have not so far.

Given all of the above, I am unconvinced that I see a good overall solution. However, one thing that seems to me a glaring weakness of the scheduler is that only realtime priority threads can be given static priorities. What I really want for foldingathome, and similar tasks, is static, low priority. Something that would not boost up, no matter how well behaved it was or how much it had been starved, or how close to the same memory segments the needed code was.

I think that there are probably (at least) three approaches here. One I consider unnacceptable at the outset, which is to alter the semantics of nice 19 such that it does not boost. Since this would break existing assumptions and code, I do not think it is feasible.

Secondly, one could add additional nice levels which would correspond to new static priorities below the bottom of the current user ones. This should not interfere with the O(1) scheduler implementation as I understand it, because current I believe 5 32-bit words are used to flag the queue usage, and 140 priorities leaves 20 more bits available for new priorities. This has its own problems however, in that existing tools which examine process priorities could break on priorities outside the known 'nice' range of -20 to 19.

Finally, new scheduling classes could be introduced, together with new system calls so that applications could select a different scheduling class at startup. In this way, applications could volunteer to use a scheduling class which never received dynamic 'reward' boosts that would raise their priorities. I believe Solaris has done this since Solaris 9, with the 'FX' scheduling class.

Stepping back:

1) Is my problem 'expected' based on others' understanding of the current design of the scheduler, or do I have a one-off problem to troubleshoot here?

2) Am I overlooking obvious alternative (but clean) fixes?

3) Does anyone else see the need for static, but low process priorities?

4) What is the view of introducing a new scheduler class to handle this?

I welcome any further feedback on this. I will try to follow replies on-list, but would appreciate being CC'd off-list as well. Please make the obvious substitution to my email address in order to bypass the spam-killer.

Thanks,
Nathanael Hoyle

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

#3 lore raw
In reply to Nathanael Hoyle
Quoted 6 lines · Nathanael Hoyle

On Fri, 2009-01-30 at 07:16 +0100, Jan Engelhardt wrote:

The one discussion I saw referencing SCHED_BATCH seemed to imply that it was a non-standard kernel patch by Con Kolivas in one of his -ck variants that never made it into mainline and is not being maintained. Is this inaccurate?

The presence of SCHED_BATCH in linux/sched.h tells me it is available (on the other hand, SCHED_ISO, also from -ck, is only listed as a comment.)

Quoted 6 lines

I was unfamiliar with SCHED_IDLE. Having done a little Googling now, I finally find reference to the man page for sched_setscheduler(2). This appears that it is likely what I wanted.

I think the information I had been able to find was somehwat out of date.

The manpage does say it, but if your local distro does not mention SCHED_BATCH/SCHED_IDLE, then that's a pretty sad distro.

The doc in sched_setschedule seems complete to me as of man-pages 3.13.

Quoted 3 lines

Is there currently a standardized userspace tool to use to run a command in order to alter its scheduling class? Obviously writing one would be trivial, but didn't know if something like:

man chrt

#4 lore raw
Quoted 11 lines · Jan Engelhardt
Quoted 6 lines · Nathanael Hoyle

On Fri, 2009-01-30 at 07:16 +0100, Jan Engelhardt wrote:

The one discussion I saw referencing SCHED_BATCH seemed to imply that it was a non-standard kernel patch by Con Kolivas in one of his -ck variants that never made it into mainline and is not being maintained. Is this inaccurate?

The presence of SCHED_BATCH in linux/sched.h tells me it is available (on the other hand, SCHED_ISO, also from -ck, is only listed as a comment.)

Fair enough. I should have re-checked recent sources after your mention rather than going on the old thread I found.

Quoted 10 lines
Quoted 6 lines

I was unfamiliar with SCHED_IDLE. Having done a little Googling now, I finally find reference to the man page for sched_setscheduler(2). This appears that it is likely what I wanted.

I think the information I had been able to find was somehwat out of date.

The manpage does say it, but if your local distro does not mention SCHED_BATCH/SCHED_IDLE, then that's a pretty sad distro.

Quoted 7 lines

The doc in sched_setschedule seems complete to me as of man-pages 3.13.

Quoted 3 lines

Is there currently a standardized userspace tool to use to run a command in order to alter its scheduling class? Obviously writing one would be trivial, but didn't know if something like:

man chrt

The latest version of man chrt that I can find implies that it handles SCHED_BATCH but not SCHED_IDLE. To that end, if anyone else is interested, I have thrown together the above-suggested 'runidle' which will invoke the passed command using the SCHED_IDLE scheduler; it's nothing fancy.

I am running foldingathome under it at the moment, and it seems to be improving the situation somewhat, but I still need/want to test with Mike's referenced patches.

runidle.c:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sched.h>
#include <linux/sched.h>

extern char **environ;

int
main(int argc, char **argv) {
if(argc<2) {
perror("Must specify at least one argument: the path to the program
to " \
"execute. Additional arguments may be specified which will be

passed " \
      "to the called program.");
    return EXIT_FAILURE;

}

  sched_setscheduler(0, SCHED_IDLE, NULL);

  if(argc==2) {
    if(execve(argv[1], NULL, environ) == -1) {
      perror("Failed to execve target!");
    }
  } else {
    if(execve(argv[1], argv+1, environ) == -1) {
      perror("Failed to execve target!");
    }
  }

/* should be unreachable */
return EXIT_FAILURE;
}

-Nathanael

#5 lore raw
Quoted 3 lines · Nathanael Hoyle

I am running foldingathome under it at the moment, and it seems to be improving the situation somewhat, but I still need/want to test with Mike's referenced patches.

You will most definitely encounter evilness running SCHED_IDLE tasks in a kernel without the SCHED_IDLE fixes.

-Mike

#14 lore raw
In reply to Nathanael Hoyle
Quoted 40 lines · Nathanael Hoyle
Quoted 11 lines · Jan Engelhardt
Quoted 6 lines · Nathanael Hoyle

On Fri, 2009-01-30 at 07:16 +0100, Jan Engelhardt wrote:

The one discussion I saw referencing SCHED_BATCH seemed to imply that it was a non-standard kernel patch by Con Kolivas in one of his -ck variants that never made it into mainline and is not being maintained. Is this inaccurate?

The presence of SCHED_BATCH in linux/sched.h tells me it is available (on the other hand, SCHED_ISO, also from -ck, is only listed as a comment.)

Fair enough. I should have re-checked recent sources after your mention rather than going on the old thread I found.

Quoted 10 lines
Quoted 6 lines

I was unfamiliar with SCHED_IDLE. Having done a little Googling now, I finally find reference to the man page for sched_setscheduler(2). This appears that it is likely what I wanted.

I think the information I had been able to find was somehwat out of date.

The manpage does say it, but if your local distro does not mention SCHED_BATCH/SCHED_IDLE, then that's a pretty sad distro.

Quoted 7 lines

The doc in sched_setschedule seems complete to me as of man-pages 3.13.

Quoted 3 lines

Is there currently a standardized userspace tool to use to run a command in order to alter its scheduling class? Obviously writing one would be trivial, but didn't know if something like:

man chrt

The latest version of man chrt that I can find implies that it handles SCHED_BATCH but not SCHED_IDLE. To that end, if anyone else is interested, I have thrown together the above-suggested 'runidle' which will invoke the passed command using the SCHED_IDLE scheduler; it's nothing fancy.

Quoted 4 lines

I am running foldingathome under it at the moment, and it seems to be improving the situation somewhat, but I still need/want to test with Mike's referenced patches.

<snipped old version, because of a fixed goof, and this has better formatting for mail client>

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sched.h>
#include <linux/sched.h>

extern char **environ;

int
main(int argc, char **argv) {
struct sched_param sp;
sp.sched_priority = 0;

  if(argc<2) {
    perror("Must specify at least one argument: the path to " \
      "the program to execute. Additional arguments may be " \
      "specified which will be passed to the called program.");
    return EXIT_FAILURE;
  }

  if(sched_setscheduler(0, SCHED_IDLE, &sp) == -1) {
    perror("Failed to alter scheduling class!");
    return EXIT_FAILURE;
  }

  if(argc==2) {
    if(execve(argv[1], NULL, environ) == -1) {
      perror("Failed to execve target!");
    }
  } else {
    if(execve(argv[1], argv+1, environ) == -1) {
      perror("Failed to execve target!");
    }
  }

/* should be unreachable */
return EXIT_FAILURE;
}

Quoted 1 line

-Nathanael

#23 lore raw
In reply to Nathanael Hoyle
Quoted 4 lines · Nathanael Hoyle

1) Is my problem 'expected' based on others' understanding of the current design of the scheduler, or do I have a one-off problem to troubleshoot here?

What kernel are you running (or did my eye glance over that detail in your longish email) ?

Quoted 1 line

2) Am I overlooking obvious alternative (but clean) fixes?

Maybe, we fixed a glaring bug in this department recently (or more even, if you're on older than .28).

Quoted 1 line

3) Does anyone else see the need for static, but low process priorities?

Yep, its rather common.

Quoted 1 line

4) What is the view of introducing a new scheduler class to handle this?

We should have plenty available, SCHED_IDLE should just work -- as should nice 19 for that matter.

#6 lore raw
In reply to Mike Galbraith
Quoted 11 lines · Mike Galbraith
Quoted 3 lines · Nathanael Hoyle

I am running foldingathome under it at the moment, and it seems to be improving the situation somewhat, but I still need/want to test with Mike's referenced patches.

You will most definitely encounter evilness running SCHED_IDLE tasks in a kernel without the SCHED_IDLE fixes.

-Mike

Mike,

Any reason not to apply this fairly simple patch against the 2.6.27 series kernel I'm running now? Are there other relevant changes you're aware of in the later kernel revs for this problem?

Thanks,
-Nathanael

#24 lore raw
In reply to Peter Zijlstra
Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Nathanael Hoyle

1) Is my problem 'expected' based on others' understanding of the current design of the scheduler, or do I have a one-off problem to troubleshoot here?

What kernel are you running (or did my eye glance over that detail in your longish email) ?

I didn't include it, I should have:

$ uname -a
Linux nightmare 2.6.27-gentoo-r7-nhoyle #2 SMP Wed Jan 28 19:04:37 EST
2009 x86_64 Intel(R) Core(TM)2 Quad CPU Q9450 @ 2.66GHz GenuineIntel
GNU/Linux
Quoted 5 lines
Quoted 1 line

2) Am I overlooking obvious alternative (but clean) fixes?

Maybe, we fixed a glaring bug in this department recently (or more even, if you're on older than .28).

Yep, .27 atm.

Quoted 9 lines
Quoted 1 line

3) Does anyone else see the need for static, but low process priorities?

Yep, its rather common.

Quoted 1 line

4) What is the view of introducing a new scheduler class to handle this?

We should have plenty available, SCHED_IDLE should just work -- as should nice 19 for that matter.

Thanks!
-Nathanael

#25 lore raw
Quoted 11 lines · Nathanael Hoyle
Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Nathanael Hoyle

1) Is my problem 'expected' based on others' understanding of the current design of the scheduler, or do I have a one-off problem to troubleshoot here?

What kernel are you running (or did my eye glance over that detail in your longish email) ?

Quoted 6 lines

I didn't include it, I should have:

$ uname -a
Linux nightmare 2.6.27-gentoo-r7-nhoyle #2 SMP Wed Jan 28 19:04:37 EST
2009 x86_64 Intel(R) Core(TM)2 Quad CPU Q9450 @ 2.66GHz GenuineIntel
GNU/Linux

Ah, then please do as Mike suggested, try 28.2 or 29-rc3, if you still have trouble with those, please let us know.

#7 lore raw
In reply to Nathanael Hoyle
Quoted 3 lines · Nathanael Hoyle

Any reason not to apply this fairly simple patch against the 2.6.27 series kernel I'm running now? Are there other relevant changes you're aware of in the later kernel revs for this problem?

You'd have to dig out a few other changes in order to apply it to 27.

Yes there are other relevant changes, but unless you're familiar with the source, you'll be better off just trying 28.stable of the latest rc. You'll have to find extract and back-port otherwise.

-Mike

#26 lore raw
In reply to Peter Zijlstra
Quoted 14 lines · Peter Zijlstra
Quoted 13 lines · Nathanael Hoyle
Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Nathanael Hoyle

1) Is my problem 'expected' based on others' understanding of the current design of the scheduler, or do I have a one-off problem to troubleshoot here?

What kernel are you running (or did my eye glance over that detail in your longish email) ?

I didn't include it, I should have:

Quoted 8 lines
Quoted 4 lines
$ uname -a
Linux nightmare 2.6.27-gentoo-r7-nhoyle #2 SMP Wed Jan 28 19:04:37 EST
2009 x86_64 Intel(R) Core(TM)2 Quad CPU Q9450 @ 2.66GHz GenuineIntel
GNU/Linux

Ah, then please do as Mike suggested, try 28.2 or 29-rc3, if you still have trouble with those, please let us know.

Ok, I'm now running:

Linux nightmare 2.6.28.2-nhoyle #1 SMP Fri Jan 30 04:50:03 EST 2009 x86_64 Intel(R) Core(TM)2 Quad CPU Q9450 @ 2.66GHz GenuineIntel GNU/Linux

Initial conclusion is that whatever defects were corrected (non SCHED_IDLE specific defects that is), the newer kernel version does the trick. Video playback is as smooth as ever when running foldingathome at simple nice 19 priority.

I am not sure that I can perceive a difference so far in testing that versus using SCHED_IDLE. I will probably continue to use the latter anyhow, as that represents more accurately the semantics that I'm trying to achieve.

I had previously tried upgrading my kernel version in case that would fix it, but even the latest available kernels in the portage tree for Gentoo are older than 2.6.28.2. The 'stable' ones were all still 2.6.27. I have had some concerns about Gentoo as a distro for some time, but it still allows me more freedom and perfomance optimization than do most other distros. I'll leave that at that for now to avoid starting any religous wars over distros.

Once I downloaded and built the latest vanilla 2.6.28.2 sources from kernel.org though, everything seems improved, as mentioned above.

Thanks to each of you who responded for all the help. I will continue to experiment over the next week or so and provide feedback if I see anything further unusual, but so far things seem good.

-Nathanael

#27 lore raw
Quoted 10 lines · Nathanael Hoyle

Ok, I'm now running:

Linux nightmare 2.6.28.2-nhoyle #1 SMP Fri Jan 30 04:50:03 EST 2009 x86_64 Intel(R) Core(TM)2 Quad CPU Q9450 @ 2.66GHz GenuineIntel GNU/Linux

Initial conclusion is that whatever defects were corrected (non SCHED_IDLE specific defects that is), the newer kernel version does the trick. Video playback is as smooth as ever when running foldingathome at simple nice 19 priority.

Good to hear, thanks for testing.

Peter, since 27 is a long term maintenance kernel, do you think 1af5f73 and 046e7f7 (at least) are 27.stable candidates?

-Mike

#28 lore raw
Quoted 13 lines · Mike Galbraith
Quoted 10 lines · Nathanael Hoyle

Ok, I'm now running:

Linux nightmare 2.6.28.2-nhoyle #1 SMP Fri Jan 30 04:50:03 EST 2009 x86_64 Intel(R) Core(TM)2 Quad CPU Q9450 @ 2.66GHz GenuineIntel GNU/Linux

Initial conclusion is that whatever defects were corrected (non SCHED_IDLE specific defects that is), the newer kernel version does the trick. Video playback is as smooth as ever when running foldingathome at simple nice 19 priority.

Quoted 4 lines

Good to hear, thanks for testing.

Peter, since 27 is a long term maintenance kernel, do you think 1af5f73 and 046e7f7 (at least) are 27.stable candidates?

1af5f730fc1bf7c62ec9fb2d307206e18bf40a69 and e17036dac189dd034c092a91df56aa740db7146d you mean?

I guess that makes sense.

#29 lore raw
Quoted 14 lines · Peter Zijlstra
Quoted 13 lines · Mike Galbraith
Quoted 10 lines · Nathanael Hoyle

Ok, I'm now running:

Linux nightmare 2.6.28.2-nhoyle #1 SMP Fri Jan 30 04:50:03 EST 2009 x86_64 Intel(R) Core(TM)2 Quad CPU Q9450 @ 2.66GHz GenuineIntel GNU/Linux

Initial conclusion is that whatever defects were corrected (non SCHED_IDLE specific defects that is), the newer kernel version does the trick. Video playback is as smooth as ever when running foldingathome at simple nice 19 priority.

Quoted 7 lines
Quoted 4 lines

Good to hear, thanks for testing.

Peter, since 27 is a long term maintenance kernel, do you think 1af5f73 and 046e7f7 (at least) are 27.stable candidates?

1af5f730fc1bf7c62ec9fb2d307206e18bf40a69 and e17036dac189dd034c092a91df56aa740db7146d you mean?

Yeah.

Quoted 1 line

I guess that makes sense.

(adds cc)

-Mike

#15 lore raw
In reply to Nathanael Hoyle
Quoted 13 lines · Nathanael Hoyle

<snipped old version, because of a fixed goof, and this has better formatting for mail client>

extern char **environ;
[...]

 if(argc==2) {
   if(execve(argv[1], NULL, environ) == -1) {
     perror("Failed to execve target!");
   }
 } else {
   if(execve(argv[1], argv+1, environ) == -1) {
     perror("Failed to execve target!");
   }
Quoted 1 line

}

Are you sure your first execve even works? I would have used

if (execvp(argv[1], &argv[1]) < 0)

	...

just so (a) I do not have to deal with the ugly 'extern char **environ' [such should have been in a libc header imho] or use
int main(int argc, char **argv, char **envp); (b) execvp so that it looks through $PATH, just as /bin/su (resp. the shell it starts) would do.

#22 lore raw
In reply to Nathanael Hoyle
Quoted 10 lines · Nathanael Hoyle
Quoted 8 lines · V.Radhakrishnan

However, the kernel also supports SCHED_FIFO and SCHED_RR which supports Real Time capabilities, albeit as root.
[...]
If you wish 100% smooth display, you could make it run as SCHED_FIFO which means that your foldingathome would wait quietly for the movie to get completed fully. What's "wrong" with that aproach, which is essentially what you want ?

Quoted 4 lines

My view of what's "wrong" with that approach is that it requires root privileges to boost the scheduling priority of each and every process (although in this case, mplayer is the issue) which I want to not be affected by foldingathome's CPU usage.

SCHED_FIFO is dangerous - it is easy to essentially lock up your box simply because the process in question (e.g. video decoder) just runs forever (e.g. bug causing busyloop, and/or others do not get to run (no other processes in the same priority class). Or they (X.org, for displaying your video and handling user input) run only for short amounts of time only, giving a borked responsiveness experience to the user.

It was about time SCHED_{BATCH,IDLE} came along ;-)

Quoted 7 lines

While I happen to be root on
this system, since it is my desktop, I would imagine there are instances where the root user/administrator of a system wanted to be able to run items which had no impact on other users, including allowing them to run fast and responsive applications. Aside from that, it's a PITA to start mplayer playing, go renice -19 it and resume watching my movie every time.

Even making mplayer -19 will not completely cause FAH to get zero CPU time if the Regular Desktop Processes Everybody Needs would already max out the CPU.

#16 lore raw
In reply to Nathanael Hoyle
Quoted 11 lines · Nathanael Hoyle
Quoted 5 lines
Quoted 3 lines

Is there currently a standardized userspace tool to use to run a command in order to alter its scheduling class? Obviously writing one would be trivial, but didn't know if something like:

man chrt

The latest version of man chrt that I can find implies that it handles SCHED_BATCH but not SCHED_IDLE. To that end, if anyone else is interested, I have thrown together the above-suggested 'runidle' which will invoke the passed command using the SCHED_IDLE scheduler; it's nothing fancy.

Should have added -i to chrt instead and submit ;-)

#8 lore raw
In reply to Mike Galbraith
Quoted 9 lines · Mike Galbraith
Quoted 4 lines · Nathanael Hoyle

I am running foldingathome under it at the moment, and it seems to be improving the situation somewhat, but I still need/want to test with Mike's referenced patches.

You will most definitely encounter evilness running SCHED_IDLE tasks in a kernel without the SCHED_IDLE fixes.

Speaking of SCHED_IDLE fixes, is 6bc912b71b6f33b041cfde93ca3f019cbaa852bc going to be put into the next stable 2.6.28 release? Without it on 2.6.28.2, I can still produce minutes-long freezes with BOINC or other idle processes.

With the above commit on top of 2.6.28.2 and also cce7ade803699463ecc62a065ca522004f7ccb3d, the problem is solved, though I assume cce7ad isn't actually required to fix that, and I can test that if desired.

#9 lore raw
Quoted 15 lines · Brian Rogers
Quoted 9 lines · Mike Galbraith
Quoted 4 lines · Nathanael Hoyle

I am running foldingathome under it at the moment, and it seems to be improving the situation somewhat, but I still need/want to test with Mike's referenced patches.

You will most definitely encounter evilness running SCHED_IDLE tasks in a kernel without the SCHED_IDLE fixes.

Speaking of SCHED_IDLE fixes, is 6bc912b71b6f33b041cfde93ca3f019cbaa852bc going to be put into the next stable 2.6.28 release? Without it on 2.6.28.2, I can still produce minutes-long freezes with BOINC or other idle processes.

Quoted 4 lines

With the above commit on top of 2.6.28.2 and also cce7ade803699463ecc62a065ca522004f7ccb3d, the problem is solved, though I assume cce7ad isn't actually required to fix that, and I can test that if desired.

I think they both should go to stable, but dunno if they're headed that direction or not.

One way to find out, CCs added.

-Mike

Acked-by Tested-by Acked-by
#10 lore raw
Quoted 21 lines · Mike Galbraith
Quoted 15 lines · Brian Rogers
Quoted 9 lines · Mike Galbraith
Quoted 4 lines · Nathanael Hoyle

I am running foldingathome under it at the moment, and it seems to be improving the situation somewhat, but I still need/want to test with Mike's referenced patches.

You will most definitely encounter evilness running SCHED_IDLE tasks in a kernel without the SCHED_IDLE fixes.

Speaking of SCHED_IDLE fixes, is 6bc912b71b6f33b041cfde93ca3f019cbaa852bc going to be put into the next stable 2.6.28 release? Without it on 2.6.28.2, I can still produce minutes-long freezes with BOINC or other idle processes.

Quoted 4 lines

With the above commit on top of 2.6.28.2 and also cce7ade803699463ecc62a065ca522004f7ccb3d, the problem is solved, though I assume cce7ad isn't actually required to fix that, and I can test that if desired.

Quoted 4 lines

I think they both should go to stable, but dunno if they're headed that direction or not.

One way to find out, CCs added.

For those who may want to run SCHED_IDLE tasks in .27, I've integrated and lightly tested the fixes required to do so. One additional commit was needed to get SCHED_IDLE vs nice 19 working right, namely f9c0b09. Without that, SCHED_IDLE tasks received more CPU than nice 19 tasks.

Since .27 is in long-term maintenance, I'd integrate into stable, but that's not my decision. Anyone who applies the below to their stable kernel gets to keep all the pieces should something break ;-)

commit f9c0b0950d5fd8c8c5af39bc061f27ea8fddcac3

Author: Peter Zijlstra <[email protected]>
Date:   Fri Oct 17 19:27:04 2008 +0200

sched: revert back to per-rq vruntime

Vatsa rightly points out that having the runqueue weight in the vruntime calculations can cause unfairness in the face of task joins/leaves.

Suppose: dv = dt * rw / w

Then take 10 tasks t_n, each of similar weight. If the first will run 1 then its vruntime will increase by 10. Now, if the next 8 tasks leave after having run their 1, then the last task will get a vruntime increase of 2 after having run 1.

Which will leave us with 2 tasks of equal weight and equal runtime, of which one will not be scheduled for 8/2=4 units of time.

Ergo, we cannot do that and must use: dv = dt / w.

This means we cannot have a global vruntime based on effective priority, but must instead go back to the vruntime per rq model we started out with.

This patch was lightly tested by doing starting while loops on each nice level and observing their execution time, and a simple group scenario of 1:2:3 pinned to a single cpu.

---
 kernel/sched_fair.c |   32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)
kernel/sched_fair.c +15 -17
Index: linux-2.6.27/kernel/sched_fair.c
===================================================================
@@ -334,7 +334,7 @@ int sched_nr_latency_handler(struct ctl_
 #endif
 
 /*
- * delta *= w / rw
+ * delta *= P[w / rw]
  */
 static inline unsigned long
 calc_delta_weight(unsigned long delta, struct sched_entity *se)
@@ -348,15 +348,13 @@ calc_delta_weight(unsigned long delta, s
 }
 
 /*
- * delta *= rw / w
+ * delta /= w
  */
 static inline unsigned long
 calc_delta_fair(unsigned long delta, struct sched_entity *se)
 {
-	for_each_sched_entity(se) {
-		delta = calc_delta_mine(delta,
-				cfs_rq_of(se)->load.weight, &se->load);
-	}
+	if (unlikely(se->load.weight != NICE_0_LOAD))
+		delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
 
 	return delta;
 }
@@ -386,26 +384,26 @@ static u64 __sched_period(unsigned long
  * We calculate the wall-time slice from the period by taking a part
  * proportional to the weight.
  *
- * s = p*w/rw
+ * s = p*P[w/rw]
  */
 static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
 {
-	return calc_delta_weight(__sched_period(cfs_rq->nr_running), se);
+	unsigned long nr_running = cfs_rq->nr_running;
+
+	if (unlikely(!se->on_rq))
+		nr_running++;
+
+	return calc_delta_weight(__sched_period(nr_running), se);
 }
 
 /*
  * We calculate the vruntime slice of a to be inserted task
  *
- * vs = s*rw/w = p
+ * vs = s/w
  */
-static u64 sched_vslice_add(struct cfs_rq *cfs_rq, struct sched_entity *se)
+static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
 {
-	unsigned long nr_running = cfs_rq->nr_running;
-
-	if (!se->on_rq)
-		nr_running++;
-
-	return __sched_period(nr_running);
+	return calc_delta_fair(sched_slice(cfs_rq, se), se);
 }
 
 /*
@@ -683,7 +681,7 @@ place_entity(struct cfs_rq *cfs_rq, stru
 	 * stays open at the end.
 	 */
 	if (initial && sched_feat(START_DEBIT))
-		vruntime += sched_vslice_add(cfs_rq, se);
+		vruntime += sched_vslice(cfs_rq, se);
 
 	if (!initial) {
 		/* sleeps upto a single latency don't count. */

commit 1af5f730fc1bf7c62ec9fb2d307206e18bf40a69

Author: Peter Zijlstra <[email protected]>
Date:   Fri Oct 24 11:06:13 2008 +0200

sched: more accurate min_vruntime accounting

Mike noticed the current min_vruntime tracking can go wrong and skip the current task. If the only remaining task in the tree is a nice 19 task with huge vruntime, new tasks will be inserted too far to the right too, causing some interactibity issues.

min_vruntime can only change due to the leftmost entry disappearing (dequeue_entity()), or by the leftmost entry being incremented past the next entry, which elects a new leftmost (__update_curr())

Due to the current entry not being part of the actual tree, we have to compare the leftmost tree entry with the current entry, and take the leftmost of these two.

So create a update_min_vruntime() function that takes computes the leftmost vruntime in the system (either tree of current) and increases the cfs_rq->min_vruntime if the computed value is larger than the previously found min_vruntime. And call this from the two sites we've identified that can change min_vruntime.

---
 kernel/sched_fair.c |   49 +++++++++++++++++++++++++------------------------
 1 file changed, 25 insertions(+), 24 deletions(-)
kernel/sched_fair.c +25 -24

89 lines not on this page · raw message

commit e17036dac189dd034c092a91df56aa740db7146d

Author: Peter Zijlstra <[email protected]>
Date:   Thu Jan 15 14:53:39 2009 +0100

sched: fix update_min_vruntime

Impact: fix SCHED_IDLE latency problems

OK, so we have 1 running task A (which is obviously curr and the tree is equally obviously empty).

'A' nicely chugs along, doing its thing, carrying min_vruntime along as it goes.

Then some whacko speed freak SCHED_IDLE task gets inserted due to SMP balancing, which is very likely far right, in that case

update_curr
update_min_vruntime
cfs_rq->rb_leftmost := true (the crazy task sitting in a tree)
vruntime = se->vruntime

and voila, min_vruntime is waaay right of where it ought to be.

OK, so why did I write it like that to begin with...

Aah, yes.

Say we've just dequeued current

schedule
deactivate_task(prev)
dequeue_entity
update_min_vruntime

Then we'll set

vruntime = cfs_rq->min_vruntime;

we find !cfs_rq->curr, but do find someone in the tree. Then we _must_ do vruntime = se->vruntime, because

vruntime = min_vruntime(vruntime := cfs_rq->min_vruntime, se->vruntime)

will not advance vruntime, and cause lags the other way around (which we fixed with that initial patch: 1af5f730fc1bf7c62ec9fb2d307206e18bf40a69 (sched: more accurate min_vruntime accounting).

---
 kernel/sched_fair.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/sched_fair.c +1 -1
Index: linux-2.6.27/kernel/sched_fair.c
===================================================================
@@ -233,7 +233,7 @@ static void update_min_vruntime(struct c
 						   struct sched_entity,
 						   run_node);
 
-		if (vruntime == cfs_rq->min_vruntime)
+		if (!cfs_rq->curr)
 			vruntime = se->vruntime;
 		else
 			vruntime = min_vruntime(vruntime, se->vruntime);

commit 6bc912b71b6f33b041cfde93ca3f019cbaa852bc

Author: Peter Zijlstra <[email protected]>
Date:   Thu Jan 15 14:53:38 2009 +0100

sched: SCHED_OTHER vs SCHED_IDLE isolation

Stronger SCHED_IDLE isolation:

- no SCHED_IDLE buddies
- never let SCHED_IDLE preempt on wakeup
- always preempt SCHED_IDLE on wakeup
- limit SLEEPER fairness for SCHED_IDLE.

---
 kernel/sched_fair.c |   21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)
kernel/sched_fair.c +16 -5
Index: linux-2.6.27/kernel/sched_fair.c
===================================================================
@@ -689,9 +689,13 @@ place_entity(struct cfs_rq *cfs_rq, stru
 			unsigned long thresh = sysctl_sched_latency;
 
 			/*
-			 * convert the sleeper threshold into virtual time
+			 * Convert the sleeper threshold into virtual time.
+			 * SCHED_IDLE is a special sub-class.  We care about
+			 * fairness only relative to other SCHED_IDLE tasks,
+			 * all of which have the same weight.
 			 */
-			if (sched_feat(NORMALIZED_SLEEPER))
+			if (sched_feat(NORMALIZED_SLEEPER) &&
+					task_of(se)->policy != SCHED_IDLE)
 				thresh = calc_delta_fair(thresh, se);
 
 			vruntime -= thresh;
@@ -1347,15 +1351,22 @@ static void check_preempt_wakeup(struct
 	if (unlikely(se == pse))
 		return;
 
-	cfs_rq_of(pse)->next = pse;
+	if (likely(task_of(se)->policy != SCHED_IDLE))
+		cfs_rq_of(pse)->next = pse;
 
 	/*
-	 * Batch tasks do not preempt (their preemption is driven by
+	 * Batch and idle tasks do not preempt (their preemption is driven by
 	 * the tick):
 	 */
-	if (unlikely(p->policy == SCHED_BATCH))
+	if (unlikely(p->policy != SCHED_NORMAL))
 		return;
 
+	/* Idle tasks are by definition preempted by everybody. */
+	if (unlikely(curr->policy == SCHED_IDLE)) {
+		resched_task(curr);
+		return;
+	}
+
 	if (!sched_feat(WAKEUP_PREEMPT))
 		return;
 
#30 lore raw
In reply to Nathanael Hoyle
Quoted 22 lines · Nathanael Hoyle

All (though perhaps of special interest to a few such as Ingo, Peter, and David),

I am posting regarding an issue I have been dealing with recently, though this post is not really a request for troubleshooting. Instead I'd like to ramble for just a moment about my understanding of the current 2.6 scheduler, describe the behavior I'm seeing, and discuss a couple of the architectural solutions I've considered, as well as pose the question whether anyone else views this as a general-case problem worthy of being addressed, or whether this is something that gets ignored by and large. It is my hope that this is not too off-topic for this group.

First, let me explain the issue I encountered. I am running a relatively powerful system for a home desktop, an Intel Core 2 Quad Q9450 with 4 GB of RAM. If it matters for the discussion, it also has 4 drives in an mdraid raid-5 array, and decent I/O throughput. In normal circumstances it is quite responsive as a desktop (kde 3.5.4 atm). It is further a very carefully configured kernel build, including only those things which I truly need, and excluding everything else. I often use it to watch DVD movies, and have had no trouble with performance in general.

Quoted 6 lines

Recently I installed the Folding@Home client, which many of you may be familiar with, intended to utilize spare CPU cycles to perform protein folding simulations in order to further medical research. It is not a multi-threaded client at this point, so it simply runs four instances on my system, since it has four cores. It is configured to run at nice-level 19.

I too have seen this behaviour on my quad core Q6600 mythtv box, and I too run folding@home on it and have a 4 drive raid5.

Quoted 90 lines

Because it is heavily optimized, and needs little external data to perform its work, it spends almost all of its time cpu-bound, with little to no io-wait or blocking on network calls, etc. I had been using it for about a week with no real difficulty until I went to watch another DVD and found that the video was slightly stuttery/jerky so long as foldingathome was running in the background. Once I shut it down, the video playback resumed its normal smooth form.

There are a couple simple solutions to this:

Substantially boosting the process priority of the mplayer process also returns the video to smooth playback, but this is undesirable in that it requires manual intervention each time, and root privileges. It fails to achieve what I want, which is for the foldingathome computation to not interfere with anything else I may try to do. I want my compiles to be as *exactly* as fast as they were without it as possible, etc.

Stopping foldingathome before I do something performance sensitive is also possible, but again smacks of workaround rather than solution. The scheduler should be able to resolve the goal without me stopping the other work.

I have done a bit of research on how the kernel scheduler works, and why I am seeing this behavior. I had previously, apparently ignorantly, equated 'nice 19' with being akin to Microsoft Windows' 'idle' thread priority, and assumed it would never steal CPU cycles from a process with a higher(lower, depending on nomenclature) priority.

It is my current understanding that when mplayer is running (also typically CPU bound, occassionally it becomes I/O bound briefly), one of the instances of foldingathome, which is sharing the CPU (core) with mplayer starts getting starved, and the scheduler dynamically rewards it with up to four additional priority levels based on the time remaining in its quantum which it was not allowed to execute for.

At this point, when mplayer blocks for just a moment, say to page in the data for the next video frame, foldingathome gets scheduled again, and gets to run for at least MIN_TIMESLICE (plus, due to the lack of kernel pre-emptibility, possibly longer). It appears that it takes too long to switch back to mplayer and the result is the stuttering picture I observe.

I have tried adjusting CONFIG_HZ_xxx from 300 (where I had it) to 1000, and noted some improvement, but not complete remedy.

In my prior searching on this, I found only one poster with the same essential problem (from 2004, and regarding distributed.net in the background, which is essentially the same problem). The only technical answer given him was to perhaps try tuning the MIN_TIMESLICE value downward. It is my understanding that this parameter is relatively important in order to avoid cache thrashing, and I do not wish to alter it and have not so far.

Given all of the above, I am unconvinced that I see a good overall solution. However, one thing that seems to me a glaring weakness of the scheduler is that only realtime priority threads can be given static priorities. What I really want for foldingathome, and similar tasks, is static, low priority. Something that would not boost up, no matter how well behaved it was or how much it had been starved, or how close to the same memory segments the needed code was.

I think that there are probably (at least) three approaches here. One I consider unnacceptable at the outset, which is to alter the semantics of nice 19 such that it does not boost. Since this would break existing assumptions and code, I do not think it is feasible.

Secondly, one could add additional nice levels which would correspond to new static priorities below the bottom of the current user ones. This should not interfere with the O(1) scheduler implementation as I understand it, because current I believe 5 32-bit words are used to flag the queue usage, and 140 priorities leaves 20 more bits available for new priorities. This has its own problems however, in that existing tools which examine process priorities could break on priorities outside the known 'nice' range of -20 to 19.

Finally, new scheduling classes could be introduced, together with new system calls so that applications could select a different scheduling class at startup. In this way, applications could volunteer to use a scheduling class which never received dynamic 'reward' boosts that would raise their priorities. I believe Solaris has done this since Solaris 9, with the 'FX' scheduling class.

Stepping back:

1) Is my problem 'expected' based on others' understanding of the current design of the scheduler, or do I have a one-off problem to troubleshoot here?

2) Am I overlooking obvious alternative (but clean) fixes?

Quoted 8 lines

3) Does anyone else see the need for static, but low process priorities?

4) What is the view of introducing a new scheduler class to handle this?

I welcome any further feedback on this. I will try to follow replies on-list, but would appreciate being CC'd off-list as well. Please make the obvious substitution to my email address in order to bypass the spam-killer.

Well I haven't looked into it myself, but I can certainly confirm that the current bahaviour is downright awful with this particular mix of processes.

Signature · 1 line
Len Sorensen
#11 lore raw
In reply to Mike Galbraith
Quoted 27 lines · Mike Galbraith
Quoted 21 lines · Mike Galbraith
Quoted 15 lines · Brian Rogers
Quoted 9 lines · Mike Galbraith
Quoted 4 lines · Nathanael Hoyle

I am running foldingathome under it at the moment, and it seems to be improving the situation somewhat, but I still need/want to test with Mike's referenced patches.

You will most definitely encounter evilness running SCHED_IDLE tasks in a kernel without the SCHED_IDLE fixes.

Speaking of SCHED_IDLE fixes, is 6bc912b71b6f33b041cfde93ca3f019cbaa852bc going to be put into the next stable 2.6.28 release? Without it on 2.6.28.2, I can still produce minutes-long freezes with BOINC or other idle processes.

Quoted 4 lines

With the above commit on top of 2.6.28.2 and also cce7ade803699463ecc62a065ca522004f7ccb3d, the problem is solved, though I assume cce7ad isn't actually required to fix that, and I can test that if desired.

Quoted 4 lines

I think they both should go to stable, but dunno if they're headed that direction or not.

One way to find out, CCs added.

Quoted 8 lines

For those who may want to run SCHED_IDLE tasks in .27, I've integrated and lightly tested the fixes required to do so. One additional commit was needed to get SCHED_IDLE vs nice 19 working right, namely f9c0b09. Without that, SCHED_IDLE tasks received more CPU than nice 19 tasks.

Since .27 is in long-term maintenance, I'd integrate into stable, but that's not my decision. Anyone who applies the below to their stable kernel gets to keep all the pieces should something break ;-)

I'm going to hold off and not do this, as it seems too risky.

But thanks for the pointers, perhaps someone else will want to do this for their distro kernels if they have problems with this.

thanks,

greg k-h

#12 lore raw
Quoted 39 lines · Greg KH
Quoted 27 lines · Mike Galbraith
Quoted 16 lines · Mike Galbraith
Quoted 14 lines · Brian Rogers
Quoted 12 lines · Mike Galbraith
Quoted 5 lines · Nathanael Hoyle

I am running foldingathome under it at the moment, and it seems to be improving the situation somewhat, but I still need/want to test with Mike's referenced patches.

You will most definitely encounter evilness running SCHED_IDLE tasks in a kernel without the SCHED_IDLE fixes.

Quoted 9 lines
Quoted 4 lines

Speaking of SCHED_IDLE fixes, is 6bc912b71b6f33b041cfde93ca3f019cbaa852bc going to be put into the next stable 2.6.28 release? Without it on 2.6.28.2, I can still produce minutes-long freezes with BOINC or other idle processes.

With the above commit on top of 2.6.28.2 and also cce7ade803699463ecc62a065ca522004f7ccb3d, the problem is solved, though I assume cce7ad isn't actually required to fix that, and I can test that if desired.

I think they both should go to stable, but dunno if they're headed that direction or not.

One way to find out, CCs added.

Quoted 9 lines

For those who may want to run SCHED_IDLE tasks in .27, I've integrated and lightly tested the fixes required to do so. One additional commit was needed to get SCHED_IDLE vs nice 19 working right, namely f9c0b09. Without that, SCHED_IDLE tasks received more CPU than nice 19 tasks.

Since .27 is in long-term maintenance, I'd integrate into stable, but that's not my decision. Anyone who applies the below to their stable kernel gets to keep all the pieces should something break ;-)

Quoted 5 lines

I'm going to hold off and not do this, as it seems too risky.

But thanks for the pointers, perhaps someone else will want to do this for their distro kernels if they have problems with this.

Is this statement meant to apply to both 2.6.27 and 2.6.28, or just 2.6.27?

#13 lore raw
Quoted 44 lines · Brian Rogers
Quoted 43 lines · Greg KH
Quoted 40 lines · Mike Galbraith
Quoted 24 lines · Mike Galbraith
Quoted 17 lines · Brian Rogers
Quoted 10 lines · Mike Galbraith
Quoted 4 lines · Nathanael Hoyle

I am running foldingathome under it at the moment, and it seems to be improving the situation somewhat, but I still need/want to test with Mike's referenced patches.

You will most definitely encounter evilness running SCHED_IDLE tasks in
a kernel without the SCHED_IDLE fixes.

Speaking of SCHED_IDLE fixes, is 6bc912b71b6f33b041cfde93ca3f019cbaa852bc going to be put into the next stable 2.6.28 release? Without it on 2.6.28.2, I can still produce minutes-long freezes with BOINC or other idle processes.

Quoted 5 lines

With the above commit on top of 2.6.28.2 and also cce7ade803699463ecc62a065ca522004f7ccb3d, the problem is solved, though I assume cce7ad isn't actually required to fix that, and I can test that if desired.

Quoted 5 lines

I think they both should go to stable, but dunno if they're headed that direction or not.

One way to find out, CCs added.

For those who may want to run SCHED_IDLE tasks in .27, I've integrated and lightly tested the fixes required to do so. One additional commit was needed to get SCHED_IDLE vs nice 19 working right, namely f9c0b09. Without that, SCHED_IDLE tasks received more CPU than nice 19 tasks.

Since .27 is in long-term maintenance, I'd integrate into stable, but that's not my decision. Anyone who applies the below to their stable kernel gets to keep all the pieces should something break ;-)

Quoted 7 lines
Quoted 5 lines

I'm going to hold off and not do this, as it seems too risky.

But thanks for the pointers, perhaps someone else will want to do this for their distro kernels if they have problems with this.

Is this statement meant to apply to both 2.6.27 and 2.6.28, or just 2.6.27?

Both.