Chapter 14

Performance Analysis

Chapter 14: Linux Performance Analysis

Performance analysis is systematic science, not guesswork. From USE/RED methodology through top/vmstat/iostat basics, strace syscall tracing, perf hardware counters, eBPF/bpftrace kernel-level observability, and FlameGraph visualization โ€” this chapter builds a complete Linux performance troubleshooting toolkit, anchored by a full "CPU 100%" investigation case study.

1. Performance Analysis Methodology

USE Model

Proposed by Brendan Gregg: for every resource (CPU, memory, disk, network, bus) check three dimensions:

RED Model

Designed for request-oriented microservices, proposed by Tom Wilkie. For each service check:

60-Second Troubleshooting Checklist

uptime                    # ่ดŸ่ฝฝๅ‡ๅ€ผ๏ผŒๅˆคๆ–ญ่ถ‹ๅŠฟ๏ผˆ1/5/15ๅˆ†้’Ÿ๏ผ‰
dmesg | tail -20          # ๅ†…ๆ ธๆœ€่ฟ‘้”™่ฏฏไฟกๆฏ
vmstat 1 5                # ๆฏ็ง’้‡‡ๆ ท5ๆฌก๏ผŒ็œ‹CPU/ๅ†…ๅญ˜/IO
mpstat -P ALL 1 3         # ๆฏ้ข—CPUๅˆฉ็”จ็އ
pidstat 1 5               # ๅ„่ฟ›็จ‹CPUไฝฟ็”จๆƒ…ๅ†ต
iostat -xz 1 3            # ็ฃ็›˜IOๅˆฉ็”จ็އไธŽ็ญ‰ๅพ…ๆ—ถ้—ด
free -h                   # ๅ†…ๅญ˜ไธŽswapไฝฟ็”จ
sar -n DEV 1 3            # ็ฝ‘็ปœๆŽฅๅฃๆต้‡
sar -n TCP,ETCP 1 3       # TCP่ฟžๆŽฅ็ปŸ่ฎก
top                       # ๆ€ปไฝ“ๆฆ‚่งˆ๏ผŒๆŒ‰CPU/ๅ†…ๅญ˜ๆŽ’ๅบ

2. Core Performance Tools

top / htop

top header CPU line field meanings:

Field Meaning Warning threshold
%us User-space CPU (app code) >70%
%sy Kernel-space CPU (syscalls) >20%
%ni Niced process CPU โ€”
%id Idle CPU 5%
%hi Hardware interrupt handling (driver layer) >5%
%si Software interrupt (network RX, timers) >5%
%st CPU stolen by hypervisor (steal time) >5%

vmstat

vmstat 1 10   # ๆฏ็ง’้‡‡ๆ ท๏ผŒๅ…ฑ10ๆฌก

# ่พ“ๅ‡บๅˆ—่ฏดๆ˜Ž๏ผš
# procs: r๏ผˆ่ฟ่กŒ้˜Ÿๅˆ—้•ฟๅบฆ๏ผ‰b๏ผˆ้˜ปๅกžไบŽไธๅฏไธญๆ–ญ็ก็œ ็š„่ฟ›็จ‹ๆ•ฐ๏ผ‰
# memory: swpd free buff cache๏ผˆๅ•ไฝKB๏ผ‰
# swap: si๏ผˆswap in KB/s๏ผ‰so๏ผˆswap out KB/s๏ผ‰
# io: bi๏ผˆ็ฃ็›˜่ฏป blocks/s๏ผ‰bo๏ผˆ็ฃ็›˜ๅ†™ blocks/s๏ผ‰
# system: in๏ผˆไธญๆ–ญ/s๏ผ‰cs๏ผˆไธŠไธ‹ๆ–‡ๅˆ‡ๆข/s๏ผ‰
# cpu: us sy id wa st

