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.
npx skills add mohitmishra786/low-level-dev-skills --skill memory-hierarchy-and-caches -a claude-codeWhat 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_prefetchfor loops - Diagnose L1 and LLC misses using
perf stathardware 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-code2Manually: put the files in place
~/.claude/skills/memory-hierarchy-and-caches/SKILL.mdCopy the whole skill folder; for one project use .claude/skills/memory-hierarchy-and-caches/
1Skills CLI (Vercel)
npx skills add mohitmishra786/low-level-dev-skills --skill memory-hierarchy-and-caches -a codex2Manually: put the files in place
.agents/skills/memory-hierarchy-and-caches/SKILL.mdFor all projects use ~/.agents/skills/
1Skills CLI (Vercel)
npx skills add mohitmishra786/low-level-dev-skills --skill memory-hierarchy-and-caches -a cursor2Manually: put the files in place
.cursor/skills/memory-hierarchy-and-caches/SKILL.mdCursor also reads .agents/skills/ and .claude/skills/
1GitHub CLI
gh skill install mohitmishra786/low-level-dev-skills memory-hierarchy-and-caches2Manually: put the files in place
.github/skills/memory-hierarchy-and-caches/SKILL.md1Skills CLI (Vercel)
npx skills add mohitmishra786/low-level-dev-skills --skill memory-hierarchy-and-caches -a gemini-cli2Platform CLI
gemini skills install https://github.com/mohitmishra786/low-level-dev-skills --path skills/computer-architecture/memory-hierarchy-and-cachesManually: put the files in place
.devin/skills/memory-hierarchy-and-caches/SKILL.mdThe legacy .windsurf/skills/ path still works
Skills CLI (Vercel)
npx skills add mohitmishra786/low-level-dev-skills --skill memory-hierarchy-and-caches -a opencodeClaude.ai
Zip the folder and upload it in Settings → Capabilities → Skills
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