Skip to main content
skill CX Codex

memory-hierarchy-and-caches — Claude Code skill: how to install

This skill guides agents through CPU cache levels, MESI coherence protocols, and false sharing diagnostics. It helps optimize data layouts when thread scaling drops.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add mohitmishra786/low-level-dev-skills --skill memory-hierarchy-and-caches -a claude-code
Open on GitHub

What memory-hierarchy-and-caches does

The skill provides hardware context regarding CPU memory stages, running from per-core L1 caches (~32 KiB, around 4 cycles) up to shared L3 blocks and DRAM. It focuses on cache line mechanics and set-associativity. When threads concurrently modify adjacent fields on a shared 64-byte boundary, MESI invalidations stall execution. That ruins multicore scalability.

Instruction material outlines fixes like alignas(64) struct padding, manual prefetching via __builtin_prefetch, and measuring hardware events with perf stat. It also covers embedded SoC setups where MCUs lack L2 or L3 caches, requiring manual cache invalidation for DMA operations. Stride prefetchers handle sequential loops well. Irregular lookups simply miss.

Without running getconf LEVEL1_DCACHE_LINESIZE, hardcoded 64-byte padding assumptions will fail on architectures with non-standard line sizes.

What it helps with

  • Resolve false sharing between worker threads using struct alignment
  • Insert manual prefetch hints with __builtin_prefetch for loops
  • Diagnose L1 and LLC misses using perf stat hardware counters
  • Handle DMA cache invalidation on embedded microcontrollers

Example prompts for the agent

  • /memory-hierarchy-and-caches Diagnose false sharing in this per-thread stats array
  • /memory-hierarchy-and-caches How does struct padding restore multicore scaling here?
  • /memory-hierarchy-and-caches Analyze cache miss counters from my perf stat output

How to install memory-hierarchy-and-caches

1Skills CLI (Vercel)

npx skills add mohitmishra786/low-level-dev-skills --skill memory-hierarchy-and-caches -a claude-code

2Manually: put the files in place

~/.claude/skills/memory-hierarchy-and-caches/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/memory-hierarchy-and-caches/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about memory-hierarchy-and-caches

How to install memory-hierarchy-and-caches in Claude Code?

Run npx skills add mohitmishra786/low-level-dev-skills --skill memory-hierarchy-and-caches -a claude-code. The file will be written to ~/.claude/skills/memory-hierarchy-and-caches/SKILL.md.

What does memory-hierarchy-and-caches do?

It teaches the agent CPU cache hierarchy, line sizes, associativity, and MESI protocol invalidations. It guides layout changes to fix false sharing across threads.

How to measure cache misses using this skill?

Run perf stat -e cache-references,cache-misses,L1-dcache-load-misses ./app. The skill analyzes these counters to see if stalls come from L1 loads or last-level cache capacity.

Source text

SKILL.md · MIT
---
name: memory-hierarchy-and-caches
description: Memory hierarchy skill for caches, coherence, and locality. Use when explaining cache levels, associativity, false sharing, prefetching, or MESI coherence. Activates on queries about cache hierarchy, L1 L2 L3, false sharing, cache line, prefetch, or cache coherence.
---

# Memory Hierarchy and Caches

## Purpose

Teach CPU memory hierarchy: cache levels, associativity, line size, coherence protocols, false sharing, and prefetch behavior — architectural depth complementing optimization practice in `skills/low-level-programming/cpu-cache-opt`.

## When to Use

- Explaining why padding fixes scalability
- Choosing struct layout for multicore
- Interpreting cache miss counters
- Understanding DMA vs CPU cache on embedded SoCs

## Workflow

### 1. Hierarchy (typical desktop/ server)

```
Registers
├── L1d / L1i (per core, ~32 KiB, ~4 cycles)
├── L2 (per core, ~256 KiB – 1 MiB)
├── L3 (shared last-level, MiB – tens of MiB)
├── DRAM (hundreds of cycles)
└── Storage / NUMA remote (much slower)
```

