What is async-profiler?
async-profiler is a low-overhead sampling profiler for Java that collects stack traces and produces profiling data in multiple output formats. It uses native code and OS-level APIs to capture accurate performance data without the overhead of traditional JVMTI-based profilers. Key features:- Low overhead (typically <1% CPU)
- Captures both Java and native frames
- Supports multiple event types: CPU, wall-clock, allocations, and lock contention
- Works without instrumenting bytecode
- Outputs JFR (Java Flight Recorder) format with full metadata
Recording Profiles
Useasprof to record profiles. The -o jfr output format is recommended for ap-query because it preserves:
- Event types (cpu/wall/alloc/lock)
- Per-sample timestamps (required for timeline and
--from/--to) - Line numbers and thread information
- Hardware counter events
Common Profiling Commands
CPU profiling (on-CPU time only):Recording Options
-d DURATION— Recording duration in seconds (default: until manually stopped)-e EVENT— Event type to profile:cpu— CPU cycles (default)wall— Wall-clock timealloc— Object allocationslock— Lock contention- Hardware counters:
branch-misses,cache-misses,cycles, etc.
-o FORMAT— Output format (jfrrecommended for ap-query)-f FILE— Output file path--cstack MODE— Native stack capture mode (fp, dwarf, lbr)-i INTERVAL— Sampling interval in nanoseconds
Hardware Counter Events
Async-profiler can record hardware performance counters beyond the standard CPU/wall/alloc/lock events. When you record with a hardware event (e.g.,asprof -e branch-misses), ap-query automatically discovers and uses that event name from the JFR metadata.
ap-query supports:
branch-missescache-missescyclesinstructions- Any other event supported by async-profiler and your CPU
jdk.ActiveSetting metadata and normalized (e.g., cpu-clock becomes cpu).
Integration with ap-query
Once you’ve recorded a JFR profile with async-profiler, ap-query provides full-featured analysis:- Automatic event detection: ap-query reads event metadata from JFR and auto-selects the appropriate event type
- Timeline analysis: Per-sample timestamps enable
timelinecommand and--from/--towindowing - Multi-event support: Single JFR files can contain multiple event types (cpu, wall, alloc, lock)
- Thread information: Thread names and IDs are preserved from JFR metadata
- Line numbers: Source line numbers are available for
linescommand
Example Workflow
Why JFR over Collapsed Text?
While ap-query accepts collapsed-stack text format (asprof -o collapsed), JFR is strongly preferred:
| Feature | JFR | Collapsed Text |
|---|---|---|
| Event types | Preserved (cpu/wall/alloc/lock) | Lost (all merged) |
| Timeline | Full per-sample timestamps | No timestamps |
--from/--to | Supported | Not supported |
| Line numbers | Preserved | Depends on -l flag |
| Thread info | Preserved | Optional (-t) |
| Multi-event | Single file | Separate files needed |
asprof -o jfr.
Getting async-profiler
Download the latest release from: https://github.com/async-profiler/async-profiler/releases Extract and use theasprof script: