pfinder实现原理揭秘
京东科技开发者
共 18757字,需浏览 38分钟
·
2024-06-28 11:10
引言
在现代软件开发过程中,性能优化和故障排查是保证应用稳定运行的关键任务之一。Java作为一种广泛使用的编程语言,其生态中涌现出了许多优秀的监控和诊断工具,诸如:SkyWalking、Zipkin等,它们帮助开发者和运维人员深入了解应用的运行状态,快速定位和解决问题。在京东内部,则使用的是自研的pfinder。
pfinder简介
pfinder功能
APM类组件对比
字节码修改
ASM实现
public void visitCode() {
super.visitCode();
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitLdcInsn("start");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
}
public void visitInsn(int opcode) {
if ((opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN) || opcode == Opcodes.ATHROW) {
//方法在返回之前,打印"end"
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitLdcInsn("end");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
}
mv.visitInsn(opcode);
}
javassist实现
ClassPool cp = ClassPool.getDefault();
CtClass cc = cp.get("com.ggc.javassist.HelloWord");
CtMethod m = cc.getDeclaredMethod("printHelloWord");
m.insertBefore("{ System.out.println(\"start\"); }");
m.insertAfter("{ System.out.println(\"end\"); }");
Class c = cc.toClass();
cc.writeFile("/Users/gonghanglin/workspace/workspace_me/bytecode_enhance/bytecode_enhance_javassist/target/classes/com/ggc/javassist");
HelloWord h = (HelloWord)c.newInstance();
h.printHelloWord();
bytebuddy实现
// 使用ByteBuddy动态生成一个新的HelloWord类
Class<?> dynamicType = new ByteBuddy()
.subclass(HelloWord.class) // 指定要修改的类
.method(ElementMatchers.named("printHelloWord")) // 指定要拦截的方法名
.intercept(MethodDelegation.to(LoggingInterceptor.class)) // 指定拦截器
.make()
.load(HelloWord.class.getClassLoader()) // 加载生成的类
.getLoaded();
// 创建动态生成类的实例,并调用方法
HelloWord dynamicService = (HelloWord) dynamicType.newInstance();
dynamicService.printHelloWord();
public class LoggingInterceptor {
public static Object intercept( Object[] allArguments, Method method, Callable<?> callable) throws Exception {
// 打印start
System.out.println("start");
try {
// 调用原方法
Object result = callable.call();
// 打印end
System.out.println("end");
return result;
} catch (Exception e) {
System.out.println("exception end");
throw e;
}
}
}
bytekit实现
// Parse the defined Interceptor class and related annotations
DefaultInterceptorClassParser interceptorClassParser = new DefaultInterceptorClassParser();
List<InterceptorProcessor> processors = interceptorClassParser.parse(HelloWorldInterceptor.class);
// load bytecode
ClassNode classNode = AsmUtils.loadClass(HelloWord.class);
// Enhanced process of loaded bytecodes
for (MethodNode methodNode : classNode.methods) {
MethodProcessor methodProcessor = new MethodProcessor(classNode, methodNode);
for (InterceptorProcessor interceptor : processors) {
interceptor.process(methodProcessor);
}
}
public class HelloWorldInterceptor {
@AtEnter(inline = true)
public static void atEnter() {
System.out.println("start");
}
@AtExit(inline = true)
public static void atEit() {
System.out.println("end");
}
}
特性 | ASM | Javassist | ByteBuddy | ByteKit |
性能 | ASM的性能最高,因为它直接操作字节码,没有中间环节 | 劣于ASM | 介于javassist和ASM之间 | 介于javassist和ASM之间 |
易用性 | 需精通字节码,学习成本高,不支持debug | Java语法进行开发,但是采用的硬编码形式开发,不支持debug | 比Javassist更高级,更符文Java开发习惯,可以对增强代码进行断点调试 | 比Javassist更高级,更符文Java开发习惯,可以对增强代码进行断点调试 |
功能 | 直接操作字节码,功能最为强大。 | 功能相对完备 | 功能相对完备 | 功能相对完备,对比ByteBuddy,ByteKit能防止重复增强 |
字节码注入
相信大家经常使用idea去debug我们写的代码,我们是否想过debug是如何实现的呢?暂时先卖个关子。
JVMTIAgent
JVM在设计之初就考虑到了对JVM运行时内存、线程等指标的监控和分析和代码debug功能的实现,基于这两点,早在JDK5之前,JVM规范就定义了JVMPI(JVM分析接口)和JVMDI(JVM调试接口),JDK5之后,这两个规范就合并成为了JVMTI(JVM工具接口)。JVMTI其实是一种JVM规范,每个JVM厂商都有不同的实现,另外,JVMTI接口需使用C语言开发,以动态链接的形式加载并运行。
JVMTI接口 | |
接口 | 功能 |
Agent_OnLoad(JavaVM *vm, char *options, void *reserved); | agent在启动时加载的情况下,也就是在vm参数里通过-agentlib来指定,那在启动过程中就会去执行这个agent里的Agent_OnLoad函数。 |
Agent_OnAttach(JavaVM* vm, char* options, void* reserved); | agent是attach到目标进程上,然后给对应的目标进程发送load命令来加载agent,在加载过程中就会调用Agent_OnAttach函数。 |
Agent_OnUnload(JavaVM *vm); | 在agent卸载的时候调用 |
instrument主要方法 | |
方法 | 功能 |
void addTransformer(ClassFileTransformer transformer) | 添加一个字节码转换器,用来修改加载类的字节码 |
Class[] getAllLoadedClasses() | 返回当前JVM中加载的所有的类的数组 |
Class[] getInitiatedClasses(ClassLoader loader) | 返回指定的类加载器中的所有的类的数据 |
void redefineClasses(ClassDefinition... definitions) | 用给定的类的字节码数组替换指定的类的字节码文件,也就是重新定义指定的类 |
void retransformClasses(Class<?>... classes) | 指定一系列的Class对象,被指定的类都会重新变回去(去掉附加的字节码) |
<archive>
<manifestEntries>
// 指定premain()的所在方法
<Agent-CLass>com.ggc.agent.GhlAgent</Agent-CLass>
<Premain-Class>com.ggc.agent.GhlAgent</Premain-Class>
<Can-Redefine-Classes>true</Can-Redefine-Classes>
<Can-Retransform-Classes>true</Can-Retransform-Classes>
</manifestEntries>
</archive>
2.agen主类
public class GhlAgent {
public static Logger log = LoggerFactory.getLogger(GhlAgent.class);
public static void agentmain(String agentArgs, Instrumentation instrumentation) {
log.info("agentmain方法");
boot(instrumentation);
}
public static void premain(String agentArgs, Instrumentation instrumentation) {
log.info("premain方法");
boot(instrumentation);
}
private static void boot(Instrumentation instrumentation) {
//创建一个代理增强对象
new AgentBuilder.Default().type(ElementMatchers.nameStartsWith("com.jd.aviation.performance.service.impl"))//拦截指定的类
.transform((builder, typeDescription, classLoader, javaModule) ->
builder.method(ElementMatchers.isMethod().and(ElementMatchers.isPublic())
).intercept(MethodDelegation.to(TimingInterceptor.class))
).installOn(instrumentation);
}
}
3.拦截器
public class TimingInterceptor {
public static Logger log = LoggerFactory.getLogger(TimingInterceptor.class);
public static Object intercept(@SuperCall Callable<?> callable) throws Exception {
long start = System.currentTimeMillis();
try {
// 原方法调用
return callable.call();
} finally {
long end = System.currentTimeMillis();
log.info("Method call took {} ms",(end - start));
}
}
}
4.效果
4.1.pfinder应用架构
4.2.pfinder插件增强代码解析
protected boolean doInitialize(ProfilerContext profilerContext) {
AgentEnvService agentEnvService = (AgentEnvService)profilerContext.getService(AgentEnvService.class);
Instrumentation instrumentation = agentEnvService.instrumentation();
if (instrumentation == null) {
LOGGER.info("Instrumentation missing, PFinder PluginRegistrar enhance ignored!");
return false;
}
this.pluginLoaders = profilerContext.getAllService(PluginLoader.class);
this.enhanceHandler = new EnhancePluginHandler(profilerContext);
ElementMatcher.Junction<TypeDescription> typeMatcherChain = null;
for (PluginLoader pluginLoader : this.pluginLoaders) {
pluginLoader.loadPlugins(profilerContext);
for (ElementMatcher.Junction<TypeDescription> typeMatcher : (Iterable<ElementMatcher.Junction<TypeDescription>>)pluginLoader.typeMatchers()) {
if (typeMatcherChain == null) {
typeMatcherChain = typeMatcher; continue;
}
typeMatcherChain = typeMatcherChain.or((ElementMatcher)typeMatcher);
}
}
if (typeMatcherChain == null) {
LOGGER.warn("no any enhance-point. pfinder enhance will be ignore.");
return false;
}
ConfigurationService configurationService = (ConfigurationService)profilerContext.getService(ConfigurationService.class);
String enhanceExcludePolicy = (String)configurationService.get(ConfigKey.PLUGIN_ENHANCE_EXCLUDE);
LoadedClassSummaryHandler loadedClassSummaryHandler = null;
if (((Boolean)configurationService.get(ConfigKey.LOADED_CLASSES_SUMMARY_ENABLED, Boolean.valueOf(false))).booleanValue()) {
loadedClassSummaryHandler = new LoadedClassSummaryHandler.DefaultImpl(configurationService, ((ScheduledService)profilerContext.getService(ScheduledService.class)).getDefault());
}
(new AgentBuilder.Default())
.with(AgentBuilder.RedefinitionStrategy.RETRANSFORMATION)
.with(AgentBuilder.RedefinitionStrategy.REDEFINITION)
.with(new AgentBuilder.RedefinitionStrategy.Listener()
{
public void onBatch(int index, List<Class<?>> batch, List<Class<?>> types) {}
public Iterable<? extends List<Class<?>>> onError(int index, List<Class<?>> batch, Throwable throwable, List<Class<?>> types) {
return Collections.emptyList();
}
public void onComplete(int amount, List<Class<?>> types, Map<List<Class<?>>, Throwable> failures) {
for (Map.Entry<List<Class<?>>, Throwable> entry : failures.entrySet()) {
for (Class<?> aClass : entry.getKey()) {
PluginRegistrar.LOGGER.warn("Redefine class: {} failure! ignored!", new Object[] { aClass.getName(), entry.getValue() });
}
}
}
}).ignore((ElementMatcher)ElementMatchers.nameStartsWith("org.groovy.")
.or((ElementMatcher)ElementMatchers.nameStartsWith("jdk.nashorn."))
.or((ElementMatcher)ElementMatchers.nameStartsWith("javax.script."))
.or((ElementMatcher)ElementMatchers.nameContains("javassist"))
.or((ElementMatcher)ElementMatchers.nameContains(".asm."))
.or((ElementMatcher)ElementMatchers.nameContains("$EnhancerBySpringCGLIB$"))
.or((ElementMatcher)ElementMatchers.nameStartsWith("sun.reflect"))
.or((ElementMatcher)ElementMatchers.nameStartsWith("org.apache.jasper"))
.or((ElementMatcher)pfinderIgnoreMather())
.or((ElementMatcher)Matchers.forPatternLine(enhanceExcludePolicy))
.or((ElementMatcher)ElementMatchers.isSynthetic()))
.type((ElementMatcher)typeMatcherChain)
.transform(this)
.with(new Listener(loadedClassSummaryHandler))
.installOn(instrumentation);
return true;
}
5.1.多线程traceId丢失问题
public class TracingRunnable
implements PfinderWrappedRunnable
{
private final Runnable origin;
private final TracingSnapshot<?> snapshot;
private final Component component;
private final String operationName;
private final String interceptorName;
private final InterceptorClassLoader interceptorClassLoader;
public TracingRunnable(Runnable origin, TracingSnapshot<?> snapshot, Component component, String operationName, String interceptorName, InterceptorClassLoader interceptorClassLoader) {
this.origin = origin;
this.snapshot = snapshot;
this.component = component;
this.operationName = operationName;
this.interceptorClassLoader = interceptorClassLoader;
this.interceptorName = interceptorName;
}
public void run() {
TracingContext tracingContext = ContextManager.tracingContext();
if (tracingContext.isTracing() && tracingContext.traceId().equals(this.snapshot.getTraceId())) {
this.origin.run();
return;
}
LowLevelAroundTracingContext context = SpringAsyncTracingContext.create(this.operationName, this.interceptorName, this.snapshot, this.interceptorClassLoader, this.component);
context.onMethodEnter();
try {
this.origin.run();
} catch (RuntimeException ex) {
context.onException(ex);
throw ex;
} finally {
context.onMethodExit();
}
}
public Runnable getOrigin() {
return this.origin;
}
public String toString() {
return "TracingRunnable{origin=" + this.origin + ", snapshot=" + this.snapshot + ", component=" + this.component + ", operationName='" + this.operationName + '\'' + '}';
}
}
拿线程池执行Runnable任务来说,pfinder通过TracingRunnable包装我们的Runnable的实现,利用构造函数将主线程的traceId通过snapshot参数传给TracingRunnable,在run方法中将参数snapshot放到上下文中,最后从上下文中取出放到子线程的MDC中,从而实现traceId跨线程传递。
5.2.热部署
评论