52

I am trying to profile and optimize algorithms and I would like to understand the specific impact of the caches on various processors. For recent Intel x86 processors (e.g. Q9300), it is very hard to find detailed information about cache structure. In particular, most web sites (including Intel.com) that post processor specs do not include any reference to L1 cache. Is this because the L1 cache does not exist or is this information for some reason considered unimportant? Are there any articles or discussions about the elimination of the L1 cache?

[edit] After running various tests and diagnostic programs (mostly those discussed in the answers below), I have concluded that my Q9300 seems to have a 32K L1 data cache. I still haven't found a clear explanation as to why this information is so difficult to come by. My current working theory is that the details of L1 caching are now being treated as trade secrets by Intel.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Brent Bradburn
  • 51,587
  • 17
  • 154
  • 173
  • This was indicated by Norman Ramsey in a comment below, but I didn't realize what he meant at the time. [CPUID](http://en.wikipedia.org/wiki/CPUID) is an x86 instruction which can be used to query cache details. – Brent Bradburn Nov 16 '13 at 00:40
  • I just encountered the `lscpu` command on Linux, which gives a very nice display of CPU data on x86 -- including a cache summary. – Brent Bradburn Jun 23 '14 at 15:12

7 Answers7

67

It is near impossible to find specs on Intel caches. When I was teaching a class on caches last year, I asked friends inside Intel (in the compiler group) and they couldn't find specs.

But wait!!! Jed, bless his soul, tells us that on Linux systems, you can squeeze lots of information out of the kernel:

