/** * 方法开始时调用,采集开始时间 * * @param methodId */ public static void Start(int methodId) { if (!Manager.instance().canProfile()) { return; } long threadId = Thread.currentThread().getId(); if (threadId >= size) { return; } long startTime; if (Manager.isNeedNanoTime()) { startTime = System.nanoTime(); } else { startTime = System.currentTimeMillis(); } try { ThreadData thrData = threadProfile[(int) threadId]; if (thrData == null) { thrData = new ThreadData(); threadProfile[(int) threadId] = thrData; } long[] frameData = new long[3]; frameData[0] = methodId; frameData[1] = thrData.stackNum; frameData[2] = startTime; thrData.stackFrame.push(frameData); thrData.stackNum++; } catch (Exception e) { e.printStackTrace(); } }
/** * 方法退出时调用,采集结束时间 * * @param methodId */ public static void End(int methodId) { if (!Manager.instance().canProfile()) { return; } long threadId = Thread.currentThread().getId(); if (threadId >= size) { return; } long endTime; if (Manager.isNeedNanoTime()) { endTime = System.nanoTime(); } else { endTime = System.currentTimeMillis(); } try { ThreadData thrData = threadProfile[(int) threadId]; if (thrData == null || thrData.stackNum <= 0 || thrData.stackFrame.size() == 0) { // 没有执行start,直接执行end/可能是异步停止导致的 return; } // 栈太深则抛弃部分数据 if (thrData.profileData.size() > 20000) { thrData.stackNum--; thrData.stackFrame.pop(); return; } thrData.stackNum--; long[] frameData = thrData.stackFrame.pop(); long id = frameData[0]; if (methodId != id) { return; } long useTime = endTime - frameData[2]; if (Manager.isNeedNanoTime()) { if (useTime > 500000) { frameData[2] = useTime; thrData.profileData.push(frameData); } } else if (useTime > 1) { frameData[2] = useTime; thrData.profileData.push(frameData); } } catch (Exception e) { e.printStackTrace(); } }
/** * @param args * @param inst */ public static void premain(String args, Instrumentation inst) { inst.addTransformer(new ProfTransformer()); Manager.instance().startupThread(); }