开发者

Measure size and way-order of L1 and L2 caches

How can I programmatically m开发者_C百科easure (not query the OS) the size and order of associativity of L1 and L2 caches (data caches)?

Assumptions about system:

  • It has L1 and L2 cache (may be L3 too, may be cache sharing),
  • It may have a hardware prefetch unit (just like P4+),
  • It has a stable clocksource (tickcounter or good HPET for gettimeofday).

There are no assumptions about OS (it can be Linux, Windows, or something else), and we can't use POSIX queries.

Language is C, and compiler optimizations may be disabled.


I think all you need to do is repeatedly access memory in ever-increasing chunks (to determine cache size), and I think you can vary the strides to determine associativity.

So you would start out trying to access very short segments of memory and keep doubling the size until access slows down. Every time access slows down you've determined the size of another level of cache.


Here is the code from ATLAS. It is for L1 cache size

ATLAS/tune/sysinfo/L1CacheSize.c

(https://github.com/vtjnash/atlas-3.10.0/blob/master/tune/sysinfo/L1CacheSize.c)

int GetL1Size(int MaxSize, double tol)
{
int L1Size, tmp, correct=1;
fprintf(stderr, "\n Calculating L1 cache size:\n");

but it is only l1 cache and only size of it, not the way-count.


You might find the STREAM benchmark useful or interesting or both.


Question is outdated a little, but the answer is here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