changeset 3:ae0095fba08f

Record and print stats
author Omair Majid <omajid@redhat.com>
date Mon, 25 Aug 2014 15:26:39 -0400
parents b225c94e1fa0
children 0ac082b5cb28
files agent/ProfileUsingJavassist.java
diffstat 1 files changed, 61 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/agent/ProfileUsingJavassist.java	Mon Aug 25 11:46:56 2014 -0400
+++ b/agent/ProfileUsingJavassist.java	Mon Aug 25 15:26:39 2014 -0400
@@ -6,6 +6,9 @@
 import java.security.ProtectionDomain;
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
 
 import javassist.ByteArrayClassPath;
 import javassist.CannotCompileException;
@@ -34,25 +37,31 @@
     public static void premain(String agentArgs, Instrumentation instrumentation) {
         System.out.println("premain() called");
         installProfiler(instrumentation);
+        instrumentAlreadyLoadedClasses(instrumentation);
+
+        addShutdownHookToPrintStatsOnEnd();
     }
 
     public static void agentmain(String agentArgs, Instrumentation instrumentation) {
         System.out.println("agentmain() called");
+        installProfiler(instrumentation);
+        instrumentAlreadyLoadedClasses(instrumentation);
 
-        installProfiler(instrumentation);
+        addShutdownHookToPrintStatsOnEnd();
+    }
 
+    private static void installProfiler(Instrumentation instrumentation) {
+        ClassInstrumenter profiler = new ClassInstrumenter();
+        instrumentation.addTransformer(profiler, true);
+    }
+
+    private static void instrumentAlreadyLoadedClasses(Instrumentation instrumentation) {
         for (Class<?> klass : instrumentation.getAllLoadedClasses()) {
 
             if (klass.getName().startsWith(ProfileUsingJavassist.class.getName())) {
                 continue;
             }
 
-            for (String regex : ignorePackageRegexps) {
-                if (klass.getName().matches(regex)) {
-                    continue;
-                }
-            }
-
             if (!instrumentation.isModifiableClass(klass)) {
                 continue;
             }
@@ -67,9 +76,46 @@
         }
     }
 
-    private static void installProfiler(Instrumentation instrumentation) {
-        ClassInstrumenter profiler = new ClassInstrumenter();
-        instrumentation.addTransformer(profiler, true);
+
+    private static void addShutdownHookToPrintStatsOnEnd() {
+        Runtime.getRuntime().addShutdownHook(new Thread() {
+            @Override
+            public void run() {
+                System.out.println("=====");
+                System.out.println("Collected stats");
+                Map<String, AtomicLong> data = Profiler.getInstance().getData();
+                for (Map.Entry<String, AtomicLong> entry : data.entrySet()) {
+                    System.out.format("\t%s\t\t\t%20d%n", entry.getKey(), entry.getValue().get());
+                }
+                System.out.println("=====");
+            }
+        });
+    }
+
+
+    static class Profiler {
+
+        private static final Profiler profiler = new Profiler();
+
+        private ConcurrentHashMap<String,AtomicLong> profileData = new ConcurrentHashMap<>();
+
+        public static Profiler getInstance() {
+            return profiler;
+        }
+
+        public void addData(String dataName, long time) {
+            AtomicLong value = profileData.get(dataName);
+            if (value == null) {
+                value = profileData.putIfAbsent(dataName, new AtomicLong(time));
+            }
+            if (value != null) {
+                value.addAndGet(time);
+            }
+        }
+
+        public Map<String, AtomicLong> getData() {
+            return profileData;
+        }
     }
 
     static class ClassInstrumenter implements ClassFileTransformer {
@@ -88,6 +134,10 @@
                 }
             }
 
+            if (className.startsWith(ProfileUsingJavassist.class.getName())) {
+                return null;
+            }
+
             System.out.println("transforming '" + className + "'");
             ClassPool classPool = ClassPool.getDefault();
 
@@ -102,7 +152,7 @@
                             !Modifier.isNative(modifiers)) {
                         method.addLocalVariable(PREFIX + "StartTime", CtClass.longType);
                         method.insertBefore(PREFIX + "StartTime = System.nanoTime();");
-                        method.insertAfter("System.out.println(\"" + methodName + " took \" + (System.nanoTime() - " + PREFIX + "StartTime));");
+                        method.insertAfter("ProfileUsingJavassist.Profiler.getInstance().addData(\"" + className + "." + methodName + "\", (System.nanoTime() - " + PREFIX + "StartTime));");
                     }
                 }
                 return klass.toBytecode();