Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
vcfghtyjc
V2EX  ›  Linux

Linux 下如何关闭 CPU cache?

  •  
  •   vcfghtyjc · Jul 18, 2020 · 12776 views
    This topic created in 2154 days ago, the information mentioned may be changed or developed.

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

    谢谢!

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

    另外,是否用 CPU 周期数比单纯的测执行时间更有意义呢?
    tempdban
        105
    tempdban  
       Jul 19, 2020 via Android
    @redsonic amd 这么 yes 直接搞在了 cr0 里。
    tempdban
        106
    tempdban  
       Jul 19, 2020 via Android
    @redsonic 哦 i 厂也在 cr0 里,代码片段收藏了,谢谢兄弟
    secondwtq
        107
    secondwtq  
       Jul 20, 2020
    @Gwzlchn 德国有人测过 x86: https://uops.info
    vicsun2020
        108
    vicsun2020  
       Jul 20, 2020 via iPhone
    @Nich0la5 估计是为了写论文。。
    1  2  
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3297 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 57ms · UTC 10:57 · PVG 18:57 · LAX 03:57 · JFK 06:57
    ♥ Do have faith in what you're doing.