Site Tools


l2-cache

Table of Contents

L2 cache

L2 cache sits between the fast L1 cache and the large shared L3 cache, typically 256 KB–2 MB per core on modern x86 designs. It is unified (holding both instructions and data) and usually private per core, servicing memory requests in tens of cycles.

L2 can be inclusive, exclusive, or non-inclusive depending on cache hierarchy policy, which affects how cache-coherence protocols track shared data.

Example

This example shows L2 cache performance with access patterns that exceed L1 but fit in L2.

// compile: gcc -O2 -o l2cache l2cache.c
// run: ./l2cache
// description: demonstrate L2 cache access patterns
 
#include <stdio.h>
#include <time.h>
#include <string.h>
 
#define SIZE_L1 8192     // ~8KB, fits in L1
#define SIZE_L2 262144   // ~256KB, fits in L2
 
int main() {
    int *arr = malloc(SIZE_L2 * sizeof(int));
    memset(arr, 0, SIZE_L2 * sizeof(int));
 
    clock_t start, end;
    volatile int sum = 0;
 
    start = clock();
    for (int i = 0; i < SIZE_L2; i++) sum += arr[i];
    end = clock();
    printf("L2 access time: %ld cycles\n", end - start);
 
    free(arr);
    return 0;
}
l2-cache.md · Last modified: by 127.0.0.1