/* * (non-Javadoc) * * @see java.lang.Thread#run() */ public void run() { try { while (true) { if (Manager.instance().canDump()) { Manager.instance().setProfileFlag(true); TimeUnit.SECONDS.sleep(eachProfUseTime); Manager.instance().setProfileFlag(false); // 等待已开始的End方法执行完成 TimeUnit.MILLISECONDS.sleep(500L); dumpProfileData(); } TimeUnit.SECONDS.sleep(eachProfIntervalTime); } } catch (Exception e) { e.printStackTrace(); } finally { Manager.instance().setProfileFlag(false); if (fileWriter != null) { fileWriter.closeFile(); } // 等待已开始的End方法执行完成 try { TimeUnit.MILLISECONDS.sleep(500L); } catch (InterruptedException e) { e.printStackTrace(); } Profiler.clearData(); } }
/* (non-Javadoc) * @see java.lang.instrument.ClassFileTransformer#transform(java.lang.ClassLoader, java.lang.String, java.lang.Class, java.security.ProtectionDomain, byte[]) */ public byte[] transform( ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { if (loader != null && ProfFilter.isNotNeedInjectClassLoader(loader.getClass().getName())) { return classfileBuffer; } if (!ProfFilter.isNeedInject(className)) { return classfileBuffer; } if (ProfFilter.isNotNeedInject(className)) { return classfileBuffer; } if (Manager.instance().isDebugMode()) { System.out.println( " ---- TProfiler Debug: ClassLoader:" + loader + " ---- class: " + className); } // 记录注入类数 Profiler.instrumentClassCount.getAndIncrement(); try { ClassReader reader = new ClassReader(classfileBuffer); ClassWriter writer = new ClassWriter(ClassWriter.COMPUTE_MAXS); ClassAdapter adapter = new ProfClassAdapter(writer, className); reader.accept(adapter, 0); // 生成新类字节码 return writer.toByteArray(); } catch (Throwable e) { e.printStackTrace(); // 返回旧类字节码 return classfileBuffer; } }