c - 内存性能/缓存难题

标签 c performance cpu cpu-cache

我有一个内存性能难题。我正在尝试对从主内存中获取一个字节所需的时间进行基准测试,以及各种 BIOS 设置和内存硬件参数如何影响它。我为 Windows 编写了以下代码,在一个循环中,通过读取另一个缓冲区来刷新缓存,然后以不同的步幅一次读取目标缓冲区一个字节。我认为一旦步长是缓存行大小,这就是我试图测量的数量,因为每次读取都会进入主内存。这是基准代码(请注意,缓冲区的大小是步长 x 1MB,并且我将线程固定到核心 1):

#include <stdio.h>
#include <memory.h>

#define NREAD       (1024*1024)
#define CACHE_SIZE  (50*1024*1024)

char readTest(int stride) {
    LARGE_INTEGER frequency;
    LARGE_INTEGER start;
    LARGE_INTEGER end;
    int rep, i,ofs;
    double time, min_time=1e100, max_time=0.0, mean_time=0.0;
    char *buf = (char *)malloc(NREAD*stride);
    char *flusher = (char *)malloc(CACHE_SIZE); 
    char jnk=0;
    for(rep=0; rep<255; rep++) {
        // read the flusher to flush the cache
        for(ofs = 0; ofs<CACHE_SIZE; ofs+=64) jnk+=flusher[ofs];
        if (QueryPerformanceFrequency(&frequency) == FALSE) exit(-1);
        if (QueryPerformanceCounter(&start) == FALSE) exit(-2);

        // here's the timed loop
        for(ofs=0; ofs<NREAD*stride; ofs+=stride) jnk += buf[ofs];

        if (QueryPerformanceCounter(&end) == FALSE) exit(-3);
        time = (double)(end.QuadPart - start.QuadPart) / (double)frequency.QuadPart*1e6;
        max_time = time > max_time ? time : max_time;
        min_time = time < min_time ? time : min_time;
        mean_time += time;
    }
    mean_time /= 255;
    printf("Stride = %4i, Max: %6.0f us, Min: %6.0f us, Mean: %6.0f us, B/W: %4.0f MB/s\n", stride, max_time, min_time, mean_time, NREAD/min_time);
    free(buf);
    free(flusher);
    return jnk;
}

int main(int argc, char* argv[]) {
    SetThreadAffinityMask(GetCurrentThread(), 1);  // pin to core 1 to avoid weirdness
    // run the tests
    readTest(1);    readTest(2);    readTest(4);    readTest(6);    readTest(8);
    readTest(12);   readTest(16);   readTest(24);   readTest(32);   readTest(48);
    readTest(64);   readTest(96);   readTest(128);  readTest(192);  readTest(256);
    readTest(384);  readTest(512);  readTest(768);  readTest(1024); readTest(1536);
    return 0;
}

被定时的内循环汇编为:

        // here's the timed loop
        for(ofs=0; ofs<NREAD*stride; ofs+=stride) jnk += buf[ofs];
00F410AF  xor         eax,eax  
00F410B1  test        edi,edi  
00F410B3  jle         readTest+0C2h (0F410C2h)  
00F410B5  mov         edx,dword ptr [buf]  
00F410B8  add         bl,byte ptr [eax+edx]  
00F410BB  add         eax,dword ptr [stride]  
00F410BE  cmp         eax,edi  
00F410C0  jl          readTest+0B5h (0F410B5h)  

我在双处理器 E5-2609 机器上运行了这个,结果如下:

