Course contentsShow
← Linux
Lesson 1746 of 1,81914. Observability and Performance TroubleshootingFree lesson

Archiving Performance Snapshots

Create automated procedures for archiving performance state during critical system incidents.

In this lesson, you will learn how to design automated procedures that capture and persist a "system state-of-mind" the exact moment a critical performance incident triggers.

When a production system hits a severe bottleneck, transient data is often lost the moment the service restarts or the kernel stabilizes. To conduct a meaningful post-mortem, you need a high-fidelity snapshot of the kernel and application state taken while the issue is live.

The Anatomy of an Automated Snapshot

An automated archiving procedure must be non-intrusive to avoid exacerbating the incident. Instead of running heavy analytical tools during a crisis, your automation should trigger a "capture-and-dump" script. This script should collect three specific layers:

  1. Kernel State: CPU scheduler run-queues, active BPF programs, and kernel stack traces.
  2. Resource Contention: Open file descriptors, blocked I/O requests, and memory pressure stalls (/proc/pressure/memory).
  3. Process Context: CPU usage and memory maps for the specific thread IDs (TIDs) contributing to the latency spikes.

Implementation Pattern

The most efficient approach is to bind this to your alerting system (e.g., via a webhook or a local daemon). When your performance baseline thresholds are breached, the following logic executes:

#!/bin/bash
# Snapshot trigger script for critical performance degradation
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
DIR="/var/log/performance_snapshots/$TIMESTAMP"
mkdir -p "$DIR"

# Capture volatile state without overhead
cat /proc/pressure/cpu > "$DIR/cpu_pressure.log"
bpftool prog show > "$DIR/bpf_progs.txt"
ps -eo pid,tid,pcpu,pmem,state,cmd --sort=-pcpu | head -n 20 > "$DIR/top_threads.log"
dmesg | tail -n 500 > "$DIR/kernel_logs.txt"

# Compress and ship to off-node storage for long-term retention
tar -czf "$DIR.tar.gz" "$DIR" && rm -rf "$DIR"

By decoupling the capture of the snapshot from the analysis of the snapshot, you ensure that your performance diagnostics remain stable under pressure. This raw data becomes the source of truth for offline analysis, allowing you to correlate infrastructure state with the metrics captured in your observability pipelines.

Key Takeaway: Automated performance snapshots turn volatile system behavior into persistent evidence by capturing minimal, high-impact kernel and process state at the precise moment of failure.