Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: tools/profviz/composer.js

Issue 17592002: Add v8.log visualizer page. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: small changes Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/presubmit.py ('k') | tools/profviz/profviz.css » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 Array.prototype.top = function() {
29 if (this.length == 0) return undefined;
30 return this[this.length - 1];
31 }
32
33
34 function PlotScriptComposer(kResX, kResY) {
35 // Constants.
36 var kV8BinarySuffixes = ["/d8", "/libv8.so"];
37 var kStackFrames = 8; // Stack frames to display in the plot.
38
39 var kTimerEventWidth = 0.33; // Width of each timeline.
40 var kExecutionFrameWidth = 0.2; // Width of the top stack frame line.
41 var kStackFrameWidth = 0.1; // Width of the lower stack frame lines.
42 var kGapWidth = 0.05; // Gap between stack frame lines.
43
44 var kY1Offset = 10; // Offset for stack frame vs. event lines.
45 var kPauseLabelPadding = 5; // Padding for pause time labels.
46 var kNumPauseLabels = 7; // Number of biggest pauses to label.
47 var kCodeKindLabelPadding = 100; // Padding for code kind labels.
48
49 var kTickHalfDuration = 0.5; // Duration of half a tick in ms.
50 var kMinRangeLength = 0.0005; // Minimum length for an event in ms.
51
52 var kNumThreads = 2; // Number of threads.
53 var kExecutionThreadId = 0; // ID of main thread.
54
55 // Data structures.
56 function TimerEvent(label, color, pause, thread_id) {
57 assert(thread_id >= 0 && thread_id < kNumThreads, "invalid thread id");
58 this.label = label;
59 this.color = color;
60 this.pause = pause;
61 this.ranges = [];
62 this.thread_id = thread_id;
63 this.index = ++num_timer_event;
64 }
65
66 function CodeKind(color, kinds) {
67 this.color = color;
68 this.in_execution = [];
69 this.stack_frames = [];
70 for (var i = 0; i < kStackFrames; i++) this.stack_frames.push([]);
71 this.kinds = kinds;
72 }
73
74 function Range(start, end) {
75 // Everthing here are in milliseconds.
76 this.start = start;
77 this.end = end;
78 }
79
80 Range.prototype.duration = function() { return this.end - this.start; }
81
82 function Tick(tick) {
83 this.tick = tick;
84 }
85
86 // Init values.
87 var num_timer_event = kY1Offset + 0.5;
88
89 var TimerEvents = {
90 'V8.Execute':
91 new TimerEvent("execution", "#000000", false, 0),
92 'V8.External':
93 new TimerEvent("external", "#3399FF", false, 0),
94 'V8.CompileFullCode':
95 new TimerEvent("compile unopt", "#CC0000", true, 0),
96 'V8.RecompileSynchronous':
97 new TimerEvent("recompile sync", "#CC0044", true, 0),
98 'V8.RecompileParallel':
99 new TimerEvent("recompile async", "#CC4499", false, 1),
100 'V8.CompileEval':
101 new TimerEvent("compile eval", "#CC4400", true, 0),
102 'V8.Parse':
103 new TimerEvent("parse", "#00CC00", true, 0),
104 'V8.PreParse':
105 new TimerEvent("preparse", "#44CC00", true, 0),
106 'V8.ParseLazy':
107 new TimerEvent("lazy parse", "#00CC44", true, 0),
108 'V8.GCScavenger':
109 new TimerEvent("gc scavenge", "#0044CC", true, 0),
110 'V8.GCCompactor':
111 new TimerEvent("gc compaction", "#4444CC", true, 0),
112 'V8.GCContext':
113 new TimerEvent("gc context", "#4400CC", true, 0),
114 };
115
116 var CodeKinds = {
117 'external ': new CodeKind("#3399FF", [-2]),
118 'runtime ': new CodeKind("#000000", [-1]),
119 'full code': new CodeKind("#DD0000", [0]),
120 'opt code ': new CodeKind("#00EE00", [1]),
121 'code stub': new CodeKind("#FF00FF", [2]),
122 'built-in ': new CodeKind("#AA00AA", [3]),
123 'inl.cache': new CodeKind("#4444AA",
124 [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]),
125 'reg.exp. ': new CodeKind("#0000FF", [15]),
126 };
127
128 var code_map = new CodeMap();
129 var execution_pauses = [];
130 var event_stack = [];
131 var last_time_stamp = [];
132 for (var i = 0; i < kNumThreads; i++) {
133 event_stack[i] = [];
134 last_time_stamp[i] = -1;
135 }
136
137 var range_start = undefined;
138 var range_end = undefined;
139 var obj_index = 0;
140 var pause_tolerance = 0.005; // Milliseconds.
141 var distortion = 0;
142
143 // Utility functions.
144 function assert(something, message) {
145 if (!something) print(new Error(message).stack);
146 }
147
148 function FindCodeKind(kind) {
149 for (name in CodeKinds) {
150 if (CodeKinds[name].kinds.indexOf(kind) >= 0) {
151 return CodeKinds[name];
152 }
153 }
154 }
155
156 function TicksToRanges(ticks) {
157 var ranges = [];
158 for (var i = 0; i < ticks.length; i++) {
159 var tick = ticks[i].tick;
160 ranges.push(
161 new Range(tick - kTickHalfDuration, tick + kTickHalfDuration));
162 }
163 return ranges;
164 }
165
166 function MergeRanges(ranges) {
167 ranges.sort(function(a, b) { return a.start - b.start; });
168 var result = [];
169 var j = 0;
170 for (var i = 0; i < ranges.length; i = j) {
171 var merge_start = ranges[i].start;
172 if (merge_start > range_end) break; // Out of plot range.
173 var merge_end = ranges[i].end;
174 for (j = i + 1; j < ranges.length; j++) {
175 var next_range = ranges[j];
176 // Don't merge ranges if there is no overlap (incl. merge tolerance).
177 if (next_range.start > merge_end + pause_tolerance) break;
178 // Merge ranges.
179 if (next_range.end > merge_end) { // Extend range end.
180 merge_end = next_range.end;
181 }
182 }
183 if (merge_end < range_start) continue; // Out of plot range.
184 if (merge_end < merge_start) continue; // Not an actual range.
185 result.push(new Range(merge_start, merge_end));
186 }
187 return result;
188 }
189
190 function RestrictRangesTo(ranges, start, end) {
191 var result = [];
192 for (var i = 0; i < ranges.length; i++) {
193 if (ranges[i].start <= end && ranges[i].end >= start) {
194 result.push(new Range(Math.max(ranges[i].start, start),
195 Math.min(ranges[i].end, end)));
196 }
197 }
198 return result;
199 }
200
201 // Public methods.
202 this.collectData = function(input, distortion_per_entry) {
203
204 // Parse functions.
205 var parseTimeStamp = function(timestamp) {
206 distortion += distortion_per_entry;
207 return parseInt(timestamp) / 1000 - distortion;
208 }
209
210 var processTimerEventStart = function(name, start) {
211 // Find out the thread id.
212 var new_event = TimerEvents[name];
213 if (new_event === undefined) return;
214 var thread_id = new_event.thread_id;
215
216 start = Math.max(last_time_stamp[thread_id] + kMinRangeLength, start);
217
218 // Last event on this thread is done with the start of this event.
219 var last_event = event_stack[thread_id].top();
220 if (last_event !== undefined) {
221 var new_range = new Range(last_time_stamp[thread_id], start);
222 last_event.ranges.push(new_range);
223 }
224 event_stack[thread_id].push(new_event);
225 last_time_stamp[thread_id] = start;
226 };
227
228 var processTimerEventEnd = function(name, end) {
229 // Find out about the thread_id.
230 var finished_event = TimerEvents[name];
231 var thread_id = finished_event.thread_id;
232 assert(finished_event === event_stack[thread_id].pop(),
233 "inconsistent event stack");
234
235 end = Math.max(last_time_stamp[thread_id] + kMinRangeLength, end);
236
237 var new_range = new Range(last_time_stamp[thread_id], end);
238 finished_event.ranges.push(new_range);
239 last_time_stamp[thread_id] = end;
240 };
241
242 var processCodeCreateEvent = function(type, kind, address, size, name) {
243 var code_entry = new CodeMap.CodeEntry(size, name);
244 code_entry.kind = kind;
245 code_map.addCode(address, code_entry);
246 };
247
248 var processCodeMoveEvent = function(from, to) {
249 code_map.moveCode(from, to);
250 };
251
252 var processCodeDeleteEvent = function(address) {
253 code_map.deleteCode(address);
254 };
255
256 var processSharedLibrary = function(name, start, end) {
257 var code_entry = new CodeMap.CodeEntry(end - start, name);
258 code_entry.kind = -2; // External code kind.
259 for (var i = 0; i < kV8BinarySuffixes.length; i++) {
260 var suffix = kV8BinarySuffixes[i];
261 if (name.indexOf(suffix, name.length - suffix.length) >= 0) {
262 code_entry.kind = -1; // V8 runtime code kind.
263 break;
264 }
265 }
266 code_map.addLibrary(start, code_entry);
267 };
268
269 var processTimerEventStart = function(name, start) {
270 // Find out the thread id.
271 var new_event = TimerEvents[name];
272 if (new_event === undefined) return;
273 var thread_id = new_event.thread_id;
274
275 start = Math.max(last_time_stamp[thread_id] + kMinRangeLength, start);
276
277 // Last event on this thread is done with the start of this event.
278 var last_event = event_stack[thread_id].top();
279 if (last_event !== undefined) {
280 var new_range = new Range(last_time_stamp[thread_id], start);
281 last_event.ranges.push(new_range);
282 }
283 event_stack[thread_id].push(new_event);
284 last_time_stamp[thread_id] = start;
285 };
286
287 var processTimerEventEnd = function(name, end) {
288 // Find out about the thread_id.
289 var finished_event = TimerEvents[name];
290 var thread_id = finished_event.thread_id;
291 assert(finished_event === event_stack[thread_id].pop(),
292 "inconsistent event stack");
293
294 end = Math.max(last_time_stamp[thread_id] + kMinRangeLength, end);
295
296 var new_range = new Range(last_time_stamp[thread_id], end);
297 finished_event.ranges.push(new_range);
298 last_time_stamp[thread_id] = end;
299 };
300
301 var processCodeCreateEvent = function(type, kind, address, size, name) {
302 var code_entry = new CodeMap.CodeEntry(size, name);
303 code_entry.kind = kind;
304 code_map.addCode(address, code_entry);
305 };
306
307 var processCodeMoveEvent = function(from, to) {
308 code_map.moveCode(from, to);
309 };
310
311 var processCodeDeleteEvent = function(address) {
312 code_map.deleteCode(address);
313 };
314
315 var processSharedLibrary = function(name, start, end) {
316 var code_entry = new CodeMap.CodeEntry(end - start, name);
317 code_entry.kind = -3; // External code kind.
318 for (var i = 0; i < kV8BinarySuffixes.length; i++) {
319 var suffix = kV8BinarySuffixes[i];
320 if (name.indexOf(suffix, name.length - suffix.length) >= 0) {
321 code_entry.kind = -1; // V8 runtime code kind.
322 break;
323 }
324 }
325 code_map.addLibrary(start, code_entry);
326 };
327
328 var processTickEvent = function(
329 pc, sp, timer, unused_x, unused_y, vmstate, stack) {
330 var tick = new Tick(timer);
331
332 var entry = code_map.findEntry(pc);
333 if (entry) FindCodeKind(entry.kind).in_execution.push(tick);
334
335 for (var i = 0; i < kStackFrames; i++) {
336 if (!stack[i]) break;
337 var entry = code_map.findEntry(stack[i]);
338 if (entry) FindCodeKind(entry.kind).stack_frames[i].push(tick);
339 }
340 };
341 // Collect data from log.
342 var logreader = new LogReader(
343 { 'timer-event-start': { parsers: [null, parseTimeStamp],
344 processor: processTimerEventStart },
345 'timer-event-end': { parsers: [null, parseTimeStamp],
346 processor: processTimerEventEnd },
347 'shared-library': { parsers: [null, parseInt, parseInt],
348 processor: processSharedLibrary },
349 'code-creation': { parsers: [null, parseInt, parseInt, parseInt, null],
350 processor: processCodeCreateEvent },
351 'code-move': { parsers: [parseInt, parseInt],
352 processor: processCodeMoveEvent },
353 'code-delete': { parsers: [parseInt],
354 processor: processCodeDeleteEvent },
355 'tick': { parsers: [parseInt, parseInt, parseTimeStamp,
356 null, null, parseInt, 'var-args'],
357 processor: processTickEvent }
358 });
359
360 var line;
361 while (line = input()) {
362 logreader.processLogLine(line);
363 }
364
365 // Collect execution pauses.
366 for (name in TimerEvents) {
367 var event = TimerEvents[name];
368 if (!event.pause) continue;
369 var ranges = event.ranges;
370 for (var j = 0; j < ranges.length; j++) execution_pauses.push(ranges[j]);
371 }
372 execution_pauses = MergeRanges(execution_pauses);
373 };
374
375
376 this.findPlotRange = function(
377 range_start_override, range_end_override, result_callback) {
378 var start_found = (range_start_override || range_start_override == 0);
379 var end_found = (range_end_override || range_end_override == 0);
380 range_start = start_found ? range_start_override : Infinity;
381 range_end = end_found ? range_end_override : -Infinity;
382
383 if (!start_found || !end_found) {
384 for (name in TimerEvents) {
385 var ranges = TimerEvents[name].ranges;
386 for (var i = 0; i < ranges.length; i++) {
387 if (ranges[i].start < range_start && !start_found) {
388 range_start = ranges[i].start;
389 }
390 if (ranges[i].end > range_end && !end_found) {
391 range_end = ranges[i].end;
392 }
393 }
394 }
395
396 for (codekind in CodeKinds) {
397 var ticks = CodeKinds[codekind].in_execution;
398 for (var i = 0; i < ticks.length; i++) {
399 if (ticks[i].tick < range_start && !start_found) {
400 range_start = ticks[i].tick;
401 }
402 if (ticks[i].tick > range_end && !end_found) {
403 range_end = ticks[i].tick;
404 }
405 }
406 }
407 }
408 // Set pause tolerance to something appropriate for the plot resolution
409 // to make it easier for gnuplot.
410 pause_tolerance = (range_end - range_start) / kResX / 10;
411
412 if (typeof result_callback === 'function') {
413 result_callback(range_start, range_end);
414 }
415 };
416
417
418 this.assembleOutput = function(output) {
419 output("set yrange [0:" + (num_timer_event + 1) + "]");
420 output("set xlabel \"execution time in ms\"");
421 output("set xrange [" + range_start + ":" + range_end + "]");
422 output("set style fill pattern 2 bo 1");
423 output("set style rect fs solid 1 noborder");
424 output("set style line 1 lt 1 lw 1 lc rgb \"#000000\"");
425 output("set xtics out nomirror");
426 output("unset key");
427
428 function DrawBar(row, color, start, end, width) {
429 obj_index++;
430 command = "set object " + obj_index + " rect";
431 command += " from " + start + ", " + (row - width);
432 command += " to " + end + ", " + (row + width);
433 command += " fc rgb \"" + color + "\"";
434 output(command);
435 }
436
437 var percentages = {};
438 var total = 0;
439 for (var name in TimerEvents) {
440 var event = TimerEvents[name];
441 var ranges = RestrictRangesTo(event.ranges, range_start, range_end);
442 ranges = MergeRanges(ranges);
443 var sum =
444 ranges.map(function(range) { return range.duration(); })
445 .reduce(function(a, b) { return a + b; }, 0);
446 percentages[name] = (sum / (range_end - range_start) * 100).toFixed(1);
447 }
448
449 // Name Y-axis.
450 var ytics = [];
451 for (name in TimerEvents) {
452 var index = TimerEvents[name].index;
453 var label = TimerEvents[name].label;
454 ytics.push('"' + label + ' (' + percentages[name] + '%%)" ' + index);
455 }
456 ytics.push('"code kind color coding" ' + kY1Offset);
457 ytics.push('"code kind in execution" ' + (kY1Offset - 1));
458 ytics.push('"top ' + kStackFrames + ' js stack frames"' + ' ' +
459 (kY1Offset - 2));
460 ytics.push('"pause times" 0');
461 output("set ytics out nomirror (" + ytics.join(', ') + ")");
462
463 // Plot timeline.
464 for (var name in TimerEvents) {
465 var event = TimerEvents[name];
466 var ranges = MergeRanges(event.ranges);
467 for (var i = 0; i < ranges.length; i++) {
468 DrawBar(event.index, event.color,
469 ranges[i].start, ranges[i].end,
470 kTimerEventWidth);
471 }
472 }
473
474 // Plot code kind gathered from ticks.
475 for (var name in CodeKinds) {
476 var code_kind = CodeKinds[name];
477 var offset = kY1Offset - 1;
478 // Top most frame.
479 var row = MergeRanges(TicksToRanges(code_kind.in_execution));
480 for (var j = 0; j < row.length; j++) {
481 DrawBar(offset, code_kind.color,
482 row[j].start, row[j].end, kExecutionFrameWidth);
483 }
484 offset = offset - 2 * kExecutionFrameWidth - kGapWidth;
485 // Javascript frames.
486 for (var i = 0; i < kStackFrames; i++) {
487 offset = offset - 2 * kStackFrameWidth - kGapWidth;
488 row = MergeRanges(TicksToRanges(code_kind.stack_frames[i]));
489 for (var j = 0; j < row.length; j++) {
490 DrawBar(offset, code_kind.color,
491 row[j].start, row[j].end, kStackFrameWidth);
492 }
493 }
494 }
495
496 // Add labels as legend for code kind colors.
497 var padding = kCodeKindLabelPadding * (range_end - range_start) / kResX;
498 var label_x = range_start;
499 var label_y = kY1Offset;
500 for (var name in CodeKinds) {
501 label_x += padding;
502 output("set label \"" + name + "\" at " + label_x + "," + label_y +
503 " textcolor rgb \"" + CodeKinds[name].color + "\"" +
504 " font \"Helvetica,9'\"");
505 obj_index++;
506 }
507
508 if (execution_pauses.length == 0) {
509 // Force plot and return without plotting execution pause impulses.
510 output("plot 1/0");
511 return;
512 }
513
514 // Label the longest pauses.
515 execution_pauses.sort(
516 function(a, b) { return b.duration() - a.duration(); });
517
518 var max_pause_time = execution_pauses[0].duration();
519 padding = kPauseLabelPadding * (range_end - range_start) / kResX;
520 var y_scale = kY1Offset / max_pause_time / 2;
521 for (var i = 0; i < execution_pauses.length && i < kNumPauseLabels; i++) {
522 var pause = execution_pauses[i];
523 var label_content = (pause.duration() | 0) + " ms";
524 var label_x = pause.end + padding;
525 var label_y = Math.max(1, (pause.duration() * y_scale));
526 output("set label \"" + label_content + "\" at " +
527 label_x + "," + label_y + " font \"Helvetica,7'\"");
528 obj_index++;
529 }
530
531 // Scale second Y-axis appropriately.
532 var y2range = max_pause_time * num_timer_event / kY1Offset * 2;
533 output("set y2range [0:" + y2range + "]");
534 // Plot graph with impulses as data set.
535 output("plot '-' using 1:2 axes x1y2 with impulses ls 1");
536 for (var i = 0; i < execution_pauses.length; i++) {
537 var pause = execution_pauses[i];
538 output(pause.end + " " + pause.duration());
539 obj_index++;
540 }
541 output("e");
542 return obj_index;
543 };
544 }
OLDNEW
« no previous file with comments | « tools/presubmit.py ('k') | tools/profviz/profviz.css » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698