Skip to content

Commit

Permalink
Make the profiler jvm-agent work with Java 6
Browse files Browse the repository at this point in the history
  • Loading branch information
omajid committed Dec 11, 2014
1 parent 6b98b2f commit 4cae844
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 13 deletions.
17 changes: 14 additions & 3 deletions vm-profiler/jvm-agent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public void stopProfiling() {
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;
Expand Down Expand Up @@ -171,13 +171,25 @@ private void writeProfilingResultsToDisk() {
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class ProfileRecorder {
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

Expand All @@ -57,10 +57,10 @@ public class ProfileRecorder {
* <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;
}

Expand Down Expand Up @@ -127,4 +127,4 @@ private void addData(String dataName, long time) {
public Map<String, AtomicLong> getData() {
return profileData;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -97,7 +98,22 @@ private static void invokeMain(Instrumentation instrumentation) {
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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void stopProfilingInstrumentsAllCode() throws Exception {
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);

Expand All @@ -151,7 +151,7 @@ public void stopProfilingSavesProfilingResultsToDisk() throws Exception {
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);

Expand Down

0 comments on commit 4cae844

Please sign in to comment.