|
| 1 | +--- |
| 2 | +description: |
| 3 | +globs: |
| 4 | +alwaysApply: false |
| 5 | +--- |
| 6 | +# Java Profiling Workflow / Step 1 / Collect data to measure potential issues |
| 7 | + |
| 8 | +## ⚠️ CRITICAL INSTRUCTION FOR AI ASSISTANTS ⚠️ |
| 9 | + |
| 10 | +**WHEN USING THE PROFILING SCRIPT TEMPLATE:** |
| 11 | +- **COPY THE BASH SCRIPT EXACTLY** from `java-profiling-script-template.md` |
| 12 | +- **NO MODIFICATIONS, INTERPRETATIONS, OR ENHANCEMENTS** allowed |
| 13 | +- **USE EVERY LINE VERBATIM** - do not change logic, structure, or features |
| 14 | +- **DO NOT CREATE YOUR OWN VERSION** - use the provided template only |
| 15 | +- The template script is complete, tested, and production-ready |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +This rule provides comprehensive Java application profiling using async-profiler v4.0, including automatic OS detection, profiler download, and flamegraph generation with the latest features. |
| 20 | + |
| 21 | +## Problem Identification |
| 22 | + |
| 23 | +Before starting the profiling process, identify what specific performance problem you're trying to solve. |
| 24 | + |
| 25 | +**Reference the profiling questions template:** [java-profiling-questions-template.md](mdc:.cursor/rules/templates/java-profiling-questions-template.md) |
| 26 | + |
| 27 | +The template categorizes problems into: |
| 28 | +- **Performance Bottlenecks** (CPU hotspots, inefficient algorithms, unnecessary allocations, string operations) |
| 29 | +- **Memory-Related Problems** (memory leaks, heap usage, object retention, off-heap issues) |
| 30 | +- **Concurrency and Threading Issues** (lock contention, thread pool issues, deadlocks, context switching) |
| 31 | +- **Garbage Collection Problems** (GC pressure, long pauses, generational issues) |
| 32 | +- **I/O and Network Bottlenecks** (blocking operations, connection leaks, serialization issues) |
| 33 | + |
| 34 | +## Overview |
| 35 | + |
| 36 | +Java profiling helps identify performance bottlenecks, memory leaks, and CPU hotspots in Java applications. This rule automates the process of: |
| 37 | +1. **Problem Identification**: Determine what specific performance issue you're investigating |
| 38 | +2. **Process Detection**: Detecting running Java processes using `jps` |
| 39 | +3. **Environment Setup**: Identifying the operating system and architecture |
| 40 | +4. **Tool Download**: Downloading the appropriate async-profiler v4.0 binary |
| 41 | +5. **Profile Execution**: Attaching to a Java process and generating flamegraphs and heatmaps |
| 42 | +6. **Results Analysis**: Storing and interpreting profiling results |
| 43 | + |
| 44 | +## New Features in v4.0 |
| 45 | + |
| 46 | +Based on the [async-profiler v4.0 release](mdc:https:/github.com/async-profiler/async-profiler/releases/tag/v4.0), the latest version includes: |
| 47 | + |
| 48 | +### Major Features |
| 49 | +- **Interactive Heatmaps**: Visualize performance data over time |
| 50 | +- **Native Memory Leak Profiler**: Detect memory leaks in native code |
| 51 | +- **jfrconv Binary**: Enhanced JFR conversion capabilities |
| 52 | +- **Improved JDK 23+ Support**: Full compatibility with the latest JDK versions |
| 53 | + |
| 54 | +### Enhanced Profiling Capabilities |
| 55 | +- **Wall Clock Profiling Optimization**: Better performance for wall-clock profiling |
| 56 | +- **VMStructs-based Stack Walker**: More accurate stack traces with `--cstack vm/vmx` |
| 57 | +- **C API**: Access to thread-local profiling context |
| 58 | +- **Grain Option**: Coarsen flame graphs for better visualization |
| 59 | +- **Inverted Flame Graphs**: Flip flame graphs vertically with `--inverted` |
| 60 | + |
| 61 | +## Project Organization |
| 62 | + |
| 63 | +The profiling setup uses a clean folder structure with everything contained in the profiler directory: |
| 64 | + |
| 65 | +``` |
| 66 | +your-project/ |
| 67 | +└── profiler/ # ← All profiling-related files |
| 68 | + ├── scripts/ # ← Profiling scripts and tools |
| 69 | + │ └── java-profile.sh # ← Main profiling script |
| 70 | + ├── results/ # ← Generated profiling output |
| 71 | + │ ├── *.html # ← Flamegraph files |
| 72 | + │ └── *.jfr # ← JFR recording files |
| 73 | + ├── current/ # ← Symlink to current profiler version |
| 74 | + └── async-profiler-*/ # ← Downloaded profiler binaries |
| 75 | +``` |
| 76 | + |
| 77 | +**Usage:** |
| 78 | +- Run `./profiler/scripts/java-profile.sh` for comprehensive interactive profiling |
| 79 | + |
| 80 | +## Automated Profiling Script |
| 81 | + |
| 82 | +**IMPORTANT**: Use the exact bash script from the template without any modification or interpretation. |
| 83 | + |
| 84 | +The complete interactive profiling script is available at: [java-profiling-script-template.md](mdc:.cursor/rules/templates/java-profiling-script-template.md) |
| 85 | + |
| 86 | +**CRITICAL INSTRUCTION FOR AI ASSISTANTS:** |
| 87 | +- **COPY THE BASH SCRIPT EXACTLY** from the template file |
| 88 | +- **DO NOT MODIFY, INTERPRET, OR ENHANCE** the script content |
| 89 | +- **DO NOT ADD NEW FEATURES** or change the logic |
| 90 | +- **USE THE SCRIPT VERBATIM** - every line, comment, and function exactly as provided |
| 91 | +- The script is already complete and tested - no improvements needed |
| 92 | + |
| 93 | +**Script Location:** |
| 94 | +``` |
| 95 | +└── profiler/ # ← All profiling-related files |
| 96 | + ├── scripts/ # ← Profiling scripts and tools |
| 97 | + │ └── java-profile.sh # ← Copy exact script from template |
| 98 | +``` |
| 99 | + |
| 100 | +**Setup Instructions:** |
| 101 | +1. Copy the **EXACT** bash script content from `java-profiling-script-template.md` |
| 102 | +2. Save it as `profiler/scripts/java-profile.sh` |
| 103 | +3. Make it executable: `chmod +x profiler/scripts/java-profile.sh` |
| 104 | +4. **NO MODIFICATIONS** to the script content are needed or allowed |
| 105 | + |
| 106 | +## Profiling Execution Workflow |
| 107 | + |
| 108 | +After setting up the profiling scripts, follow this execution workflow to profile your Java application and store results in the profiler/results directory: |
| 109 | + |
| 110 | +### Step 1: Create and Make Script Executable |
| 111 | + |
| 112 | +**CRITICAL**: Use the exact script from the template without any changes: |
| 113 | + |
| 114 | +```bash |
| 115 | +# Create the profiler directory structure |
| 116 | +mkdir -p profiler/scripts |
| 117 | +mkdir -p profiler/results |
| 118 | + |
| 119 | +# Copy the EXACT bash script from java-profiling-script-template.md |
| 120 | +# Save it as profiler/scripts/java-profile.sh |
| 121 | +# DO NOT MODIFY THE SCRIPT CONTENT - USE IT EXACTLY AS PROVIDED |
| 122 | + |
| 123 | +# Make the script executable |
| 124 | +chmod +x profiler/scripts/java-profile.sh |
| 125 | +``` |
| 126 | + |
| 127 | +**REMINDER**: The script in `java-profiling-script-template.md` is complete and should be copied verbatim. No interpretation, enhancement, or modification is required or allowed. |
| 128 | + |
| 129 | +### Step 2: Execute the Profiling Script |
| 130 | + |
| 131 | +Run the script from your project root directory: |
| 132 | + |
| 133 | +```bash |
| 134 | +# Execute the interactive profiling script |
| 135 | +./profiler/scripts/java-profile.sh |
| 136 | +``` |
| 137 | + |
| 138 | +**Expected execution flow:** |
| 139 | +1. **Problem Identification**: The script will first ask you to identify what problem you're trying to solve |
| 140 | +2. **Process Selection**: Lists all Java processes and allows you to select one |
| 141 | +3. **Directory Setup**: Prompts for profiler directory (default: ./profiler) |
| 142 | +4. **Auto-setup**: Downloads and configures async-profiler automatically |
| 143 | +5. **Interactive Menu**: Presents profiling options based on your problem type |
| 144 | + |
| 145 | +### Step 3: Problem-Specific Profiling Commands |
| 146 | + |
| 147 | +Based on the problem you identified, execute these specific profiling commands: |
| 148 | + |
| 149 | +#### For CPU Hotspots and Performance Bottlenecks |
| 150 | +```bash |
| 151 | +# Quick CPU profiling (30 seconds) |
| 152 | +./profiler/current/bin/asprof -d 30 -f ./profiler/results/cpu-hotspots-$(date +%Y%m%d-%H%M%S).html <PID> |
| 153 | + |
| 154 | +# Extended CPU profiling with method filtering |
| 155 | +./profiler/current/bin/asprof -d 60 -i 'com/yourpackage/*' -f ./profiler/results/filtered-cpu-$(date +%Y%m%d-%H%M%S).html <PID> |
| 156 | +``` |
| 157 | + |
| 158 | +#### For Memory-Related Problems |
| 159 | +```bash |
| 160 | +# Memory allocation profiling |
| 161 | +./profiler/current/bin/asprof -e alloc -d 60 -f ./profiler/results/memory-allocations-$(date +%Y%m%d-%H%M%S).html <PID> |
| 162 | + |
| 163 | +# Memory leak detection (longer duration) |
| 164 | +./profiler/current/bin/asprof -e alloc -d 300 --alloc 1m -f ./profiler/results/memory-leak-$(date +%Y%m%d-%H%M%S).html <PID> |
| 165 | + |
| 166 | +# Native memory profiling |
| 167 | +./profiler/current/bin/asprof -e native -d 60 -f ./profiler/results/native-memory-$(date +%Y%m%d-%H%M%S).html <PID> |
| 168 | +``` |
| 169 | + |
| 170 | +#### For Concurrency and Threading Issues |
| 171 | +```bash |
| 172 | +# Lock contention profiling |
| 173 | +./profiler/current/bin/asprof -e lock -d 60 -f ./profiler/results/lock-contention-$(date +%Y%m%d-%H%M%S).html <PID> |
| 174 | + |
| 175 | +# Wall clock profiling (includes waiting threads) |
| 176 | +./profiler/current/bin/asprof -e wall -d 60 -f ./profiler/results/wall-clock-$(date +%Y%m%d-%H%M%S).html <PID> |
| 177 | + |
| 178 | +# Thread-specific profiling |
| 179 | +./profiler/current/bin/asprof -d 60 -t -f ./profiler/results/thread-analysis-$(date +%Y%m%d-%H%M%S).html <PID> |
| 180 | +``` |
| 181 | + |
| 182 | +#### For Garbage Collection Problems |
| 183 | +```bash |
| 184 | +# GC analysis with JFR |
| 185 | +./profiler/current/bin/asprof -d 120 -o jfr -f ./profiler/results/gc-analysis-$(date +%Y%m%d-%H%M%S).jfr <PID> |
| 186 | + |
| 187 | +# Convert JFR to flamegraph for GC analysis |
| 188 | +./profiler/current/bin/jfr2flame ./profiler/results/gc-analysis-*.jfr ./profiler/results/gc-flamegraph-$(date +%Y%m%d-%H%M%S).html |
| 189 | +``` |
| 190 | + |
| 191 | +#### For I/O and Network Bottlenecks |
| 192 | +```bash |
| 193 | +# I/O profiling with wall clock events |
| 194 | +./profiler/current/bin/asprof -e wall -d 90 -f ./profiler/results/io-analysis-$(date +%Y%m%d-%H%M%S).html <PID> |
| 195 | + |
| 196 | +# Combined CPU and I/O analysis |
| 197 | +./profiler/current/bin/asprof -e cpu,wall -d 90 -f ./profiler/results/cpu-io-combined-$(date +%Y%m%d-%H%M%S).html <PID> |
| 198 | +``` |
| 199 | + |
| 200 | +### Step 4: Advanced Analysis with Interactive Heatmaps |
| 201 | + |
| 202 | +For time-based analysis, create interactive heatmaps: |
| 203 | + |
| 204 | +```bash |
| 205 | +# Generate JFR recording |
| 206 | +./profiler/current/bin/asprof -d 120 -o jfr -f ./profiler/results/profile-$(date +%Y%m%d-%H%M%S).jfr <PID> |
| 207 | + |
| 208 | +# Convert to interactive heatmap |
| 209 | +./profiler/current/bin/jfrconv --cpu -o heatmap ./profiler/results/profile-*.jfr ./profiler/results/heatmap-cpu-$(date +%Y%m%d-%H%M%S).html |
| 210 | + |
| 211 | +# Create allocation heatmap |
| 212 | +./profiler/current/bin/jfrconv --alloc -o heatmap ./profiler/results/profile-*.jfr ./profiler/results/heatmap-alloc-$(date +%Y%m%d-%H%M%S).html |
| 213 | +``` |
| 214 | + |
| 215 | +### Step 5: Results Organization and Analysis |
| 216 | + |
| 217 | +After profiling, organize and analyze your results: |
| 218 | + |
| 219 | +```bash |
| 220 | +# List all generated profiling files |
| 221 | +ls -la ./profiler/results/ |
| 222 | + |
| 223 | +# View recent profiling results organized by type |
| 224 | +echo "=== CPU Profiling Results ===" |
| 225 | +ls -lt ./profiler/results/*cpu* ./profiler/results/*flamegraph* 2>/dev/null | head -5 |
| 226 | + |
| 227 | +echo "=== Memory Profiling Results ===" |
| 228 | +ls -lt ./profiler/results/*memory* ./profiler/results/*alloc* 2>/dev/null | head -5 |
| 229 | + |
| 230 | +echo "=== Threading/Lock Analysis ===" |
| 231 | +ls -lt ./profiler/results/*lock* ./profiler/results/*wall* ./profiler/results/*thread* 2>/dev/null | head -5 |
| 232 | + |
| 233 | +echo "=== Heatmap Analysis ===" |
| 234 | +ls -lt ./profiler/results/*heatmap* 2>/dev/null | head -5 |
| 235 | + |
| 236 | +echo "=== JFR Recordings ===" |
| 237 | +ls -lt ./profiler/results/*.jfr 2>/dev/null | head -5 |
| 238 | +``` |
| 239 | + |
| 240 | +## Interpreting Flamegraphs |
| 241 | + |
| 242 | +### Reading Flamegraphs |
| 243 | +- **Width**: Represents the time spent in a method (wider = more time) |
| 244 | +- **Height**: Represents the call stack depth |
| 245 | +- **Color**: Randomly assigned for visibility (no special meaning) |
| 246 | +- **Search**: Use the search box to find specific methods |
| 247 | +- **Zoom**: Click on any frame to zoom in |
| 248 | + |
| 249 | +### Common Patterns to Look For |
| 250 | +- **Wide flames**: Methods consuming significant CPU time |
| 251 | +- **Tall stacks**: Deep call chains that might indicate recursion issues |
| 252 | +- **Flat profiles**: Even distribution might indicate I/O bound operations |
| 253 | +- **Spikes**: Hotspots that are good optimization candidates |
| 254 | + |
| 255 | +## Resources |
| 256 | + |
| 257 | +- [Async-profiler v4.0 Release](mdc:https:/github.com/async-profiler/async-profiler/releases/tag/v4.0) |
| 258 | +- [Async-profiler GitHub Repository](mdc:https:/github.com/async-profiler/async-profiler) |
| 259 | +- [Async-profiler Documentation](mdc:https:/github.com/async-profiler/async-profiler/wiki) |
| 260 | +- [Java Flight Recorder Guide](mdc:https:/docs.oracle.com/javacomponents/jmc-5-4/jfr-runtime-guide/about.htm) |
| 261 | +- [Brendan Gregg's Flame Graph Resources](mdc:http:/www.brendangregg.com/flamegraphs.html) |
| 262 | + |
0 commit comments