- Why do these userpace contexts switch faster than kernel thread context switches?
Because the CPU does not need to switch to kernel mode and return to user mode.
- What exactly makes the kernel context switch much more expensive?
Basically, switching to kernel mode. IIRC, page tables are the same in kernel mode and user mode in Linux, so at least there is no penalty for canceling TLBs.
- How expensive is the kernel context switch? How long does it take?
It is required to measure and can vary from machine to machine. I assume that a typical desktop / server machine these days can execute several hundred thousand context switches per second, possibly several millions.
- Does kernel switching time depend on the number of threads?
Depends on how the kernel handler handles this. AFAIK, on ββLinux it is quite efficient even with a large number of threads, but more threads mean that using more memory means more cache pressure and therefore lower performance. I also expect some overhead to process thousands of sockets.
source share