简体   繁体   English

与用户空间上下文切换相比,内核上下文切换有多贵?

[英]How expensive are kernel context switches compared to userspace context switches?

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. 根据C10k本文 ,随着越来越多的客户端连接并创建越来越多的线程,每个连接1个线程的服务器的吞吐量会降低。 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. 我最感兴趣的是Linux内核如何处理上下文切换,但也欢迎有关其他操作系统的信息。

  • 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. 因为CPU不需要切换到内核模式并返回用户模式。

  • 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. IIRC,Linux中的内核模式和用户模式的页表相同,所以至少没有TLB失效惩罚。

  • 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. AFAIK,在Linux中它非常高效,即使有大量的线程数,但更多的线程意味着更多的内存使用意味着更多的缓存压力,因此可能会降低性能。 I also expect some overhead involved in the handling of thousands of sockets. 我还期望在处理数千个套接字时涉及一些开销。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM