Saltar a contenido

Week 6 - Swapping, OOM, Memory Pressure (PSI)

6.1 Conceptual Core

  • Swap is the kernel's overflow valve when anon memory pressure exceeds available RAM. Modern systems often run swapless or with a small swap (e.g., zswap or zram).
  • The OOM killer is the kernel's last-resort mechanism: when all reclaim has failed and an allocation cannot be satisfied, it kills the process with the highest oom_score (heuristic of memory usage and adjusted by oom_score_adj).
  • PSI (Pressure Stall Information)-/proc/pressure/{cpu,memory,io} and per-cgroup `pressure.{cpu,memory,io} - reports time the system or a cgroup spent stalled on each resource. The modern signal for "this system is sad."

6.2 Mechanical Detail

  • swapon, swapoff, /proc/swaps, vm.swappiness.
  • zram (compressed RAM-backed swap) configuration via systemd-zram-generator or manually with zramctl.
  • OOM tuning:
  • oom_score_adj per-process (/proc/<pid>/oom_score_adj, range -1000 to 1000).
  • systemd OOMScoreAdjust= directive.
  • vm.overcommit_memory (0/1/2): allow / always-allow / strict accounting.
  • PSI semantics:
  • `some - at least one task stalled.
  • `full - all runnable tasks stalled (system-wide can't reach this for CPU).
  • Numbers are 10s/60s/300s averages of stall percentage.

6.3 Lab-"Pressure and the OOM Killer"

  1. Write a memory-eater program. Run inside a memory.high=512M cgroup. Observe pressure.memory rise.
  2. Push past memory.max; watch the OOM killer. Check dmesg and journalctl -k | grep -i 'killed process'.
  3. Set oom_score_adj=-500 on a critical process; verify it survives an OOM event triggered by another, lower-priority hog.
  4. Measure PSI under realistic load: capture pressure.memory every second for 5 minutes during a workload spike. Plot.

6.4 Hardening Drill

  • Add MemoryHigh= and MemoryMax= to every long-running service. Use MemoryHigh as a soft target (slows allocations) and MemoryMax as the hard cliff.

6.5 Performance Tuning Slice

  • Hook bpftrace -e 'kprobe:oom_kill_process { printf("%s killed %s\n", comm, str(arg0->comm)) }' to observe OOM events live.

Comments