Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Measure size and way-order of L1 and L2 caches

Tags:

c

cpu-cache

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.

like image 377
osgx Avatar asked Apr 05 '10 03:04

osgx


People also ask

Which has the larger capacity L1 or L2 cache?

Modern systems often use at least two levels of caches, as shown in Figure 8.16. The first-level (L1) cache is small enough to provide a one- or two-cycle access time. The second-level (L2) cache is also built from SRAM but is larger, and therefore slower, than the L1 cache.

What is the size of L1 L2 L3 cache?

Cache is graded as Level 1 (L1), Level 2 (L2) and Level 3 (L3): L1 is usually part of the CPU chip itself and is both the smallest and the fastest to access. Its size is often restricted to between 8 KB and 64 KB. L2 and L3 caches are bigger than L1.

Why do the sizes of the caches L1 and L2 have to be different in a CPU?

If the size of L1 was the same or bigger than the size of L2, then L2 could not accomodate for more cache lines than L1, and would not be able to deal with L1 cache misses. From the design/cost perspective, L1 cache is bound to the processor and faster than L2.


2 Answers

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.

like image 60
Gabe Avatar answered Sep 20 '22 15:09

Gabe


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.

like image 40
osgx Avatar answered Sep 18 '22 15:09

osgx