How to find the size of the L1 cache line size with IO timing measurements?

后端 未结 8 663
深忆病人
深忆病人 2021-01-30 09:10

As a school assignment, I need to find a way to get the L1 data cache line size, without reading config files or using api calls. Supposed to use memory accesses read/write timi

8条回答
  •  隐瞒了意图╮
    2021-01-30 09:26

    Allocate a BIG char array (make sure it is too big to fit in L1 or L2 cache). Fill it with random data.

    Start walking over the array in steps of n bytes. Do something with the retrieved bytes, like summing them.

    Benchmark and calculate how many bytes/second you can process with different values of n, starting from 1 and counting up to 1000 or so. Make sure that your benchmark prints out the calculated sum, so the compiler can't possibly optimize the benchmarked code away.

    When n == your cache line size, each access will require reading a new line into the L1 cache. So the benchmark results should get slower quite sharply at that point.

    If the array is big enough, by the time you reach the end, the data at the beginning of the array will already be out of cache again, which is what you want. So after you increment n and start again, the results will not be affected by having needed data already in the cache.

提交回复
热议问题