edA-qa mort-ora-y
edA-qa mort-ora-y

Reputation: 31861

sched_yield slow down other threads

We have code that makes use of sched_yield inside a loop. When we do this we seem to get a slower performance of other threads, in particular those involving kernel calls (like IO and mutex/event handling). I'm trying to determine the exact cause of this behaviour.

Can excessive calls to sched_yield lead to a bottleneck in the kernel?

My suspicion is if we keep asking the kernel to check its process list then other threads will suffer as key data structures may be continually locked -- whereas if we didn't call sched_yield those kernel locks would tend to be uncontested. Does this make sense, or should it be totally okay to repeatedly call sched_yield.

Upvotes: 1

Views: 2605

Answers (1)

Mat
Mat

Reputation: 206689

Have a look at the sched_yield man page for Linux:

Avoid calling sched_yield() unnecessarily or inappropriately (e.g., when resources needed by other schedulable threads are still held by the caller), since doing so will result in unnecessary context switches, which will degrade system performance.

Calling it in a tight loop will cause problems. Reduce the rate at which you're calling it.

(And check that you need to call it in the first place. The scheduler often does the Right Thing all by itself.)

Other options you could find interesting to investigate if you have a low priority thread:

  • sched_setscheduler - with SCHED_IDLE or SCHED_BATCH maybe (affects the whole process)
  • thread_setschedparam - per thread, but might have restrictions on what policies you can use (can't find it right now).

Or the good old nice command of course.

Upvotes: 6

Related Questions