forked from nitsanw/java-collapsed-stacks-massage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMassage.java
More file actions
executable file
·109 lines (103 loc) · 3.93 KB
/
Copy pathMassage.java
File metadata and controls
executable file
·109 lines (103 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Massage
{
public static void main(String[] args) throws IOException
{
String mappings = System.getProperty("mapping","io/netty/=NETTY[j],org/apache/cassandra/utils/flow=FLOW[j],io/reactivex=RX[j]");
boolean keepTid = Boolean.getBoolean("keepTid");
Map<String,String> prefix2fold = new HashMap<>();
String[] elements = mappings.split(",");
for (String element : elements)
{
String[] kv = element.split("=");
prefix2fold.put(kv[0],kv[1]);
}
Set<String> javaThreads = new HashSet<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = reader.readLine()) != null)
{
int countSeparatorIndex = line.lastIndexOf(" ");
final String stack = line.substring(0, countSeparatorIndex);
final String count = line.substring(countSeparatorIndex + 1);
int postPrefix = stack.indexOf(";") + 1;
final String prefix = stack.substring(0, postPrefix);
String tmp = keepTid && prefix.startsWith("java-") ? stack : stack.substring(postPrefix);
if (stack.contains(";GCTaskThread") ||
stack.contains(";CompilerThread::") ||
stack.contains(";GangWorker::") ||
stack.contains(";WatcherThread::") ||
stack.contains(";ConcurrentG1RefineThread::") ||
stack.contains(";VMThread") ||
stack.contains(";G1ParScanThreadState::") ||
stack.contains(";G1RemSet::") ||
stack.contains("CompileBroker"))
{
System.out.print("JVMThread;");
printRemaining(count, tmp, prefix2fold);
}
else if (javaThreads.contains(prefix) ||
stack.contains("[j]") ||
stack.contains("[i]") ||
stack.contains("Interpreter") ||
stack.contains("...") ||
stack.contains("java"))
{
System.out.print("JavaThread[j];");
int ix;
// trim stacks up to first Java frame
if ((ix = stack.indexOf("Interpreter")) != -1 || (ix=stack.indexOf("java/lang/Thread")) != -1)
{
if (prefix.startsWith("java-"))
{
javaThreads.add(prefix);
tmp = keepTid ? prefix + stack.substring(ix) : stack.substring(ix);
}
else
{
tmp = stack.substring(ix);
}
}
printRemaining(count, tmp, prefix2fold);
}
else
{
System.out.println(tmp + " " + count);
}
}
}
private static void printRemaining(
String count,
String stack,
Map<String, String> prefix2folds)
{
String[] frames = stack.split(";");
System.out.print(frames[0]);
for (int i = 1; i < frames.length; i++)
{
for (Map.Entry<String,String> prefix2fold : prefix2folds.entrySet())
{
if (frames[i].startsWith(prefix2fold.getKey()))
{
frames[i] = prefix2fold.getValue();
break;
}
}
// skip same frame by ref equality -> the folded frames
if (frames[i] != frames[i - 1])
{
System.out.print(";");
System.out.print(frames[i]);
}
}
System.out.print(" ");
System.out.print(count);
System.out.println();
}
}