# ๅ…ณ้”ฎไฟกๅท๏ผš
# r ๆŒ็ปญ > CPU ๆ ธๆ•ฐ โ†’ CPU ้ฅฑๅ’Œ
# so > 0 ๆŒ็ปญ โ†’ ๅ†…ๅญ˜ไธ่ถณๅœจๆข้กต
# cs ๆž้ซ˜ โ†’ ่ฟ‡ๅคš็บฟ็จ‹ๅˆ‡ๆขๆˆ–็ณป็ปŸ่ฐƒ็”จ

iostat -dx

iostat -dx 1 5   # ๆ‰ฉๅฑ•็ฃ็›˜็ปŸ่ฎก๏ผŒๆฏ็ง’้‡‡ๆ ท

# ๅ…ณ้”ฎๆŒ‡ๆ ‡๏ผš
# r/s w/s    ่ฏปๅ†™่ฏทๆฑ‚ๆ•ฐ/็ง’
# rkB/s wkB/s ่ฏปๅ†™ๅžๅ้‡ KB/s
# await      ๅนณๅ‡IO็ญ‰ๅพ…ๆ—ถ้—ด๏ผˆms๏ผ‰โ€”โ€” HDDๆญฃๅธธ 0 ๆŒ็ปญๅขž้•ฟ โ†’ ๅ†…ๅญ˜ไธ่ถณ๏ผŒๆ€ง่ƒฝไผšๅคงๅน…ไธ‹้™

sar โ€” Historical data replay

# sar ๆ•ฐๆฎ็”ฑ sysstat ๆœๅŠกๆฏ10ๅˆ†้’Ÿๆ”ถ้›†ไธ€ๆฌก๏ผŒๅญ˜ไบŽ /var/log/sa/

# ๆŸฅ็œ‹ๆ˜จๅคฉ็š„ CPU ๅˆฉ็”จ็އ
sar -u -f /var/log/sa/sa$(date -d yesterday +%d)

# ๆŸฅ็œ‹ไปŠๅคฉ็š„็ฃ็›˜IO
sar -d 1 5

# ๆŸฅ็œ‹็ฝ‘็ปœๆŽฅๅฃๆต้‡
sar -n DEV 1 5

# ๆŸฅ็œ‹่ฟ่กŒ้˜Ÿๅˆ—ไธŽ่ดŸ่ฝฝ
sar -q 1 5

# ๆŸฅ็œ‹ๅ†…ๅญ˜้กตๆขๅ…ฅๆขๅ‡บ
sar -B 1 5

3. strace: Syscall Tracing

strace intercepts and records all syscalls of a target process using ptrace, making it ideal for debugging "stuck processes", "unexpected file access", and "permission errors". Warning: strace significantly degrades the target process โ€” use with caution in production.

# ่ทŸ่ธชๆ–ฐๅฏๅŠจ็š„่ฟ›็จ‹
strace ls /tmp

# ้™„ๅŠ ๅˆฐ่ฟ่กŒไธญ็š„่ฟ›็จ‹
strace -p 1234

# ๅธฆๆ—ถ้—ดๆˆณๅ’Œ่€—ๆ—ถ๏ผˆ-tt ๅพฎ็ง’ๆ—ถ้—ด๏ผŒ-T ๆฏไธช่ฐƒ็”จ่€—ๆ—ถ๏ผ‰
strace -tt -T -p 1234

# ๅชๅ…ณๆณจ็‰นๅฎš็ณป็ปŸ่ฐƒ็”จ๏ผˆๆ–‡ไปถ่ฎฟ้—ฎ็ฑป๏ผ‰
strace -e trace=openat,read,write,close -p 1234

# ็ปŸ่ฎกๆจกๅผ๏ผšๆ˜พ็คบๆฏไธช่ฐƒ็”จ็š„ๆฌกๆ•ฐๅ’Œๆ€ป่€—ๆ—ถ๏ผˆๆœ€ๆœ‰็”จ๏ผ‰
strace -c -p 1234
# ่พ“ๅ‡บ็คบไพ‹๏ผš
# % time     seconds  usecs/call     calls    errors syscall
# 45.23       0.001234         12       100        0 epoll_wait
# 23.10       0.000631          6       100        0 read

