changeset 1614:37bcf1f12fd8

Make the profiler jvm-agent work with Java 6 Reviewed-by: vanaltj Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2014-December/012210.html
author Omair Majid <omajid@redhat.com>
date Wed, 10 Dec 2014 19:47:50 -0500
parents b7cf43343de2
children dee72f587e04
files vm-profiler/jvm-agent/pom.xml vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/InstrumentationControl.java vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/ProfileRecorder.java vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/ProfilerAgent.java vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/ProfilerInstrumentor.java vm-profiler/jvm-agent/src/test/java/com/redhat/thermostat/vm/profiler/agent/jvm/InstrumentationControlTest.java
diffstat 6 files changed, 52 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/vm-profiler/jvm-agent/pom.xml	Wed Dec 10 15:05:03 2014 -0500
+++ b/vm-profiler/jvm-agent/pom.xml	Wed Dec 10 19:47:50 2014 -0500
@@ -48,10 +48,13 @@
   <name>JVM-side agent for the Thermostat VM Profiler</name>
 
   <!--
-    This is a stand alone agent for the JVM. It runs outside of
-    thermostat and does not know about thermostat.  It is not OSGi, and
-    has no dependencies on OSGi.
+    This is a stand alone agent for the JVM. It is meant to be loaded
+    into target JVMs. It runs outside of thermostat and does not know
+    about thermostat. It is not OSGi, and has no dependencies on OSGi
+    and does not know about OSGi. It must be compiled with the target
+    bytecode format for the oldest supported JVM version.
   -->
+
   <build>
     <plugins>
       <plugin>
@@ -66,6 +69,14 @@
           </archive>
         </configuration>
       </plugin>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <configuration>
+          <source>1.6</source>
+          <target>1.6</target>
+        </configuration>
+      </plugin>
     </plugins>
   </build>
 
