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

查看:39
本文介绍了多核 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?

上下文切换发生在中断发生时,并且该中断连同内核线程和进程状态数据一起指定了一组与中断前运行的线程集不同的运行线程.请注意,在操作系统术语中,中断可能是导致驱动程序运行并且驱动程序请求调度运行的真实"硬件中断,也可能是来自已经运行的线程的系统调用.在任何一种情况下,操作系统调度状态机都会决定是否更改在可用内核上运行的线程集.

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天全站免登陆