allocation-tracker
allocation-tracker copied to clipboard
Add support for dynamically attaching the allocation-tracker-agent.
Introduced an agentmain method and enabling retransform support for the agent allows to dynamically attach the allocation-tracker.
The following is an example for dynamically attaching a the agent locally.
The Application that produces some garbage: AllocationTrackerExample
package de.tutorials.training;
import java.lang.management.ManagementFactory;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/**
* @author Thomas Darimont
*/
public class AllocationTrackerExample {
public static void main(String[] args) throws Exception {
System.out.println(ManagementFactory.getRuntimeMXBean().getName());
System.out.println("Started");
Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> new Grabage(), 1, 2, TimeUnit.SECONDS);
}
static class Grabage {
Grabage() {
System.out.println("Created new Garbage@" + new Date());
}
}
}
The AgentAttacher helper performs the dynamic attachment of the agent programmatically by using classes from JDK tools.jar.
run with the following arguments: $PATH_TO_AGENT_JAR% %PID_OF_ATTACH_TARGET% %PACKAGE_PREFIX%
e.g.:
"D:\development\java\repos\git\github\allocation-tracker\target\allocation-tracker-agent-0.0.1-SNAPSHOT.jar" 5012 de.tutorials.training
package de.tutorials.training;
import com.sun.tools.attach.VirtualMachine;
/**
* @author Thomas Darimont
*/
public class AgentAtacher {
public static void main(String[] args) {
assert args.length == 3;
String agentPath = args[0];
String pid = args[1];
String packages = args[2];
try {
VirtualMachine vm = VirtualMachine.attach(pid);
vm.loadAgent(agentPath, packages);
vm.detach();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Hello Thomas, thank you for your interest and contribution! This is a really useful feature, and I would like to merge it with a few changes we should discuss: a) The tools jar dependency will not work in jdk9. Not sure how to tackle it however. b) with the addition of agentmain, it looks like it is work extracting and OO-fying the code a bit more. I dont really like that agentmain invokes premain. Also the check for the agent being loaded already looks like this should be a stateful object instance which is created by agentmain or premain and then configured. c) the retransform logic duplicates the candidate checking from https://github.com/codecentric/allocation-tracker/blob/master/src/main/java/de/codecentric/performance/agent/allocation/AllocationTrackerClassFileTransformer.java#L36 - yes its only minor, but if one day someone would change how the naming works (like regex) this could get out of sync. d) we should document how to do attach (using a jdk) in the readme.
what do you think?
Hi Fabian,
I'm glad you like it - I just stumbled upon your toy and gave it a quick spin :)
a) yes you're right but I don't think that this will be a problem for a while ;-) Nevertheless one should have an eye on how others are going to tackle this problem, e.g.: https://issues.jenkins-ci.org/browse/JENKINS-25993
b) Yes, I realized that too - I didn't want to mess around with the code to much in the first place - to reduce the example to the bare minimum. I can give it another round of oo-love :)
c) see b) :)
d) Yep - the plan was actually to add the attaching capability to the allocation-tracker.jar itself so that people could just attach the agent with the agent...
Cheers, Thomas
ad a) I don't know whether it will always stay that way but as I found out here: https://gist.github.com/thomasdarimont/dbb6796505eeaa692c91 the stuff that was in the tools.jar is available from within jrunscript. So it might be worthwhile so check how those classes are provided in that environment.
i have 9 running on my mac. I will try to come up with a maven config which includes the attach dependencies for you. I am fine with making the change bigger to make it more OO. i prefer that over a refactoring step afterwards.
Attaching itself would be a fancy feature :) I once considered making the whole control controlled by attaching, rather than by jmx.
@thomasdarimont I created #16 - does that work for you? then I will merge it.