Search code examples
calgorithmcachingcpu-cache

Programmatically determine the associativity of an L1 cache


I searched for similar questions; one was similar but there wasn't a definitive answer.

I can write a C program to determine both the line length and size of a cache but I can't think of a way to determine its associativity. Is there some way to accomplish this?


Solution

  • The observable artefact of a k-way associative cache is that k+1 accesses to the same set will cause an eviction. Thus you will see a marked difference in accesses-per-second between hammering on k cache lines in a tight loop and hammering on k+1 cache lines in a tight loop. There will also be a performance difference between hammering on k+1 cache lines in a set in a tight loop and hammering on k+1 cache lines, not all in a set, in a tight loop.

    You linked me to Varadharajan Chandran's Master's thesis, which does something like this (and much more) in order to infer lots of characteristics of both the processor cache and the TLB.