Skip to contentSkip to conversation

sched_yield() on 2.6.25

DISCUSSION Bodo Eggert 3 participants 5 messages Jun 11, 2008 – Jun 13, 2008 4 messages not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#4 lore raw
Quoted 8 lines · Peter Zijlstra
Quoted 6 lines · Jakub Jozwicki

From the man sched_yield:

A process can relinquish the processor voluntarily without blocking by calling sched_yield(). The process will then be moved to the end of the queue for its static priority and a new process gets to run.

Quoted 8 lines
Quoted 2 lines

and also IEEE/Open Group:
http://www.opengroup.org/onlinepubs/000095399/functions/sched_yield.html

Yeah, except that is for Real-Time scheduling classes, SCHED_OTHER doesn't have static priority queues.

SCHED_OTHER doesn't have a specified implementation - so relying on it to do anything specific is well outside the scope of definition.

OTOH, it's sane not to schedule exactly the thread which just tried to say "I can't do any sane work, please run another thread.

#5 lore raw

Hello,

Quoted 12 lines · Bodo Eggert
Quoted 11 lines · Peter Zijlstra
Quoted 8 lines · Jakub Jozwicki

From the man sched_yield:

A process can relinquish the processor voluntarily without blocking by calling sched_yield(). The process will then be moved to the end of the queue for its static priority and a new process gets to run.

and also IEEE/Open Group:
http://www.opengroup.org/onlinepubs/000095399/functions/sched_yield.html

Quoted 9 lines
Quoted 5 lines

Yeah, except that is for Real-Time scheduling classes, SCHED_OTHER doesn't have static priority queues.

SCHED_OTHER doesn't have a specified implementation - so relying on it to do anything specific is well outside the scope of definition.

OTOH, it's sane not to schedule exactly the thread which just tried to say "I can't do any sane work, please run another thread.

That's not the definition of sched_yield(). See the earlier emails, and the quote above.

As the code after sched_yield() has to be executed the thread will be rescheduled soon (or even immediately) anyway.

The users not understanding the limited scope where sched_yield() behaves deterministicly, seem to think that _yield() will yield() AND lower the thread's dynamic priority for SCHED_OTHER. Is downgrading the dynamic priority a behavioral option?

On the other hand, I don't think anything should encourage the use of sched_yield() outside of the rare SCHED_FIFO/RR case.

Regards,

Signature · 1 line
Leon
#6 lore raw

Leon Woestenberg wrote:
[...]

Quoted 11 lines

That's not the definition of sched_yield(). See the earlier emails, and the quote above.

As the code after sched_yield() has to be executed the thread will be rescheduled soon (or even immediately) anyway.

The users not understanding the limited scope where sched_yield() behaves deterministicly, seem to think that _yield() will yield() AND lower the thread's dynamic priority for SCHED_OTHER. Is downgrading the dynamic priority a behavioral option?

That can be done of course, but that too will cause breakage. Consider a multithreaded app mistakenly relying on sched_yield.

Priority downgrading might work really well as long as this app runs alone, the yielding thread stops and the others progress, so sched_yield works for "userspace locking". And it works so well, the app uses it a lot.

Then someone recompiles the distro or runs some other kinds of cpu hogs that drives the load well above 1. Users expect the apps to run a little slower because of this. But a load of 5 still ought to give you 1/5 of the cpu - and with today's CPU's that might still be better than a 5-year old machine. Interactive software should almost not notice, as it don't use the cpu that much anyway - and it gets priority over cpu hogs when it occationally needs to do something.

But now this multithreaded app practically
stops because it yields a lot - an everytime it lowers its priority below not only its own other threads, but below the various cpu hogs as well. (Compilers gets dynamic boosts too, as they wait a little for the disk now and then. A parallel compile still keeps the total load high.)

I remember seeing openoffice taking 5min to start some years ago, with a compile going on. Of course there were other problems like swapping and a smaller computer, but other apps were merely slow, not that glacial.

Quoted 3 lines

On the other hand, I don't think anything should encourage the use of sched_yield() outside of the rare SCHED_FIFO/RR case.

Exactly. There seems to be no way to make sched_yield work "as expected" for all the ways it is abused, so better use something else.

Helge Hafting

