Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BTrace support for custom JFR events #429

Merged
merged 20 commits into from
Nov 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmarks/agent-benchmark/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ task btracec(type: JavaExec) {
jmhClasses.dependsOn btracec

jmh {
def agent = "-javaagent:${project.buildDir}/../../../btrace-dist/build/resources/main/libs/btrace-agent.jar=stdout=false,noServer=true,script=${project.buildDir}/classes/btrace/TraceScript.class"
def agent = "-javaagent:${project.buildDir}/../../../btrace-dist/build/resources/main/${project.version}/libs/btrace-agent.jar=stdout=false,noServer=true,script=${project.buildDir}/classes/btrace/TraceScript.class"
println(agent)
duplicateClassesStrategy = 'warn'
// jvmArgsAppend = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,13 +324,14 @@ private static BTraceConfig getConfig() throws IOException {

Path distLibs = null;
String basedir = System.getProperty("jmh.basedir");
String version = System.getProperty("project.version");
Path root = null;
if (basedir == null) {
root = fs.getPath(".").toAbsolutePath();
} else {
root = Paths.get(basedir).getParent();
}
distLibs = root.resolve("btrace-dist/build/resources/main/libs");
distLibs = root.resolve("btrace-dist/build/resources/main/" + version + "/libs");

Path agentPath = distLibs.resolve("btrace-agent.jar");
Path bootPath = distLibs.resolve("btrace-boot.jar");
Expand All @@ -342,7 +343,7 @@ private static BTraceConfig getConfig() throws IOException {
agentPath, tmpDir.resolve("btrace-agent.jar"), StandardCopyOption.REPLACE_EXISTING);
Files.copy(bootPath, tmpDir.resolve("btrace-boot.jar"), StandardCopyOption.REPLACE_EXISTING);

URL traceLoc = BTraceBench.class.getResource("/TraceScript.class");
URL traceLoc = BTraceBench.class.getResource("/TraceScript.btclass");
String trace = traceLoc.getPath();

return new BTraceConfig(tmpDir, targetPath.toString(), trace);
Expand Down
18 changes: 15 additions & 3 deletions benchmarks/runtime-benchmarks/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
plugins {
id 'java'
id("me.champeau.gradle.jmh") version "0.5.0-rc-2"
id("me.champeau.gradle.jmh") version "0.5.0"
}

description 'A JMH benchmark to assert the overhead imposed by various types of BTrace instrumentation.'
Expand Down Expand Up @@ -30,13 +30,25 @@ task btracec(type: JavaExec) {
classpath configurations.compile
main 'org.openjdk.btrace.compiler.Compiler'
args '-d'
args "${buildDir}/classes/btrace"
args "${buildDir}/classes/java/jmh/"
args '-packext'
args 'btclass'
args fileTree(dir: "src/jmh/btrace", include: 'TraceScript.java')
}
jmhClasses.dependsOn btracec

jmhJar {
include 'META-INF/BenchmarkList'
include 'META-INF/CompilerHints'
include 'org/openjdk/jmh/**'
include 'org/openjdk/btrace/BTraceBench.class'
include 'org/openjdk/btrace/generated/**/*'
include 'jmh*'
include '*.btclass'
}

jmh {
duplicateClassesStrategy = 'warn'
jvmArgsAppend = ["-Djmh.basedir=${project.buildDir}/.."]
jvmArgsAppend = "-Djmh.basedir=${project.buildDir}/.. -Dproject.version=${project.version}"
}

Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ private static BTraceConfig getConfig() throws IOException {
agentPath, tmpDir.resolve("btrace-agent.jar"), StandardCopyOption.REPLACE_EXISTING);
Files.copy(bootPath, tmpDir.resolve("btrace-boot.jar"), StandardCopyOption.REPLACE_EXISTING);

URL traceLoc = BTraceBench.class.getResource("/TraceScript.class");
URL traceLoc = BTraceBench.class.getResource("/TraceScript.btclass");
String trace = traceLoc.getPath();

return new BTraceConfig(tmpDir, targetPath.toString(), trace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ public void setup() throws Exception {

@Setup(Level.Invocation)
public void setupRun() throws Exception {
classStream = ProbeLoadingBenchmark.class.getResourceAsStream("/scripts/TraceScript.class");
classStream = ProbeLoadingBenchmark.class.getResourceAsStream("/btrace/TraceScript.class");
System.out.println("====> " + classStream);
}

@TearDown(Level.Invocation)
Expand Down
38 changes: 5 additions & 33 deletions btrace-agent/build.gradle
Original file line number Diff line number Diff line change
@@ -1,33 +1,5 @@
//plugins {
// id "com.github.johnrengelman.shadow" version "6.0.0"
//}

apply plugin: 'java'
//
//shadowJar {
// baseName = "${project.name}"
// version = "${project.version}"
// classifier = null
//
// exclude 'afu/**'
// exclude 'javax/**'
// exclude 'com/**'
// exclude 'sun/**'
// exclude 'org/relaxng/**'
// exclude 'org/checkerframework/**'
// exclude 'org/codehaus/**'
//
// exclude 'org/jctools/maps/**'
// exclude 'org/jctools/util/**'
// exclude 'META-INF/services/**'
//
// exclude 'org/objectweb/asm/commons/**'
// exclude 'org/objectweb/asm/util/**'
// exclude 'org/objectweb/asm/xml/**'
//
// configurations = [project.configurations.compile]
// relocate 'org.jctools.queues', 'org.openjdk.btrace.org.jctools.queues'
// relocate 'org.objectweb.asm', 'org.openjdk.btrace.org.objectweb.asm'
//}
//
//build.dependsOn shadowJar
dependencies {
compile files("${JAVA_8_HOME}/lib/tools.jar")
compile project(':btrace-runtime')
compile project(':btrace-instr')
}
16 changes: 14 additions & 2 deletions btrace-agent/src/main/java/org/openjdk/btrace/agent/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,13 @@ void retransformLoaded() throws UnmodifiableClassException {
} catch (VerifyError e) {
debugPrint(e);
try {
onCommand(new MessageCommand("[BTRACE WARN] Class verification failed: " + c.getName() + " (" + e.getMessage() + ")"));
onCommand(
new MessageCommand(
"[BTRACE WARN] Class verification failed: "
+ c.getName()
+ " ("
+ e.getMessage()
+ ")"));
} catch (IOException ioException) {
if (isDebug()) {
debug.debug(ioException);
Expand All @@ -524,7 +530,13 @@ void retransformLoaded() throws UnmodifiableClassException {
} catch (VerifyError e1) {
debugPrint(e1);
try {
onCommand(new MessageCommand("[BTRACE WARN] Class verification failed: " + c.getName() + " (" + e.getMessage() + ")"));
onCommand(
new MessageCommand(
"[BTRACE WARN] Class verification failed: "
+ c.getName()
+ " ("
+ e.getMessage()
+ ")"));
} catch (IOException ioException) {
if (isDebug()) {
debug.debug(ioException);
Expand Down
9 changes: 9 additions & 0 deletions btrace-agent/src/main/java/org/openjdk/btrace/agent/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,14 @@ private static void parseArgs() {
}
break;
}
case BOOT_CLASS_PATH:
{
settings.setBootClassPath(!p.isEmpty() ? p : "");
if (isDebug()) {
debugPrint("probe boot class path is " + settings.getBootClassPath());
}
break;
}

default:
{
Expand Down Expand Up @@ -572,6 +580,7 @@ private static void processClasspaths(String libs) {
} else {
if (f.isFile() && f.getName().toLowerCase().endsWith(".jar")) {
JarFile jf = asJarFile(f);
debugPrint("Adding jar: " + jf.toString());
inst.appendToBootstrapClassLoaderSearch(jf);
} else {
debugPrint("ignoring boot classpath element '" + path + "' - only jar files allowed");
Expand Down
6 changes: 6 additions & 0 deletions btrace-client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ plugins {
id 'net.nemerosa.versioning' version '2.6.1'
}

dependencies {
compile project(':btrace-core')
compile project(':btrace-compiler')
compile project(':btrace-instr')
}

jar {
manifest {
attributes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ public byte[] compile(String fileName, String classPath, PrintWriter err, String
public void attach(String pid, String sysCp, String bootCp) throws IOException {
try {
String agentPath = "/btrace-agent.jar";
URL btracePkg = Client.class.getClassLoader().getResource("org/openjdk/btrace");
URL btracePkg = Client.class.getClassLoader().getResource("org/openjdk/btrace/client");
if (btracePkg != null) {
String tmp = btracePkg.toString();
tmp = tmp.substring(0, tmp.indexOf('!'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ public static void main(String[] args) throws Exception {
if (code == null) {
errorExit("BTrace compilation failed", 1);
}
if (isDebug()) {
debugPrint("Boot classpath: " + classPath);
}
if (!hostDefined) client.attach(pid.toString(), null, classPath);
registerExitHook(client);
if (con != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.openjdk.btrace.client;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.util.TraceClassVisitor;
import org.openjdk.btrace.core.SharedSettings;
import org.openjdk.btrace.instr.BTraceProbe;
import org.openjdk.btrace.instr.BTraceProbeFactory;
import org.openjdk.btrace.instr.OnMethod;
import org.openjdk.btrace.instr.OnProbe;

public final class ProbePrinter {
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Usage: btracep <probe_file>");
System.exit(0);
}
Path probePath = Paths.get(args[0]);

try (InputStream probeDataStream =
new BufferedInputStream(new FileInputStream(probePath.toFile()))) {
BTraceProbe probe =
new BTraceProbeFactory(SharedSettings.GLOBAL).createProbe(probeDataStream);

probe.checkVerified();
System.out.println("Name: " + probe.getClassName(false));
System.out.println("Verified: " + probe.isVerified());
System.out.println("Transforming: " + probe.isTransforming());

System.out.println("=== Probe handlers");
for (OnMethod om : probe.onmethods()) {
System.out.println(om);
}
for (OnProbe op : probe.onprobes()) {
System.out.println(op);
}

System.out.println("=== Dataholder class");
ClassReader dataholderReader = new ClassReader(probe.getDataHolderBytecode());
dataholderReader.accept(new TraceClassVisitor(new PrintWriter(System.out)), 0);

System.out.println("=== Full probe class");
ClassReader probeReader = new ClassReader(probe.getFullBytecode());
probeReader.accept(new TraceClassVisitor(new PrintWriter(System.out)), 0);
}
}
}
5 changes: 5 additions & 0 deletions btrace-compiler/build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
dependencies {
compile files("${JAVA_8_HOME}/lib/tools.jar")
compile project(path: ':btrace-core')
compile project(path: ':btrace-runtime')
runtimeOnly project(path: ':btrace-instr')

testCompile project(path: ':btrace-instr')
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -101,11 +103,22 @@ Map<String, byte[]> compile(
// temp hack; need turn off verifier
SharedSettings.GLOBAL.setTrusted(true);

String[] pathElements = classPath.split(File.pathSeparator);
List<URL> urlElements = new ArrayList<>(pathElements.length);

for (String pathElement : pathElements) {
File f = new File(pathElement);
urlElements.add(f.toURI().toURL());
}
URLClassLoader generatorCL =
new URLClassLoader(
urlElements.toArray(new URL[0]), Compiler.class.getClassLoader());
ServiceLoader<PackGenerator> generators =
ServiceLoader.load(PackGenerator.class, Compiler.class.getClassLoader());
ServiceLoader.load(PackGenerator.class, generatorCL);
Iterator<PackGenerator> iter = generators.iterator();
if (iter.hasNext()) {
PackGenerator generator = iter.next();
SharedSettings.GLOBAL.setBootClassPath(classPath);
classData = generator.generateProbePack(classData);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void visit(
== 0) {
shortSyntax =
true; // specifying "class <MyClass>" rather than "public class <MyClass>" means using
// short syntax
// short syntax
access |= Opcodes.ACC_PUBLIC; // force the public modifier on the btrace class
}
className = name;
Expand Down
Loading