btrace icon indicating copy to clipboard operation
btrace copied to clipboard

Slow start attaching to jvm with many classes

Open zeocio opened this issue 7 years ago • 3 comments

When running the client attach to an already running jvm loaded with many (>100k) classes it take a long time to go through this logic:

   com.sun.btrace.runtime.ClassInfo.loadExternalClass
   com.sun.btrace.runtime.ClassInfo.<init>
   com.sun.btrace.runtime.ClassCache.get
   com.sun.btrace.runtime.ClassCache.get
   com.sun.btrace.runtime.ClassCache.get
   com.sun.btrace.agent.Client.retransformLoaded
   com.sun.btrace.agent.Main$3.run
   java.util.concurrent.Executors$RunnableAdapter.call
   java.util.concurrent.FutureTask.run
   java.util.concurrent.ThreadPoolExecutor.runWorker
   java.util.concurrent.ThreadPoolExecutor$Worker.run
   java.lang.Thread.run

I made a small change in src/share/classes/com/sun/btrace/agent/Client.java from:

                 ClassCache cc = ClassCache.getInstance();
                 for (Class c : inst.getAllLoadedClasses()) {
                     if (c != null) {
-                        cc.get(c);
+                        //cc.get(c);
                         if (inst.isModifiableClass(c) &&  isCandidate(c)) {
+                            cc.get(c);
                             debugPrint("candidate " + c + " added");
                             list.add(c);
                         }

and saw a very significant speed up. My use cases (clazz and method name matching with regex) worked out fine with the change. Is this something we could do in general for all use cases?

zeocio avatar Mar 02 '19 01:03 zeocio

Are all tests passing with your change? In fact, the call cc.get(c) could be omitted altogether. It was meant to pre-cache the class but it is really not necessary as the cache entry will be create later when required.

jbachorik avatar Mar 02 '19 14:03 jbachorik

All tests passed with the change.

Is cached ClassInfo needed for evaluating class subtype relationship predicates in isCandidate(c)? If not maybe we can have a cmdline option for disabling pre-caching.

There might be additional optimizations for similar use cases where the number of loaded classes is large and the predicates are based on name regex match only.

zeocio avatar Mar 02 '19 19:03 zeocio

@zeocio Thanks for identifying the issue! Fixed now.

jbachorik avatar Apr 09 '19 05:04 jbachorik