Skip to content

Commit

Permalink
[#10858] Add InterceptorHolder instead of InterceptorRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
jaehong-kim committed Apr 9, 2024
1 parent 244286c commit 86f8f20
Show file tree
Hide file tree
Showing 32 changed files with 1,083 additions and 296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ profiler.application.namespace=

profiler.interceptorregistry.size=65536

# InterceptorHolder improves transform performance.
profiler.interceptor.holder.enable=true

# Manually override jvm vendor name (Oracle, IBM, OpenJDK, etc)
# You probably won't ever need to set this value.
profiler.jvm.vendor.name=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ profiler.application.namespace=

profiler.interceptorregistry.size=8192

# InterceptorHolder improves transform performance.
profiler.interceptor.holder.enable=true

# Manually override jvm vendor name (Oracle, IBM, OpenJDK, etc)
# You probably won't ever need to set this value.
profiler.jvm.vendor.name=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,29 @@ public DefaultInterceptorRegistryAdaptor(int maxRegistrySize) {
this.index = new WeakAtomicReferenceArray<Interceptor>(maxRegistrySize, Interceptor.class);
}


@Override
public int addInterceptor(Interceptor interceptor) {
if (interceptor == null) {
return -1;
}

final int newId = nextId();
if (newId >= registrySize) {
throw new IndexOutOfBoundsException("Interceptor registry size exceeded. Check the \"profiler.interceptorregistry.size\" setting. size=" + index.length() + " id=" + id);
}
final int newId = checkMaxSize(nextId());
index.set(newId, interceptor);
return newId;
}

@Override
public int addInterceptor() {
return checkMaxSize(nextId());
}

private int checkMaxSize(int id) {
if (id >= registrySize) {
throw new IndexOutOfBoundsException("Interceptor registry size exceeded. Check the \"profiler.interceptorregistry.size\" setting. size=" + index.length() + " id=" + id);
}
return id;
}

private int nextId() {
return id.getAndIncrement();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public EmptyRegistryAdaptor() {
}


@Override
public int addInterceptor() {
return 0;
}

@Override
public int addInterceptor(Interceptor interceptor) {
return -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public final class InterceptorRegistry {
private static final AtomicReference<InterceptorRegistryAdaptor> REGISTRY =
new AtomicReference<>(EmptyRegistryAdaptor.EMPTY);

private static boolean interceptorHolderEnable = true;

public static void bind(final InterceptorRegistryAdaptor interceptorRegistryAdaptor, final Object lock) {
Objects.requireNonNull(interceptorRegistryAdaptor, "interceptorRegistryAdaptor");

Expand All @@ -53,4 +55,12 @@ public static void unbind(final Object lock) {
public static Interceptor getInterceptor(int key) {
return REGISTRY.get().getInterceptor(key);
}

public static void setInterceptorHolderEnable(boolean enable) {
interceptorHolderEnable = enable;
}

public static boolean isInterceptorHolderEnable() {
return interceptorHolderEnable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
* @author emeroad
*/
public interface InterceptorRegistryAdaptor {
int addInterceptor();

int addInterceptor(Interceptor interceptor);

Interceptor getInterceptor(int key);

void clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,10 @@ public void addSimpleInterceptor() {

Assertions.assertSame(mock, find);
}

@Test
public void addInterceptor() {
InterceptorRegistryAdaptor registry = new DefaultInterceptorRegistryAdaptor();
int key = registry.addInterceptor();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,21 @@
package com.navercorp.test.pinpoint.jdk8.interfaces;

import com.navercorp.pinpoint.bootstrap.instrument.InstrumentContext;
import com.navercorp.pinpoint.bootstrap.instrument.InstrumentException;
import com.navercorp.pinpoint.bootstrap.interceptor.Interceptor;
import com.navercorp.pinpoint.profiler.instrument.ASMClass;
import com.navercorp.pinpoint.profiler.instrument.ASMClassNodeAdapter;
import com.navercorp.pinpoint.profiler.instrument.ASMClassWriter;
import com.navercorp.pinpoint.profiler.instrument.ASMFieldNodeAdapter;
import com.navercorp.pinpoint.profiler.instrument.ASMInterceptorHolder;
import com.navercorp.pinpoint.profiler.instrument.ASMMethodNodeAdapter;
import com.navercorp.pinpoint.profiler.instrument.EngineComponent;
import com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinition;
import com.navercorp.pinpoint.profiler.instrument.interceptor.InterceptorDefinitionFactory;
import com.navercorp.pinpoint.profiler.interceptor.registry.DefaultInterceptorRegistryBinder;
import com.navercorp.pinpoint.profiler.interceptor.registry.InterceptorRegistryBinder;
import com.navercorp.pinpoint.profiler.util.JavaAssistUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
Expand All @@ -47,6 +46,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
Expand All @@ -55,28 +55,18 @@
* @author jaehong.kim
*/
public class MethodInterfaceTest {
private final static InterceptorRegistryBinder interceptorRegistryBinder = new DefaultInterceptorRegistryBinder();
private final static InstrumentContext pluginContext = mock(InstrumentContext.class);
private AtomicInteger interceptorIdCounter = new AtomicInteger();

private final Logger logger = LogManager.getLogger(this.getClass());

@BeforeAll
public static void beforeClass() {
interceptorRegistryBinder.bind();
}

@AfterAll
public static void afterClass() {
interceptorRegistryBinder.unbind();
}

@Test
public void addInterceptor() throws Exception {
final String targetInterfaceName = "com.navercorp.test.pinpoint.jdk8.interfaces.MethodInterface";
final String targetClassName = "com.navercorp.test.pinpoint.jdk8.interfaces.MethodInterfaceClass";
logger.debug("Add interceptor interface={}, class={}", targetInterfaceName, targetClassName);

final int interceptorId = interceptorRegistryBinder.getInterceptorRegistryAdaptor().addInterceptor(new SimpleInterceptor());
final Interceptor interceptor = new SimpleInterceptor();
final InterceptorDefinition interceptorDefinition = new InterceptorDefinitionFactory().createInterceptorDefinition(SimpleInterceptor.class);
final List<String> methodNameList = Arrays.asList("currentTimeMillis", "foo");
TestClassLoader classLoader = new TestClassLoader();
Expand All @@ -100,6 +90,13 @@ public void handle(ClassNode classNode) {
if (!methodNameList.contains(methodNode.name)) {
continue;
}
int interceptorId = interceptorIdCounter.incrementAndGet();
try {
ASMInterceptorHolder.create(interceptorId, classLoader, interceptor);
} catch (InstrumentException e) {
throw new RuntimeException(e);
}

methodNodeAdapter.addBeforeInterceptor(interceptorId, interceptorDefinition, 99);
logger.debug("Add before interceptor in method={}", methodNode.name);
methodNodeAdapter.addAfterInterceptor(interceptorId, interceptorDefinition, 99);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,27 @@
/**
* @author Woonduk Kang(emeroad)
*/
public class InterceptorRegistryBinderProvider implements Provider<InterceptorRegistryBinder> {
public class InterceptorRegistryBinderProvider implements Provider<InterceptorRegistryBinder> {

private final InterceptorRegistryBinder interceptorRegistryBinder;

@Inject
public InterceptorRegistryBinderProvider(InstrumentConfig instrumentConfig) {
this(getInterceptorRegistrySize(instrumentConfig));
this(getInterceptorRegistrySize(instrumentConfig), getInterceptorHolderEnable(instrumentConfig));
}

private static int getInterceptorRegistrySize(InstrumentConfig instrumentConfig) {
Objects.requireNonNull(instrumentConfig, "instrumentConfig");
return instrumentConfig.getInterceptorRegistrySize();
}

public InterceptorRegistryBinderProvider(int interceptorSize) {
this.interceptorRegistryBinder = new DefaultInterceptorRegistryBinder(interceptorSize);
private static boolean getInterceptorHolderEnable(InstrumentConfig instrumentConfig) {
Objects.requireNonNull(instrumentConfig, "instrumentConfig");
return instrumentConfig.getInterceptorHolderEnable();
}

public InterceptorRegistryBinderProvider(int interceptorSize, boolean interceptorHolderEnable) {
this.interceptorRegistryBinder = new DefaultInterceptorRegistryBinder(interceptorSize, interceptorHolderEnable);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ private int addInterceptor0(TargetFilter annotation, Class<? extends Interceptor
Objects.requireNonNull(filterTypeName, "type of @TargetFilter");

ObjectBinderFactory objectBinderFactory = engineComponent.getObjectBinderFactory();
final InterceptorArgumentProvider interceptorArgumentProvider = objectBinderFactory.newInterceptorArgumentProvider(this);
final InterceptorArgumentProvider interceptorArgumentProvider = objectBinderFactory.newInterceptorArgumentProvider();
final AutoBindingObjectFactory filterFactory = objectBinderFactory.newAutoBindingObjectFactory(pluginContext, classNode.getClassLoader(), interceptorArgumentProvider);
final ObjectFactory objectFactory = ObjectFactory.byConstructor(filterTypeName, (Object[]) annotation.constructorArguments());
final MethodFilter filter = (MethodFilter) filterFactory.createInstance(objectFactory);
Expand Down
Loading

0 comments on commit 86f8f20

Please sign in to comment.