/clojure-libraries:clj-async-profilerfrom clojure-libraries
Low-overhead sampling profiler for Clojure/JVM, built on async-profiler, that produces interactive flamegraphs and diffgraphs
Ships in clojure-libraries. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Embedded, high-precision sampling profiler for Clojure. Wraps async-profiler (bundled, v4.5) and renders interactive HTML flamegraphs. Overhead is low enough for production.
Repository: https://github.com/clojure-goes-fast/clj-async-profiler Knowledge base: https://clojure-goes-fast.com/kb/profiling/clj-async-profiler/ Latest version: 1.9.0 (2026-10-18) License: EPL (clj-async-profiler), Apache-2.0 (async-profiler)
;; deps.edn — put it in a :dev/:profile alias, not in production deps
{:aliases
{:profile
{:extra-deps {com.clojure-goes-fast/clj-async-profiler {:mvn/version "1.9.0"}}
:jvm-opts ["-Djdk.attach.allowAttachSelf"
"-XX:+UnlockDiagnosticVMOptions"
"-XX:+DebugNonSafepoints"]}}}
;; Leiningen project.clj
:profiles {:dev {:dependencies [[com.clojure-goes-fast/clj-async-profiler "1.9.0"]]
:jvm-opts ["-Djdk.attach.allowAttachSelf"]}}
Ad-hoc from the CLI:
clj -J-Djdk.attach.allowAttachSelf \
-Sdeps '{:deps {com.clojure-goes-fast/clj-async-profiler {:mvn/version "1.9.0"}}}'
| Requirement | Detail |
|---|---|
| OS | Linux (x64, aarch64), macOS (universal). Others: build async-profiler yourself |
| JVM | A JDK, not a JRE (needs jdk.attach module) |
| Clojure | 1.10+ |
| JDK 9+ | -Djdk.attach.allowAttachSelf to profile the current process |
| Babashka | Not supported — requires a JVM with the attach API |
Linux kernel settings (needed for full native/kernel stacks):
sudo sysctl -w kernel.perf_event_paranoid=1
sudo sysctl -w kernel.kptr_restrict=0
:interval ns (default 1ms) and records the stack. No instrumentation, no safepoint bias.results/ as <yyyyMMdd_HHmmss>-NN-<event>-collapsed.txt (flamegraphs: ...-NN-<event>-flamegraph.html).stop increments an integer ID. Use the ID (or the stacks file path) to regenerate flamegraphs or diff runs.:cpu (default), :alloc, :lock, :wall, :itimer, hardware counters, and so on./tmp/clj-async-profiler/{results,internal} by default. -Dclj-async-profiler.output-dir=/path sets the parent: files go to /path/clj-async-profiler/results/.All functions are in clj-async-profiler.core.
(require '[clj-async-profiler.core :as prof])
profile (macro)Starts the profiler, runs the body, stops it and writes a flamegraph. Returns the body's value. An optional first-argument map holds start and stop options.
(prof/profile (dotimes [_ 1000] (reduce + (range 100000))))
(prof/profile {:event :alloc :title "alloc: parse"}
(dotimes [_ 100] (parse-big-file)))
Profile for long enough to collect thousands of samples (at least about 1s of CPU). Wrap short operations in dotimes.
profile-forProfiles for N seconds, whatever the process is doing. Good for load tests or for a running server.
(prof/profile-for 30) ; => flamegraph File
(prof/profile-for 10 {:event :wall :threads true})
(prof/start) ; => "Profiling started\n" (nil on some macOS setups)
(prof/start {:event :alloc}) ; throws ex-info if async-profiler refuses to start
;; ... workload ...
(prof/stop) ; => java.io.File of the flamegraph
(prof/stop {:generate-flamegraph? false}) ; => collapsed stacks File only
stop throws ex-info (carrying the status message) if the profiler isn't running.
status(prof/status) ; => "Profiling is running for 12 seconds\n" or "Profiler is not active\n"
(prof/status {:pid 1234})
list-event-types(prof/list-event-types) ; prints the list and returns event keywords
(prof/list-event-types {:silent? true}) ; => (:cpu :alloc :lock :wall :itimer ...)
Used by start, profile, profile-for and print-jvm-opt-for-startup-profiling.
| Option | Default | Meaning |
|---|---|---|
:pid | current process | Attach to another local JVM by PID |
:event | :cpu | Event to sample (see list-event-types) |
:interval | 1000000 | Sampling interval in ns (for :alloc, in bytes) |
:threads | false | Split stacks per thread (thread frame at the root) |
:features | none | [:vtable :comptask] or :all. :vtable shows megamorphic call targets; :comptask shows JIT tasks |
:framebuf | 1000000 | Frame buffer size. Increase it if you see frame_buffer_overflow |
Used by stop, profile, profile-for, generate-flamegraph and generate-diffgraph.
| Option | Meaning |
|---|---|
:generate-flamegraph? | stop only; default true |
:title | Flamegraph page title |
:config | Map that preconfigures the flamegraph UI (see below) |
:transform | Clojure fn applied to each stack string before rendering. Return nil to drop the stack |
:config{:transforms [{:type :remove :what "frame_buffer_overflow"}
{:type :filter :what "my.app"} ; keep only matching stacks
{:type :replace :what #"(;manifold.deferred[^;]+)+"
:replacement ";manifold.deferred/..."}
{:type :highlight :what "Reflector" :color "#ff0000"}]
:highlight "Reflector" ; quick-search highlight
:reverse true ; icicle / "who calls the hot leaf" view
:sort-by :name} ; or by width
Transform keys: :type (one of :filter :remove :replace :highlight), :what (string or regex), :replacement (string, required for :replace), :enabled (bool), :color (string). Invalid transforms fail an assert at render time.
The UI also has a denoise toggle (since 1.8.0). It strips --NNNN gensym suffixes and clojure.core.protocols/fn frames, which makes Clojure stacks easier to read.
Use Save config in the flamegraph UI to persist transforms. Later flamegraphs reuse them.
;; Re-render an earlier run by ID or stacks file, with different options
(prof/generate-flamegraph 3 {:config {:reverse true}})
(prof/generate-flamegraph "/tmp/clj-async-profiler/results/20261201_120000-03-cpu-collapsed.txt" {})
;; Before/after comparison. Both runs must use the same :event, else ex-info
(prof/generate-diffgraph 1 2 {})
(prof/serve-ui 8080) ; http://localhost:8080 lists all results
(prof/serve-ui "0.0.0.0" 8080) ; expose on all interfaces (careful)
The UI lists flamegraphs, diffgraphs and raw files. You can also generate a diff from it.
(prof/clear-results) ; wipe the results dir
(prof/set-default-profiling-options {:interval 500000 :threads true})
(reset! prof/async-profiler-agent-path "/opt/async-profiler/lib/libasyncProfiler.so")
Profiles a JVM from its first instruction, by passing an -agentpath option to the target JVM.
(prof/print-jvm-opt-for-startup-profiling {:event :cpu})
;; Prints "-agentpath:/tmp/.../libasyncProfiler.so=start,event=cpu,file=...,collapsed"
;; Launch the target with that option (prefix with -J for the clj CLI). Once it exits:
(prof/generate-flamegraph "<printed file>" {})
(require '[clj-async-profiler.flamebin :as flamebin])
(flamebin/upload-flamegraph 3 {}) ; private: URL includes a read token
(flamebin/upload-flamegraph 3 {:public? true}) ; listed publicly
(flamebin/upload-diffgraph 1 2 {})
Uploads send stack frames (class, function and namespace names) to a third-party service. Don't upload profiles from proprietary code without approval.
(defn work [] (reduce + (map #(* % %) (range 1e5))))
(prof/profile {:title "work"} (dotimes [_ 2000] (work)))
(prof/profile (dotimes [_ 1000] (old-impl data))) ; run 1
(prof/profile (dotimes [_ 1000] (new-impl data))) ; run 2
(prof/generate-diffgraph 1 2 {})
Run IDs reset when the JVM restarts. Pass file paths when comparing runs across sessions.
(prof/profile {:event :alloc} (dotimes [_ 100] (into [] (map str) (range 1e5))))
Frame width is sampled allocation volume. Look for boxing, seq realization and str concatenation.
(prof/profile {:event :wall :threads true}
@(future (Thread/sleep 500) (slurp "https://example.com")))
:cpu hides time spent blocked. Use :wall for latency problems. Combine it with :threads true to separate thread pools.
(prof/profile {:event :lock} (run-concurrent-workload))
(prof/profile-for 60 {:pid 41234 :event :cpu}) ; PID from `jps -l`
The target must run under the same user, with a compatible JDK. Attaching to another process doesn't need allowAttachSelf.
(prof/profile {:config {:highlight "clojure.lang.Reflector"}} (hot-code))
Wide Reflector or Numbers.ops frames point to missing type hints. Use (set! *warn-on-reflection* true) and *unchecked-math* :warn-on-boxed to find the call sites.
(prof/generate-flamegraph 5 {:config {:transforms [{:type :filter :what "my.app."}]
:reverse true}})
(prof/profile {:transform (fn [s] (when-not (re-find #"nrepl" s)
(clojure.string/replace s #"\$fn__\d+" "")))}
(work))
my.ns/my-fn, my.ns/my-fn/fn--1234 (anonymous). Denoise strips the gensym suffix.| Symptom | Cause / Fix |
|---|---|
Can not attach to current VM | Missing -Djdk.attach.allowAttachSelf. (System/getProperty "jdk.attach.allowAttachSelf") must return "", not nil |
package jdk.attach not in java.base / ClassNotFoundException VirtualMachine | Running on a JRE. Install a full JDK |
UnsatisfiedLinkError loading libasyncProfiler | Unsupported platform or arch. Build async-profiler and reset! async-profiler-agent-path |
ex-info "Profiler is not active" from stop | start was never called, or the run already stopped |
ex-info from start (e.g. Event ... is not supported) | Unsupported event on this OS or kernel. Check list-event-types and perf_event_paranoid |
ex-info "Profiler runs must be of the same event type." | generate-diffgraph was given runs with different :events |
Stacks show only libjvm.so frames | Install JDK debug symbols (e.g. openjdk-*-dbg) |
| Small or inlined methods missing | Add -XX:+UnlockDiagnosticVMOptions -XX:+DebugNonSafepoints |
frame_buffer_overflow frames | Increase :framebuf, or remove them with a :remove transform |
| Very few samples / noisy graph | Profile longer or loop the body. Aim for over 1000 samples |
Guard profiling in code that may run without the agent:
(try (prof/profile (work))
(catch Exception e
(println "Profiling unavailable:" (ex-message e))
(work)))
:interval gives more detail at the cost of more overhead.:threads true produces larger output and slower rendering. Use it only when per-thread data matters.profile-for with bounded durations.:alloc interval is in bytes. Raise it (e.g. 10000000) to reduce overhead in allocation-heavy code..so to a temp directory at first use./tmp, which may be cleared on reboot. Set clj-async-profiler.output-dir to keep results.:cpu uses itimer-based sampling. Hardware counters are unavailable.--cap-add SYS_ADMIN or a seccomp profile allowing perf_event_open; otherwise use :itimer.:wall to see parked virtual threads.examples.clj uses bb to drive a JVM through clojure.What SKILL.md sets; muted values are Claude Code's defaults
npx claudepluginhub hugoduncan/library-skills --plugin clojure-libraries10 skills in this plugin
/clayREPL-friendly data visualization and literate programming for Clojure with Kindly conventionOther plugins' skills for the same job