|
| 1 | +--- |
| 2 | +name: hotspot-jit-forensics |
| 3 | +description: Diagnose Java performance issues by inspecting HotSpot bytecode, tiered compilation state, inlining decisions, C2 assembly, compilation limits, and object layout. Produces reproducible artifacts (jit.xml, directives, JFR, disassembly) and links them to actionable code changes. |
| 4 | +--- |
| 5 | + |
| 6 | +# HotSpot C2 / JIT Forensics & Optimization |
| 7 | + |
| 8 | +A practical playbook for diagnosing Java performance issues by looking at what HotSpot *actually* does. |
| 9 | + |
| 10 | +Use this skill when you suspect: |
| 11 | +- Lost inlining, megamorphic dispatch, or deoptimization. |
| 12 | +- A hot method is slow because it never reaches C2. |
| 13 | +- High CPU in tiny methods (often a no-inline or missed intrinsic). |
| 14 | +- High allocation rate or cache-unfriendly object layout. |
| 15 | +- Compilation failures/bailouts (node limits, method too big, deep inlining). |
| 16 | +- Code cache pressure, safepoint stalls, or lock contention. |
| 17 | + |
| 18 | +--- |
| 19 | + |
| 20 | +## Quick start (repeatable artifacts) |
| 21 | + |
| 22 | +1) **Collect JVM facts** |
| 23 | +```bash |
| 24 | +.codex/skills/hotspot-jit-forensics/scripts/jit-facts.sh --out jit-facts.txt |
| 25 | +``` |
| 26 | +For a running process: |
| 27 | +```bash |
| 28 | +.codex/skills/hotspot-jit-forensics/scripts/jit-facts.sh --pid <pid> --out jit-facts.txt |
| 29 | +``` |
| 30 | + |
| 31 | +2) **Generate a C2 directives file** |
| 32 | +```bash |
| 33 | +.codex/skills/hotspot-jit-forensics/scripts/jit-directives.sh \ |
| 34 | + --method "com/foo/MyClass.myMethod()" \ |
| 35 | + --out c2-directives.json5 |
| 36 | +``` |
| 37 | + |
| 38 | +3) **Run the target command with compiler logging** |
| 39 | +```bash |
| 40 | +.codex/skills/hotspot-jit-forensics/scripts/jit-run-log.sh \ |
| 41 | + --directives c2-directives.json5 \ |
| 42 | + --logfile jit.xml \ |
| 43 | + -- java -XX:+UnlockDiagnosticVMOptions -jar app.jar |
| 44 | +``` |
| 45 | + |
| 46 | +Artifacts produced: |
| 47 | +- `jit-facts.txt` (version, flags, OS/arch, optional jcmd output) |
| 48 | +- `c2-directives.json5` (method-scoped compiler diagnostics) |
| 49 | +- `jit.xml` (HotSpot compilation log) |
| 50 | +- Console output with inlining and assembly (if `hsdis` is available) |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +## Core workflow |
| 55 | + |
| 56 | +### 1) Profile first (do not guess) |
| 57 | +Use **JFR** or **async-profiler** to identify the actual hot method(s). |
| 58 | + |
| 59 | +### 2) Confirm compilation tier |
| 60 | +Determine if the method is interpreted, C1, or C2. |
| 61 | + |
| 62 | +Runtime: |
| 63 | +```bash |
| 64 | +jcmd <pid> Compiler.codelist | head |
| 65 | +jcmd <pid> Compiler.queue |
| 66 | +jcmd <pid> Compiler.codecache |
| 67 | +``` |
| 68 | +Start-up (noisy): |
| 69 | +```bash |
| 70 | +java -XX:+PrintCompilation -jar app.jar |
| 71 | +``` |
| 72 | + |
| 73 | +### 3) Capture inlining + compilation decisions for ONE method |
| 74 | +Prefer **Compiler Directives** with a focused match. |
| 75 | + |
| 76 | +### 4) If assembly doesn’t print, fix hsdis |
| 77 | +HotSpot only prints assembly with the **hsdis** plugin installed. |
| 78 | + |
| 79 | +### 5) Read “why not inlined?” and “why not compiled?” |
| 80 | +Check `jit.xml` (or JITWatch) for inline failures and compilation bailouts. |
| 81 | + |
| 82 | +### 6) Inspect object layout |
| 83 | +Use **JOL** (CLI or code) and `jcmd` class histograms. |
| 84 | + |
| 85 | +### 7) Cross-check system effects |
| 86 | +GC, safepoints, locks, and code cache can dominate CPU. |
| 87 | + |
| 88 | +--- |
| 89 | + |
| 90 | +## Key flags (diagnosis only) |
| 91 | + |
| 92 | +- `-XX:+UnlockDiagnosticVMOptions` |
| 93 | +- `-XX:+LogCompilation -XX:LogFile=jit.xml` |
| 94 | +- `-XX:+CompilerDirectivesPrint -XX:CompilerDirectivesFile=c2-directives.json5` |
| 95 | +- `-XX:+PrintCompilation` |
| 96 | +- `-Xlog:safepoint=info` (JDK 9+) |
| 97 | +- `-Xlog:gc*` (JDK 9+) |
| 98 | + |
| 99 | +--- |
| 100 | + |
| 101 | +## Script reference |
| 102 | + |
| 103 | +### `jit-facts.sh` |
| 104 | +Collect JVM + OS facts and optional `jcmd` diagnostics into one file. |
| 105 | + |
| 106 | +``` |
| 107 | +Usage: jit-facts.sh [--pid <pid>] [--out <file>] |
| 108 | +``` |
| 109 | + |
| 110 | +### `jit-directives.sh` |
| 111 | +Generate a compiler directives file for a target method. |
| 112 | + |
| 113 | +``` |
| 114 | +Usage: jit-directives.sh --method <pattern> [--out <file>] |
| 115 | +``` |
| 116 | + |
| 117 | +### `jit-run-log.sh` |
| 118 | +Run a `java` command with directive-based C2 logging. |
| 119 | + |
| 120 | +``` |
| 121 | +Usage: jit-run-log.sh --directives <file> --logfile <file> -- java <args...> |
| 122 | +``` |
| 123 | + |
| 124 | +--- |
| 125 | + |
| 126 | +## Deliverables checklist |
| 127 | + |
| 128 | +- Repro command line (exact). |
| 129 | +- JVM version + flags (`jit-facts.txt`). |
| 130 | +- Profile (`recording.jfr` or `cpu.svg`/`alloc.svg`). |
| 131 | +- `jit.xml` (LogCompilation). |
| 132 | +- directives file (`c2-directives.json5`). |
| 133 | +- Assembly snippet for target method(s). |
| 134 | +- Object layout output (JOL internals + footprint). |
| 135 | +- Summary: “hypothesis → evidence → change → result”. |
| 136 | + |
| 137 | +--- |
| 138 | + |
| 139 | +## Reference URLs |
| 140 | + |
| 141 | +- https://docs.oracle.com/en/java/javase/21/docs/specs/man/java.html |
| 142 | +- https://docs.oracle.com/en/java/javase/12/vm/compiler-control1.html |
| 143 | +- https://docs.oracle.com/en/java/javase/12/vm/writing-directives.html |
| 144 | +- https://docs.oracle.com/en/java/javase/11/troubleshoot/diagnostic-tools.html |
| 145 | +- https://openjdk.org/projects/code-tools/jol/ |
| 146 | +- https://openjdk.org/jeps/520 |
0 commit comments