V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
vcfghtyjc
V2EX  ›  Linux

Linux 下如何关闭 CPU cache?

  •  
  •   vcfghtyjc · 2020-07-18 11:25:02 +08:00 · 10174 次点击
    这是一个创建于 1380 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    谢谢!

    108 条回复    2020-07-20 22:22:35 +08:00
    1  2  
    byaiu
        101
    byaiu  
       2020-07-19 15:34:57 +08:00 via Android
    上 8086,80286 这种 CPU,可完美解决
    mightofcode
        102
    mightofcode  
       2020-07-19 16:52:20 +08:00
    为什么会这这种需求
    redsonic
        103
    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
        104
    vcfghtyjc  
    OP
       2020-07-19 22:45:38 +08:00
    @Gwzlchn 这个是执行单条指令中的抖动,能否分享一下同样指令执行时间的标准差大概是多少?

    另外,是否用 CPU 周期数比单纯的测执行时间更有意义呢?
    tempdban
        105
    tempdban  
       2020-07-19 23:23:56 +08:00 via Android
    @redsonic amd 这么 yes 直接搞在了 cr0 里。
    tempdban
        106
    tempdban  
       2020-07-19 23:25:13 +08:00 via Android
    @redsonic 哦 i 厂也在 cr0 里,代码片段收藏了,谢谢兄弟
    secondwtq
        107
    secondwtq  
       2020-07-20 11:36:23 +08:00
    @Gwzlchn 德国有人测过 x86: https://uops.info
    vicsun2020
        108
    vicsun2020  
       2020-07-20 22:22:35 +08:00 via iPhone
    @Nich0la5 估计是为了写论文。。
    1  2  
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1338 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 17:26 · PVG 01:26 · LAX 10:26 · JFK 13:26
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.