-
Notifications
You must be signed in to change notification settings - Fork 867
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
Faster type matching #5724
Merged
Merged
Faster type matching #5724
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6271192
Faster type matching
laurit 75a1af4
make findLoadedClass accessible on java17
laurit 8ee20b5
enable jaxrs instrumentation for quarkus test
laurit 09ecfb2
fix websphere
laurit 452013b
fix muzzle
laurit 0ca911a
javadoc formating
laurit c4c77c8
ignore classes that are know to fail to load for virtual field transf…
laurit b1aa471
add back jaxrs and jaxws annotation instrumentations
laurit 850942b
Apply suggestions from code review
laurit bae2644
fix compile error
laurit bdaa8db
comments
laurit 335eb51
replace deprecated method usage
laurit a2fecda
add comment
laurit 8af1e6a
add an spi to get access to bootstrap proxy from muzzle module
laurit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...ntelemetry/javaagent/instrumentation/internal/classloader/DefineClassInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.internal.classloader; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArguments; | ||
|
||
import io.opentelemetry.javaagent.bootstrap.DefineClassHelper; | ||
import io.opentelemetry.javaagent.bootstrap.DefineClassHelper.Handler.DefineClassContext; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import java.nio.ByteBuffer; | ||
import java.security.ProtectionDomain; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class DefineClassInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("java.lang.ClassLoader"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
named("defineClass") | ||
.and( | ||
takesArguments( | ||
String.class, byte[].class, int.class, int.class, ProtectionDomain.class)), | ||
DefineClassInstrumentation.class.getName() + "$DefineClassAdvice"); | ||
transformer.applyAdviceToMethod( | ||
named("defineClass") | ||
.and(takesArguments(String.class, ByteBuffer.class, ProtectionDomain.class)), | ||
DefineClassInstrumentation.class.getName() + "$DefineClassAdvice2"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class DefineClassAdvice { | ||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static DefineClassContext onEnter( | ||
@Advice.This ClassLoader classLoader, | ||
@Advice.Argument(0) String className, | ||
@Advice.Argument(1) byte[] classBytes, | ||
@Advice.Argument(2) int offset, | ||
@Advice.Argument(3) int length) { | ||
return DefineClassHelper.beforeDefineClass( | ||
classLoader, className, classBytes, offset, length); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit(@Advice.Enter DefineClassContext context) { | ||
DefineClassHelper.afterDefineClass(context); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class DefineClassAdvice2 { | ||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static DefineClassContext onEnter( | ||
@Advice.This ClassLoader classLoader, | ||
@Advice.Argument(0) String className, | ||
@Advice.Argument(1) ByteBuffer classBytes) { | ||
return DefineClassHelper.beforeDefineClass(classLoader, className, classBytes); | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) | ||
public static void onExit(@Advice.Enter DefineClassContext context) { | ||
DefineClassHelper.afterDefineClass(context); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...agent-bootstrap/src/main/java/io/opentelemetry/javaagent/bootstrap/DefineClassHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.bootstrap; | ||
|
||
import java.nio.ByteBuffer; | ||
|
||
public class DefineClassHelper { | ||
|
||
/** Helper class for {@code ClassLoader.defineClass} callbacks. */ | ||
public interface Handler { | ||
DefineClassContext beforeDefineClass( | ||
ClassLoader classLoader, String className, byte[] classBytes, int offset, int length); | ||
|
||
void afterDefineClass(DefineClassContext context); | ||
|
||
/** Context returned from {@code beforeDefineClass} and passed to {@code afterDefineClass}. */ | ||
interface DefineClassContext { | ||
void exit(); | ||
} | ||
} | ||
|
||
private static volatile Handler handler; | ||
|
||
public static Handler.DefineClassContext beforeDefineClass( | ||
ClassLoader classLoader, String className, byte[] classBytes, int offset, int length) { | ||
return handler.beforeDefineClass(classLoader, className, classBytes, offset, length); | ||
} | ||
|
||
public static Handler.DefineClassContext beforeDefineClass( | ||
ClassLoader classLoader, String className, ByteBuffer byteBuffer) { | ||
// see how ClassLoader handles ByteBuffer | ||
// https://github.com/openjdk/jdk11u/blob/487c3344fee3502b4843e7e11acceb77ad16100c/src/java.base/share/classes/java/lang/ClassLoader.java#L1095 | ||
int length = byteBuffer.remaining(); | ||
if (byteBuffer.hasArray()) { | ||
return beforeDefineClass( | ||
classLoader, | ||
className, | ||
byteBuffer.array(), | ||
byteBuffer.position() + byteBuffer.arrayOffset(), | ||
length); | ||
} else { | ||
byte[] classBytes = new byte[length]; | ||
byteBuffer.duplicate().get(classBytes); | ||
return beforeDefineClass(classLoader, className, classBytes, 0, length); | ||
} | ||
} | ||
|
||
public static void afterDefineClass(Handler.DefineClassContext context) { | ||
handler.afterDefineClass(context); | ||
} | ||
|
||
/** | ||
* Sets the {@link Handler} with callbacks to execute when {@code ClassLoader.defineClass} is | ||
* called. | ||
*/ | ||
public static void internalSetHandler(Handler handler) { | ||
if (DefineClassHelper.handler != null) { | ||
// Only possible by misuse of this API, just ignore. | ||
return; | ||
} | ||
DefineClassHelper.handler = handler; | ||
} | ||
|
||
private DefineClassHelper() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
javaagent-tooling/src/main/java/io/opentelemetry/javaagent/tooling/DefineClassHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.tooling; | ||
|
||
import io.opentelemetry.javaagent.bootstrap.DefineClassHelper.Handler; | ||
import java.nio.charset.StandardCharsets; | ||
import org.objectweb.asm.ClassReader; | ||
|
||
public class DefineClassHandler implements Handler { | ||
public static final DefineClassHandler INSTANCE = new DefineClassHandler(); | ||
private static final ThreadLocal<DefineClassContextImpl> defineClassContext = | ||
ThreadLocal.withInitial(() -> DefineClassContextImpl.NOP); | ||
|
||
private DefineClassHandler() {} | ||
|
||
@Override | ||
public DefineClassContext beforeDefineClass( | ||
ClassLoader classLoader, String className, byte[] classBytes, int offset, int length) { | ||
// with OpenJ9 class data sharing we don't get real class bytes | ||
if (classBytes == null | ||
|| (classBytes.length == 40 | ||
&& new String(classBytes, StandardCharsets.ISO_8859_1) | ||
.startsWith("J9ROMCLASSCOOKIE"))) { | ||
return null; | ||
} | ||
|
||
DefineClassContextImpl context = null; | ||
// attempt to load super types of currently loaded class | ||
// for a class to be loaded all of its super types must be loaded, here we just change the order | ||
// of operations and load super types before transforming the bytes for current class so that | ||
// we could use these super types for resolving the advice that needs to be applied to current | ||
// class | ||
try { | ||
ClassReader cr = new ClassReader(classBytes, offset, length); | ||
String superName = cr.getSuperName(); | ||
if (superName != null) { | ||
Class.forName(superName.replace('/', '.'), false, classLoader); | ||
} | ||
String[] interfaces = cr.getInterfaces(); | ||
for (String interfaceName : interfaces) { | ||
Class.forName(interfaceName.replace('/', '.'), false, classLoader); | ||
} | ||
} catch (Throwable throwable) { | ||
// loading of super class or interface failed | ||
// mark current class as failed to skip matching and transforming it | ||
// we'll let defining the class proceed as usual so that it would throw the same exception as | ||
// it does when running without the agent | ||
context = DefineClassContextImpl.enter(className); | ||
} | ||
return context; | ||
} | ||
|
||
@Override | ||
public void afterDefineClass(DefineClassContext context) { | ||
if (context != null) { | ||
context.exit(); | ||
} | ||
} | ||
|
||
/** | ||
* Detect whether loading the specified class is known to fail. | ||
* | ||
* @param className class being loaded | ||
* @return true if it is known that loading class with given name will fail | ||
*/ | ||
public static boolean isFailedClass(String className) { | ||
DefineClassContextImpl context = defineClassContext.get(); | ||
return context.failedClassName != null && context.failedClassName.equals(className); | ||
} | ||
|
||
private static class DefineClassContextImpl implements DefineClassContext { | ||
private static final DefineClassContextImpl NOP = new DefineClassContextImpl(); | ||
|
||
private final DefineClassContextImpl previous; | ||
private final String failedClassName; | ||
|
||
private DefineClassContextImpl() { | ||
previous = null; | ||
failedClassName = null; | ||
} | ||
|
||
private DefineClassContextImpl(DefineClassContextImpl previous, String failedClassName) { | ||
this.previous = previous; | ||
this.failedClassName = failedClassName; | ||
} | ||
|
||
static DefineClassContextImpl enter(String failedClassName) { | ||
DefineClassContextImpl previous = defineClassContext.get(); | ||
DefineClassContextImpl context = new DefineClassContextImpl(previous, failedClassName); | ||
defineClassContext.set(context); | ||
return context; | ||
} | ||
|
||
@Override | ||
public void exit() { | ||
defineClassContext.set(previous); | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...rc/main/java/io/opentelemetry/javaagent/tooling/bootstrap/BootstrapProxyProviderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.tooling.bootstrap; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.tooling.Utils; | ||
import io.opentelemetry.javaagent.tooling.muzzle.BootstrapProxyProvider; | ||
|
||
@AutoService(BootstrapProxyProvider.class) | ||
public class BootstrapProxyProviderImpl implements BootstrapProxyProvider { | ||
|
||
@Override | ||
public ClassLoader getBootstrapProxy() { | ||
return Utils.getBootstrapProxy(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh! for some reason I didn't think it was so cheap to get the superclass and interfaces!
I had a similar optimization to preload super types in glowroot: https://github.com/glowroot/glowroot/blob/main/agent/core/src/main/java/org/glowroot/agent/impl/PreloadSomeSuperTypesCache.java
but it stored the superclass/interfaces into a file cache during loading, and then used that to speed up subsequent startups
(this is so much better 🤩)