We are analyzing a performance issue, we could potentially have at most 500 working threads, the CPU usage is not very high. Could context switch cause high CPU? In other word, since CPU usage is not high, context switch (500 threads) is not problem here.
Only if programming forces excessive context switches. Your operating system's scheduler will only perform a context switch if it believes that it is advantageous to performance to do so. The scheduler is very smart and will limit the number of context switches to avoid a negative impact on performance if it is possible for it to do so.
However, if poor programming prevents the scheduler from doing, so, then you can get excessive context switches.
For example, say I have one core and 500 threads. If each of those 500 threads tries to do lots of work, the scheduler will let each thread run for a reasonable amount of time such that the total overhead of context switches is not terribly high. Some of those threads may see very high latency, but each thread will run for long enough that context switches won't kill performance.
But say the scheduler schedules the first thread and then, just a few instructions later, that thread is no longer ready-to-run. Now the scheduler has no choice but to switch contexts. If that next thread again runs for only a very short period of time and then is no longer ready-to-run, you will force another context switch. This will kill performance.
Forced context switches are bad. Context switches the scheduler elects to perform are good.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With