Stride =    1, Max:   2362 us, Min:    937 us, Mean:    950 us, B/W: 1119 MB/s
Stride =    2, Max:   1389 us, Min:    968 us, Mean:    978 us, B/W: 1083 MB/s
Stride =    4, Max:   1694 us, Min:   1026 us, Mean:   1037 us, B/W: 1022 MB/s
Stride =    6, Max:   2418 us, Min:   1098 us, Mean:   1124 us, B/W:  955 MB/s
Stride =    8, Max:   2835 us, Min:   1234 us, Mean:   1252 us, B/W:  850 MB/s
Stride =   12, Max:   4203 us, Min:   1527 us, Mean:   1559 us, B/W:  687 MB/s
Stride =   16, Max:   5130 us, Min:   1816 us, Mean:   1849 us, B/W:  577 MB/s
Stride =   24, Max:   7370 us, Min:   2408 us, Mean:   2449 us, B/W:  435 MB/s
Stride =   32, Max:  10039 us, Min:   2901 us, Mean:   3014 us, B/W:  361 MB/s
Stride =   48, Max:  14248 us, Min:   4652 us, Mean:   4731 us, B/W:  225 MB/s
Stride =   64, Max:  19149 us, Min:   6340 us, Mean:   6447 us, B/W:  165 MB/s
Stride =   96, Max:  28848 us, Min:   8475 us, Mean:   8615 us, B/W:  124 MB/s
Stride =  128, Max:  37449 us, Min:   9900 us, Mean:  10160 us, B/W:  106 MB/s
Stride =  192, Max:  51718 us, Min:  11282 us, Mean:  11563 us, B/W:   93 MB/s
Stride =  256, Max:  62193 us, Min:  11558 us, Mean:  11924 us, B/W:   91 MB/s
Stride =  384, Max:  86943 us, Min:  11829 us, Mean:  12260 us, B/W:   89 MB/s
Stride =  512, Max: 108661 us, Min:  11847 us, Mean:  12401 us, B/W:   89 MB/s
Stride =  768, Max: 167951 us, Min:  11797 us, Mean:  12946 us, B/W:   89 MB/s
Stride = 1024, Max: 211700 us, Min:  12893 us, Mean:  13979 us, B/W:   81 MB/s
Stride = 1536, Max: 332214 us, Min:  12967 us, Mean:  15077 us, B/W:   81 MB/s

这是我的问题:

  • 为什么在步幅大于缓存行大小(Sandy Bridge 为 64 字节)后性能继续下降?我假设一旦步幅大到每次读取都需要缓存行传输时,性能最差,但即使在那之后,时间也会增加两倍……我错过了什么?
  • 为什么最长时间(发生在循环的第一次迭代中)比最短时间长 2-4 倍?我每次迭代都会刷新缓存...

最佳答案

缓存行不是跟踪内存的唯一粒度。从虚拟地址到物理地址的转换发生在 页面 粒度。您的系统几乎肯定正在使用 4k 页面。

步长为 64 时,每页有 64 个条目,因此有 16384 页。 L2 TLB 只能跟踪这些页面中的 512 个,因此您在每个新页面(每第 64 次访问)上都会发生 L2 TLB 未命中。

步幅为 1024,每页有 4 个条目,因此有 262144 页。现在每 4 次访问就会出现 L2 TLB 未命中。

tl;dr:TLB 未命中会让您丧命。您可以使用性能计数器直接观察这一点,而不是让 Stack Overflow 为您阅读茶叶。您还可以让您的系统使用一个或多个“ super 页面”来分配缓冲区,以扩展您的 TLB 范围(尽管不同的系统对该功能的支持程度不同)。

关于c - 内存性能/缓存难题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19796176/

相关文章:

c - C 如何能够直接在 for 循环中比较和递增结构数组?

performance - 使用更少的循环进行 LU 分解

python - 使用 Python 进行实时文本处理

assembly - I/O地址空间如何映射到设备?

linux-kernel - 什么是 CPU 内核/特权模式,操作系统如何保护它?

c - 为什么 C 输出内存位置/指针而不是数组的值?

c - 使用 %C、%S、%ls、%lc 时 printf 返回 -1 的示例

java - C语言 : Difference between float and void in functions

c++ - 一般问题 : What to pass as pointer in C/C++?

c - 扩展页表有什么用?