Linux 下如何关闭 CPU cache?

2020-07-18 11:25:02 +08:00
 vcfghtyjc

为了避免由于数据在不同层的 cache 所带来的运行时间的差异,我想要关闭 CPU cache,让所有数据都存在 DRAM 里。稍微搜索了一下,好像没什么清晰的解决方案,想问问有没有大神知道如何做。

谢谢!

10230 次点击
所在节点    Linux
108 条回复
sunnybird
2020-07-18 13:18:18 +08:00
大佬在做啥子呢
Dox
2020-07-18 13:21:00 +08:00
你这问题和“怎么让电脑拔了内存还能运行”差不多
ghostheaven
2020-07-18 13:34:48 +08:00
realpg
2020-07-18 13:35:51 +08:00
@reus #20
然而 这里面说的都不是真正意义上的关 CACHE
按照这个操作,cache 就成了楼主理解的样子,变成了一种很快的 ram……
reus
2020-07-18 13:44:16 +08:00
@realpg 我不知道你在说什么。反正 intel 手册里是这样写的:

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 ”。
imbushuo
2020-07-18 14:41:05 +08:00
@reus 从程序员的角度,cache 被关掉了;从架构的角度,cache 还是在那里,data path 没有改变(所以看怎么理解了
realpg
2020-07-18 14:48:18 +08:00
@reus #25
AMD 我是不清楚。
你对 Intel CPU 的机制一无所知,就单纯看了那个标题,就觉得你懂了而已。

既然你啥也不懂,所以我就告诉你结论好了。

经过 intel 手册的这个操作,L3 Cache 会变成什么样我不清楚,因为我折腾这玩意的时代,只有服务器 CPU 有 L3,普遍 CPU 只有 L2,而且 L1 L2 这几年的机制和特性都没变化。在这些操作以后,L1 L2 会变成跟 RAM 一样的用途,然后速度比 RAM 快很多。

如果不经过这个操作,L1 L2 并不是一种超快的 RAM,是要参与 CPU 逻辑的。
nevin47
2020-07-18 14:54:20 +08:00
@realpg 正解,data path 是不会变的
realpg
2020-07-18 14:58:46 +08:00
@nevin47 #28
现代的年轻人很少经历 CPU 级 BUG,所以对这些都一无所知。
不过估计未来国产 X86 低端 CPU 开卖+政采多了以后,估计就要回到我们瞎折腾那个年代,会遭遇 CPU BUG……
然后就有机会了解底层 CPU 流程,各种汇编底层开关了
inframe
2020-07-18 16:04:23 +08:00
最快的 SRAM 放着不用,要走总线直接读写 DRAM ?
reus
2020-07-18 16:17:52 +08:00
@imbushuo 所以这样做,可以消除 cache locality 吗?楼主就是想达到这个目的。
reus
2020-07-18 16:19:26 +08:00
@realpg 狗屁不通。
xyjincan
2020-07-18 16:42:28 +08:00
System.sleep(1000)
dalabenba
2020-07-18 17:02:28 +08:00
@vcfghtyjc iomem 里面是内存布局,system ram 就是,cache 是要修改页表属性的,在 bios 阶段会设置 mtrr,起来后修改对于应页表的 pat 来区分 cache 行为的,我也没试过在运行状态下修改 mtrr
你可以说下如果关了 cache 你预期会出现什么结果,你能得到什么结论,说不定有别的办法
cheng6563
2020-07-18 17:04:39 +08:00
现代的 CPU 桌面 CPU 保证不了运行时间一致吧
DoctorCat
2020-07-18 17:29:51 +08:00
让 v2 少一些争吵,英特尔社区给你参考: https://software.intel.com/en-us/comment/1746645
ryncsn
2020-07-18 17:33:39 +08:00
@realpg RAM 一样的用途?具体是什么样的?很好奇,可以通过地址访问?🤔
wangritian
2020-07-18 19:31:51 +08:00
避免程序运行过快?
barrysn
2020-07-18 19:37:22 +08:00
建议去翻一遍计算机组成原理
lonewolfakela
2020-07-18 20:34:54 +08:00
要搞这种级别的 profiling 的话,一般都是弄个 gem5 之类的 cpu 仿真器吧……

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/691081

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX