From bb0e2bafbe2e1d4b2b07d33c13c7f419de6fb988 Mon Sep 17 00:00:00 2001 From: Alejandro Rivera Date: Sun, 24 Jan 2016 17:20:13 +0800 Subject: [PATCH 1/3] Agent can now also be loaded dynamically after VM startup. This requires having an `Agent-Class` entry in the MANIFEST, as well as having an `agentmain()` method. --- pom.xml | 1 + src/main/java/com/etsy/statsd/profiler/Agent.java | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/pom.xml b/pom.xml index 228ed22..a4f60c3 100644 --- a/pom.xml +++ b/pom.xml @@ -170,6 +170,7 @@ com.etsy.statsd.profiler.Agent + com.etsy.statsd.profiler.Agent diff --git a/src/main/java/com/etsy/statsd/profiler/Agent.java b/src/main/java/com/etsy/statsd/profiler/Agent.java index d2b1853..a1822ce 100644 --- a/src/main/java/com/etsy/statsd/profiler/Agent.java +++ b/src/main/java/com/etsy/statsd/profiler/Agent.java @@ -29,6 +29,11 @@ private Agent() { } static AtomicReference isRunning = new AtomicReference<>(true); static LinkedList errors = new LinkedList<>(); + + public static void agentmain(final String args, final Instrumentation instrumentation) { + premain(args, instrumentation); + } + /** * Start the profiler * From c9a7f6debab5d0400fe8a21587f8c5737af6eb15 Mon Sep 17 00:00:00 2001 From: Alejandro Rivera Date: Sun, 24 Jan 2016 17:40:14 +0800 Subject: [PATCH 2/3] Added instructions about runtime loading of the agent --- README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/README.md b/README.md index 3b6ae5a..5260656 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ The profiler is enabled using the JVM's `-javaagent` argument. You are required -javaagent:/usr/etsy/statsd-jvm-profiler/statsd-jvm-profiler.jar=server=hostname,port=num ``` +The profiler can also be loaded dynamically (after the JVM has already started), but this technique requires relying on Sun's `tools.jar`, meaning it's an implementation-specific solution that might not work for all JVMs. For more information see the [Dynamic Loading section](#dynamic-loading-of-agent). + An example of setting up Cascading/Scalding jobs to use the profiler can be found in the `example` directory. ### Global Options @@ -97,6 +99,34 @@ You can disable either the memory or CPU metrics using the `profilers` argument: The `visualization` directory contains some utilities for visualizing the output of the profiler. +## Dynamic Loading of Agent + +1. Make sure you have the `tools.jar` available in your classpath during compilation and runtime. This JAR is usually found in the JAVA_HOME directory under the `/lib` folder for Oracle Java installations. +2. Make sure the `jvm-profiler` JAR is available during runtime. +3. During your application boostrap process, do the following: + +```scala + val jarPath: String = s"$ABSOLUTE_PATH_TO/com.etsy.statsd-jvm-profiler-$VERSION.jar" + val agentArgs: String = s"server=$SERVER,port=$PORT" + attachJvmAgent(jarPath, agentArgs) + + def attachJvmAgent(profilerJarPath: String, agentArgs: String): Unit = { + val nameOfRunningVM: String = java.lang.management.ManagementFactory.getRuntimeMXBean.getName + val p: Integer = nameOfRunningVM.indexOf('@') + val pid: String = nameOfRunningVM.substring(0, p) + + try { + val vm: com.sun.tools.attach.VirtualMachine = com.sun.tools.attach.VirtualMachine.attach(pid) + vm.loadAgent(profilerJarPath, agentArgs) + vm.detach() + LOGGER.info("Dynamically loaded StatsD JVM Profiler Agent..."); + } catch { + case e: Exception => LOGGER.warn(s"Could not dynamically load StatsD JVM Profiler Agent ($profilerJarPath)", e); + } + } +``` + + ## Contributing Contributions are highly encouraged! Check out [the contribution guidlines](https://github.com/etsy/statsd-jvm-profiler/blob/master/CONTRIBUTING.md). From 054c2985d4b68d47a6f2c48e4813bfb0b6cf804e Mon Sep 17 00:00:00 2001 From: Alejandro Rivera Date: Sun, 24 Jan 2016 17:49:20 +0800 Subject: [PATCH 3/3] Added AlejandroRivera (me) to the list of contributors --- CONTRIBUTING.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index ec57b48..470aa38 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -19,4 +19,5 @@ Every pull request will be built with [Travis CI](https://travis-ci.org/etsy/sta - Joe Meissler [stickperson](https://github.com/stickperson) - Ben Darfler [bdarfler](https://github.com/bdarfler) - Ihor Bobak [ibobak](https://github.com/ibobak) -- Jeff Fenchel [jfenc91](https://github.com/jfenc91) \ No newline at end of file +- Jeff Fenchel [jfenc91](https://github.com/jfenc91) +- Alejandro Rivera [AlejandroRivera](https://github.com/AlejandroRivera)