# ่ทŸ่ธชๅญ่ฟ›็จ‹
strace -f -p 1234

# ๅธธ่ง้—ฎ้ข˜่ฏŠๆ–ญ๏ผš
# ่ฟ›็จ‹ๅกไฝ็œ‹ๅˆฐ futex(...)  โ†’ ้”็ซžไบ‰
# ่ฟ›็จ‹ๅกไฝ็œ‹ๅˆฐ epoll_wait  โ†’ ๆญฃๅธธ็ญ‰ๅพ…IOไบ‹ไปถ
# ๅคง้‡ stat() ่ฐƒ็”จ         โ†’ ่ทฏๅพ„ๆŸฅๆ‰พๆ…ข๏ผŒๆฃ€ๆŸฅ inode cache

4. ltrace: Library Call Tracing

# ่ทŸ่ธชๅŠจๆ€ๅบ“ๅ‡ฝๆ•ฐ่ฐƒ็”จ๏ผˆ็”จๆˆทๆ€๏ผŒไธ่ฟ›ๅ…ฅๅ†…ๆ ธ๏ผ‰
ltrace ./myapp

# ้™„ๅŠ ๅˆฐ่ฟ่กŒไธญ่ฟ›็จ‹
ltrace -p 1234

# ๅช่ทŸ่ธช malloc/free ๅ†…ๅญ˜ๅˆ†้…
ltrace -e malloc,free,realloc -p 1234

# ็ปŸ่ฎก่ฐƒ็”จๆฌกๆ•ฐ
ltrace -c ./myapp

# ๅŒๆ—ถๆ˜พ็คบ็ณป็ปŸ่ฐƒ็”จ๏ผˆ-S๏ผ‰
ltrace -S ./myapp

# ๅˆ†ๆžๅŠจๆ€้“พๆŽฅไพ่ต–
ldd /usr/bin/nginx
objdump -p /usr/bin/nginx | grep NEEDED

5. perf: Hardware Performance Counters

perf directly accesses the CPU's PMU (Performance Monitoring Unit) with extremely low overhead (typically perf.out


