When async-profiler -e cpu on a JVM inside a Docker container reports perf events unavailable, or shows no kernel frames, why does that happen, and what are your options?
answer
- seccomp and perf_event_paranoid
- silent fallback to ctimer
- user-space only perf_events
- privileged profiling from the host
- fdtransfer hands over descriptors
basics
~20 sDocker's default seccomp profile or a restrictive perf_event_paranoid blocks perf_event_open, so async-profiler falls back to ctimer, which cannot collect kernel stacks. Accept ctimer, force user-space perf_events, profile from the host as a privileged user, or relax seccomp deliberately.
solid answer
~40 sThe `cpu` engine needs the `perf_event_open` system call, and a container often cannot make it: Docker's default seccomp profile restricts it, `kernel.perf_event_paranoid` may be in restricted mode, or the hypervisor may not virtualise performance counters. async-profiler 4.x tests this with a dummy event and transparently falls back to `ctimer`, so the usual symptom is not an error but a profile without kernel frames. If `perf_events` work but kernel symbols are hidden by `kernel.kptr_restrict`, it stays on `perf_events`, warns, and also shows no kernel stacks. My options, in order of intrusiveness: accept `ctimer` when the question is about Java code; use `-e cpu-clock --all-user` where user-space `perf_events` are allowed; run `asprof` on the host as a privileged user with the host PID, or `--fdtransfer`; or change the container's seccomp profile, which is a security decision.
code
bash · 10 lines# inside the container: user-space perf_events, no kernel frames
asprof -e cpu-clock --all-user -d 30 -f cpu.html <pid>
# on the host, as a privileged user, with the host-namespace PID
docker top <container>
# the target JVM writes the file, so the path is in the container's filesystem
asprof -d 30 -f /tmp/cpu.html <host-pid>
# on the host, as a privileged user: a helper provides perf_events to the target
asprof --fdtransfer -d 30 -f /tmp/cpu.html <host-pid>go deeper
Recall that containers often block the system call async-profiler's cpu mode relies on, and that it has fallback CPU engines.
Explain the difference between cpu, ctimer and itimer, and that ctimer and itimer cannot show kernel frames.
Diagnose a quiet fallback versus hidden kernel symbols, and pick the least intrusive option the investigation needs, from ctimer to host-side profiling.
Decide whether the platform offers a node-level profiling procedure rather than letting teams relax seccomp per container, and who approves it.
## Why `cpu` mode is fragile in a container async-profiler's most accurate CPU engine, `-e cpu`, is built on Linux **`perf_events`**: it opens one perf event descriptor per thread, each configured to deliver a signal after an interval of that thread's CPU time. It is the only engine that can collect **kernel stack traces**. It depends on the `perf_event_open` system call, and its documentation lists the typical reasons that call fails: 1. `/proc/sys/kernel/perf_event_paranoid` is set to a **restricted** level (2 or higher). 2. **seccomp** forbids `perf_event_open` in the container, as Docker's default profile does. 3. The host runs under a **hypervisor** that does not virtualise performance counters. 4. The platform does not support the API at all. ## What you actually see The symptom is often **quiet**. When you ask for `-e cpu`, async-profiler 4.x creates a dummy perf event first; if that fails, it **falls back transparently to `ctimer`**, an engine based on `timer_create`. The flame graph still looks reasonable, because `ctimer` also measures CPU time, but: - it has **no kernel frames** at all; - its resolution is limited to the kernel's jiffy, so the minimum supported interval is 10 ms or 4 ms for a kernel `HZ` of 100 or 250. The engine actually used is recorded in JFR output, which is how to confirm a fallback after the fact. A second, different cause produces the same "no kernel frames" picture: `perf_events` work, but kernel symbols are hidden by `kernel.kptr_restrict`. async-profiler then keeps using `perf_events`, prints a warning and omits kernel stacks. Telling the two apart matters, because only the first one changes the engine. Two resource limits produce explicit errors instead: - `perf_event mmap failed: Operation not permitted`: each thread gets an 8 kB buffer, and the total exceeded the locked-memory limit (`ulimit -l` plus `kernel.perf_event_mlock_kb` per CPU core). - Running out of file descriptors: one descriptor per thread can exhaust a low `ulimit -n` in a JVM with thousands of threads. ## The engines side by side | Engine | Kernel stacks | Works in a default container | File descriptors | Resolution | |---|---|---|---|---| | `cpu` (`perf_events`) | yes | usually not | one per thread | high | | `ctimer` (`timer_create`) | no | yes | none | jiffy | | `itimer` (`setitimer`) | no | yes | none | jiffy, uneven between threads | `itimer` is the oldest fallback: only one signal can be delivered to the process at a time and signals are not distributed evenly between running threads, so `ctimer` is generally the better substitute. ## Options, least intrusive first 1. **Accept `ctimer`.** If the question is which Java code burns CPU, a profile without kernel frames answers it. 2. **Force user-space `perf_events`.** Where the paranoid level still allows user-space measurement, `-e cpu-clock --all-user` keeps `perf_events`' accuracy and per-thread fairness without kernel frames. 3. **Profile from the host.** Run `asprof` on the node as a privileged user with the JVM's **host-namespace PID** (from `ps` or `docker top`). It switches to the target's PID and mount namespaces and to the target's user automatically. The container must see `libasyncProfiler.so` at the same absolute path, or you pass `--libpath`. 4. **Hand over descriptors with `--fdtransfer`.** Run from the host as a privileged user, it starts a background process that opens the perf events and provides them to the target, which lacks that access itself. 5. **Relax the container's restrictions.** `--security-opt seccomp=unconfined`, possibly with `--cap-add SYS_ADMIN`, restores `perf_event_open` but widens what the container can do; that belongs to whoever owns the platform's security posture, not to one incident. ## Reading a profile taken with a fallback engine A `ctimer` or `itimer` profile is still a CPU profile, and it is read the same way, with three adjustments: - **Kernel time is folded away.** A thread busy in a system call shows its Java and native frames, with no orange kernel frames above them, so heavy system-call or page-fault time is visible only as a wide native frame such as a libc call. - **Short intervals are not available.** An interval below the jiffy is under the minimum the engine supports, so it buys no extra resolution. - **Attribution between threads is less even with `itimer`.** Because its signals are not distributed evenly, per-thread proportions from an `itimer` profile are less trustworthy than from `ctimer` or `cpu`. ## Choosing Decide by what the investigation needs. Kernel frames matter when the hot path is in system calls, page faults or locking in the kernel; otherwise option 1 or 2 is enough. For a recurring need, a node-level procedure for options 3 and 4 is cheaper than weakening every container's sandbox.
- How do you tell a ctimer fallback from hidden kernel symbols when a containerised JVM's profile shows no kernel frames?Check which engine ran. A JFR recording from async-profiler records the actual engine, so `perf_events` versus `ctimer` is visible after the fact. If `perf_events` ran, the missing frames come from hidden kernel symbols, which async-profiler warns about; if `ctimer` ran, `perf_event_open` was unavailable and no engine change will add kernel frames without lifting that restriction.
- Why can async-profiler's cpu engine fail on a JVM with thousands of threads even outside a container?It opens one perf event descriptor and an 8 kB buffer per thread. Thousands of threads can exhaust a low `ulimit -n`, or push the buffers past the locked-memory limit and produce `perf_event mmap failed`. Raising the limits fixes it, and a privileged process is not subject to the locked-memory limit.
- If -e cpu runs without an error, perf_events must be in use.
- ctimer collects kernel stacks just as the cpu engine does.
- Inside a container you profile the JVM by its container PID from the host.
- Disabling seccomp is the only way to profile a containerised JVM.
- itimer is as accurate and as fair between threads as cpu.