I have a server with an average of 3000 context switching with the cpu
idle 98% of the time. How much context switching it too much? Is 3000
too much, if yes, how can I diagnose what creates those context
switchings?
A context switch happens when the CPU switches to another thread. This happens all the time and is completely normal. It allows multiple threads and programs to run at the same time.
Context switches occur when either the active thread is idle, or after a few milliseconds. If your CPU usage is 100%, you will have, depending on your kernel HZ setting, at least 100, 250 or 1000 context switches per second. If your CPU is mostly idle, it depends on what kind of programs you're running.
A media player is likely to generate a lot of context switches, because it decodes the media step by step, while waiting for the previous data to be played. On the other hand, compressing things doesn't need to wait for anything, so it can work non-stop, therefore generating not much context switches.
Anyway, if you're having a performance problem, context switches are usually not important.
You'll get HZ * num_of_cpus context switches just from timer interrupts. If your server has 3 CPUs, that's 3000 context switches right there. If you have 2 CPUs and a daemon or task that wakes on an interrupt, that also would add up to 3000 context switches.
What's the server specs and what are you running on there?
While it is quite hard to determine exactly what produces context switching, I would say that 3000/sec is completely normal. In fact you have proof that's true, as your processor is only 2% utilized (and even that is cumulative, only part of that is actually spent switching). Linux kernel scheduler is very efficient compared to other operating system scheduleres.
With a crude test programs, I can measure that this old PIII @ 1GHz easily handles few hundred thousand context switches, so 3000/sec is really nothing to be concerned, especially if you have a more modern CPU.
Context switching explained
A context switch happens when the CPU switches to another thread. This happens all the time and is completely normal. It allows multiple threads and programs to run at the same time.
Context switches occur when either the active thread is idle, or after a few milliseconds. If your CPU usage is 100%, you will have, depending on your kernel HZ setting, at least 100, 250 or 1000 context switches per second. If your CPU is mostly idle, it depends on what kind of programs you're running.
A media player is likely to generate a lot of context switches, because it decodes the media step by step, while waiting for the previous data to be played. On the other hand, compressing things doesn't need to wait for anything, so it can work non-stop, therefore generating not much context switches.
Anyway, if you're having a performance problem, context switches are usually not important.
How many CPUs do you have?
You'll get HZ * num_of_cpus context switches just from timer interrupts. If your server has 3 CPUs, that's 3000 context switches right there. If you have 2 CPUs and a daemon or task that wakes on an interrupt, that also would add up to 3000 context switches.
What's the server specs and what are you running on there?
3000 ctx switches not much
While it is quite hard to determine exactly what produces context switching, I would say that 3000/sec is completely normal. In fact you have proof that's true, as your processor is only 2% utilized (and even that is cumulative, only part of that is actually spent switching). Linux kernel scheduler is very efficient compared to other operating system scheduleres.
With a crude test programs, I can measure that this old PIII @ 1GHz easily handles few hundred thousand context switches, so 3000/sec is really nothing to be concerned, especially if you have a more modern CPU.