GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing raw.githubusercontent.com — raw.githubusercontent.com # Setting up a host
What `jonoffcpu` needs from the kernel, the JVM and the container runtime, and
how to provide it on a Linux host, in Docker, and in the Linux VM behind Docker
Desktop. The agent checks what it needs at startup and fails closed: a host
that lacks a feature gets an error, never a capture with degraded data.
- [Requirements](#requirements)
- [The bundled native libraries](#the-bundled-native-libraries)
- [Kernel settings](#kernel-settings)
- [Applying them to a VM's kernel](#applying-them-to-a-vms-kernel)
- [Running the collector without root](#running-the-collector-without-root)
- [Profiling in Docker](#profiling-in-docker)
- [Docker Desktop on macOS and Windows](#docker-desktop-on-macos-and-windows)
## Requirements
- 64-bit Linux on x86-64 or arm64, with [BTF](https://docs.kernel.org/bpf/btf.html),
eBPF task storage, the `tp_btf/sched_exit_tp` tracepoint, and the
`bpf_send_signal_task` helper. The
agent checks the running kernel and fails closed if any of these is missing.
This checks the two you can see from a shell; it prints a non-zero count on a
suitable kernel:
```sh
test -r /sys/kernel/btf/vmlinux && grep -ac btf_trace_sched_exit_tp /sys/kernel/btf/vmlinux
```
- Privileges to load and attach the BPF programs: the
[capabilities](https://man7.org/linux/man-pages/man7/capabilities.7.html)
`CAP_BPF` and `CAP_PERFMON`, or root. See [Kernel settings](#kernel-settings) for the
sysctls that async-profiler needs alongside them.
- Java 17 or newer for the agent; Java 21 or newer for the correlator.
- On Java 24 and newer, add `--sun-misc-unsafe-memory-access=allow` to the JVM
being profiled and to the correlator. The bundled protobuf codec that reads
and writes the capture stream uses `sun.misc.Unsafe`, which the JDK reports
once per JVM as a terminally deprecated call; the flag silences that warning
and changes nothing else.
- For the sleeping and run-queue split (on by default), a kernel built with
`CONFIG_SCHED_INFO`; see
[Sleeping and run-queue time](off-cpu-profiling.md#sleeping-and-run-queue-time).
## The bundled native libraries
The agent JAR is self-contained. It embeds the JNI bridge, the native
collector, and the patched async-profiler for Linux x86-64 and arm64, each
linked against both [glibc](https://en.wikipedia.org/wiki/Glibc) and
[musl](https://en.wikipedia.org/wiki/Musl) (Alpine), verifies them against a SHA-256
manifest, and extracts them to a private temporary directory at startup.
Nothing needs to be installed on the host. The agent picks the glibc or musl
bundle from the C library mapped into the running JVM and never falls back to
the other one; on an unusual host,
`-Dio.github.jonoffcpu.agent.nativeLibc=glibc` or `=musl` selects it
explicitly. If the temporary directory is mounted `noexec`, point
`-Dio.github.jonoffcpu.agent.nativeWorkDir` at an executable location.
## Kernel settings
jonoffcpu's own eBPF collector needs privileges (`CAP_BPF` and `CAP_PERFMON`, or
root), and nothing else. The settings below are about the *other* half of the
capture: async-profiler runs inside the JVM, usually unprivileged, and the
kernel restricts by default what an unprivileged process may observe. Without
them the capture still completes, but parts of it are degraded — typically
missing kernel frames, truncated native stacks, or no `cpu` event at all.
| Setting | Suggested value | Why |
| --- | --- | --- |
| [`kernel.perf_event_paranoid`](https://docs.kernel.org/admin-guide/sysctl/kernel.html#perf-event-paranoid) | `1` | The gate on `perf_event_open`. The common default `2` lets an unprivileged process measure only its own user space, so async-profiler's `cpu` engine cannot sample kernel stacks; `>= 2` is also the usual reason `perf_event_open` fails outright and the profiler falls back or errors. `1` allows per-process profiling including kernel stacks. `CAP_PERFMON` bypasses the check. |
| [`kernel.kptr_restrict`](https://docs.kernel.org/admin-guide/sysctl/kernel.html#kptr-restrict) | `0` | Kernel symbol addresses in [`/proc/kallsyms`](https://man7.org/linux/man-pages/man5/proc_kallsyms.5.html) read back as zeros unless the reader has `CAP_SYSLOG` (`1`), or for everyone (`2`). Both async-profiler and jonoffcpu's collector symbolize kernel frames from that file, so with addresses hidden the kernel part of a stack stays as raw addresses. |
| [`kernel.perf_event_max_stack`](https://docs.kernel.org/admin-guide/sysctl/kernel.html#perf-event-max-stack) | `1024` | The maximum call-chain depth `perf_events` records, `127` by default, which silently truncates deep JVM native stacks. Raising it only affects async-profiler: jonoffcpu's BPF stack map has a fixed depth of 127. Do not lower it below 127 — the collector's stack map cannot be created if the sysctl is smaller than the map's depth. |
| [`kernel.perf_event_mlock_kb`](https://docs.kernel.org/admin-guide/sysctl/kernel.html#perf-event-mlock-kb) | `2048` | async-profiler mmaps an 8 KB perf buffer per thread, bounded by `ulimit -l` plus this value times the number of CPUs. On a thread-heavy application the default `516` runs out and native stacks are dropped for the remaining threads. |
Apply them for the current boot:
```sh
sudo sysctl -w kernel.perf_event_paranoid=1
sudo sysctl -w kernel.kptr_restrict=0
sudo sysctl -w kernel.perf_event_max_stack=1024
sudo sysctl -w kernel.perf_event_mlock_kb=2048
```
Use `sysctl` rather than `sudo echo 1 > /proc/sys/…`: the redirection is
performed by the calling shell, which is still unprivileged, so that form fails
with "Permission denied" before `sudo` runs. `sudo tee`
(`echo 1 | sudo tee /proc/sys/kernel/perf_event_paranoid`) works as well. To
make the values persist across reboots, put them in
`/etc/sysctl.d/99-jonoffcpu.conf` as `key = value` lines.
In a container, these are host-wide kernel settings: `kernel.perf_event_*` and
`kernel.kptr_restrict` are not namespaced, so set them on the host, not inside
the container. For the same reason `docker run --sysctl` refuses them, since it
accepts only namespaced keys.
### Applying them to a VM's kernel
With Docker Desktop on macOS or Windows, and with Colima, Lima or any other
Linux VM, the kernel that matters is the VM's: `sysctl` on the workstation
changes nothing that the containers can see. Write the values from a privileged
container, which shares the VM kernel's `/proc/sys`:
```sh
docker run --rm --privileged alpine sh -c '
echo 1 > /proc/sys/kernel/perf_event_paranoid
echo 0 > /proc/sys/kernel/kptr_restrict
echo 1024 > /proc/sys/kernel/perf_event_max_stack
echo 2048 > /proc/sys/kernel/perf_event_mlock_kb
echo 0 > /proc/sys/kernel/unprivileged_bpf_disabled'
```
`--privileged` is what makes `/proc/sys` writable; without it the container gets
it read-only, and adding `--cap-add SYS_ADMIN` or
`--security-opt seccomp=unconfined` changes nothing that `--privileged` has not
already granted. The writes affect the whole VM and last until it restarts, so
this is a per-boot step rather than a one-time setup. The commands are listed
one per line on purpose: the last one fails with `EPERM` on a kernel where
`unprivileged_bpf_disabled` already reads `1`, and chaining them with `&&` would
hide the earlier successes behind that failure. It is also the one line that is
optional — see [Running the collector without root](#running-the-collector-without-root).
### Running the collector without root
[`kernel.unprivileged_bpf_disabled`](https://docs.kernel.org/admin-guide/sysctl/kernel.html#unprivileged-bpf-disabled)
`= 0` re-enables the `bpf()` syscall for
callers that hold no BPF capability:
```sh
sudo sysctl -w kernel.unprivileged_bpf_disabled=0
```
It does **not** make jonoffcpu work unprivileged. Unprivileged `bpf()` only ever
permitted socket-filter programs, while the collector loads tracepoint programs
and uses helpers that require `CAP_BPF` plus `CAP_PERFMON`; with those
capabilities the sysctl is not consulted at all. It is worth setting only where
something else in the toolchain trips over the syscall gate. Note that the value
`1` is a one-way latch: once the sysctl reads `1`, the kernel refuses to change
it until the next boot, so a host that has disabled unprivileged BPF that way
has to be rebooted (distributions that default to `2` can be changed at
runtime).
## Profiling in Docker
The agent and the collector both run inside the container with the JVM: the
agent extracts the collector from its JAR and starts it as a child process, and
the eBPF program resolves thread ids inside the target's own PID namespace. So
the container is profiled as it is: neither `--pid=host` nor `--net=host` is
needed, and no kernel headers have to be mounted, because the collector is CO-RE
and reads the kernel's own BTF. General-purpose BPF toolbox images ask for all
of these because they trace the whole host from outside, and because BCC
compiles its programs against kernel headers at runtime. The only case that
needs `--pid=host`, or `--pid=container: |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |