10

According to C10k and this paper, throughput of 1-thread-per-connection servers degrade as more and more clients connect and more and more threads are created. According to those two sources, this is because the more threads exist, the more time is spent on context switching compared to actual work done by those threads. Evented servers don't seem to suffer as much from performance degredation at high connection counts.

However, evented servers also do context switches between clients, they just do it in userspace.

  • Why are these userspace context switches faster than kernel thread context switches?
  • What exactly does a kernel context switch do that's so much more expensive?
  • How expensive is a kernel context switch exactly? How much time does it take?
  • Does kernel context switching time depend on the number of threads?

I'm mostly interested in how the Linux kernel handles context switching but information about other OSes is welcome too.

1 Answer 1

5
  • Why are these userspace context switches faster than kernel thread context switches?

Because the CPU does not need to switch to kernel mode and back to user mode.

  • What exactly does a kernel context switch do that's so much more expensive?

Mostly the switch to kernel mode. IIRC, the page tables are the same in kernel mode and user mode in Linux, so at least there is no TLB invalidation penalty.

  • How expensive is a kernel context switch exactly? How much time does it take?

Needs to be measured and can vary from machine to machine. I guess that a typical desktop/server machine these days can do a few hundred thousands of context switches per second, probably a few million.

  • Does kernel context switching time depend on the number of threads?

Depends on how the kernel scheduler handles this. AFAIK, in Linux it is pretty efficient, even with large thread counts, but more threads means more memory usage means more cache pressure and thus likely lower performance. I also expect some overhead involved in the handling of thousands of sockets.

7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.