#7 lore raw
In reply to Leon Woestenberg
Quoted 19 lines · Leon Woestenberg
Quoted 18 lines · Bodo Eggert
Quoted 16 lines · Peter Zijlstra
Quoted 8 lines · Jakub Jozwicki

From the man sched_yield:

A process can relinquish the processor voluntarily without blocking by calling sched_yield(). The process will then be moved to the end of the queue for its static priority and a new process gets to run.

and also IEEE/Open Group:
http://www.opengroup.org/onlinepubs/000095399/functions/sched_yield.html

Yeah, except that is for Real-Time scheduling classes, SCHED_OTHER doesn't have static priority queues.

SCHED_OTHER doesn't have a specified implementation - so relying on it to do anything specific is well outside the scope of definition.

Quoted 8 lines
Quoted 3 lines

OTOH, it's sane not to schedule exactly the thread which just tried to say "I can't do any sane work, please run another thread.

That's not the definition of sched_yield(). See the earlier emails, and the quote above.

As the code after sched_yield() has to be executed the thread will be rescheduled soon (or even immediately) anyway.

The code after yield() is most likely to not run successfully (and as a result will return to the yield call) unless some time passes, and this time can pass while another process gets the CPU. It might even depend on another process to change the system state.

Besides that, "Schedule another process, if you can" is part of the semantics of yield. The code after yield should therefore be expected to NOT be the very next code to run.

If you can't do that, it's fine, the process will abuse some more innocent electrons for busy waiting, but if you can support this yield() semantics, the system will perform much better. Won't it?

Quoted 4 lines

The users not understanding the limited scope where sched_yield() behaves deterministicly, seem to think that _yield() will yield() AND lower the thread's dynamic priority for SCHED_OTHER. Is downgrading the dynamic priority a behavioral option?

I expect it to be. It may cause lower-nice-level processes to run, but the (lack of) definition allows it.

Quoted 2 lines

On the other hand, I don't think anything should encourage the use of sched_yield() outside of the rare SCHED_FIFO/RR case.

I agree that sleeping should be prefered, but if you really have to busy-wait for the next thread, you'll want yield() semantics.

#8 lore raw
Quoted 2 lines · Bodo Eggert

On Thu, 12 Jun 2008, Leon Woestenberg wrote:

[...]

Quoted 5 lines
Quoted 4 lines

As the code after sched_yield() has to be executed the thread will be rescheduled soon (or even immediately) anyway.

Quoted 9 lines

The code after yield() is most likely to not run successfully (and as a result will return to the yield call) unless some time passes, and this time can pass while another process gets the CPU. It might even depend on another process to change the system state.

Besides that, "Schedule another process, if you can" is part of the semantics of yield. The code after yield should therefore be expected to NOT be the very next code to run.

If you can't do that, it's fine, the process will abuse some more innocent electrons for busy waiting, but if you can support this yield() semantics, the system will perform much better. Won't it?

The system as a whole will perform better if the busy-wait is avoided - yes. A multithreaded app actually using yield this way may suffer badly though.

Consider a case where two threads use yield() for synchronization, and there is some contention. Ideally, the two threads want to ping-pong the cpu between them. In the precence of other processes, they want to use up their fair amount of cpu in this manner. If they busy-wait, then both the app and the system overall gets slower.

Now, consider what happens if yield() really do lower the priority, or does something like "sleep(1) unless there really is nothing else to run". Busy-waiting will disappear, and the overall performance will be fine. The app running alone will also seem fine.

The app running with some contention - massive use of yield - on a machine with some other load will almost stop. Every yield will wait for all the other processes on the system, not only the process/thread we needed to wait for.

Ideally, yield() from a non-realtime process should cause a segfault, that would crash all software abusing it, forcing a change. Too drastic though. :-/

Quoted 9 lines
Quoted 5 lines

The users not understanding the limited scope where sched_yield() behaves deterministicly, seem to think that _yield() will yield() AND lower the thread's dynamic priority for SCHED_OTHER. Is downgrading the dynamic priority a behavioral option?

I expect it to be. It may cause lower-nice-level processes to run, but the (lack of) definition allows it.

And this is the problem - suddenly those niced cpu hogs trumps an interactive process that seems hopelessly stuck.

Helge Hafting