plot-timer-events.js 18.7 KB
Newer Older
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
// Copyright 2012 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28 29
var kV8BinarySuffixes = ["/d8", "/libv8.so"];
var kStackFrames = 8;
30

31 32 33 34 35 36 37
var kTimerEventWidth = 0.33;
var kExecutionFrameWidth = 0.2;
var kStackFrameWidth = 0.1;
var kGapWidth = 0.05;

var kPauseTolerance = 0.1;  // Milliseconds.
var kY1Offset = 10;
38 39

var kResX = 1600;
40 41
var kResY = 600;
var kPauseLabelPadding = 5;
42
var kNumPauseLabels = 7;
43 44
var kTickHalfDuration = 0.5;  // Milliseconds
var kCodeKindLabelPadding = 100;
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
var num_timer_event = kY1Offset + 0.5;


function TimerEvent(color, pause, no_execution) {
  this.color = color;
  this.pause = pause;
  this.ranges = [];
  this.no_execution = no_execution;
  this.index = ++num_timer_event;
}


var TimerEvents = {
  'V8.Execute':              new TimerEvent("#000000", false, false),
  'V8.External':             new TimerEvent("#3399FF", false,  true),
  'V8.CompileFullCode':      new TimerEvent("#CC0000",  true,  true),
  'V8.RecompileSynchronous': new TimerEvent("#CC0044",  true,  true),
  'V8.RecompileParallel':    new TimerEvent("#CC4499", false, false),
  'V8.CompileEval':          new TimerEvent("#CC4400",  true,  true),
  'V8.Parse':                new TimerEvent("#00CC00",  true,  true),
  'V8.PreParse':             new TimerEvent("#44CC00",  true,  true),
  'V8.ParseLazy':            new TimerEvent("#00CC44",  true,  true),
  'V8.GCScavenger':          new TimerEvent("#0044CC",  true,  true),
  'V8.GCCompactor':          new TimerEvent("#4444CC",  true,  true),
  'V8.GCContext':            new TimerEvent("#4400CC",  true,  true),
}

var kExecutionEvent = TimerEvents['V8.Execute'];


function CodeKind(color, kinds) {
  this.color = color;
  this.in_execution = [];
  this.stack_frames = [];
  for (var i = 0; i < kStackFrames; i++) this.stack_frames.push([]);
  this.kinds = kinds;
}


var CodeKinds = {
  'external ': new CodeKind("#3399FF", [-3]),
  'reg.exp. ': new CodeKind("#0000FF", [-2]),
  'runtime  ': new CodeKind("#000000", [-1]),
  'full code': new CodeKind("#DD0000", [0]),
  'opt code ': new CodeKind("#00EE00", [1]),
  'code stub': new CodeKind("#FF00FF", [2]),
  'built-in ': new CodeKind("#AA00AA", [3]),
  'inl.cache': new CodeKind("#4444AA", [4, 5, 6, 7, 8, 9, 10, 11, 12, 13]),
}


97 98
var xrange_start;
var xrange_end;
99 100
var obj_index = 0;
var execution_pauses = [];
101
var code_map = new CodeMap();
102

103 104 105 106 107 108 109 110
var xrange_start_override = undefined;
var xrange_end_override = undefined;
var distortion_per_entry = 0.005;  // Milliseconds

var sort_by_start = [];
var sort_by_end = [];
var sorted_ticks = [];

111 112 113 114 115 116 117 118

function Range(start, end) {
  // Everthing from here are in milliseconds.
  this.start = start;
  this.end = end;
}


119 120 121 122 123
function Tick(tick) {
  this.tick = tick;
}


124 125 126 127 128 129 130 131 132
Range.prototype.duration = function() { return this.end - this.start; }


