clojure-goes-fast / clj-async-profiler

Embedded high-precision Clojure profiler
http://clojure-goes-fast.com/kb/profiling/clj-async-profiler/
411 stars 17 forks source link

Error: Can not attach to current VM #36

Closed vkurup closed 3 months ago

vkurup commented 3 months ago

Hi, I'm guessing I'm doing something wrong, but I haven't figured out what yet. I'm trying to run through the first steps of the tutorial on a M3 Macbook Pro, but I get the Can not attach to current VM error. I've followed all of the steps in the tutorial:

~/dev/profiling-tutorial $ uname -v
Darwin Kernel Version 23.5.0: Wed May  1 20:17:33 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6031

~/dev/profiling-tutorial $ java -version
openjdk version "17.0.10" 2024-01-16
OpenJDK Runtime Environment Temurin-17.0.10+7 (build 17.0.10+7)
OpenJDK 64-Bit Server VM Temurin-17.0.10+7 (build 17.0.10+7, mixed mode)

~/dev/profiling-tutorial $ cat deps.edn
{:paths ["src"]
 :deps {com.clojure-goes-fast/clj-async-profiler {:mvn/version "1.2.0"}}}

~/dev/profiling-tutorial $ clj -J-Djdk.attach.allowAttachSelf
[Rebel readline] Type :repl/help for online help info
user=> (require 'tutorial.binary-trees)
nil
user=> (require '[clj-async-profiler.core :as prof])
nil
user=> (prof/profile (tutorial.binary-trees/main 20))
Execution error (IOException) at sun.tools.attach.HotSpotVirtualMachine/<init> (HotSpotVirtualMachine.java:76).
Can not attach to current VM
user=>
alexander-yakushev commented 3 months ago

Hi, this error usually happens when jdk.attach.allowAttachSelf is not enabled, but you are definitely trying to enable it. Please check if (System/getProperty "jdk.attach.allowAttachSelf") in the REPL returns "" (an empty string) for you.

vkurup commented 3 months ago

Hmm, it actually returns nil:

user=> (System/getProperty "jdk.attach.allowAttachSelf")
nil

I also tried to set it via setProperty in the REPL (to "1" and "true") but neither attempt seemed to get the profiler to attach.

alexander-yakushev commented 3 months ago

Yeah, chaging it at runtime wouldn't help. Try doing it like this:

;; deps.edn
{:paths ["src"]
 :deps {com.clojure-goes-fast/clj-async-profiler {:mvn/version "1.2.0"}}
 :aliases {:attach {:jvm-opts ["-Djdk.attach.allowAttachSelf"]}}
}

Then clj -A:attach and then so on.

Could it be by chance that your clj binary is some sort of alias/wrapper that doesn't pass all the arguments down?

vkurup commented 3 months ago

Ahh, that was it! I'm not sure what my clj binary is doing, but switching to the clojure binary fixes my issue. Thanks for the help debugging my setup!

alexander-yakushev commented 3 months ago

No probs, that was a tricky one.