多核上下文切换期间是否清除了处理器缓存? [英] Is processor cache flushed during context switch in multicore?

查看:77
本文介绍了多核上下文切换期间是否清除了处理器缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我讨论了为什么seq 解决方案

只要有任何类型的上下文切换,操作系统都会保存其状态,以便可以在任何内核上再次重新启动(假设它尚未与任何内核绑定).使用处理器相似性功能的特定核心).

保存由于某种原因或其他原因而未完成的状态将破坏线程或处理的全部目的,因此将高速缓存作为开关的一部分进行刷新.

Volatile与上下文切换无关.它只是告诉编译器,内存位置可能随时更改,恕不另行通知,因此,每次源代码要求它时,都必须对其进行访问,即优化内存位置访问的常规编译器行为不适用于声明为volatile的位置.

Recently, I discussed why there is a volatile mark at seq in Java Actors demo

@volatile private var seq = 0L
private def nextSeq: Long = {
  val next = seq
  seq += 1
  next
}

One answer was that threads can be migrated and variables lost (other cores will have incoherent values in their private caches). But, you not normally mark every variable with volatile to enable multicore execution. So, cores must flush the caches whenever context is switched. But, I cannot find this statement pronounced explicitly anywhere. Everybody, eg. Wikipedia, only care about registers and stack memory

The state of the process includes all the registers that the process may be using, especially the program counter, plus any other operating system specific data that may be necessary. This data is usually stored in a data structure called a process control block (PCB), or switchframe. In order to switch processes, the PCB for the first process must be created and saved. The PCBs are sometimes stored upon a per-process stack in kernel memory (as opposed to the user-mode call stack), or there may be some specific operating system defined data structure for this information.

What do we have in reality regarding migrating the general purpose data/variables?

解决方案

Whenever there is any sort of context switch the OS will save its state such that it can be restarted again on any core (assuming it has not been tied to a specific core using a processor affinity function).

Saving a state that for some reason or other is incomplete defeats the entire purpose of threading or processing, therefore the caches are flushed as part of the switch.

Volatile has nothing whatsover to do with context switching. It merely tells the compiler that the memory location is subject to change without notice and therefore must be accessed each time the source code dictates it i e the usual compiler behaviour of optimizing accesses to memory locations does not apply to the volatile-declared location.

这篇关于多核上下文切换期间是否清除了处理器缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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