function ProcessTimerEvent(name, start, length) {
  var event = TimerEvents[name];
  if (event === undefined) return;
  start /= 1000;  // Convert to milliseconds.
  length /= 1000;
  var end = start + length;
133 134 135 136
  var range = new Range(start, end);
  event.ranges.push(range);
  sort_by_start.push(range);
  sort_by_end.push(range);
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
function ProcessCodeCreateEvent(type, kind, address, size, name) {
  var code_entry = new CodeMap.CodeEntry(size, name);
  code_entry.kind = kind;
  code_map.addCode(address, code_entry);
}


function ProcessCodeMoveEvent(from, to) {
  code_map.moveCode(from, to);
}


function ProcessCodeDeleteEvent(address) {
  code_map.deleteCode(address);
}


function ProcessSharedLibrary(name, start, end) {
  var code_entry = new CodeMap.CodeEntry(end - start, name);
  code_entry.kind = -3;  // External code kind.
  for (var i = 0; i < kV8BinarySuffixes.length; i++) {
    var suffix = kV8BinarySuffixes[i];
    if (name.indexOf(suffix, name.length - suffix.length) >= 0) {
      code_entry.kind = -1;  // V8 runtime code kind.
      break;
    }
  }
  code_map.addLibrary(start, code_entry);
}


function FindCodeKind(kind) {
  for (name in CodeKinds) {
    if (CodeKinds[name].kinds.indexOf(kind) >= 0) {
      return CodeKinds[name];
    }
  }
}


function ProcessTickEvent(pc, sp, timer, unused_x, unused_y, vmstate, stack) {
  timer /= 1000;
182
  var tick = new Tick(timer);
183

184
  var entered = false;
185 186 187
  var entry = code_map.findEntry(pc);
  if (entry) {
    FindCodeKind(entry.kind).in_execution.push(tick);
188
    entered = true;
189 190 191 192 193 194 195
  }

  for (var i = 0; i < kStackFrames; i++) {
    if (!stack[i]) break;
    var entry = code_map.findEntry(stack[i]);
    if (entry) {
      FindCodeKind(entry.kind).stack_frames[i].push(tick);
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
      entered = true;
    }
  }

  if (entered) sorted_ticks.push(tick);
}


function ProcessDistortion(distortion_in_picoseconds) {
  distortion_per_entry = distortion_in_picoseconds / 1000000;
}


function ProcessPlotRange(start, end) {
  xrange_start_override = start;
  xrange_end_override = end;
}


function Undistort() {
  // Undistort timers wrt instrumentation overhead.
  sort_by_start.sort(function(a, b) { return b.start - a.start; });
  sort_by_end.sort(function(a, b) { return b.end - a.end; });
  sorted_ticks.sort(function(a, b) { return b.tick - a.tick; });
  var distortion = 0;

  var next_start = sort_by_start.pop();
  var next_end = sort_by_end.pop();
  var next_tick = sorted_ticks.pop();

  function UndistortTicksUntil(tick) {
    while (next_tick) {
      if (next_tick.tick > tick) return;
      next_tick.tick -= distortion;
      next_tick = sorted_ticks.pop();
    }
  }

  while (true) {
    var next_start_start = next_start ? next_start.start : Infinity;
    var next_end_end = next_end ? next_end.end : Infinity;
    if (!next_start && !next_end) {
      UndistortTicksUntil(Infinity);
      break;
    }
    if (next_start_start <= next_end_end) {
      UndistortTicksUntil(next_start_start);
      // Undistort the start time stamp.
      next_start.start -= distortion;
      next_start = sort_by_start.pop();
    } else {
      // Undistort the end time stamp.  We completely attribute the overhead
      // to the point when we stop and log the timer, so we increase the
      // distortion only here.
      UndistortTicksUntil(next_end_end);
      next_end.end -= distortion;
      distortion += distortion_per_entry;
      next_end = sort_by_end.pop();
    }
  }

  sort_by_start = undefined;
  sort_by_end = undefined;
  sorted_ticks = undefined;

  // Make sure that start <= end applies for every range.
  for (name in TimerEvents) {
    var ranges = TimerEvents[name].ranges;
    for (var j = 0; j < ranges.length; j++) {
      if (ranges[j].end < ranges[j].start) ranges[j].end = ranges[j].start;
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
function FindPlotRange() {
  var start_found = (xrange_start_override || xrange_start_override == 0);
  var end_found = (xrange_end_override || xrange_end_override == 0);
  xrange_start = start_found ? xrange_start_override : Infinity;
  xrange_end = end_found ? xrange_end_override : -Infinity;

  if (start_found && end_found) return;

  var execution_ranges = kExecutionEvent.ranges;
  for (var i = 0; i < execution_ranges.length; i++) {
    if (execution_ranges[i].start < xrange_start && !start_found) {
      xrange_start = execution_ranges[i].start;
    }
    if (execution_ranges[i].end > xrange_end && !end_found) {
      xrange_end = execution_ranges[i].end;
    }
  }

  for (codekind in CodeKinds) {
    var ticks = CodeKinds[codekind].in_execution;
    for (var i = 0; i < ticks.length; i++) {
      if (ticks[i].tick < xrange_start && !start_found) {
        xrange_start = ticks[i].tick;
      }
      if (ticks[i].tick > xrange_end && !end_found) {
        xrange_end = ticks[i].tick;
      }
    }
  }
}


303 304 305
function CollectData() {
  // Collect data from log.
  var logreader = new LogReader(
306 307 308 309 310 311 312 313 314 315 316 317 318
    { 'timer-event' :   { parsers: [null, parseInt, parseInt],
                          processor: ProcessTimerEvent },
      'shared-library': { parsers: [null, parseInt, parseInt],
                          processor: ProcessSharedLibrary },
      'code-creation':  { parsers: [null, parseInt, parseInt, parseInt, null],
                          processor: ProcessCodeCreateEvent },
      'code-move':      { parsers: [parseInt, parseInt],
                          processor: ProcessCodeMoveEvent },
      'code-delete':    { parsers: [parseInt],
                          processor: ProcessCodeDeleteEvent },
      'tick':           { parsers: [parseInt, parseInt, parseInt,
                                    null, null, parseInt, 'var-args'],
                          processor: ProcessTickEvent },
319 320 321 322
      'distortion':     { parsers: [parseInt],
                          processor: ProcessDistortion },
      'plot-range':     { parsers: [parseInt, parseInt],
                          processor: ProcessPlotRange },
323
    });
324 325 326 327 328 329

  var line;
  while (line = readline()) {
    logreader.processLogLine(line);
  }

330 331
  Undistort();

332 333 334 335 336
  // Collect execution pauses.
  for (name in TimerEvents) {
    var event = TimerEvents[name];
    if (!event.pause) continue;
    var ranges = event.ranges;
337 338 339 340 341 342 343 344 345 346 347 348
    for (var j = 0; j < ranges.length; j++) execution_pauses.push(ranges[j]);
  }
  execution_pauses = MergeRanges(execution_pauses);

  // Knock out time not spent in javascript execution.  Note that this also
  // includes time spent external code, which do not contribute to execution
  // pauses.
  var exclude_ranges = [];
  for (name in TimerEvents) {
    var event = TimerEvents[name];
    if (!event.no_execution) continue;
    var ranges = event.ranges;
349 350
    // Add ranges of this event to the pause list.
    for (var j = 0; j < ranges.length; j++) {
351
      exclude_ranges.push(ranges[j]);
352 353
    }
  }
354 355 356 357 358

  kExecutionEvent.ranges = MergeRanges(kExecutionEvent.ranges);
  exclude_ranges = MergeRanges(exclude_ranges);
  kExecutionEvent.ranges = ExcludeRanges(kExecutionEvent.ranges,
                                         exclude_ranges);
359 360 361
}


362
function DrawBar(row, color, start, end, width) {
363 364
  obj_index++;
  command = "set object " + obj_index + " rect";
365 366
  command += " from " + start + ", " + (row - width);
  command += " to " + end + ", " + (row + width);
367 368 369 370 371
  command += " fc rgb \"" + color + "\"";
  print(command);
}


372 373 374 375 376 377 378 379 380 381
function TicksToRanges(ticks) {
  var ranges = [];
  for (var i = 0; i < ticks.length; i++) {
    var tick = ticks[i].tick;
    ranges.push(new Range(tick - kTickHalfDuration, tick + kTickHalfDuration));
  }
  return ranges;
}


382
function MergeRanges(ranges) {
383 384 385 386 387 388 389 390 391 392
  ranges.sort(function(a, b) { return a.start - b.start; });
  var result = [];
  var j = 0;
  for (var i = 0; i < ranges.length; i = j) {
    var merge_start = ranges[i].start;
    if (merge_start > xrange_end) break;  // Out of plot range.
    var merge_end = ranges[i].end;
    for (j = i + 1; j < ranges.length; j++) {
      var next_range = ranges[j];
      // Don't merge ranges if there is no overlap (including merge tolerance).
393
      if (next_range.start > merge_end + kPauseTolerance) break;
394 395 396 397 398 399
      // Merge ranges.
      if (next_range.end > merge_end) {  // Extend range end.
        merge_end = next_range.end;
      }
    }
    if (merge_end < xrange_start) continue;  // Out of plot range.
400
    if (merge_end < merge_start) continue;  // Not an actual range.
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
    result.push(new Range(merge_start, merge_end));
  }
  return result;
}


function ExcludeRanges(include, exclude) {
  // We assume that both input lists are sorted and merged with MergeRanges.
  var result = [];
  var exclude_index = 0;
  var include_index = 0;
  var include_start, include_end, exclude_start, exclude_end;

  function NextInclude() {
    if (include_index >= include.length) return false;
    include_start = include[include_index].start;
    include_end = include[include_index].end;
    include_index++;
    return true;
  }

  function NextExclude() {
    if (exclude_index >= exclude.length) {
      // No more exclude, finish by repeating case (2).
      exclude_start = Infinity;
      exclude_end = Infinity;
      return false;
    }
    exclude_start = exclude[exclude_index].start;
    exclude_end = exclude[exclude_index].end;
    exclude_index++;
    return true;
  }

  if (!NextInclude() || !NextExclude()) return include;

  while (true) {
    if (exclude_end <= include_start) {
      // (1) Exclude and include do not overlap.
      // Include       #####
      // Exclude   ##
      NextExclude();
    } else if (include_end <= exclude_start) {
      // (2) Exclude and include do not overlap.
      // Include   #####
      // Exclude         ###
      result.push(new Range(include_start, include_end));
      if (!NextInclude()) break;
    } else if (exclude_start <= include_start &&
               exclude_end < include_end &&
               include_start < exclude_end) {
      // (3) Exclude overlaps with begin of include.
      // Include    #######
      // Exclude  #####
      // Result        ####
      include_start = exclude_end;
      NextExclude();
    } else if (include_start < exclude_start &&
               include_end <= exclude_end &&
               exclude_start < include_end) {
      // (4) Exclude overlaps with end of include.
      // Include    #######
      // Exclude        #####
      // Result     ####
      result.push(new Range(include_start, exclude_start));
      if (!NextInclude()) break;
    } else if (exclude_start > include_start && exclude_end < include_end) {
      // (5) Exclude splits include into two parts.
      // Include    #######
      // Exclude      ##
      // Result     ##  ###
      result.push(new Range(include_start, exclude_start));
      include_start = exclude_end;
      NextExclude();
    } else if (exclude_start <= include_start && exclude_end >= include_end) {
      // (6) Exclude entirely covers include.
      // Include    ######
      // Exclude   #########
      if (!NextInclude()) break;
    } else {
      throw new Error("this should not happen!");
    }
  }

  return result;
}


function GnuplotOutput() {
490 491
  FindPlotRange();

492 493
  print("set terminal pngcairo size " + kResX + "," + kResY +
        " enhanced font 'Helvetica,10'");
494
  print("set yrange [0:" + (num_timer_event + 1) + "]");
495 496 497 498 499 500 501 502 503 504 505 506
  print("set xlabel \"execution time in ms\"");
  print("set xrange [" + xrange_start + ":" + xrange_end + "]");
  print("set style fill pattern 2 bo 1");
  print("set style rect fs solid 1 noborder");
  print("set style line 1 lt 1 lw 1 lc rgb \"#000000\"");
  print("set xtics out nomirror");
  print("unset key");

  // Name Y-axis.
  var ytics = [];
  for (name in TimerEvents) {
    var index = TimerEvents[name].index;
507
    ytics.push('"' + name + '"' + ' ' + index);
508
  }
509 510 511 512
  ytics.push('"code kind being executed"' + ' ' + (kY1Offset - 1));
  ytics.push('"top ' + kStackFrames + ' js stack frames"' + ' ' +
             (kY1Offset - 2));
  ytics.push('"pause times" 0');
513 514 515 516 517
  print("set ytics out nomirror (" + ytics.join(', ') + ")");

  // Plot timeline.
  for (var name in TimerEvents) {
    var event = TimerEvents[name];
518
    var ranges = MergeRanges(event.ranges);
519
    for (var i = 0; i < ranges.length; i++) {
520 521 522 523 524 525 526 527 528 529 530
      DrawBar(event.index, event.color,
              ranges[i].start, ranges[i].end,
              kTimerEventWidth);
    }
  }

  // Plot code kind gathered from ticks.
  for (var name in CodeKinds) {
    var code_kind = CodeKinds[name];
    var offset = kY1Offset - 1;
    // Top most frame.
531
    var row = MergeRanges(TicksToRanges(code_kind.in_execution));
532 533 534 535 536 537 538 539
    for (var j = 0; j < row.length; j++) {
      DrawBar(offset, code_kind.color,
              row[j].start, row[j].end, kExecutionFrameWidth);
    }
    offset = offset - 2 * kExecutionFrameWidth - kGapWidth;
    // Javascript frames.
    for (var i = 0; i < kStackFrames; i++) {
      offset = offset - 2 * kStackFrameWidth - kGapWidth;
540
      row = MergeRanges(TicksToRanges(code_kind.stack_frames[i]));
541 542 543 544
      for (var j = 0; j < row.length; j++) {
        DrawBar(offset, code_kind.color,
                row[j].start, row[j].end, kStackFrameWidth);
      }
545 546 547
    }
  }

548 549 550 551 552 553 554 555 556 557 558
  // Add labels as legend for code kind colors.
  var padding = kCodeKindLabelPadding * (xrange_end - xrange_start) / kResX;
  var label_x = xrange_start;
  var label_y = kY1Offset;
  for (var name in CodeKinds) {
    label_x += padding;
    print("set label \"" + name + "\" at " + label_x + "," + label_y +
          " textcolor rgb \"" + CodeKinds[name].color + "\"" +
          " font \"Helvetica,9'\"");
  }

559 560 561 562 563 564 565 566 567 568 569
  if (execution_pauses.length == 0) {
    // Force plot and return without plotting execution pause impulses.
    print("plot 1/0");
    return;
  }

  // Label the longest pauses.
  execution_pauses.sort(
      function(a, b) { return b.duration() - a.duration(); });

  var max_pause_time = execution_pauses[0].duration();
570 571
  padding = kPauseLabelPadding * (xrange_end - xrange_start) / kResX;
  var y_scale = kY1Offset / max_pause_time / 2;
572 573 574 575 576 577 578 579 580 581
  for (var i = 0; i < execution_pauses.length && i < kNumPauseLabels; i++) {
    var pause = execution_pauses[i];
    var label_content = (pause.duration() | 0) + " ms";
    var label_x = pause.end + padding;
    var label_y = Math.max(1, (pause.duration() * y_scale));
    print("set label \"" + label_content + "\" at " +
          label_x + "," + label_y + " font \"Helvetica,7'\"");
  }

  // Scale second Y-axis appropriately.
582 583
  var y2range = max_pause_time * num_timer_event / kY1Offset * 2;
  print("set y2range [0:" + y2range + "]");
584 585 586 587 588 589 590 591 592 593 594 595
  // Plot graph with impulses as data set.
  print("plot '-' using 1:2 axes x1y2 with impulses ls 1");
  for (var i = 0; i < execution_pauses.length; i++) {
    var pause = execution_pauses[i];
    print(pause.end + " " + pause.duration());
  }
  print("e");
}


CollectData();
GnuplotOutput();