grep . /sys/devices/system/cpu/cpu0/cache/index*/*

This will give you associativity, set size, and a bunch of other information (but not latency). For example, I learned that although AMD advertises their 128K L1 cache, my AMD machine has a split I and D cache of 64K each.


Two suggestions which are now mostly obsolete thanks to Jed:

  • AMD publishes a lot more information about its caches, so you can at least got some information about a modern cache. For example, last year's AMD L1 caches delivered two words per cycle (peak).

  • The open-source tool valgrind has all sorts of cache models inside it, and it is invaluable for profiling and understanding cache behavior. It comes with a very nice visualization tool kcachegrind which is part of the KDE SDK.


For example: in Q3 2008, AMD K8/K10 CPUs use 64 byte cache lines, with a 64kB each L1I/L1D split cache. L1D is 2-way associative and exclusive with L2, with latency of 3 cycles. L2 cache is 16-way associative and latency is about 12 cycles.

AMD Bulldozer-family CPUs use a split L1 with a 16kiB 4-way associative L1D per cluster (2 per core).

Intel CPUs have kept L1 the same for a long time (from Pentium M to Haswell to Skylake, and presumably many generations after that): Split 32kB each I and D caches, with L1D being 8-way associative. 64 byte cache lines, matching the burst-transfer size of DDR DRAM. Load-use latency is ~4 cycles.

Also see the tag wiki for links to more performance and microarchitectural data.

Community
  • 1
  • 1
Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
  • I've already started trying to use kcachegrind. As far as I have found so far, I have to tell the tool what my cache details are -- that's what led me to ask the question. You mentioned "cache models". Do you mean to say that valgrind might know the details that I'm looking for? – Brent Bradburn Apr 04 '09 at 03:48
  • 1
    Yes definitely---valgrind queries the CPUID, and if it recognizes your CPU, it uses a model for that CPU. – Norman Ramsey Apr 04 '09 at 18:58
  • Like some of the other tools that I have run on Linux (cpuid and x86info), valgrind seems to be confused about my machine's cache configuration. Maybe this is just a matter of not recognizing my CPU or maybe it is an indication that the information being withheld by Intel. – Brent Bradburn Apr 04 '09 at 23:06
  • 17
    Intel L1 is 8-way associative. On Linux, you can pull all the numbers from `/sys/devices/system/cpu/cpu*/index*/cache`. Also, systems with glibc usually have `getconf(1)`, use like `getconf LEVEL1_DCACHE_ASSOC`. – Jed Feb 01 '10 at 12:24
  • @Jed: Thanks for posting those great suggestions! @Norman: Neat trick with grep -- thanks for updating your post! @getconf: Where've you been all my life? :-) – Brent Bradburn Feb 17 '10 at 01:34
  • Unfortunately, Jed's solution doesn't work for ARM: [Getting cache details in ARM processors - Linux](http://stackoverflow.com/questions/17170755/getting-cache-details-in-arm-processors-linux) – Brent Bradburn Jun 22 '14 at 18:08
  • @Jed Can you guys take a look at this caching question as well? http://stackoverflow.com/questions/30555623/how-many-bits-are-in-the-address-field-for-a-directly-mapped-cache – committedandroider May 31 '15 at 16:50
  • On MacOS you can check this information as well, just using `sysctl -a | grep cache` – mvlabat Jan 11 '19 at 10:50
31

This Intel Manual: Intel® 64 and IA-32 Architectures Optimization Reference Manual has a decent discussion of cache considerations.

enter image description here

Page 46, Section 2.2.5.1 Intel® 64 and IA-32 Architectures Optimization Reference Manual

Even MicroSlop is waking up to the need for more tools to monitor cache usage and performance, and has a GetLogicalProcessorInformation() function example (...while blazing new trails in creating ridiculously long function names in the process) I think I'll code up.

UPDATE I: Hazwell increases cache load performance 2X, from Inside the Tock; Haswell's Architecture

If there were any doubt how critical it is to make the best possible use of cache, this presentation by Cliff Click, formerly of Azul, should dispel any and all doubt. In his words, "memory is the new disk!".

Haswell’s URS (Unified Reservation Station)

UPDATE II: SkyLake's significantly improved cache performance specifications.

SkyLake Cache Specifications

8

You are looking at the consumer specifications, not the developer specifications. Here is the documentation you want. The cache sizes vary by processor family sub-models, so they typically are not in the IA-32 development manuals, but you can easily look them up on NewEgg and such.

Edit: More specifically: Chapter 10 of Volume 3A (Systems Programming Guide), Chapter 7 of the Optimization Reference Manual, and potentially something in the TLB page-caching manual, although I would assume that one is further out from the L1 than you care about.

Not Sure
  • 5,873
  • 3
  • 23
  • 29
  • I couldn't find real cache data in these manuals. Can you cite volume and page number? – Norman Ramsey Apr 04 '09 at 01:08
  • I'm not really sure what you mean by "real", but chapter 7 of the Optimization manual is one place that goes into some detail. There's also the entire manual on the TLB and page caching. It would help to know what *exactly* you're looking for. – Not Sure Apr 04 '09 at 01:14
  • There's also Chapter 10 of Volume 3A, the Systems programming guide. – Not Sure Apr 04 '09 at 01:16
  • I found Table 10-1 of Volume 3A. It doesn't list individual processors but it does give details (or at least numerical ranges) for cache information for various processor families. It is still a little bit ambiguous (Core 2 Quad isn't explicitly listed for L1), but it's something. Thanks! – Brent Bradburn Apr 04 '09 at 03:17
  • Like most other resources, newegg doesn't list my Q9300 as having an L1 cache (I also didn't find it clearly indicated in the Intel documentation that you cited). I'm guessing that the L1 cache doesn't exist on that chip -- but I'm still just guessing. – Brent Bradburn Apr 04 '09 at 03:24
  • My hope was that doing a google search with [q9300 "L1 cache" site:intel.com] would reveal the processor-specific data from "developer specifications". No such luck. – Brent Bradburn Apr 04 '09 at 23:33
  • Your Q9300 definitely does have an L1 cache - just google "intel q9300 l1 cache" and you'll get specs from the hardware review sites. I can't think of any decent modern processor without an L1 and L2 cache - use CPU-z or somesuch to find out what it is on your machine. – Not Sure Apr 06 '09 at 17:25
8

I did some more investigating. There is a group at ETH Zurich who built a memory-performance evaluation tool which might be able to get information about the size at least (and maybe also associativity) of L1 and L2 caches. The program works by trying different read patterns experimentally and measuring the resulting throughput. A simplified version was used for the popular textbook by Bryant and O'Hallaron.

Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
  • I tried these out (and I had written a similar program). The results suggest discontinuous performance results at 32K and 3M on my Q9300. Thanks for the help! – Brent Bradburn Apr 04 '09 at 22:32
2

L1 caches exist on these platforms. This will almost definitly remain true until memory and front side bus speeds exceed the speed of the CPU, which is a very likely a long way off.

On Windows, you can use the GetLogicalProcessorInformation to get some level of cache information (size, line size, associativity, etc.) The Ex version on Win7 will give even more data, like which cores share which cache. CpuZ also gives this information.

Michael
  • 54,279
  • 5
  • 125
  • 144
  • Thanks for the suggestions. I was able to run CpuZ -- it told me that my L1 data cache was 32K Bytes (per core). Now I just need to figure out whether or not I trust that information. – Brent Bradburn Apr 04 '09 at 02:12
  • Can you explain why you are so confident in the accuracy of CpuZ? It's nice that such a tool exists but my confidence is shaken by the fact that I can't find strong corroborating data. – Brent Bradburn Apr 04 '09 at 03:38
  • I have seen data that indicates that the L2 cache runs at the CPU clock speed (2.5 GHz). To me this suggests that the front side bus speed is irrelevant to the question of L1 existence -- the L2 cache is faster than the FSB. – Brent Bradburn Apr 04 '09 at 03:42
  • This post also spurred me to find similar Linux based programs: cpuid and x86info. x86info gave me data for L1 that matched what CpuZ said. However, various inconsistencies and warnings by the two programs still left me doubting. – Brent Bradburn Apr 04 '09 at 23:23
2

Locality of Reference has a major impact on performance of some algorithms; The size and speed of L1, L2 (and on newer CPUs L3) cache obviously play a large part in this. Matrix multiplication is one such algorithm.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
1

Intel Manual Vol. 2 specifies the following formula to compute cache size:

This Cache Size in Bytes

= (Ways + 1) * (Partitions + 1) * (Line_Size + 1) * (Sets + 1)

= (EBX[31:22] + 1) * (EBX[21:12] + 1) * (EBX[11:0] + 1) * (ECX + 1)

Where the Ways, Partitions, Line_Size and Sets are queried using cpuid with eax set to 0x04.

Providing the header file declaration

x86_cache_size.h:

unsigned int get_cache_line_size(unsigned int cache_level);

The implementation looks as follows:

;1st argument - the cache level
get_cache_line_size:
    push rbx
    ;set line number argument to be used with CPUID instruction
    mov ecx, edi 
    ;set cpuid initial value
    mov eax, 0x04
    cpuid

    ;cache line size
    mov eax, ebx
    and eax, 0x7ff
    inc eax

    ;partitions
    shr ebx, 12
    mov edx, ebx
    and edx, 0x1ff
    inc edx
    mul edx

    ;ways of associativity
    shr ebx, 10
    mov edx, ebx
    and edx, 0x1ff
    inc edx
    mul edx

    ;number of sets
    inc ecx
    mul ecx

    pop rbx

    ret

Which on my machine works as follows:

#include "x86_cache_size.h"

int main(void){
    unsigned int L1_cache_size = get_cache_line_size(1);
    unsigned int L2_cache_size = get_cache_line_size(2);
    unsigned int L3_cache_size = get_cache_line_size(3);
    //L1 size = 32768, L2 size = 262144, L3 size = 8388608
    printf("L1 size = %u, L2 size = %u, L3 size = %u\n", L1_cache_size, L2_cache_size, L3_cache_size);
}
Community
  • 1
  • 1
St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • 2
    No need to write this in asm; there's a CPUID intrinsic for most compilers. Like GNU C `#include ` for `__get_cpuid`. https://github.com/gcc-mirror/gcc/blob/master/gcc/config/i386/cpuid.h – Peter Cordes Aug 17 '19 at 14:36