📈

Using Flamegraphs to Debug Claude Code Performance

Generate flamegraph visualizations to understand exactly what Claude Code is doing when CPU usage spikes.

AdvancedFlamegraphDeep Debugging
Quick Command
$ sudo ./cli/target/release/claude-diagnose --pid <PID> -D --flamegraph -o debug.svg

!The Problem

You need deeper insight into what Claude Code is actually doing during a CPU spike, beyond just knowing CPU percentage.

?Common Causes

  • Standard monitoring doesn't show call stacks
  • Need to identify specific bottlenecks
  • Reporting bugs to Anthropic
  • Understanding internal Claude behavior

🔍How to Diagnose

First, identify the target process:

```bash
# Find the high-CPU process
claude-trace -v
```

Note the PID of the process you want to profile.

Solution

**Generate a flamegraph using claude-diagnose:**

```bash
# Build the diagnostic tool first
cd ~/claude-trace/cli && cargo build --release

# Generate flamegraph (requires sudo for DTrace)
sudo ./target/release/claude-diagnose --pid <PID> -D --flamegraph -o debug.svg --duration 10
```

Open `debug.svg` in a browser. The width of each bar represents time spent in that function.

**What to look for:**
- Wide bars at the top = where most time is spent
- "poll" or "epoll" = file watching
- "read" or "write" = file I/O
- "send" or "recv" = network I/O

🛡Prevention

- Keep claude-diagnose built and ready for when you need it
- Save flamegraphs when investigating issues to track patterns
- Use simpler tools first (claude-trace) before going this deep

Monitor Claude Code in Real-Time

Get Claude Trace for instant visibility into every Claude process.

Get Claude Trace