1---2name: cpu-profiling-guide3enabled: true4description: |5 Use when performing cpu profiling guide — guides systematic CPU profiling to6 identify and resolve CPU-bound performance bottlenecks. Covers profiler7 selection and setup, flame graph analysis, hot path identification,8 optimization strategies, and validation procedures for common language9 runtimes.10required_connections:11 - prefix: apm12 label: "APM / Profiling Platform"13config_fields:14 - key: runtime15 label: "Application Runtime"16 required: true17 placeholder: "e.g., JVM, Node.js, Python, Go, .NET, Rust"18 - key: symptom19 label: "Primary CPU Symptom"20 required: true21 placeholder: "e.g., high CPU at idle, CPU spikes under load, slow response times"22 - key: profiling_tool23 label: "Preferred Profiling Tool"24 required: false25 placeholder: "e.g., async-profiler, pprof, py-spy, perf, dotTrace"26features:27 - PERFORMANCE28 - DEBUGGING29 - CPU30metadata:31 source: https://skillmd.com/skills/cloudthinker-ai/cpu-profiling-guide32---3334# CPU Profiling Guide3536## Phase 1: Symptom Assessment371. Characterize the CPU issue38 - [ ] Sustained high CPU (> 80% continuously)39 - [ ] CPU spikes correlated with specific operations40 - [ ] High CPU at low traffic (idle CPU waste)41 - [ ] CPU-bound latency (slow responses despite available I/O)42 - [ ] One core saturated (single-threaded bottleneck)432. Collect baseline metrics44 - [ ] CPU utilization per core and per process45 - [ ] Thread/goroutine count46 - [ ] Context switch rate47 - [ ] System vs. user CPU time ratio483. Correlate CPU with application events (requests, jobs, deployments)4950### CPU Baseline5152| Metric | Current | Normal Baseline | Status |53|--------|---------|-----------------|--------|54| Overall CPU | % | % | |55| User CPU | % | % | |56| System CPU | % | % | |57| Context switches/sec | | | |58| Active threads | | | |59| Load average | | | |6061## Phase 2: Profiler Setup621. Select appropriate profiler for runtime63 - JVM: async-profiler, JFR (Java Flight Recorder)64 - Node.js: --prof, 0x, clinic.js65 - Python: py-spy, cProfile, Pyroscope66 - Go: pprof (built-in)67 - .NET: dotTrace, PerfView68 - Native: perf, Instruments (macOS)692. Configure profiler with minimal overhead (< 2% CPU impact)703. Choose sampling rate (typically 99Hz or 997Hz)714. Profile in production or production-like environment725. Capture profiles during problematic period7374## Phase 3: Flame Graph Analysis751. Generate flame graphs from profile data762. Analyze flame graph patterns77 - [ ] Wide plateaus: functions consuming most CPU time78 - [ ] Deep stacks: excessive call depth or recursion79 - [ ] Repeated patterns: hot loops80 - [ ] Framework vs. application code ratio813. Identify top CPU-consuming functions8283### Top CPU Consumers8485| Function | Self Time % | Total Time % | Calls/sec | Category | Actionable |86|----------|-----------|-------------|----------|----------|-----------|87| | % | % | | App/Framework/GC | Yes/No |8889## Phase 4: Hot Path Investigation901. Investigate top CPU consumers91 - [ ] Inefficient algorithms (O(n^2) or worse)92 - [ ] Unnecessary computation in hot paths93 - [ ] Excessive object allocation triggering GC94 - [ ] Serialization/deserialization overhead95 - [ ] Regular expression backtracking96 - [ ] Logging in tight loops97 - [ ] Lock contention causing spin-waits98 - [ ] Redundant computation (missing caching)992. Review code for identified hot functions1003. Check if work can be avoided, cached, or batched101102### Root Cause Checklist103104| Issue Type | Location | CPU Impact % | Fix Effort | Priority |105|-----------|----------|-------------|-----------|----------|106| Algorithmic inefficiency | | % | | |107| Excessive GC pressure | | % | | |108| Unnecessary computation | | % | | |109| Missing cache | | % | | |110| Lock contention | | % | | |111112## Phase 5: Optimization Implementation1131. Apply targeted optimizations114 - [ ] Replace inefficient algorithms115 - [ ] Add caching for repeated computations116 - [ ] Reduce allocation rate in hot paths117 - [ ] Move work off hot path (async, batch, lazy)118 - [ ] Optimize data structures119 - [ ] Reduce serialization overhead120 - [ ] Fix lock contention (finer-grained locks, lock-free)1212. Benchmark each optimization in isolation1223. Verify no functionality regression123124## Phase 6: Validation1251. Re-profile after optimizations1262. Generate comparison flame graphs (before vs. after)1273. Verify CPU utilization reduction1284. Load test to confirm improvement under peak traffic1295. Monitor for regressions over time130131### Optimization Results132133| Optimization | Before (CPU %) | After (CPU %) | Reduction | Latency Impact |134|-------------|---------------|-------------|-----------|----------------|135| | % | % | -% | -ms |136| **Total** | **%** | **%** | **-%** | **-ms** |137138## Counter-Rationalizations139140| Shortcut | Counter | Why |141|----------|---------|-----|142| "We can skip some steps for this case" | Adapt the workflow steps, don't skip them | Skipped steps are where incidents and oversights originate |143| "The user seems to already know what to do" | Complete all workflow phases with the user | The workflow catches blind spots that experience alone misses |144| "This is a minor case, full process is overkill" | Scale the process down, don't turn it off | Minor cases become major when unstructured; the process scales, not disappears |145| "I'll fill in the details later" | Complete each section before moving on | Deferred details are forgotten; real-time capture is more accurate |146| "The template output isn't necessary" | Always produce the structured output format | Structured output enables comparison, audit trails, and handoff to other teams |147148## Output Format149- **CPU Baseline**: Metrics before optimization150- **Flame Graphs**: Before and after comparison151- **Hot Path Analysis**: Top CPU consumers with root causes152- **Optimization Summary**: Changes made with measured impact153- **Monitoring Setup**: Continuous profiling and CPU alerts154155## Action Items156- [ ] Collect CPU baseline and characterize the problem157- [ ] Set up profiler in production-like environment158- [ ] Capture and analyze flame graphs159- [ ] Identify and prioritize hot path optimizations160- [ ] Implement and benchmark optimizations161- [ ] Validate with re-profiling and load testing162- [ ] Set up continuous profiling for future regressions