Embedded MCUs may have only tightly-coupled memory (no L2/L3).

### 2. Cache line and associativity

- Line size: commonly **64 bytes** on x86/ARM64 (verify with `getconf LEVEL1_DCACHE_LINESIZE`)
- Set-associative: line maps to one set, competes within ways
- Conflict misses: many aliases same set

### 3. False sharing

```c
/* Bad — two atomics on same cache line */
struct {
    atomic_int counter_a;
    atomic_int counter_b;
} stats;

/* Good — pad to cache line */
struct alignas(64) {
    atomic_int counter_a;
    char pad[64 - sizeof(atomic_int)];
    atomic_int counter_b;
} stats;
```

### 4. Coherence (multicore)

MESI states: Modified, Exclusive, Shared, Invalid. Writes invalidate other cores' copies of the line — why atomics and locks ping cache lines.

### 5. Prefetching

```c
#ifdef __builtin_prefetch
for (int i = 0; i < n; i++) {
    __builtin_prefetch(&data[i + 8], 0, 3);
    process(data[i]);
}
#endif
```

Hardware stride prefetchers detect sequential access; random access misses.

### 6. Measurement

```bash
perf stat -e cache-references,cache-misses,L1-dcache-load-misses ./app
```

### 7. Agent usage

```
/memory-hierarchy-and-caches Diagnose false sharing in this per-thread stats array
```

## Common Problems

| Symptom | Cause | Fix |
|---------|-------|-----|
| Scaling collapses | False sharing | Line-align per-thread data |
| High LLC misses | Working set > cache | Block algorithms; NUMA pin |
| DMA incoherence | CPU cache vs device | Flush/invalidate on MCU; dma_sync on Linux |
| Prefetch hurt | irregular access | Remove manual prefetch |
| Huge struct copies | AoS cold lines | SoA layout — see cpu-cache-opt |

## Related Skills

- `skills/low-level-programming/cpu-cache-opt` — practical optimization
- `skills/computer-architecture/cpu-pipelines-and-hazards` — load-use stalls
- `skills/computer-architecture/virtual-memory-paging-and-tlb` — TLB misses
- `skills/allocators/numa-programming` — remote memory latency
- `skills/profilers/hardware-counters` — perf counter events

© mohitmishra786 · MIT · mohitmishra786/low-level-dev-skills

Similar skills

All skills for Code review and refactoring →

From the same set95

custom-allocators Guides agents through designing custom C and Rust memory allocators, tuning jemalloc or mimalloc, and measuring heap fragmentation. CX Codex +1 350installs reverse-engineering A skill that walks the agent through binary analysis: Ghidra decompilation, radare2 workflows, Binary Ninja scripting. Useful when there is no source and you need the logic back. CX Codex +1 350installs abi-and-calling-conventions A skill that teaches your coding agent the ABI rules: which registers carry arguments, how the stack aligns, what breaks across FFI. CX Codex +1 342installs linux-kernel-architecture Gives your agent a map of the Linux kernel: boot sequence, major subsystems, initcall levels, and where code lives in linux.git. CX Codex +1 333installs cuda Guides the agent through CUDA C/C++ work: thread hierarchy, shared memory, streams, nvcc flags and Thrust. CX Codex +1 320installs qemu-for-kernel-development Guides agents in booting custom Linux kernels and testing drivers inside QEMU. Useful for rapid module development, Buildroot rootfs integration, and GDB debugging. CX Codex +1 319installs kernel-debugging-advanced A skill for Linux kernel tracing: ftrace, trace-cmd, perf, kprobes, kgdb and vmcore analysis. Useful when a driver stalls with no obvious bug. CX Codex +1 317installs virtual-memory-paging-and-tlb This skill guides coding agents through virtual memory translation, x86-64 multi-level page tables, and TLB lookups. It helps troubleshoot page faults and huge page tuning. CX Codex +1 316installs

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor