summaryrefslogtreecommitdiff
path: root/chromium/third_party/catapult/tracing/tracing/extras/importer/v8/v8_log_importer.html
blob: bcc9e77896f30a0637c7348c214c29f10a5be1dd (plain)
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<!DOCTYPE html>
<!--
Copyright (c) 2013 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<link rel="import" href="/tracing/base/color_scheme.html">
<link rel="import" href="/tracing/extras/importer/v8/codemap.html">
<link rel="import" href="/tracing/extras/importer/v8/log_reader.html">
<link rel="import" href="/tracing/importer/importer.html">
<link rel="import" href="/tracing/model/model.html">
<link rel="import" href="/tracing/model/slice.html">

<script>

'use strict';

/**
 * @fileoverview V8LogImporter imports v8.log files into the provided model.
 */
tr.exportTo('tr.e.importer.v8', function() {
  var CodeEntry = tr.e.importer.v8.CodeMap.CodeEntry;
  var CodeMap = tr.e.importer.v8.CodeMap;
  var ColorScheme = tr.b.ColorScheme;
  var DynamicFuncCodeEntry = tr.e.importer.v8.CodeMap.DynamicFuncCodeEntry;
  var FunctionEntry = tr.e.importer.v8.CodeMap.FunctionEntry;

  function V8LogImporter(model, eventData) {
    this.importPriority = 3;
    this.model_ = model;

    this.logData_ = eventData;

    this.code_map_ = new CodeMap();
    this.v8_timer_thread_ = undefined;
    this.v8_thread_ = undefined;

    this.root_stack_frame_ = new tr.model.StackFrame(
        undefined, 'v8-root-stack-frame', 'v8-root-stack-frame', 0);

    // We reconstruct the stack timeline from ticks.
    this.v8_stack_timeline_ = new Array();
  }

  var kV8BinarySuffixes = ['/d8', '/libv8.so'];


  var TimerEventDefaultArgs = {
    'V8.Execute': { pause: false, no_execution: false},
    'V8.External': { pause: false, no_execution: true},
    'V8.CompileFullCode': { pause: true, no_execution: true},
    'V8.RecompileSynchronous': { pause: true, no_execution: true},
    'V8.RecompileParallel': { pause: false, no_execution: false},
    'V8.CompileEval': { pause: true, no_execution: true},
    'V8.Parse': { pause: true, no_execution: true},
    'V8.PreParse': { pause: true, no_execution: true},
    'V8.ParseLazy': { pause: true, no_execution: true},
    'V8.GCScavenger': { pause: true, no_execution: true},
    'V8.GCCompactor': { pause: true, no_execution: true},
    'V8.GCContext': { pause: true, no_execution: true}
  };

  /**
   * @return {boolean} Whether obj is a V8 log string.
   */
  V8LogImporter.canImport = function(eventData) {
    if (typeof(eventData) !== 'string' && !(eventData instanceof String))
      return false;

    return eventData.substring(0, 11) === 'v8-version,' ||
           eventData.substring(0, 12) === 'timer-event,' ||
           eventData.substring(0, 5) === 'tick,' ||
           eventData.substring(0, 15) === 'shared-library,' ||
           eventData.substring(0, 9) === 'profiler,' ||
           eventData.substring(0, 14) === 'code-creation,';
  };

  V8LogImporter.prototype = {

    __proto__: tr.importer.Importer.prototype,

    get importerName() {
      return 'V8LogImporter';
    },

    processTimerEvent_: function(name, startInUs, lengthInUs) {
      var args = TimerEventDefaultArgs[name];
      if (args === undefined) return;
      var startInMs = tr.b.convertUnit(startInUs,
          tr.b.UnitScale.Metric.MICRO, tr.b.UnitScale.Metric.MILLI);
      var lengthInMs = tr.b.convertUnit(lengthInUs,
          tr.b.UnitScale.Metric.MICRO, tr.b.UnitScale.Metric.MILLI);
      var colorId = ColorScheme.getColorIdForGeneralPurposeString(name);
      var slice = new tr.model.ThreadSlice('v8', name, colorId, startInMs,
          args, lengthInMs);
      this.v8_timer_thread_.sliceGroup.pushSlice(slice);
    },

    processTimerEventStart_: function(name, startInUs) {
      var args = TimerEventDefaultArgs[name];
      if (args === undefined) return;
      var startInMs = tr.b.convertUnit(startInUs,
          tr.b.UnitScale.Metric.MICRO, tr.b.UnitScale.Metric.MILLI);
      this.v8_timer_thread_.sliceGroup.beginSlice('v8', name, startInMs, args);
    },

    processTimerEventEnd_: function(name, endInUs) {
      var endInMs = tr.b.convertUnit(endInUs,
          tr.b.UnitScale.Metric.MICRO, tr.b.UnitScale.Metric.MILLI);
      this.v8_timer_thread_.sliceGroup.endSlice(endInMs);
    },

    processCodeCreateEvent_: function(type, kind, address, size, name,
                                      maybeFunc) {
      function parseState(s) {
        switch (s) {
          case '': return CodeMap.CodeState.COMPILED;
          case '~': return CodeMap.CodeState.OPTIMIZABLE;
          case '*': return CodeMap.CodeState.OPTIMIZED;
        }
        throw new Error('unknown code state: ' + s);
      }

      if (maybeFunc.length) {
        var funcAddr = parseInt(maybeFunc[0]);
        var state = parseState(maybeFunc[1]);
        var func = this.code_map_.findDynamicEntryByStartAddress(funcAddr);
        if (!func) {
          func = new FunctionEntry(name);
          func.kind = kind;
          this.code_map_.addCode(funcAddr, func);
        } else if (func.name !== name) {
          // Function object has been overwritten with a new one.
          func.name = name;
        }
        var entry = this.code_map_.findDynamicEntryByStartAddress(address);
        if (entry) {
          if (entry.size === size && entry.func === func) {
            // Entry state has changed.
            entry.state = state;
          }
        } else {
          entry = new DynamicFuncCodeEntry(size, type, func, state);
          entry.kind = kind;
          this.code_map_.addCode(address, entry);
        }
      } else {
        var codeEntry = new CodeEntry(size, name);
        codeEntry.kind = kind;
        this.code_map_.addCode(address, codeEntry);
      }
    },

    processCodeMoveEvent_: function(from, to) {
      this.code_map_.moveCode(from, to);
    },

    processCodeDeleteEvent_: function(address) {
      this.code_map_.deleteCode(address);
    },

    processSharedLibrary_: function(name, start, end) {
      var codeEntry = new CodeEntry(end - start, name,
                                     CodeEntry.TYPE.SHARED_LIB);
      codeEntry.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) {
          codeEntry.kind = -1;  // V8 runtime code kind.
          break;
        }
      }
      this.code_map_.addLibrary(start, codeEntry);
    },

    processCppSymbol_: function(address, size, name) {
      var codeEntry = new CodeEntry(size, name, CodeEntry.TYPE.CPP);
      codeEntry.kind = -1;
      this.code_map_.addStaticCode(address, codeEntry);
    },

    processTickEvent_: function(pc, startInUs, isExternalCallback,
                                tosOrExternalCallback, vmstate, stack) {

      var startInMs = tr.b.convertUnit(startInUs,
          tr.b.UnitScale.Metric.MICRO, tr.b.UnitScale.Metric.MILLI);

      function findChildWithEntryID(stackFrame, entryID) {
        for (var i = 0; i < stackFrame.children.length; i++) {
          if (stackFrame.children[i].entryID === entryID)
            return stackFrame.children[i];
        }
        return undefined;
      }

      function processStack(pc, func, stack) {
        var fullStack = func ? [pc, func] : [pc];
        var prevFrame = pc;
        for (var i = 0, n = stack.length; i < n; ++i) {
          var frame = stack[i];
          var firstChar = frame.charAt(0);
          if (firstChar === '+' || firstChar === '-') {
            // An offset from the previous frame.
            prevFrame += parseInt(frame, 16);
            fullStack.push(prevFrame);
            // Filter out possible 'overflow' string.
          } else if (firstChar !== 'o') {
            fullStack.push(parseInt(frame, 16));
          }
          // Otherwise, they will be skipped.
        }
        return fullStack;
      }

      if (isExternalCallback) {
        // Don't use PC when in external callback code, as it can point
        // inside callback's code, and we will erroneously report
        // that a callback calls itself. Instead use tosOrExternalCallback,
        // as simply resetting PC will produce unaccounted ticks.
        pc = tosOrExternalCallback;
        tosOrExternalCallback = 0;
      } else if (tosOrExternalCallback) {
        // Find out, if top of stack was pointing inside a JS function
        // meaning that we have encountered a frameless invocation.
        var funcEntry = this.code_map_.findEntry(tosOrExternalCallback);
        if (!funcEntry || !funcEntry.isJSFunction || !funcEntry.isJSFunction())
          tosOrExternalCallback = 0;
      }

      var processedStack = processStack(pc, tosOrExternalCallback, stack);
      var lastStackFrame = this.root_stack_frame_;
      // v8 log stacks are inverted, leaf first and the root at the end.
      processedStack = processedStack.reverse();
      for (var i = 0, n = processedStack.length; i < n; i++) {
        var frame = processedStack[i];
        if (!frame)
          break;
        var entry = this.code_map_.findEntry(frame);

        if (!entry && i !== 0) {
          continue;
        }

        var sourceInfo = undefined;
        if (entry && entry.type === CodeEntry.TYPE.CPP) {
          var libEntry = this.code_map_.findEntryInLibraries(frame);
          if (libEntry)
            sourceInfo = libEntry.name;
        }

        var entryID = entry ? entry.id : 'Unknown';
        var childFrame = findChildWithEntryID(lastStackFrame, entryID);
        if (childFrame === undefined) {
          var entryName = entry ? entry.name : 'Unknown';
          lastStackFrame = new tr.model.StackFrame(
            lastStackFrame, 'v8sf-' + tr.b.GUID.allocateSimple(), entryName,
            ColorScheme.getColorIdForGeneralPurposeString(entryName),
            sourceInfo);
          lastStackFrame.entryID = entryID;
          this.model_.addStackFrame(lastStackFrame);
        } else {
          lastStackFrame = childFrame;
        }
      }

      if (lastStackFrame !== this.root_stack_frame_) {
        var sample = new tr.model.Sample(
          undefined, this.v8_thread_, 'V8 PC',
          startInMs, lastStackFrame, 1);
        this.model_.samples.push(sample);
      }
    },

    processDistortion_: function(distortionInPicoseconds) {
      // Do nothing.
    },

    processPlotRange_: function(start, end) {
      // Do nothing.
    },

    processV8Version_: function(major, minor, build, patch, candidate) {
      // Do nothing.
    },

    /**
     * Walks through the events_ list and outputs the structures discovered to
     * model_.
     */
    importEvents: function() {
      var logreader = new tr.e.importer.v8.LogReader(
          { 'timer-event' : {
            parsers: [null, parseInt, parseInt],
            processor: this.processTimerEvent_.bind(this)
          },
          'shared-library': {
            parsers: [null, parseInt, parseInt],
            processor: this.processSharedLibrary_.bind(this)
          },
          'timer-event-start' : {
            parsers: [null, parseInt],
            processor: this.processTimerEventStart_.bind(this)
          },
          'timer-event-end' : {
            parsers: [null, parseInt],
            processor: this.processTimerEventEnd_.bind(this)
          },
          'code-creation': {
            parsers: [null, parseInt, parseInt, parseInt, null, 'var-args'],
            processor: this.processCodeCreateEvent_.bind(this)
          },
          'code-move': {
            parsers: [parseInt, parseInt],
            processor: this.processCodeMoveEvent_.bind(this)
          },
          'code-delete': {
            parsers: [parseInt],
            processor: this.processCodeDeleteEvent_.bind(this)
          },
          'cpp': {
            parsers: [parseInt, parseInt, null],
            processor: this.processCppSymbol_.bind(this)
          },
          'tick': {
            parsers: [parseInt, parseInt, parseInt, parseInt, parseInt,
                      'var-args'],
            processor: this.processTickEvent_.bind(this)
          },
          'distortion': {
            parsers: [parseInt],
            processor: this.processDistortion_.bind(this)
          },
          'plot-range': {
            parsers: [parseInt, parseInt],
            processor: this.processPlotRange_.bind(this)
          },
          'v8-version': {
            parsers: [parseInt, parseInt, parseInt, parseInt, parseInt],
            processor: this.processV8Version_.bind(this)
          }
          });

      this.v8_timer_thread_ =
          this.model_.getOrCreateProcess(-32).getOrCreateThread(1);
      this.v8_timer_thread_.name = 'V8 Timers';
      this.v8_thread_ =
          this.model_.getOrCreateProcess(-32).getOrCreateThread(2);
      this.v8_thread_.name = 'V8';

      var lines = this.logData_.split('\n');
      for (var i = 0; i < lines.length; i++) {
        logreader.processLogLine(lines[i]);
      }

      // The processing of samples adds all the root stack frame to
      // this.root_stack_frame_. But, we don't want that stack frame in the real
      // model. So get rid of it.
      this.root_stack_frame_.removeAllChildren();

      function addSlices(slices, thread) {
        for (var i = 0; i < slices.length; i++) {
          var duration = slices[i].end - slices[i].start;
          var slice = new tr.model.ThreadSlice('v8', slices[i].name,
              ColorScheme.getColorIdForGeneralPurposeString(slices[i].name),
              slices[i].start, {}, duration);
          thread.sliceGroup.pushSlice(slice);
          addSlices(slices[i].children, thread);
        }
      }
      addSlices(this.v8_stack_timeline_, this.v8_thread_);
    }
  };

  tr.importer.Importer.register(V8LogImporter);

  return {
    V8LogImporter: V8LogImporter
  };
});
</script>