Skip to content
Advertisement

What does “high involuntary context” switches mean?

I have re-written a part of code in C. When testing it with logging the resource usage using getrusage(2) C API.

Before changing the code:

user time (ms): 21503
system time (ms): 372
involuntary context switches: 20

After changing:

user time (ms): 25589
system time (ms): 80732
involuntary context switches: 821

I see a lot of involuntary context switches being done in the code I have re-written.

My question is not about how to reduce context switches. But..

  1. What happens when “involuntary context switches” are more?
  2. In what way it will affect the system?

P.S: There is no activity on the disk as nothing is being written. It just pings the server several times.

Update:

Added system and user time taken.

Program is multi-threaded. Same number of threads (3k thread) are spawned in both the cases. Only the underlying api in C is being rewritten.

Advertisement

Answer

A voluntary context switch can occur whenever a thread/process makes a system call that blocks.

An involuntary context switch occurs when a thread has been running too long (usually something like 10 ms) without making a system call that blocks and there are processes waiting for the CPU.

It looks like your program is more CPU-intensive now than before. If you have made it multi-threaded then an increase is probably expected.

821 context switches – depending on the execution time of your program this may or may not be a lot.

If you want to reduce the number of context switches you can reduce the number of worker threads so there are less threads than there are CPU cores.

Update

Assuming that the load is identical in both cases, it looks like the code modifications have increased the cpu usage. If the increased load is a concern you should analyse the code to find the bottleneck. Instrumentation can be helpful in isolating which part of the code is causing the problem.

Advertisement