Linux 下如何关闭 CPU cache?

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

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

谢谢!

10308 次点击
所在节点    Linux
108 条回复
byaiu
2020-07-19 15:34:57 +08:00
上 8086,80286 这种 CPU,可完美解决
mightofcode
2020-07-19 16:52:20 +08:00
为什么会这这种需求
redsonic
2020-07-19 21:32:51 +08:00
intel:
;Step 1 - Enter no-fill mode
mov eax, cr0
or eax, 1<<30 ; Set bit CD
and eax, ~(1<<29) ; Clear bit NW
mov cr0, eax

;Step 2 - Invalidate all the caches
wbinvd

;All memory accesses happen from/to memory now, but UC memory ordering may not be enforced still.

;For Atom processors, we are done, UC semantic is automatically enforced.

xor eax, eax
xor edx, edx
mov ecx, IA32_MTRR_DEF_TYPE ;MSR number is 2FFH
wrmsr

;P4 only, remove this code from the L1I
wbinvd

AMD:
;Step 1 - Disable the caches
mov eax, cr0
or eax, 1<<30
mov cr0, eax

;For some models we need to invalidated the L1I
wbinvd

;Step 2 - Disable speculative accesses
xor eax, eax
xor edx, edx
mov ecx, MTRRdefType ;MSR number is 2FFH
wrmsr


善用搜索 how-can-the-l1-l2-l3-cpu-caches-be-turned-off-on-modern-x86-amd64-chips
vcfghtyjc
2020-07-19 22:45:38 +08:00
@Gwzlchn 这个是执行单条指令中的抖动,能否分享一下同样指令执行时间的标准差大概是多少?

另外,是否用 CPU 周期数比单纯的测执行时间更有意义呢?
tempdban
2020-07-19 23:23:56 +08:00
@redsonic amd 这么 yes 直接搞在了 cr0 里。
tempdban
2020-07-19 23:25:13 +08:00
@redsonic 哦 i 厂也在 cr0 里,代码片段收藏了,谢谢兄弟
secondwtq
2020-07-20 11:36:23 +08:00
@Gwzlchn 德国有人测过 x86: https://uops.info
vicsun2020
2020-07-20 22:22:35 +08:00
@Nich0la5 估计是为了写论文。。

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

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

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

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

© 2021 V2EX