--- a/vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/InstrumentationControl.java	Wed Dec 10 15:05:03 2014 -0500
+++ b/vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/InstrumentationControl.java	Wed Dec 10 19:47:50 2014 -0500
@@ -125,7 +125,7 @@
     private void retransformAlreadyLoadedClasses(Instrumentation instrumentation, ProfilerInstrumentor profiler) {
         long start = System.nanoTime();
 
-        List<Class<?>> toTransform = new ArrayList<>();
+        List<Class<?>> toTransform = new ArrayList<Class<?>>();
 
         for (Class<?> klass : instrumentation.getAllLoadedClasses()) {
             boolean skipThisClass = false;
@@ -171,13 +171,25 @@
         try {
             ResultsFile resultsFile = resultsFileCreator.get();
             String path = resultsFile.getPath();
-            try (BufferedWriter out = resultsFile.getWriter()) {
+            BufferedWriter out = null;
+            try {
+                out = resultsFile.getWriter();
                 Map<String, AtomicLong> data = recorder.getData();
+                System.out.println("AGENT: Writing " + data.size() + " results to: " + path);
                 for (Map.Entry<String, AtomicLong> entry : data.entrySet()) {
                     out.write(entry.getValue().get() + "\t" + entry.getKey() + "\n");
                 }
                 resultsWrittenToDisk = true;
                 lastResults = path;
+            } finally {
+                try {
+                    if (out != null) {
+                        out.close();
+                    }
+                } catch (IOException e) {
+                    // well, we are screwed
+                    e.printStackTrace();
+                }
             }
         } catch (IOException e) {
             e.printStackTrace();
--- a/vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/ProfileRecorder.java	Wed Dec 10 15:05:03 2014 -0500
+++ b/vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/ProfileRecorder.java	Wed Dec 10 19:47:50 2014 -0500
@@ -48,7 +48,7 @@
     private static final ProfileRecorder profileRecorder = new ProfileRecorder();
 
     /** shared between threads */
-    private ConcurrentHashMap<String, AtomicLong> profileData = new ConcurrentHashMap<>();
+    private ConcurrentHashMap<String, AtomicLong> profileData = new ConcurrentHashMap<String, AtomicLong>();
 
     // TODO deal with thread id wrap-around
 
@@ -57,10 +57,10 @@
      * <p>
      * only the thread with the matching thread id is allowed to mutate 'info'
      */
-    private Map<Long, Info> threads = new ConcurrentHashMap<>();
+    private Map<Long, Info> threads = new ConcurrentHashMap<Long, Info>();
 
     final static class Info {
-        public Deque<String> stackFrames = new ArrayDeque<>();
+        public Deque<String> stackFrames = new ArrayDeque<String>();
         public long timeStamp = Long.MIN_VALUE;
     }
 
@@ -127,4 +127,4 @@
     public Map<String, AtomicLong> getData() {
         return profileData;
     }
-}
\ No newline at end of file
+}
--- a/vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/ProfilerAgent.java	Wed Dec 10 15:05:03 2014 -0500
+++ b/vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/ProfilerAgent.java	Wed Dec 10 19:47:50 2014 -0500
@@ -40,6 +40,7 @@
 import java.lang.instrument.Instrumentation;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
+import java.lang.reflect.InvocationTargetException;
 import java.util.jar.JarFile;
 
 /**
@@ -97,7 +98,22 @@
             Object main = constructor.newInstance(instrumentation);
             Method runMethod = klass.getMethod("run");
             runMethod.invoke(main);
-        } catch (ReflectiveOperationException | SecurityException e) {
+        } catch (ClassNotFoundException e) {
+            e.printStackTrace();
+            System.err.println("Unable to initialize agent");
+        } catch (NoSuchMethodException e) {
+            e.printStackTrace();
+            System.err.println("Unable to initialize agent");
+        } catch (IllegalAccessException e) {
+            e.printStackTrace();
+            System.err.println("Unable to initialize agent");
+        } catch (InstantiationException e) {
+            e.printStackTrace();
+            System.err.println("Unable to initialize agent");
+        } catch (InvocationTargetException e) {
+            e.printStackTrace();
+            System.err.println("Unable to initialize agent");
+        } catch (SecurityException e) {
             e.printStackTrace();
             System.err.println("Unable to initialize agent");
         }
--- a/vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/ProfilerInstrumentor.java	Wed Dec 10 15:05:03 2014 -0500
+++ b/vm-profiler/jvm-agent/src/main/java/com/redhat/thermostat/vm/profiler/agent/jvm/ProfilerInstrumentor.java	Wed Dec 10 19:47:50 2014 -0500
@@ -45,7 +45,7 @@
 
 public abstract class ProfilerInstrumentor implements ClassFileTransformer {
 
-    private static List<Pattern> ignorePackageRegexps = new ArrayList<>();
+    private static List<Pattern> ignorePackageRegexps = new ArrayList<Pattern>();
 
     static {
         // jdk packages
--- a/vm-profiler/jvm-agent/src/test/java/com/redhat/thermostat/vm/profiler/agent/jvm/InstrumentationControlTest.java	Wed Dec 10 15:05:03 2014 -0500
+++ b/vm-profiler/jvm-agent/src/test/java/com/redhat/thermostat/vm/profiler/agent/jvm/InstrumentationControlTest.java	Wed Dec 10 19:47:50 2014 -0500
@@ -133,7 +133,7 @@
     public void stopProfilingSavesProfilingResultsToDisk() throws Exception {
         final String DATA_LOCATION = "foobar";
 
-        Map<String, AtomicLong> profileData = new HashMap<>();
+        Map<String, AtomicLong> profileData = new HashMap<String, AtomicLong>();
         profileData.put("foo", new AtomicLong(1));
         when(recorder.getData()).thenReturn(profileData);
 
@@ -151,7 +151,7 @@
     public void vmShutdownSaveDataToDisk() throws Exception {
         final String DATA_LOCATION = "foobar";
 
-        Map<String, AtomicLong> profileData = new HashMap<>();
+        Map<String, AtomicLong> profileData = new HashMap<String, AtomicLong>();
         profileData.put("foo", new AtomicLong(1));
         when(recorder.getData()).thenReturn(profileData);