### perf top โ€” live hotspot

  
```bash
# ๅฎžๆ—ถๆ˜พ็คบๆœ€็ƒญๅ‡ฝๆ•ฐ๏ผˆ็ฑปไผผ top ไฝ†้’ˆๅฏน CPU ๆŒ‡ไปค๏ผ‰
perf top

# ๅช็œ‹ๆŸ่ฟ›็จ‹
perf top -p 1234

# perf trace๏ผš็ฑป strace ไฝ†ๅผ€้”€ๆ›ดไฝŽ๏ผˆๅŸบไบŽ eBPF/tracepoint๏ผ‰
perf trace -p 1234
perf trace -e openat,read -p 1234

Prerequisites: Using perf requires: โ‘  install linux-tools-$(uname -r), โ‘ก kernel.perf_event_paranoid=1 (allow non-root sampling), โ‘ข binaries with debug symbols (Go includes them by default; C/C++ needs -g or install -dbg packages).

6. FlameGraph Visualization

Flame graphs were invented by Brendan Gregg: the X-axis represents CPU time proportion (wider = more time), Y-axis represents call stack depth (upper calls lower), colors are random (no meaning). Flame graphs immediately reveal CPU hotspot functions.

#!/usr/bin/env bash
# flamegraph.sh โ€” ๅฎŒๆ•ด็ซ็„ฐๅ›พ็”Ÿๆˆ่„šๆœฌ

set -euo pipefail

PID="${1:?Usage: flamegraph.sh PID [duration_seconds]}"
DURATION="${2:-30}"
OUTPUT_DIR="${3:-/tmp/flamegraph-$(date +%Y%m%d-%H%M%S)}"
FLAMEGRAPH_DIR="/opt/FlameGraph"

# ๅฎ‰่ฃ… FlameGraph ๅทฅๅ…ท๏ผˆ่‹ฅๆœชๅฎ‰่ฃ…๏ผ‰
if [ ! -d "$FLAMEGRAPH_DIR" ]; then
  git clone --depth=1 https://github.com/brendangregg/FlameGraph.git "$FLAMEGRAPH_DIR"
fi

mkdir -p "$OUTPUT_DIR"

echo "[1/4] Sampling PID $PID for ${DURATION}s ..."
perf record -g --call-graph dwarf \
  -o "$OUTPUT_DIR/perf.data" \
  -p "$PID" \
  sleep "$DURATION"

echo "[2/4] Generating perf script ..."
perf script -i "$OUTPUT_DIR/perf.data" > "$OUTPUT_DIR/perf.out"

echo "[3/4] Collapsing stacks ..."
"$FLAMEGRAPH_DIR/stackcollapse-perf.pl" \
  "$OUTPUT_DIR/perf.out" > "$OUTPUT_DIR/folded.out"

echo "[4/4] Rendering flame graph ..."
"$FLAMEGRAPH_DIR/flamegraph.pl" \
  --title "CPU Flame Graph โ€” PID $PID (${DURATION}s)" \
  --width 1400 \
  "$OUTPUT_DIR/folded.out" > "$OUTPUT_DIR/flamegraph.svg"

echo "Done: $OUTPUT_DIR/flamegraph.svg"
echo "Open with: xdg-open $OUTPUT_DIR/flamegraph.svg"

Off-CPU Flame Graph (Wait Time)

# Off-CPU ็ซ็„ฐๅ›พๅˆ†ๆž็บฟ็จ‹ๅœจ้ž่ฟ่กŒ็Šถๆ€็š„ๆ—ถ้—ด๏ผˆIO็ญ‰ๅพ…ใ€้”็ญ‰ๅพ…๏ผ‰
# ้œ€่ฆ eBPF ๆ”ฏๆŒ
git clone https://github.com/brendangregg/FlameGraph.git /opt/FlameGraph

# ไฝฟ็”จ bpftrace ้‡‡้›† off-cpu ๆ•ฐๆฎ
bpftrace -e '
tracepoint:sched:sched_switch {
  if (args->prev_state == TASK_INTERRUPTIBLE || args->prev_state == TASK_UNINTERRUPTIBLE) {
    @start[args->prev_pid] = nsecs;
  }
}
tracepoint:sched:sched_switch {
  if (@start[args->next_pid]) {
    @offcpu[args->next_comm, args->next_pid] =
      hist(nsecs - @start[args->next_pid]);
    delete(@start[args->next_pid]);
  }
}' > /tmp/offcpu.txt

7. eBPF and bpftrace

eBPF (extended Berkeley Packet Filter) allows safely running user-written programs inside the kernel without modifying kernel source or loading kernel modules. eBPF programs pass through a static verifier guaranteeing they cannot crash the kernel. bpftrace is a high-level eBPF scripting language with awk-like syntax.

bpftrace Common One-liners

Goal bpftrace command
Trace all open() calls bpftrace -e 'tracepoint:syscalls:sys_enter_openat { printf("%s %s\n", comm, str(args->filename)); }'
Count slow disk IO (>1ms) bpftrace -e 'tracepoint:block:block_rq_complete { if (args->nr_sector > 0) { @lat = hist((nsecs - @start[args->sector]) / 1000); } }'
Trace new TCP connections bpftrace -e 'kprobe:tcp_connect { printf("%s โ†’ %d\n", comm, pid); }'
Count syscalls per process bpftrace -e 'tracepoint:raw_syscalls:sys_enter { @[comm] = count(); }'
Histogram of function latency bpftrace -e 'uprobe:/usr/bin/myapp:main { @start=nsecs; } uretprobe:/usr/bin/myapp:main { @lat=hist(nsecs-@start); }'
Trace OOM kill events bpftrace -e 'kprobe:oom_kill_process { printf("OOM kill: %s (pid=%d)\n", comm, pid); }'
Count kernel function call rate bpftrace -e 'kprobe:vfs_read { @[kstack] = count(); } interval:s:5 { print(@); clear(@); }'

BCC Tool Collection

# ๅฎ‰่ฃ… BCC๏ผˆUbuntu/Debian๏ผ‰
apt install bpfcc-tools linux-headers-$(uname -r)

# opensnoop: ่ทŸ่ธชๆ‰€ๆœ‰ open ่ฐƒ็”จ
opensnoop-bpfcc

# execsnoop: ่ทŸ่ธชๆ–ฐ่ฟ›็จ‹ๆ‰ง่กŒ
execsnoop-bpfcc

# tcptop: ๆŒ‰่ฟ›็จ‹็ปŸ่ฎก TCP ๆต้‡
tcptop-bpfcc

# biolatency: ็ฃ็›˜ IO ๅปถ่ฟŸๅˆ†ๅธƒ๏ผˆๆŸฑ็Šถๅ›พ๏ผ‰
biolatency-bpfcc -d 10

# runqlat: CPU ่ฟ่กŒ้˜Ÿๅˆ—็ญ‰ๅพ…ๆ—ถ้—ดๅˆ†ๅธƒ
runqlat-bpfcc 10 1

# profile: ้‡‡ๆ ท CPU ่ฐƒ็”จๆ ˆ๏ผˆ็”จไบŽ็ซ็„ฐๅ›พ๏ผ‰
profile-bpfcc -F 99 -f 30 > /tmp/out.stacks
/opt/FlameGraph/flamegraph.pl /tmp/out.stacks > profile.svg

8. Network and IO Monitoring Tools

# iotop: ๆŒ‰่ฟ›็จ‹ๅฎžๆ—ถๆ˜พ็คบ็ฃ็›˜IO๏ผˆ้œ€ root๏ผ‰
iotop -ao   # -a ็ดฏ็งฏๆจกๅผ๏ผŒ-o ๅชๆ˜พ็คบๆœ‰IO็š„่ฟ›็จ‹

# nethogs: ๆŒ‰่ฟ›็จ‹็ปŸ่ฎก็ฝ‘็ปœๅธฆๅฎฝ๏ผˆ้œ€ root๏ผ‰
nethogs eth0

# iftop: ๆŒ‰่ฟžๆŽฅๅฏน็ปŸ่ฎกๅธฆๅฎฝ๏ผˆ้œ€ root๏ผ‰
iftop -i eth0

# nload: ๆŽฅๅฃ็บงๅธฆๅฎฝๅฎžๆ—ถๅ›พ
nload eth0

# ss: ๅฅ—ๆŽฅๅญ—็ปŸ่ฎก๏ผˆๆ›ดๅฟซ็š„ netstat ๆ›ฟไปฃ๏ผ‰
ss -tunap        # ๆ˜พ็คบๆ‰€ๆœ‰ TCP/UDP ่ฟžๆŽฅๅ’Œ่ฟ›็จ‹
ss -s            # ่ฟžๆŽฅๆ•ฐ็ปŸ่ฎกๆ‘˜่ฆ
ss -o state ESTABLISHED '( dport = :80 or sport = :80 )'  # ่ฟ‡ๆปค80็ซฏๅฃ

# ็ฝ‘ๅกไธขๅŒ…็ปŸ่ฎก
ip -s link show eth0
ethtool -S eth0 | grep -i drop

9. Memory Deep Dive

# /proc/meminfo ๅ…ณ้”ฎๅญ—ๆฎต
cat /proc/meminfo
# MemTotal:        โ€” ๆ€ป็‰ฉ็†ๅ†…ๅญ˜
# MemFree:         โ€” ๅฎŒๅ…จ็ฉบ้—ฒ๏ผˆไธๅซ็ผ“ๅญ˜๏ผ‰
# MemAvailable:    โ€” ๅฎž้™…ๅฏ็”จ๏ผˆๅŒ…ๅซๅฏๅ›žๆ”ถ็ผ“ๅญ˜๏ผ‰
# Buffers:         โ€” ๅ—่ฎพๅค‡็ผ“ๅ†ฒ
# Cached:          โ€” ้กต็ผ“ๅญ˜๏ผˆๆ–‡ไปถๅ†…ๅฎน็ผ“ๅญ˜๏ผ‰
# SwapCached:      โ€” ๅทฒ่ขซๆขๅ›žๅ†…ๅญ˜ไฝ†ๆ˜ ๅฐ„่ฟ˜ๅœจswap็š„้กต
# Active/Inactive: โ€” ้กตๆดป่ทƒ็Šถๆ€๏ผˆๅฝฑๅ“ๅ›žๆ”ถ็ญ–็•ฅ๏ผ‰
# Dirty:           โ€” ๅพ…ๅˆท็›˜็š„่„้กต๏ผˆ้ซ˜ๅ€ผ่ฏดๆ˜ŽIOๅ†™ๅŽ‹ๅŠ›ๅคง๏ผ‰
# Writeback:       โ€” ๆญฃๅœจๅ›žๅ†™็ฃ็›˜็š„้กต
# Slab:            โ€” ๅ†…ๆ ธ slab ๅˆ†้…ๅ™จ็”จ้‡
# VmallocUsed:     โ€” vmalloc ๅˆ†้…็”จ้‡

# slabtop: ๅ†…ๆ ธ slab ็ผ“ๅญ˜ๅ ็”จ๏ผˆdentry/inode cache ๅธธ่งๅคงๆˆท๏ผ‰
slabtop

# smem: ็ฒพ็กฎ่ฟ›็จ‹ๅ†…ๅญ˜ๅ ็”จ๏ผˆPSS ๆฏ” RSS ๆ›ดๅ‡†็กฎ๏ผ‰
# PSS = Private + ๆŒ‰ๆฏ”ไพ‹ๅˆ†้…็š„ๅ…ฑไบซๅ†…ๅญ˜
smem -r -k -s pss | head -20
smem -P nginx -k   # ๆŒ‰่ฟ›็จ‹ๅ่ฟ‡ๆปค

# valgrind ๅ†…ๅญ˜ๆณ„ๆผๆฃ€ๆต‹๏ผˆไป…็”จไบŽๆต‹่ฏ•็Žฏๅขƒ๏ผ‰
valgrind --tool=memcheck --leak-check=full ./myapp

# valgrind heap profiling๏ผˆๅ†…ๅญ˜ๅˆ†้…็ƒญ็‚น๏ผ‰
valgrind --tool=massif --pages-as-heap=yes ./myapp
ms_print massif.out.* | head -100

10. Practice: Full "CPU 100%" Investigation

The following is a complete CPU 100% investigation walkthrough, from alert receipt to root cause identification:

## ๆญฅ้ชค1๏ผš็กฎ่ฎค็Žฐ่ฑก
uptime
# ่พ“ๅ‡บ๏ผšload average: 15.23, 14.98, 13.01
# โ†’ ๆŒ็ปญ้ซ˜่ดŸ่ฝฝ๏ผŒ15ๅˆ†้’Ÿๅ‡ๅ€ผ้ซ˜๏ผŒไธๆ˜ฏ็žฌๆ—ถๅฐ–ๅณฐ

## ๆญฅ้ชค2๏ผšๅฎšไฝ่ฟ›็จ‹
top -b -n 1 | head -30
# ๅ‘็Žฐ PID 2341 myapp ่ฟ›็จ‹ CPU ๅ  790%๏ผˆ8ๆ ธๆœบๅ™จ๏ผ‰

## ๆญฅ้ชค3๏ผš็กฎ่ฎคๆ˜ฏ็”จๆˆทๆ€่ฟ˜ๆ˜ฏๅ†…ๆ ธๆ€ CPU
pidstat -u -p 2341 1 5
# %user=785 %system=5 โ†’ ็”จๆˆทๆ€็ƒญ็‚น๏ผŒๅœจๅบ”็”จไปฃ็ ไธญ

## ๆญฅ้ชค4๏ผšๆŸฅ็œ‹็บฟ็จ‹็บง CPU๏ผˆๆ‰พๅˆฐ็ƒญ็บฟ็จ‹๏ผ‰
top -H -p 2341
# ๅ‘็Žฐ็บฟ็จ‹ TID 2345 ๅ  CPU 99%

ps -Lp 2341 -o pid,tid,pcpu,comm
# ๆ‰พๅˆฐ็ƒญ็บฟ็จ‹ tid=2345

## ๆญฅ้ชค5๏ผšperf ้‡‡ๆ ท๏ผˆ30็ง’๏ผ‰
perf record -g --call-graph dwarf -p 2341 sleep 30
perf report --stdio | head -60
# ่พ“ๅ‡บ็ƒญๅ‡ฝๆ•ฐ๏ผšjson.Marshal โ†’ 74.3%
# โ†’ JSON ๅบๅˆ—ๅŒ–ๅ ไบ†็ปๅคง้ƒจๅˆ† CPU

## ๆญฅ้ชค6๏ผš็”Ÿๆˆ็ซ็„ฐๅ›พ็กฎ่ฎค
perf script > /tmp/perf.out
/opt/FlameGraph/stackcollapse-perf.pl /tmp/perf.out > /tmp/folded.out
/opt/FlameGraph/flamegraph.pl /tmp/folded.out > /tmp/flame.svg
# ็ซ็„ฐๅ›พๆ˜พ็คบ json.Marshal ่ฐƒ็”จ้“พๅฎฝๅบฆๅ ๅ…จๅ›พ 70%+

## ๆญฅ้ชค7๏ผš็”จ bpftrace ็ปŸ่ฎก่ฐƒ็”จ้ข‘็އ
bpftrace -e '
uprobe:/opt/myapp/bin/myapp:encoding/json.Marshal {
  @calls = count();
}
interval:s:1 {
  print(@calls);
  clear(@calls);
}' &
# ่พ“ๅ‡บ๏ผšๆฏ็ง’่ฐƒ็”จ 50,000 ๆฌก

## ๆญฅ้ชค8๏ผšไปฃ็ ๅฑ‚้ข็กฎ่ฎค
# ๅฎกๆŸฅไปฃ็ ๅ‘็Žฐ๏ผš็ƒญ็‚น HTTP handler ๅฏนๆฏไธช่ฏทๆฑ‚้ƒฝ่ฟ›่กŒ
# ๅฎŒๆ•ดๅฏน่ฑกๅบๅˆ—ๅŒ–๏ผŒ่€Œๅคง้ƒจๅˆ†ๅญ—ๆฎตๅœจ่ฏทๆฑ‚้—ดไธๅ˜ๅŒ–
# โ†’ ๆ นๅ› ๏ผš็ผบๅฐ‘ๅบๅˆ—ๅŒ–็ป“ๆžœ็ผ“ๅญ˜

## ๆญฅ้ชค9๏ผšไฟฎๅคไธŽ้ชŒ่ฏ
# ๆทปๅŠ  sync.Map ็ผ“ๅญ˜ๅบๅˆ—ๅŒ–็ป“ๆžœ๏ผŒTTL=1s
# ้‡ๆ–ฐ้ƒจ็ฝฒๅŽ๏ผš
perf stat -p $(pgrep myapp) sleep 10
# IPC ไปŽ 0.3 ๆๅ‡ๅˆฐ 2.1๏ผŒCPU ไฝฟ็”จ็އไปŽ 790% ้™่‡ณ 45%

Investigation summary: Full investigation path: uptime โ†’ top โ†’ pidstat โ†’ perf record โ†’ perf report โ†’ FlameGraph โ†’ bpftrace verification โ†’ code review โ†’ fix. Each step narrows the search: machine โ†’ process โ†’ thread โ†’ function โ†’ code line.

  Previous
  โ† Ch13: systemd


  Next
  Ch15: Security โ†’
Rate this chapter
4.6  / 5  (18 ratings)

๐Ÿ’ฌ Comments