-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalyzer.js
354 lines (354 loc) · 11 KB
/
analyzer.js
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
function LockInfo(objectId){
return {
objectId:objectId,
waitings:[],
locked:null,
onChange:function(){
if(this.locked){
this.locked.onLockInfoChanged(this);
}
for(var i=0;i<this.waitings.length;i++){
this.waitings[i].onLockInfoChanged(this);
}
},
addWaitings:function(thr){
this.waitings.push(thr);
this.onChange();
},
setLocked:function(thr){
this.locked = thr;
this.onChange();
}
};
}
function LinesInfo(start,end){
var l = $('<span class="linesInfo">')
.text('line:')
.append($('<span class="lineNum">').text(start));
if( end!==undefined ){
l.append("-").append($('<span class="lineNum">').text(end));
}
return l;
}
function Thread(threads, threadId, threadName, condition, preThread, titleLine, startLineNumber){
var div = $('<div class="thread tid-'+threadId+'"><span class="state"/>')
.append($("<span>").text('"'+threadName+'" '+condition))
.append("<span class=topStack>")
.hide()
.prepend($('<div style="float:right"><span class="expand-btn glyphicon glyphicon-chevron-left">'));
var lines = [titleLine];
var hasTopStack = false;
var self = {
div:div,
threadDump:threads,
threadId:threadId,
preThread:preThread,
linesData:lines,
startLineNumber:startLineNumber,
preThreads:function(){
var thread = this;
var ret = [];
while(thread){
ret.push(thread);
thread=thread.preThread;
}
return ret;
},
addState:function(state,line){
this.state = state;
lines.push(line);
div.addClass("state-"+state);
},
addWaiting:function(objectId,line){
lines.push(line);
if(!hasTopStack){
div.find(".topStack").text(line);
hasTopStack=true;
}
div.addClass("waiting-"+objectId);
threads.addWaiting(objectId,this);
},
addStackTrace:function(line){
if(!hasTopStack && this.state === "RUNNABLE"){
div.find(".topStack").text(line);
hasTopStack=true;
}
lines.push(line);
},
addLocked:function(objectId,line){
lines.push(line);
div.append($('<div class="locked-'+objectId+'" style="display:none"><span class="count"></span>').append($('<span>').text(line)));
threads.addLocked(objectId,this);
},
lines:function(){
var txt = div.find('.lines');
if(txt.length === 0){
txt = $('<div class="lines">').hide();
var before=null;
$.each(this.preThreads(),function(){
var t = this;
var view = $("<pre class='detail'>").text(t.threadDump.time).append(LinesInfo(t.startLineNumber,t.endLineNumber));
$.each(t.linesData,function(i){
$("<div class='line' data-ln="+i+">").text(this).appendTo(view);
});
if(before){
var i = 1;
var pr;
var cur;
var removed = 0;
while((pr=before.linesData[before.linesData.length - i])!==undefined &&
(cur=t.linesData[t.linesData.length - i])!==undefined){
if(pr===cur){
view.find("div[data-ln="+(t.linesData.length - i)+"]").remove();
removed ++;
}else{
break;
}
i++;
}
if(removed){
view.append($("<div class='longprocess'>").text("\t\t and same thread dump "+removed+" lines"));
}
}
view.appendTo(txt);
before = t;
});
txt.appendTo(div);
}
return txt;
},
onLockInfoChanged:function(lockInfo){
if(lockInfo.locked==this){
if(lockInfo.waitings.length){
if(lockInfo.waitings.length==1 && lockInfo.waitings[0]==this){
return;
}
div.find(".locked-"+lockInfo.objectId).show().find(".count").text(lockInfo.waitings.length+" waiting)");
threads.div.find('.waiting-'+lockInfo.objectId).addClass('blocked');
div.addClass('blocking');
}
}
},
finish:function(endLineNumber){
this.endLineNumber = endLineNumber;
var s = div.find(".state");
var tip=[];
$.each(this.preThreads(),function(){
tip.push(this.state);
var preState = $('<span class="prestate prethread-'+this.state+'">').text((this.state||"").charAt(0)).appendTo(s);
if(this.preThread && this.linesData.length > 1){
var join = $("<span>");
if(this.preThread.linesData.length == this.linesData.length && this.linesData.join("\n")===this.preThread.linesData.join("\n")){
join.text("=").appendTo(preState);
preState.addClass("longprocess");
}else{
join.text("<").appendTo(s);
}
}
});
s.prop("title",tip.join(" <= "));
}
};
div.data('thread',self);
return self;
}
function ThreadDump(preThreadDump, time, head, startLineNumber){
var div = $("<div class='threads'>");
var lockes = {};
var threadList={};
var threadSize=0;
var timeText = time;
if(preThreadDump && preThreadDump.time){
console.log("sec",new Date(time).getTime(),new Date(preThreadDump.time).getTime());
timeText += " after "+ ((new Date(time).getTime()-(new Date(preThreadDump.time).getTime()))/1000)+"sec";
}
var timeView = $('<div class="time">').text(timeText+" "+head).append(LinesInfo(startLineNumber));
var header = $("<div class='header'>").append(timeView).appendTo(div);
var threadBox = $('<div>').appendTo(div);
function lockOf(objectId){
lockes[objectId] = lockes[objectId] || LockInfo(objectId);
return lockes[objectId];
}
function preThread(threadId){
if(preThreadDump && preThreadDump.threadList[threadId]){
return preThreadDump.threadList[threadId];
}else{
return null;
}
}
return {
div:div,
header:header,
time:time,
threadSize:threadSize,
threadList:threadList,
startLineNumber:startLineNumber,
timeText:timeText,
threadBox:threadBox,
newThread:function(threadId,threadName,contidion,titleLine,startLineNumber){
var thread = Thread(this, threadId, threadName, contidion, preThread(threadId), titleLine, startLineNumber);
thread.div.appendTo(threadBox);
threadList[threadId]=thread;
threadSize+=1;
return thread;
},
addWaiting:function(objectId,thread){
lockOf(objectId).addWaitings(thread);
},
addLocked:function(objectId,thread){
lockOf(objectId).setLocked(thread);
},
finish:function(endLineNumber){
this.endLineNumber = endLineNumber;
div.find('.header .linesInfo').html(LinesInfo(startLineNumber,endLineNumber));
var toolbar = $('<div class="bar">').text('Thread count='+threadSize).appendTo(header);
var t = $('<div class="btn-group" data-toggle="buttons">').appendTo(toolbar);
$.each("WAITING TIMED_WAITING RUNNABLE BLOCKED".split(" "),function(){
var state = this;
var checkbox = $('<input type=checkbox>').change(function(){
div.find('.state-'+state).toggle(this.checked);
});
var badge = $('<span class="badge">').text(div.find(".state-"+state).length);
$('<label class="lb-'+state+' btn btn-default">').text(state).append(badge).prepend(checkbox).appendTo(t);
});
div.delegate('.thread','dblclick',function(){
var t = $(this);
var eb = t.find('.expand-btn');
var isFolding = eb.hasClass('glyphicon-chevron-left');
if(isFolding){
t.data('thread').lines().show();
eb.removeClass('glyphicon-chevron-left').addClass('glyphicon-chevron-down');
}else{
t.data('thread').lines().hide();
eb.addClass('glyphicon-chevron-left').removeClass('glyphicon-chevron-down');
}
return false;
}).delegate('.expand-btn','click',function(){
$(this).parents('.thread').trigger('dblclick');
return false;
});
}
};
}
function Checker(pattern, func){
this.pattern = pattern;
this.func = func;
}
Checker.prototype.test = function(line){
var mm = this.pattern.exec(line);
if(mm){
this.func(mm,line);
return true;
}
};
function Parser(lines,div){
var thread = null;
var threadDumps = [];
var i=-1;
function threadFinish(){
if(thread){
thread.finish(i);
}
thread = null;
}
var oneThreadDump = null;
var preThreadDump = null;
var line;
var checkers = [
new Checker(/^\s+at [a-zA-Z].*$/,function(m,line){
thread.addStackTrace(line);
}),
new Checker(/^\s+- (waiting to lock|parking to wait for|waiting on|locked)\s+<(0x[a-f0-9]+)>/,function(m,line){
if(m[1] === "locked"){
thread.addLocked(m[2],line);
}else{
thread.addWaiting(m[2],line);
}
}),
new Checker(/^"([^"]+?)" (?:#\d+ )?(?:daemon )?(?:os_)?prio=\d+.* tid=(0x[a-f0-9]+)(?:\s+[a-zA-Z0-9_]+=0x[a-f0-9]+)*\s+([^\[]+)(?:\[|$)/, function(m,line){
threadFinish();
thread = oneThreadDump.newThread(m[2], m[1], m[3], line, i);
}),
new Checker(/^$/,function(m,line){
threadFinish();
}),
new Checker(/^\s+java.lang.Thread.State: ([A-Z_]+)/,function(m,line){
thread.addState(m[1],line);
}),
new Checker(/^(Heap|\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d$)/,function(m,line){
threadFinish();
next = findThreadDump;
oneThreadDump.finish(i);
preThreadDump = oneThreadDump;
oneThreadDump = null;
})
];
function parseThreadDump(){
var find = false;
if(line === undefined){
checkers[checkers.length-1].func([],line);
find=true;
}else for(var j=0;j<checkers.length; j++){
if(checkers[j].test(line)){
find=true;
break;
}
}
if(find===false){
console.log("unexpected line "+i+":"+line);
}
}
function findThreadDump(){
if(i>0 && /^Full thread dump/.test(line) && /^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d$/.test(lines[i-1])){
oneThreadDump = ThreadDump(preThreadDump, lines[i-1], line, i);
threadDumps.push(oneThreadDump);
if(preThreadDump){
oneThreadDump.div.insertBefore(preThreadDump.div);
}else{
oneThreadDump.div.appendTo(div);
}
next = parseThreadDump;
return;
}
}
var next = findThreadDump;
return {
threadDumps:threadDumps,
lines:lines,
lineNumber:function(){
return i;
},
hasNext:function(){
return i<lines.length;
},
next:function(){
i++;
line = lines[i];
try{
next();
}catch(e){
console.log("i=",i);
console.log("line=",lines[i-2]);
console.log("line=",lines[i-1]);
console.log("line=",lines[i]);
console.log("line=",lines[i+1]);
console.log("line=",lines[i+2]);
console.log("thread=",thread);
throw e;
}
}
};
}
function parse(lines,div,callback){
var p = Parser(lines,div);
function doParse(){
for(var i=0;i<1000 && p.hasNext(); i++){
p.next();
}
callback(p.lineNumber(),p.lines.length, p.threadDumps);
if(p.hasNext()){
setTimeout(doParse);
}
}
doParse();
}