有没有一种方法可以检查处理器缓存是否最近已刷新? [英] Is there a way to check whether the processor cache has been flushed recently?

查看:80
本文介绍了有没有一种方法可以检查处理器缓存是否最近已刷新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在i386 linux上.如果可能,最好在c/(c/posix std libs)/proc中.如果没有,那么任何汇编程序或第三方库都可以做到这一点?

On i386 linux. Preferably in c/(c/posix std libs)/proc if possible. If not is there any piece of assembly or third party library that can do this?

我正在尝试测试内核模块是否清除缓存行或整个处理器(使用wbinvd()).程序以root身份运行,但我希望尽可能保留在用户空间中.

I'm trying to develop test whether a kernel module clear a cache line or the whole proccesor(with wbinvd()). Program runs as root but I'd prefer to stay in user space if possible.

推荐答案

缓存一致性系统会竭尽全力向您隐藏此类内容.我认为您将不得不通过使用性能计数寄存器来检测高速缓存未命中,或者通过使用高分辨率计时器仔细测量读取存储器位置的时间来间接观察它.

Cache coherent systems do their utmost to hide such things from you. I think you will have to observe it indirectly, either by using performance counting registers to detect cache misses or by carefully measuring the time to read a memory location with a high resolution timer.

该程序在我的x86_64机器上运行,以演示clflush的效果.它乘以使用rdtsc读取全局变量所花费的时间.作为直接与CPU时钟相关的一条指令,直接使用rdtsc对此非常理想.

This program works on my x86_64 box to demonstrate the effects of clflush. It times how long it takes to read a global variable using rdtsc. Being a single instruction tied directly to the CPU clock makes direct use of rdtsc ideal for this.


took 81 ticks
took 81 ticks
flush: took 387 ticks
took 72 ticks

您会看到3次试用:第一个确保i在高速缓存中(之所以这样,因为它只是作为BSS的一部分而被清零),第二个是读取应该在高速缓存中的i.然后clflushi踢出缓存(及其邻居),并显示重新读取它花费的时间明显更长.最终读取将验证它是否已返回高速缓存中.结果是非常可重复的,并且差异足够大,很容易看到高速缓存未命中.如果您要校准rdtsc()的开销,则可以使差异更加明显.

You see 3 trials: The first ensures i is in the cache (which it is, because it was just zeroed as part of BSS), the second is a read of i that should be in the cache. Then clflush kicks i out of the cache (along with its neighbors) and shows that re-reading it takes significantly longer. A final read verifies it is back in the cache. The results are very reproducible and the difference is substantial enough to easily see the cache misses. If you cared to calibrate the overhead of rdtsc() you could make the difference even more pronounced.

如果您无法读取要测试的内存地址(尽管即使/dev/memmmap都可以用于这些目的),如果您知道缓存行的大小和关联性,则可以推断出您想要的内容缓存.然后,您可以使用可访问的内存位置来探查您感兴趣的集合中的活动.

If you can't read the memory address you want to test (although even mmap of /dev/mem should work for these purposes) you may be able to infer what you want if you know the cacheline size and associativity of the cache. Then you can use accessible memory locations to probe the activity in the set you're interested in.

#include <stdio.h>
#include <stdint.h>

inline void
clflush(volatile void *p)
{
    asm volatile ("clflush (%0)" :: "r"(p));
}

inline uint64_t
rdtsc()
{
    unsigned long a, d;
    asm volatile ("rdtsc" : "=a" (a), "=d" (d));
    return a | ((uint64_t)d << 32);
}

volatile int i;

inline void
test()
{
    uint64_t start, end;
    volatile int j;

    start = rdtsc();
    j = i;
    end = rdtsc();
    printf("took %lu ticks\n", end - start);
}

int
main(int ac, char **av)
{
    test();
    test();
    printf("flush: ");
    clflush(&i);
    test();
    test();
    return 0;
}

这篇关于有没有一种方法可以检查处理器缓存是否最近已刷新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