内存层次与缓存
NetVar13372026-10-26查看 332复制 1831
用于缓存、一致性和局部性的内存层次结构技能。适用于解释缓存层级、相联度、伪共享、预取或 MESI 一致性的场景。在涉及缓存层次结构、L1 L2 L3、伪共享、缓存行、预取或缓存一致性的查询时激活。
📘如何使用这个技能
- 1复制技能内容·点击上方「Copy SKILL.md」复制全部内容,或 Download 下载到本地
- 2打开你的 AI 客户端·支持 Claude Code、Trae、Cursor、ChatGPT、DeepSeek、Kimi 等任意 AI 助手
- 3粘贴为系统提示/知识文件·Claude Code:放入 .claude/skills/ 目录;普通对话:作为首条消息发送并说「请严格按这份技能文档执行」
- 4直接提出你的需求·AI 会按技能定义的工作流、格式和约束来处理,无需再解释背景
💡 提示:技能生效后,每次对话只需一句需求,AI 会按 SKILL.md 的流程执行。
SKILL.md3083 chars
---
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