Skip to content

Commit

Permalink
增加两次Profile之间的日志分隔符,对应修改日志分析代码
Browse files Browse the repository at this point in the history
如果单次采集到单线程的调用栈大于20000,则放弃之后的数据
  • Loading branch information
丁宇 committed Apr 26, 2012
1 parent d75edca commit 51339af
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 40 deletions.
Binary file modified dist/tprofiler.jar
Binary file not shown.
17 changes: 4 additions & 13 deletions src/main/java/com/taobao/profile/Profiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,26 +92,17 @@ public static void End(int methodId) {
}
try {
ThreadData thrData = threadProfile[(int) threadId];
if (thrData == null) {
if (thrData == null || thrData.stackNum <= 0 || thrData.stackFrame.size() == 0) {
// 没有执行start,直接执行end/可能是异步停止导致的
return;
}
// 栈太深则抛弃一部分数据
if (thrData.stackFrame.size() > 10000) {
// 栈太深则抛弃部分数据
if (thrData.profileData.size() > 20000) {
thrData.stackNum--;
thrData.stackFrame.pop();
return;
}

if (thrData.stackNum <= 0) {
// 没有执行start,直接执行end
return;
}
thrData.stackNum--;

if (thrData.stackFrame.size() == 0) {
// 可能是异步停止导致的,忽略
return;
}
long[] frameData = thrData.stackFrame.pop();
long id = frameData[0];
if (methodId != id) {
Expand Down
26 changes: 14 additions & 12 deletions src/main/java/com/taobao/profile/analysis/ProfilerLogAnalysis.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ private void reader() {
}
continue;
}
if ("=".equals(line)) {
currentthreadId = -1;
doMerge();
}
String[] data = line.split("\t");
if (data.length != 4) {
continue;
Expand Down Expand Up @@ -157,9 +161,9 @@ private void doMerge() {
for (int j = i + 1; j < threadList.size(); j++) {
MethodStack tmp = threadList.get(j);
long tmpStack = tmp.stackNum;
if (tmpStack - statck == 1) {
if (statck + 1 == tmpStack) {
m.useTime -= tmp.useTime;
} else if (tmpStack - statck <= 0) {
} else if (statck >= tmpStack) {
break;
}
}
Expand All @@ -172,13 +176,11 @@ private void doMerge() {
TimeSortData sortData = cacheMethodMap.get(m.methodId);
if (sortData == null) {
sortData = new TimeSortData();
sortData.methodName = methodIdMap.get(m.methodId);
sortData.valueStack.add(m.useTime);
sortData.size++;
sortData.setMethodName(methodIdMap.get(m.methodId));
sortData.addStackValue(m.useTime);
cacheMethodMap.put(m.methodId, sortData);
} else {
sortData.valueStack.add(m.useTime);
sortData.size++;
sortData.addStackValue(m.useTime);
}
}
threadList.clear();
Expand All @@ -199,17 +201,17 @@ public void printResult(String topMethodPath, String topObjectPath) {
topObjectWriter = new BufferedWriter(new FileWriter(topObjectPath));
for (TimeSortData data : list) {
StringBuilder sb = new StringBuilder();
Stack<Long> stack = data.valueStack;
Stack<Long> stack = data.getValueStack();

long executeNum = stack.size();
long allTime;
if (nano) {
allTime = Math.div(data.sum, 1000000);
allTime = Math.div(data.getSum(), 1000000);
} else {
allTime = data.sum;
allTime = data.getSum();
}
long useTime = Math.div(allTime, executeNum);
sb.append(data.methodName);
sb.append(data.getMethodName());
sb.append("\t");
sb.append(executeNum);
sb.append("\t");
Expand All @@ -218,7 +220,7 @@ public void printResult(String topMethodPath, String topObjectPath) {
sb.append(allTime);
sb.append("\n");
topMethodWriter.write(sb.toString());
if (data.methodName != null && data.methodName.contains("<init>")) {
if (data.getMethodName() != null && data.getMethodName().contains("<init>")) {
topObjectWriter.write(sb.toString());
}
}
Expand Down
53 changes: 40 additions & 13 deletions src/main/java/com/taobao/profile/analysis/TimeSortData.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,55 @@
*/
public class TimeSortData implements Comparable<TimeSortData> {

public float max;
public float min;
public long size = 0;
public long sum = 0;
public String methodName;
public Stack<Long> valueStack = new Stack<Long>();
private long sum = 0;
private String methodName;
private Stack<Long> valueStack = new Stack<Long>();

/**
* @return
* @return the methodName
*/
private long getValue() {
for (long v : valueStack) {
sum = Math.add(sum, v);
}
public String getMethodName() {
return methodName;
}

/**
* @param methodName
* the methodName to set
*/
public void setMethodName(String methodName) {
this.methodName = methodName;
}

/**
* @return the valueStack
*/
public Stack<Long> getValueStack() {
return valueStack;
}

/**
* @param useTime
*
*/
public void addStackValue(long useTime) {
valueStack.add(useTime);
sum += useTime;
}

/**
* @return the sum
*/
public long getSum() {
return sum;
}

/* (non-Javadoc)
/*
* (non-Javadoc)
*
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
public int compareTo(TimeSortData o) {
if (this.getValue() > o.getValue()) {
if (this.sum > o.sum) {
return -1;
} else {
return 1;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/taobao/profile/runtime/MethodCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class MethodCache {
/**
* 方法名缓存
*/
public static Vector<MethodInfo> mCacheMethods = new Vector<MethodInfo>(INIT_CACHE_SIZE);
private static Vector<MethodInfo> mCacheMethods = new Vector<MethodInfo>(INIT_CACHE_SIZE);

/**
* 占位并生成方法ID
Expand Down Expand Up @@ -85,7 +85,7 @@ public synchronized static void flushMethodData() {
fileWriter.append(String.valueOf(Profiler.instrumentMethodCount));
fileWriter.append("\n");

Vector<MethodInfo> vector = MethodCache.mCacheMethods;
Vector<MethodInfo> vector = mCacheMethods;
int size = vector.size();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; i++) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/taobao/profile/thread/DataDumpThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,7 @@ private void dumpProfileData() {
fileWriter.flushAppend();
profilerData.clear();
}
fileWriter.append("=\n");
fileWriter.flushAppend();
}
}

0 comments on commit 51339af

Please sign in to comment.