多核CPU,多线程和上下文切换? [英] MultiCore CPUs, Multithreading and context switching?

查看:884
本文介绍了多核CPU,多线程和上下文切换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说,我们有一个具有20个内核的CPU和一个具有20个CPU密集型的进程,彼此独立:每个CPU内核一个线程. 我试图弄清楚在这种情况下是否发生上下文切换.我相信这是因为操作系统中也有一些系统进程也需要CPU时间.

Let's say we have a CPU with 20 cores and a process with 20 CPU-intensive independent of each other threads: One thread per CPU core. I'm trying to figure out whether context switching happens in this case. I believe it happens because there are system processes in the operating system that need CPU-time too.

我了解CPU架构不同,某些答案可能会有所不同,但请您解释一下:

I understand that there are different CPU architectures and some answers may vary but can you please explain:

  • 上下文切换是如何发生的,例如在Linux或Windows以及某些已知的CPU架构上?在现代硬件的幕后到底会发生什么?
  • 如果我们有10个内核和20个线程,或者相反,该怎么办?
  • 如果我们有n个CPU,如何计算需要多少个线程?
  • 上下文切换后,CPU缓存(L1/L2)是否变空了?

谢谢

推荐答案

上下文切换是如何发生的,例如在Linux或Windows上,以及一些已知的 CPU架构?在现代硬件的幕后到底会发生什么?

How context switching happens e.g. on Linux or Windows and some known CPU architectures? And what happens under the hood on modern hardware?

上下文切换在中断发生时发生,并且该中断与内核线程和进程状态数据一起指定一组正在运行的线程,该线程与在中断之前运行的线程集不同.请注意,就操作系统而言,中断可能是导致驱动程序运行且驱动程序请求调度运行的实际"硬件中断,也可能是已运行线程的syscall.无论哪种情况,OS调度状态机都会决定是否更改在可用内核上运行的线程集.

A context-switch happens when an interrupt occurs and that interrupt, together with the kernel thread and process state data, specify a set of running threads that is different than the set running before the interrupt. Note that, in OS terms, an interrupt may be either a 'real' hardware interrupt that causes a driver to run and that driver requests a scheduling run, or a syscall from a thread that is already running. In either case, the OS scheduling state-machine decides whether to change the set of threads running on the available cores.

内核可以通过停止一个或多个线程并运行其他线程来更改正在运行的线程集.通过排队一个提示请求并生成该内核的硬件中断,以迫使该内核运行其处理器间驱动程序来处理该请求,它可以停止在任何内核上运行的任何线程.

The kernel can change the set of running threads by stopping thread/s and running others. It can stop any thread running on any core by queueing up a premption request and generating a hardware interrupt of that core to force the core to run its interprocessor driver to handle the request.

如果我们有10个内核和20个线程怎么办?

What if we have 10 cores and 20 threads?

取决于线程在做什么.如果它们处于就绪/运行状态之外的任何其他状态(例如,在I/O上阻塞或线程间通信),则它们之间将不会进行上下文切换,因为没有任何内容在运行.如果它们都准备就绪/正在运行,则其中的10个将在10个内核上永远运行,直到发生中断.大多数系统都有定期的计时器中断,这可能会导致共享线程周围的可用内核.

Depends on what the threads are doing. If they are in any other state than ready/running, (eg blocked on I/O or inter-thread comms), there will be no context-switching between them because nothing is running. If they are all ready/running, 10 of them will run forever on the 10 cores until there is an interrupt. Most systems have a periodic timer interrupt that can have the effect of sharing the available cores around the threads.

反之亦然

10个线程在10个内核上运行.其他10个内核被暂停.操作系统可以在核心周围移动线程,例如.以防止整个芯片的散热不均匀.

10 threads run on 10 cores. The other 10 cores are halted. The OS may move the threads around the cores, eg. to prevent uneven heat dissipation across the die.

如果有n个CPU,如何计算需要多少个线程?

How to calculate how many threads we need if we have n CPUs?

取决于应用程序.如果所有内核在与线程数量一样多的就绪线程上总是被用完100%,那将是很好的选择,但是,由于大多数线程被阻塞的时间比它们正在运行的时间长得多,因此这很困难,除非在某些最终情况下(例如-您的"20个内核上的20个CPU密集型线程"),以得出任何最佳数量.

App-dependent. It would be nice if all cores were always used up 100% on exactly as many ready threads as cores but, since most threads are blocked for much more time than they are running, it's difficult, except in some end-cases, (eg - your '20 CPU-intensive threads on 20 cores'), to come up with any optimal number.

上下文切换后,CPU缓存(L1/L2)是否变空了?

Does CPU cache(L1/L2) gets empty after context switching?

也许-这完全取决于线程的数据使用情况.缓存将照常按需重新加载.没有上下文切换总缓存重新加载",但是,如果线程在运行时访问不同的大型数据数组,那么(至少为L1)个缓存确实会在线程运行期间完全重新加载.

Maybe - it depends entirely on the data usage of the threads. The caches will get reloaded on-demand, as usual. There is no 'context-switch total cache reload' but, if the threads access different, large arrays of data while running, then the (L1 at least), cache will indeed get fully reloaded during the thread run.

这篇关于多核CPU,多线程和上下文切换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