Linux 下如何关闭 CPU cache?
为了避免由于数据在不同层的 cache 所带来的运行时间的差异,我想要关闭 CPU cache,让所有数据都存在 DRAM 里。稍微搜索了一下,好像没什么清晰的解决方案,想问问有没有大神知道如何做。
谢谢!
为了避免由于数据在不同层的 cache 所带来的运行时间的差异,我想要关闭 CPU cache,让所有数据都存在 DRAM 里。稍微搜索了一下,好像没什么清晰的解决方案,想问问有没有大神知道如何做。
谢谢!
https://unix.stackexchange.com/questions/316684/how-to-disable-processors-l1-and-l2-caches
11.5.3 Preventing Caching
To disable the L1, L2, and L3 caches after they have been enabled and have received cache fills, perform the following steps:
1. Enter the no-fill cache mode. (Set the CD flag in control register CR0 to 1 and the NW flag to 0.
2. Flush all caches using the WBINVD instruction.
3. Disable the MTRRs and set the default memory type to uncached or set all MTRRs for the uncached memory type (see the discussion of the discussion of the TYPE field and the E flag in Section 11.11.2.1, “IA32_MTRR_DEF_TYPE MSR”).
The caches must be flushed (step 2) after the CD flag is set to insure system memory coherency. If the caches are not flushed, cache hits on reads will still occur and data will be read from valid cache lines.The intent of the three separate steps listed above address three distinct requirements: (i) discontinue new data replacing existing data in the cache (ii) ensure data already in the cache are evicted to memory, (iii) ensure subse-quent memory references observe UC memory type semantics. Different processor implementation of caching control hardware may allow some variation of software implementation of these three requirements. See note below.
或者你可以只看第一句:“To disable the L1, L2, and L3 caches ”。
既然你啥也不懂,所以我就告诉你结论好了。
经过 intel 手册的这个操作,L3 Cache 会变成什么样我不清楚,因为我折腾这玩意的时代,只有服务器 CPU 有 L3,普遍 CPU 只有 L2,而且 L1 L2 这几年的机制和特性都没变化。在这些操作以后,L1 L2 会变成跟 RAM 一样的用途,然后速度比 RAM 快很多。
如果不经过这个操作,L1 L2 并不是一种超快的 RAM,是要参与 CPU 逻辑的。
这是他内管理的 对于 data 的逻辑缓存 nfc 打开或者在指令传递时候打上 no cache 标志 就不会在 cache 里面留下 hot cache 但是不代表不继续使用数据和指令的 L1 以及大 L2 cache
这是最直接的办法了, 没有之一
这玩意是没法关闭的。就好像说,我不在乎慢,我不要 RAM 了,慢点我能忍,直接操作 SSD,是没有这种接口的。
实际上,CPU 的大部分通用功能的数据进口和出口就是这个 cache 。这里面具体的细节,就得跟 intel 工程师去聊了。
上那个嘴硬的,他引用那个帖子,就好像,你去手机店买手机,张嘴我要 256G 内存的,店员是不会非得 SB 似的纠正你那是 256G 硬盘,不是 256G 内存,他就给你一个 8+256 的手机,他知道你要干啥,或者说,最多能干到啥地步而已。
这里面的机制基本我是粗了解,我现在也是搞运维和架构设计的,我不需要了解到 CPU 具体咋工作,但是概念一定要有,仅此而已。
拿着十年前的半桶水知识就来指点江山,呵呵
是,全世界就你知道 cpu 有 bug,这两年的什么 meltdown 、什么 specture,现代的年轻人都一无所知的!
恶臭。
我搜了一下,看到了一篇在 stack overflow 的帖子
https://stackoverflow.com/questions/48527189/is-there-a-way-to-flush-the-entire-cpu-cache-related-to-a-program
您指的是这个吗?还是有更好的资料呢?
如果是几 ms 级别的 latency 会有明显区别, 不然没什么意义
@vcfghtyjc 你先考虑调度的抖动吧。还有各种设备的中断。超算所以用的都是魔改版 Linux 。所有系统功能能关的都关掉。基本上就是只留个内存管理
@reus #71 这不叫 false sharing,因为确实是 by design 在 share 。解决办法很简单:不要释放上一次使用的内存就好。又或者每次使用前保证初始化内存。保证内存在缓存里比保证内存不在缓存里简单。
具体可以看 IBM 在超算上 os jitter 相关的论文。
HPC 上的 jitter 和普通服务器 server 应该还是有点不同的吧。因为运行在 HPC 上的程序大多依赖 MPI 做并行计算,网络应该是主要的 jitter 来源,不过也许可以看看对于单个 server 的 os jitter 吗? 有什么推荐的关键词吗?
如果我想尽量避免 OS 带来的影响,目前最轻的 Linux 发行版是什么?
https://stackoverflow.com/a/42435902
先说一下,神威平台是两级 cache,L1 分 data cache 和 ins cache 、L2 不区分。指令集就有点像 MIPS (实际是 Alpha 指令集)
访存指令比较简单了,我当时也考虑了 CPU cache 的问题,那你两条 load/store 指令之间访问数据的距离要大于一整个 L2 cache 的大小就可以了。
最坑爹的是 add/div 之类的计算指令,这个我现在也没想出来应该怎么准确测量出来,因为现在的高级处理器都是多发射+多级流水线的。两个指令之间实际上就差一个最后的写回阶段的间隔。
这个消除流水线+多发射的影响主有啥好一点的思路吗?