devutils/ATFQ/book ATFQ git

Part I · How it works

3.1 RAM and the memory hierarchy

alsomemory hierarchydramddr4ddr5memory latencymemory bandwidthlatency numbersmemory channelsdual channeldimmdimmsmemory speed

TL;DR

  • Every level trades size for speed. Rough orders of magnitude: registers under 1 ns, L1 1 ns, L2 4 ns, L3 15 ns, RAM 100 ns, NVMe SSD tens of µs, network to another continent 100 ms.
  • RAM (DRAM) keeps each bit in a tiny capacitor that leaks: it must be refreshed constantly, and it loses everything when powered off.
  • Bandwidth comes from channels: two modules in the right slots work in parallel and double it; one module alone halves it.
sudo dmidecode -t memory | grep -E 'Size|Type:|Speed|Locator'
lsmem                     # memory blocks the kernel sees

On your machine

alice@laptopexample
$ atfq 3.1
  L1 data   4 × 48.0 KiB, each shared by 2 CPUs
  L1 code   4 × 32.0 KiB, each shared by 2 CPUs
  L2        4 × 1.2 MiB, each shared by 2 CPUs
  L3        12.0 MiB, shared by all 8 CPUs
  Line      64 bytes: the block memory moves in

  RAM       10.5 GiB available of 15.5 GiB (68%) · 1.1 GiB completely free
  Cache     7.5 GiB in buff/cache, 604.0 MiB of it shared (tmpfs)
  Swap      256.0 MiB used of 8.0 GiB · swappiness 60

An example: Alice's laptop, one of the test machines. In your terminal, this block shows your own machine, read live.

Pitfalls

Deep dive 2 questions

Why is RAM so much slower than the CPU?

Distance and design. RAM sits centimetres away, on another chip, behind a memory controller; DRAM must open a row of cells, read it into a buffer, then send the bytes over the bus. Caches are SRAM inside the chip: much faster, but each bit takes six transistors instead of one capacitor, so there can't be gigabytes of it.

Where do swap and the page cache fit?

They extend the hierarchy downward. The page cache keeps file data in RAM so the disk is read once (17.4); swap moves rarely used memory to disk to free RAM (17.3). Each step down is about a thousand times slower, which is why a machine that swaps heavily crawls.

Commands
dmidecode lsmem
Paths
/proc/meminfo
See also
17. Memory
2.3 CPU caches: why memory layout matters
1.3 How virtual memory works
17.4 The page cache