changeset 8:300a027f1322

Clean up some code
author Omair Majid <omajid@redhat.com>
date Tue, 09 Sep 2014 13:01:29 -0400
parents 60308ea13b7d
children 688cf6a4a7fa
files agent/com/redhat/omajid/Agent.java agent/com/redhat/omajid/ProfilerInstrumentor.java
diffstat 2 files changed, 65 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/agent/com/redhat/omajid/Agent.java	Fri Aug 29 12:20:25 2014 -0400
+++ b/agent/com/redhat/omajid/Agent.java	Tue Sep 09 13:01:29 2014 -0400
@@ -2,58 +2,73 @@
 
 import java.lang.instrument.Instrumentation;
 import java.lang.instrument.UnmodifiableClassException;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.atomic.AtomicLong;
 
 public class Agent {
 
     public static void premain(String agentArgs, Instrumentation instrumentation) {
-        System.out.println("premain() called");
-        installProfiler(agentArgs, instrumentation);
-        instrumentAlreadyLoadedClasses(instrumentation);
-
-        addShutdownHookToPrintStatsOnEnd();
+        System.out.println("AGENT: premain() called");
+        doStuff(agentArgs, instrumentation);
     }
 
     public static void agentmain(String agentArgs, Instrumentation instrumentation) {
-        System.out.println("agentmain() called");
-        installProfiler(agentArgs, instrumentation);
-        instrumentAlreadyLoadedClasses(instrumentation);
-
-        addShutdownHookToPrintStatsOnEnd();
+        System.out.println("AGENT: agentmain() called");
+        doStuff(agentArgs, instrumentation);
     }
 
-    private static void installProfiler(String mechanism, Instrumentation instrumentation) {
+    private static void doStuff(String type, Instrumentation instrumentation) {
+        long start = System.nanoTime();
+        addShutdownHookToPrintStatsOnEnd();
+        ProfilerInstrumentor profiler = installProfiler(type, instrumentation);
+
+        System.out.println("AGENT: instrumenting already loaded classes");
+        instrumentAlreadyLoadedClasses(instrumentation, profiler);
+        long end = System.nanoTime();
+        System.out.println("AGENT: done in : " + (end - start) + "ns");
+    }
+
+    private static ProfilerInstrumentor installProfiler(String mechanism, Instrumentation instrumentation) {
         ProfilerInstrumentor profiler = null;
         if (mechanism == null || mechanism.equals("") || mechanism.equals("javassist")) {
-            System.out.println("Using javassist");
+            System.out.println("AGENT: Using javassist");
             profiler = new ProfileUsingJavassist();
         } else {
-            System.out.println("Using asm");
+            System.out.println("AGENT: Using asm");
             profiler = new ProfileUsingAsm();
         }
         instrumentation.addTransformer(profiler, true);
+        return profiler;
     }
 
-    private static void instrumentAlreadyLoadedClasses(Instrumentation instrumentation) {
+    private static void instrumentAlreadyLoadedClasses(Instrumentation instrumentation, ProfilerInstrumentor profiler) {
+        long start = System.nanoTime();
+
+        List<Class<?>> toTransform = new ArrayList<>();
+
         for (Class<?> klass : instrumentation.getAllLoadedClasses()) {
-
-            if (klass.getName().startsWith(ProfileUsingAsm.class.getName())) {
+            if (!instrumentation.isModifiableClass(klass)) {
+                continue;
+            }
+            if (!profiler.shouldInstrument(klass)) {
                 continue;
             }
 
-            if (!instrumentation.isModifiableClass(klass)) {
-                continue;
-            }
+            toTransform.add(klass);
+        }
 
-            Class<?>[] classes = new Class<?>[] { klass };
+        if (toTransform.size() > 0) {
+            System.out.println("Retransforming " + toTransform.size() + " classes");
             try {
-                instrumentation.retransformClasses(classes);
+                instrumentation.retransformClasses(toTransform.toArray(new Class<?>[toTransform.size()]));
             } catch (UnmodifiableClassException e) {
                 e.printStackTrace();
             }
-
         }
+        long end = System.nanoTime();
+        System.out.println("Retansforming took: " + (end - start) + "ns");
     }
 
     private static void addShutdownHookToPrintStatsOnEnd() {
--- a/agent/com/redhat/omajid/ProfilerInstrumentor.java	Fri Aug 29 12:20:25 2014 -0400
+++ b/agent/com/redhat/omajid/ProfilerInstrumentor.java	Tue Sep 09 13:01:29 2014 -0400
@@ -26,12 +26,6 @@
         ignorePackageRegexps.add("org.objectweb.asm\\..*");
     }
 
-    public static void main(String[] args) {
-        for (String exp : ignorePackageRegexps) {
-            System.out.println(exp);
-        }
-    }
-
     @Override
     public byte[] transform(ClassLoader loader, String className,
             Class<?> classBeingRedefined,
@@ -39,21 +33,44 @@
             throws IllegalClassFormatException {
 
         className = className.replace('/', '.');
+        if (!shouldInstrument(className)) {
+            return null;
+        }
+
+        // System.out.println("transforming '" + className + "'");
+
+        return transform(className, classfileBuffer);
+    }
+
+    public boolean shouldInstrument(Class<?> clazz) {
+        if (clazz.isArray()) {
+            return false;
+        }
+
+        // TODO what to do with anonymous classes?
+
+        return shouldInstrument(clazz.getName());
+    }
+
+    public boolean shouldInstrument(String className) {
+        if (className == null) {
+            return true;
+        }
         for (String regex : ignorePackageRegexps) {
             if (className.matches(regex)) {
-                return null;
+                return false;
             }
         }
 
         if (className.startsWith(ProfilerInstrumentor.class.getName())) {
-            return null;
+            return false;
         }
 
-        // System.out.println("transforming '" + className + "'");
-        
-        return transform(className, classfileBuffer);
+        return true;
     }
 
+
     public abstract byte[] transform(String className, byte[] classBytes);
+
 }