osgx
osgx

Reputation: 94175

Measure size and way-order of L1 and L2 caches

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:

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.

Upvotes: 14

Views: 6814

Answers (4)

osgx
osgx

Reputation: 94175

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.

Upvotes: 2

actual
actual

Reputation: 2370

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

Upvotes: 1

High Performance Mark
High Performance Mark

Reputation: 78316

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

Upvotes: 1

Gabe
Gabe

Reputation: 86698

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.

Upvotes: 6

Related Questions