views:

314

answers:

5

How can I programmatically measure (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.

+3  A: 

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.

Gabe
but how can I turn of hardware prefetch, which will mask out some sizes?
osgx
+1  A: 

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

High Performance Mark
Mark, please, take a look on http://stackoverflow.com/questions/2517694/can-the-stream-and-gups-single-cpu-benchmark-use-non-local-memory-in-numa-machi
osgx
+2  A: 

There is a program on Figure 5.32 on page 352 in Computer Architecture: A Quantitative Approach, 4th Edition. This was used at my university for a lab together with question 5.18 in the same book.

Jonas
Love that book :D
Tom
+1  A: 

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

actual
Only good illustrations, but not the solution. Also, I don't get, how to get way-count from his graphs.
osgx