blob: ba7401a2236dd3ec25a0ae1ad89cf456f1e51970 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// 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
Steve Block1e0659c2011-05-24 12:43:12 +010029function inherits(childCtor, parentCtor) {
30 childCtor.prototype.__proto__ = parentCtor.prototype;
31};
32
33
34function V8Profile(separateIc) {
35 Profile.call(this);
Steve Blocka7e24c12009-10-30 11:49:00 +000036 if (!separateIc) {
Steve Block1e0659c2011-05-24 12:43:12 +010037 this.skipThisFunction = function(name) { return V8Profile.IC_RE.test(name); };
Steve Blocka7e24c12009-10-30 11:49:00 +000038 }
39};
Steve Block1e0659c2011-05-24 12:43:12 +010040inherits(V8Profile, Profile);
Steve Blocka7e24c12009-10-30 11:49:00 +000041
42
Steve Block1e0659c2011-05-24 12:43:12 +010043V8Profile.IC_RE =
Steve Blocka7e24c12009-10-30 11:49:00 +000044 /^(?:CallIC|LoadIC|StoreIC)|(?:Builtin: (?:Keyed)?(?:Call|Load|Store)IC_)/;
45
46
47/**
48 * A thin wrapper around shell's 'read' function showing a file name on error.
49 */
50function readFile(fileName) {
51 try {
52 return read(fileName);
53 } catch (e) {
54 print(fileName + ': ' + (e.message || e));
55 throw e;
56 }
57}
58
59
Ben Murdoche0cee9b2011-05-25 10:26:03 +010060/**
61 * Parser for dynamic code optimization state.
62 */
63function parseState(s) {
64 switch (s) {
65 case "": return Profile.CodeState.COMPILED;
66 case "~": return Profile.CodeState.OPTIMIZABLE;
67 case "*": return Profile.CodeState.OPTIMIZED;
68 }
69 throw new Error("unknown code state: " + s);
70}
71
72
Steve Blocka7e24c12009-10-30 11:49:00 +000073function TickProcessor(
Ben Murdoch3ef787d2012-04-12 10:51:47 +010074 cppEntriesProvider,
75 separateIc,
76 callGraphSize,
77 ignoreUnknown,
78 stateFilter,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 distortion,
80 range,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000081 sourceMap,
82 timedRange,
83 pairwiseTimedRange,
84 onlySummary) {
Steve Block1e0659c2011-05-24 12:43:12 +010085 LogReader.call(this, {
Steve Blocka7e24c12009-10-30 11:49:00 +000086 'shared-library': { parsers: [null, parseInt, parseInt],
87 processor: this.processSharedLibrary },
88 'code-creation': {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089 parsers: [null, parseInt, parseInt, parseInt, null, 'var-args'],
Ben Murdochb0fe1622011-05-05 13:52:32 +010090 processor: this.processCodeCreation },
91 'code-move': { parsers: [parseInt, parseInt],
92 processor: this.processCodeMove },
93 'code-delete': { parsers: [parseInt],
94 processor: this.processCodeDelete },
Ben Murdoche0cee9b2011-05-25 10:26:03 +010095 'sfi-move': { parsers: [parseInt, parseInt],
Ben Murdochb0fe1622011-05-05 13:52:32 +010096 processor: this.processFunctionMove },
Steve Block44f0eee2011-05-26 01:26:41 +010097 'tick': {
98 parsers: [parseInt, parseInt, parseInt,
99 parseInt, parseInt, 'var-args'],
Ben Murdochb0fe1622011-05-05 13:52:32 +0100100 processor: this.processTick },
Steve Block3ce2e202009-11-05 08:53:23 +0000101 'heap-sample-begin': { parsers: [null, null, parseInt],
102 processor: this.processHeapSampleBegin },
103 'heap-sample-end': { parsers: [null, null],
104 processor: this.processHeapSampleEnd },
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105 'timer-event-start' : { parsers: [null, null, null],
106 processor: this.advanceDistortion },
107 'timer-event-end' : { parsers: [null, null, null],
108 processor: this.advanceDistortion },
Steve Block3ce2e202009-11-05 08:53:23 +0000109 // Ignored events.
Steve Blocka7e24c12009-10-30 11:49:00 +0000110 'profiler': null,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100111 'function-creation': null,
112 'function-move': null,
113 'function-delete': null,
Steve Block3ce2e202009-11-05 08:53:23 +0000114 'heap-sample-item': null,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000115 'current-time': null, // Handled specially, not parsed.
Steve Blocka7e24c12009-10-30 11:49:00 +0000116 // Obsolete row types.
117 'code-allocate': null,
118 'begin-code-region': null,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000119 'end-code-region': null },
120 timedRange,
121 pairwiseTimedRange);
Steve Blocka7e24c12009-10-30 11:49:00 +0000122
123 this.cppEntriesProvider_ = cppEntriesProvider;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100124 this.callGraphSize_ = callGraphSize;
Steve Blocka7e24c12009-10-30 11:49:00 +0000125 this.ignoreUnknown_ = ignoreUnknown;
126 this.stateFilter_ = stateFilter;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000127 this.sourceMap = sourceMap;
Leon Clarkee46be812010-01-19 14:06:41 +0000128 this.deserializedEntriesNames_ = [];
Steve Blocka7e24c12009-10-30 11:49:00 +0000129 var ticks = this.ticks_ =
130 { total: 0, unaccounted: 0, excluded: 0, gc: 0 };
131
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000132 distortion = parseInt(distortion);
133 // Convert picoseconds to nanoseconds.
134 this.distortion_per_entry = isNaN(distortion) ? 0 : (distortion / 1000);
135 this.distortion = 0;
136 var rangelimits = range ? range.split(",") : [];
137 var range_start = parseInt(rangelimits[0]);
138 var range_end = parseInt(rangelimits[1]);
139 // Convert milliseconds to nanoseconds.
140 this.range_start = isNaN(range_start) ? -Infinity : (range_start * 1000);
141 this.range_end = isNaN(range_end) ? Infinity : (range_end * 1000)
142
Steve Block1e0659c2011-05-24 12:43:12 +0100143 V8Profile.prototype.handleUnknownCode = function(
Steve Blocka7e24c12009-10-30 11:49:00 +0000144 operation, addr, opt_stackPos) {
Steve Block1e0659c2011-05-24 12:43:12 +0100145 var op = Profile.Operation;
Steve Blocka7e24c12009-10-30 11:49:00 +0000146 switch (operation) {
147 case op.MOVE:
148 print('Code move event for unknown code: 0x' + addr.toString(16));
149 break;
150 case op.DELETE:
151 print('Code delete event for unknown code: 0x' + addr.toString(16));
152 break;
153 case op.TICK:
154 // Only unknown PCs (the first frame) are reported as unaccounted,
155 // otherwise tick balance will be corrupted (this behavior is compatible
156 // with the original tickprocessor.py script.)
157 if (opt_stackPos == 0) {
158 ticks.unaccounted++;
159 }
160 break;
161 }
162 };
163
Steve Block1e0659c2011-05-24 12:43:12 +0100164 this.profile_ = new V8Profile(separateIc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000165 this.codeTypes_ = {};
166 // Count each tick as a time unit.
Steve Block1e0659c2011-05-24 12:43:12 +0100167 this.viewBuilder_ = new ViewBuilder(1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000168 this.lastLogFileName_ = null;
Steve Block3ce2e202009-11-05 08:53:23 +0000169
170 this.generation_ = 1;
171 this.currentProducerProfile_ = null;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000172 this.onlySummary_ = onlySummary;
Steve Blocka7e24c12009-10-30 11:49:00 +0000173};
Steve Block1e0659c2011-05-24 12:43:12 +0100174inherits(TickProcessor, LogReader);
Steve Blocka7e24c12009-10-30 11:49:00 +0000175
176
177TickProcessor.VmStates = {
178 JS: 0,
179 GC: 1,
180 COMPILER: 2,
181 OTHER: 3,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000182 EXTERNAL: 4,
183 IDLE: 5
Steve Blocka7e24c12009-10-30 11:49:00 +0000184};
185
186
187TickProcessor.CodeTypes = {
188 CPP: 0,
189 SHARED_LIB: 1
190};
191// Otherwise, this is JS-related code. We are not adding it to
192// codeTypes_ map because there can be zillions of them.
193
194
195TickProcessor.CALL_PROFILE_CUTOFF_PCT = 2.0;
196
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100197TickProcessor.CALL_GRAPH_SIZE = 5;
Steve Blocka7e24c12009-10-30 11:49:00 +0000198
199/**
200 * @override
201 */
202TickProcessor.prototype.printError = function(str) {
203 print(str);
204};
205
206
207TickProcessor.prototype.setCodeType = function(name, type) {
208 this.codeTypes_[name] = TickProcessor.CodeTypes[type];
209};
210
211
212TickProcessor.prototype.isSharedLibrary = function(name) {
213 return this.codeTypes_[name] == TickProcessor.CodeTypes.SHARED_LIB;
214};
215
216
217TickProcessor.prototype.isCppCode = function(name) {
218 return this.codeTypes_[name] == TickProcessor.CodeTypes.CPP;
219};
220
221
222TickProcessor.prototype.isJsCode = function(name) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000223 return name !== "UNKNOWN" && !(name in this.codeTypes_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000224};
225
226
227TickProcessor.prototype.processLogFile = function(fileName) {
228 this.lastLogFileName_ = fileName;
Andrei Popescu31002712010-02-23 13:46:05 +0000229 var line;
230 while (line = readline()) {
231 this.processLogLine(line);
232 }
233};
234
235
236TickProcessor.prototype.processLogFileInTest = function(fileName) {
237 // Hack file name to avoid dealing with platform specifics.
238 this.lastLogFileName_ = 'v8.log';
Steve Blocka7e24c12009-10-30 11:49:00 +0000239 var contents = readFile(fileName);
240 this.processLogChunk(contents);
241};
242
243
244TickProcessor.prototype.processSharedLibrary = function(
245 name, startAddr, endAddr) {
246 var entry = this.profile_.addLibrary(name, startAddr, endAddr);
247 this.setCodeType(entry.getName(), 'SHARED_LIB');
248
249 var self = this;
250 var libFuncs = this.cppEntriesProvider_.parseVmSymbols(
251 name, startAddr, endAddr, function(fName, fStart, fEnd) {
252 self.profile_.addStaticCode(fName, fStart, fEnd);
253 self.setCodeType(fName, 'CPP');
254 });
255};
256
257
258TickProcessor.prototype.processCodeCreation = function(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000259 type, kind, start, size, name, maybe_func) {
Leon Clarkee46be812010-01-19 14:06:41 +0000260 name = this.deserializedEntriesNames_[start] || name;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100261 if (maybe_func.length) {
262 var funcAddr = parseInt(maybe_func[0]);
263 var state = parseState(maybe_func[1]);
264 this.profile_.addFuncCode(type, name, start, size, funcAddr, state);
265 } else {
266 this.profile_.addCode(type, name, start, size);
267 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000268};
269
270
271TickProcessor.prototype.processCodeMove = function(from, to) {
272 this.profile_.moveCode(from, to);
273};
274
275
276TickProcessor.prototype.processCodeDelete = function(start) {
277 this.profile_.deleteCode(start);
278};
279
280
Leon Clarked91b9f72010-01-27 17:25:45 +0000281TickProcessor.prototype.processFunctionMove = function(from, to) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100282 this.profile_.moveFunc(from, to);
Leon Clarked91b9f72010-01-27 17:25:45 +0000283};
284
285
Steve Blocka7e24c12009-10-30 11:49:00 +0000286TickProcessor.prototype.includeTick = function(vmState) {
287 return this.stateFilter_ == null || this.stateFilter_ == vmState;
288};
289
Steve Block44f0eee2011-05-26 01:26:41 +0100290TickProcessor.prototype.processTick = function(pc,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000291 ns_since_start,
Steve Block44f0eee2011-05-26 01:26:41 +0100292 is_external_callback,
293 tos_or_external_callback,
294 vmState,
295 stack) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000296 this.distortion += this.distortion_per_entry;
297 ns_since_start -= this.distortion;
298 if (ns_since_start < this.range_start || ns_since_start > this.range_end) {
299 return;
300 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000301 this.ticks_.total++;
302 if (vmState == TickProcessor.VmStates.GC) this.ticks_.gc++;
303 if (!this.includeTick(vmState)) {
304 this.ticks_.excluded++;
305 return;
306 }
Steve Block44f0eee2011-05-26 01:26:41 +0100307 if (is_external_callback) {
308 // Don't use PC when in external callback code, as it can point
309 // inside callback's code, and we will erroneously report
Ben Murdoch8b112d22011-06-08 16:22:53 +0100310 // that a callback calls itself. Instead we use tos_or_external_callback,
311 // as simply resetting PC will produce unaccounted ticks.
312 pc = tos_or_external_callback;
313 tos_or_external_callback = 0;
Steve Block44f0eee2011-05-26 01:26:41 +0100314 } else if (tos_or_external_callback) {
315 // Find out, if top of stack was pointing inside a JS function
316 // meaning that we have encountered a frameless invocation.
317 var funcEntry = this.profile_.findEntry(tos_or_external_callback);
Leon Clarked91b9f72010-01-27 17:25:45 +0000318 if (!funcEntry || !funcEntry.isJSFunction || !funcEntry.isJSFunction()) {
Steve Block44f0eee2011-05-26 01:26:41 +0100319 tos_or_external_callback = 0;
Leon Clarked91b9f72010-01-27 17:25:45 +0000320 }
321 }
322
Steve Block44f0eee2011-05-26 01:26:41 +0100323 this.profile_.recordTick(this.processStack(pc, tos_or_external_callback, stack));
Steve Blocka7e24c12009-10-30 11:49:00 +0000324};
325
326
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000327TickProcessor.prototype.advanceDistortion = function() {
328 this.distortion += this.distortion_per_entry;
329}
330
331
Steve Block3ce2e202009-11-05 08:53:23 +0000332TickProcessor.prototype.processHeapSampleBegin = function(space, state, ticks) {
333 if (space != 'Heap') return;
Steve Block1e0659c2011-05-24 12:43:12 +0100334 this.currentProducerProfile_ = new CallTree();
Steve Block3ce2e202009-11-05 08:53:23 +0000335};
336
337
338TickProcessor.prototype.processHeapSampleEnd = function(space, state) {
339 if (space != 'Heap' || !this.currentProducerProfile_) return;
340
341 print('Generation ' + this.generation_ + ':');
342 var tree = this.currentProducerProfile_;
343 tree.computeTotalWeights();
344 var producersView = this.viewBuilder_.buildView(tree);
345 // Sort by total time, desc, then by name, desc.
346 producersView.sort(function(rec1, rec2) {
347 return rec2.totalTime - rec1.totalTime ||
348 (rec2.internalFuncName < rec1.internalFuncName ? -1 : 1); });
349 this.printHeavyProfile(producersView.head.children);
350
351 this.currentProducerProfile_ = null;
352 this.generation_++;
353};
354
355
Steve Blocka7e24c12009-10-30 11:49:00 +0000356TickProcessor.prototype.printStatistics = function() {
357 print('Statistical profiling result from ' + this.lastLogFileName_ +
358 ', (' + this.ticks_.total +
359 ' ticks, ' + this.ticks_.unaccounted + ' unaccounted, ' +
360 this.ticks_.excluded + ' excluded).');
361
362 if (this.ticks_.total == 0) return;
363
Steve Blocka7e24c12009-10-30 11:49:00 +0000364 var flatProfile = this.profile_.getFlatProfile();
365 var flatView = this.viewBuilder_.buildView(flatProfile);
366 // Sort by self time, desc, then by name, desc.
367 flatView.sort(function(rec1, rec2) {
368 return rec2.selfTime - rec1.selfTime ||
369 (rec2.internalFuncName < rec1.internalFuncName ? -1 : 1); });
370 var totalTicks = this.ticks_.total;
371 if (this.ignoreUnknown_) {
372 totalTicks -= this.ticks_.unaccounted;
373 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000374 var printAllTicks = !this.onlySummary_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000375
376 // Count library ticks
377 var flatViewNodes = flatView.head.children;
378 var self = this;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000379
Steve Blocka7e24c12009-10-30 11:49:00 +0000380 var libraryTicks = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000381 if(printAllTicks) this.printHeader('Shared libraries');
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000382 this.printEntries(flatViewNodes, totalTicks, null,
Steve Blocka7e24c12009-10-30 11:49:00 +0000383 function(name) { return self.isSharedLibrary(name); },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000384 function(rec) { libraryTicks += rec.selfTime; }, printAllTicks);
Steve Blocka7e24c12009-10-30 11:49:00 +0000385 var nonLibraryTicks = totalTicks - libraryTicks;
386
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000387 var jsTicks = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000388 if(printAllTicks) this.printHeader('JavaScript');
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000389 this.printEntries(flatViewNodes, totalTicks, nonLibraryTicks,
390 function(name) { return self.isJsCode(name); },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000391 function(rec) { jsTicks += rec.selfTime; }, printAllTicks);
Steve Blocka7e24c12009-10-30 11:49:00 +0000392
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000393 var cppTicks = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000394 if(printAllTicks) this.printHeader('C++');
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000395 this.printEntries(flatViewNodes, totalTicks, nonLibraryTicks,
396 function(name) { return self.isCppCode(name); },
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000397 function(rec) { cppTicks += rec.selfTime; }, printAllTicks);
Steve Blocka7e24c12009-10-30 11:49:00 +0000398
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000399 this.printHeader('Summary');
400 this.printLine('JavaScript', jsTicks, totalTicks, nonLibraryTicks);
401 this.printLine('C++', cppTicks, totalTicks, nonLibraryTicks);
402 this.printLine('GC', this.ticks_.gc, totalTicks, nonLibraryTicks);
403 this.printLine('Shared libraries', libraryTicks, totalTicks, null);
404 if (!this.ignoreUnknown_ && this.ticks_.unaccounted > 0) {
405 this.printLine('Unaccounted', this.ticks_.unaccounted,
406 this.ticks_.total, null);
407 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000408
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000409 if(printAllTicks) {
410 print('\n [C++ entry points]:');
411 print(' ticks cpp total name');
412 var c_entry_functions = this.profile_.getCEntryProfile();
413 var total_c_entry = c_entry_functions[0].ticks;
414 for (var i = 1; i < c_entry_functions.length; i++) {
415 c = c_entry_functions[i];
416 this.printLine(c.name, c.ticks, total_c_entry, totalTicks);
417 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400418
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000419 this.printHeavyProfHeader();
420 var heavyProfile = this.profile_.getBottomUpProfile();
421 var heavyView = this.viewBuilder_.buildView(heavyProfile);
422 // To show the same percentages as in the flat profile.
423 heavyView.head.totalTime = totalTicks;
424 // Sort by total time, desc, then by name, desc.
425 heavyView.sort(function(rec1, rec2) {
426 return rec2.totalTime - rec1.totalTime ||
427 (rec2.internalFuncName < rec1.internalFuncName ? -1 : 1); });
428 this.printHeavyProfile(heavyView.head.children);
429 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000430};
431
432
433function padLeft(s, len) {
434 s = s.toString();
435 if (s.length < len) {
436 var padLength = len - s.length;
437 if (!(padLength in padLeft)) {
438 padLeft[padLength] = new Array(padLength + 1).join(' ');
439 }
440 s = padLeft[padLength] + s;
441 }
442 return s;
443};
444
445
446TickProcessor.prototype.printHeader = function(headerTitle) {
447 print('\n [' + headerTitle + ']:');
448 print(' ticks total nonlib name');
449};
450
451
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000452TickProcessor.prototype.printLine = function(
453 entry, ticks, totalTicks, nonLibTicks) {
454 var pct = ticks * 100 / totalTicks;
455 var nonLibPct = nonLibTicks != null
456 ? padLeft((ticks * 100 / nonLibTicks).toFixed(1), 5) + '% '
457 : ' ';
458 print(' ' + padLeft(ticks, 5) + ' ' +
459 padLeft(pct.toFixed(1), 5) + '% ' +
460 nonLibPct +
461 entry);
462}
463
Steve Blocka7e24c12009-10-30 11:49:00 +0000464TickProcessor.prototype.printHeavyProfHeader = function() {
465 print('\n [Bottom up (heavy) profile]:');
466 print(' Note: percentage shows a share of a particular caller in the ' +
467 'total\n' +
468 ' amount of its parent calls.');
469 print(' Callers occupying less than ' +
470 TickProcessor.CALL_PROFILE_CUTOFF_PCT.toFixed(1) +
471 '% are not shown.\n');
472 print(' ticks parent name');
473};
474
475
Steve Blocka7e24c12009-10-30 11:49:00 +0000476TickProcessor.prototype.processProfile = function(
477 profile, filterP, func) {
478 for (var i = 0, n = profile.length; i < n; ++i) {
479 var rec = profile[i];
480 if (!filterP(rec.internalFuncName)) {
481 continue;
482 }
483 func(rec);
484 }
485};
486
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000487TickProcessor.prototype.getLineAndColumn = function(name) {
488 var re = /:([0-9]+):([0-9]+)$/;
489 var array = re.exec(name);
490 if (!array) {
491 return null;
492 }
493 return {line: array[1], column: array[2]};
494}
495
496TickProcessor.prototype.hasSourceMap = function() {
497 return this.sourceMap != null;
498};
499
500
501TickProcessor.prototype.formatFunctionName = function(funcName) {
502 if (!this.hasSourceMap()) {
503 return funcName;
504 }
505 var lc = this.getLineAndColumn(funcName);
506 if (lc == null) {
507 return funcName;
508 }
509 // in source maps lines and columns are zero based
510 var lineNumber = lc.line - 1;
511 var column = lc.column - 1;
512 var entry = this.sourceMap.findEntry(lineNumber, column);
513 var sourceFile = entry[2];
514 var sourceLine = entry[3] + 1;
515 var sourceColumn = entry[4] + 1;
516
517 return sourceFile + ':' + sourceLine + ':' + sourceColumn + ' -> ' + funcName;
518};
Steve Blocka7e24c12009-10-30 11:49:00 +0000519
520TickProcessor.prototype.printEntries = function(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000521 profile, totalTicks, nonLibTicks, filterP, callback, printAllTicks) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000522 var that = this;
Steve Blocka7e24c12009-10-30 11:49:00 +0000523 this.processProfile(profile, filterP, function (rec) {
524 if (rec.selfTime == 0) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000525 callback(rec);
526 var funcName = that.formatFunctionName(rec.internalFuncName);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000527 if(printAllTicks) {
528 that.printLine(funcName, rec.selfTime, totalTicks, nonLibTicks);
529 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000530 });
531};
532
533
534TickProcessor.prototype.printHeavyProfile = function(profile, opt_indent) {
535 var self = this;
536 var indent = opt_indent || 0;
537 var indentStr = padLeft('', indent);
538 this.processProfile(profile, function() { return true; }, function (rec) {
539 // Cut off too infrequent callers.
540 if (rec.parentTotalPercent < TickProcessor.CALL_PROFILE_CUTOFF_PCT) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000541 var funcName = self.formatFunctionName(rec.internalFuncName);
Steve Blocka7e24c12009-10-30 11:49:00 +0000542 print(' ' + padLeft(rec.totalTime, 5) + ' ' +
543 padLeft(rec.parentTotalPercent.toFixed(1), 5) + '% ' +
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000544 indentStr + funcName);
Steve Blocka7e24c12009-10-30 11:49:00 +0000545 // Limit backtrace depth.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100546 if (indent < 2 * self.callGraphSize_) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000547 self.printHeavyProfile(rec.children, indent + 2);
548 }
549 // Delimit top-level functions.
550 if (indent == 0) {
551 print('');
552 }
553 });
554};
555
556
557function CppEntriesProvider() {
558};
559
560
561CppEntriesProvider.prototype.parseVmSymbols = function(
562 libName, libStart, libEnd, processorFunc) {
563 this.loadSymbols(libName);
564
565 var prevEntry;
566
567 function addEntry(funcInfo) {
568 // Several functions can be mapped onto the same address. To avoid
569 // creating zero-sized entries, skip such duplicates.
570 // Also double-check that function belongs to the library address space.
571 if (prevEntry && !prevEntry.end &&
572 prevEntry.start < funcInfo.start &&
573 prevEntry.start >= libStart && funcInfo.start <= libEnd) {
574 processorFunc(prevEntry.name, prevEntry.start, funcInfo.start);
575 }
576 if (funcInfo.end &&
577 (!prevEntry || prevEntry.start != funcInfo.start) &&
578 funcInfo.start >= libStart && funcInfo.end <= libEnd) {
579 processorFunc(funcInfo.name, funcInfo.start, funcInfo.end);
580 }
581 prevEntry = funcInfo;
582 }
583
584 while (true) {
585 var funcInfo = this.parseNextLine();
586 if (funcInfo === null) {
587 continue;
588 } else if (funcInfo === false) {
589 break;
590 }
591 if (funcInfo.start < libStart && funcInfo.start < libEnd - libStart) {
592 funcInfo.start += libStart;
593 }
594 if (funcInfo.size) {
595 funcInfo.end = funcInfo.start + funcInfo.size;
596 }
597 addEntry(funcInfo);
598 }
599 addEntry({name: '', start: libEnd});
600};
601
602
603CppEntriesProvider.prototype.loadSymbols = function(libName) {
604};
605
606
607CppEntriesProvider.prototype.parseNextLine = function() {
608 return false;
609};
610
611
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000612function UnixCppEntriesProvider(nmExec, targetRootFS) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000613 this.symbols = [];
614 this.parsePos = 0;
615 this.nmExec = nmExec;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000616 this.targetRootFS = targetRootFS;
Steve Blocka7e24c12009-10-30 11:49:00 +0000617 this.FUNC_RE = /^([0-9a-fA-F]{8,16}) ([0-9a-fA-F]{8,16} )?[tTwW] (.*)$/;
618};
619inherits(UnixCppEntriesProvider, CppEntriesProvider);
620
621
622UnixCppEntriesProvider.prototype.loadSymbols = function(libName) {
623 this.parsePos = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000624 libName = this.targetRootFS + libName;
Steve Blocka7e24c12009-10-30 11:49:00 +0000625 try {
626 this.symbols = [
627 os.system(this.nmExec, ['-C', '-n', '-S', libName], -1, -1),
628 os.system(this.nmExec, ['-C', '-n', '-S', '-D', libName], -1, -1)
629 ];
630 } catch (e) {
631 // If the library cannot be found on this system let's not panic.
632 this.symbols = ['', ''];
633 }
634};
635
636
637UnixCppEntriesProvider.prototype.parseNextLine = function() {
638 if (this.symbols.length == 0) {
639 return false;
640 }
641 var lineEndPos = this.symbols[0].indexOf('\n', this.parsePos);
642 if (lineEndPos == -1) {
643 this.symbols.shift();
644 this.parsePos = 0;
645 return this.parseNextLine();
646 }
647
648 var line = this.symbols[0].substring(this.parsePos, lineEndPos);
649 this.parsePos = lineEndPos + 1;
650 var fields = line.match(this.FUNC_RE);
651 var funcInfo = null;
652 if (fields) {
653 funcInfo = { name: fields[3], start: parseInt(fields[1], 16) };
654 if (fields[2]) {
655 funcInfo.size = parseInt(fields[2], 16);
656 }
657 }
658 return funcInfo;
659};
660
661
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000662function MacCppEntriesProvider(nmExec, targetRootFS) {
663 UnixCppEntriesProvider.call(this, nmExec, targetRootFS);
Steve Blocka7e24c12009-10-30 11:49:00 +0000664 // Note an empty group. It is required, as UnixCppEntriesProvider expects 3 groups.
665 this.FUNC_RE = /^([0-9a-fA-F]{8,16}) ()[iItT] (.*)$/;
666};
667inherits(MacCppEntriesProvider, UnixCppEntriesProvider);
668
669
670MacCppEntriesProvider.prototype.loadSymbols = function(libName) {
671 this.parsePos = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000672 libName = this.targetRootFS + libName;
Ben Murdochda12d292016-06-02 14:46:10 +0100673
674 // It seems that in OS X `nm` thinks that `-f` is a format option, not a
675 // "flat" display option flag.
Steve Blocka7e24c12009-10-30 11:49:00 +0000676 try {
Ben Murdochda12d292016-06-02 14:46:10 +0100677 this.symbols = [os.system(this.nmExec, ['-n', libName], -1, -1), ''];
Steve Blocka7e24c12009-10-30 11:49:00 +0000678 } catch (e) {
679 // If the library cannot be found on this system let's not panic.
680 this.symbols = '';
681 }
682};
683
684
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000685function WindowsCppEntriesProvider(_ignored_nmExec, targetRootFS) {
686 this.targetRootFS = targetRootFS;
Steve Blocka7e24c12009-10-30 11:49:00 +0000687 this.symbols = '';
688 this.parsePos = 0;
689};
690inherits(WindowsCppEntriesProvider, CppEntriesProvider);
691
692
693WindowsCppEntriesProvider.FILENAME_RE = /^(.*)\.([^.]+)$/;
694
695
696WindowsCppEntriesProvider.FUNC_RE =
697 /^\s+0001:[0-9a-fA-F]{8}\s+([_\?@$0-9a-zA-Z]+)\s+([0-9a-fA-F]{8}).*$/;
698
699
700WindowsCppEntriesProvider.IMAGE_BASE_RE =
701 /^\s+0000:00000000\s+___ImageBase\s+([0-9a-fA-F]{8}).*$/;
702
703
704// This is almost a constant on Windows.
705WindowsCppEntriesProvider.EXE_IMAGE_BASE = 0x00400000;
706
707
708WindowsCppEntriesProvider.prototype.loadSymbols = function(libName) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000709 libName = this.targetRootFS + libName;
Steve Blocka7e24c12009-10-30 11:49:00 +0000710 var fileNameFields = libName.match(WindowsCppEntriesProvider.FILENAME_RE);
711 if (!fileNameFields) return;
712 var mapFileName = fileNameFields[1] + '.map';
713 this.moduleType_ = fileNameFields[2].toLowerCase();
714 try {
715 this.symbols = read(mapFileName);
716 } catch (e) {
717 // If .map file cannot be found let's not panic.
718 this.symbols = '';
719 }
720};
721
722
723WindowsCppEntriesProvider.prototype.parseNextLine = function() {
724 var lineEndPos = this.symbols.indexOf('\r\n', this.parsePos);
725 if (lineEndPos == -1) {
726 return false;
727 }
728
729 var line = this.symbols.substring(this.parsePos, lineEndPos);
730 this.parsePos = lineEndPos + 2;
731
732 // Image base entry is above all other symbols, so we can just
733 // terminate parsing.
734 var imageBaseFields = line.match(WindowsCppEntriesProvider.IMAGE_BASE_RE);
735 if (imageBaseFields) {
736 var imageBase = parseInt(imageBaseFields[1], 16);
737 if ((this.moduleType_ == 'exe') !=
738 (imageBase == WindowsCppEntriesProvider.EXE_IMAGE_BASE)) {
739 return false;
740 }
741 }
742
743 var fields = line.match(WindowsCppEntriesProvider.FUNC_RE);
744 return fields ?
745 { name: this.unmangleName(fields[1]), start: parseInt(fields[2], 16) } :
746 null;
747};
748
749
750/**
751 * Performs very simple unmangling of C++ names.
752 *
753 * Does not handle arguments and template arguments. The mangled names have
754 * the form:
755 *
756 * ?LookupInDescriptor@JSObject@internal@v8@@...arguments info...
757 */
758WindowsCppEntriesProvider.prototype.unmangleName = function(name) {
759 // Empty or non-mangled name.
760 if (name.length < 1 || name.charAt(0) != '?') return name;
761 var nameEndPos = name.indexOf('@@');
762 var components = name.substring(1, nameEndPos).split('@');
763 components.reverse();
764 return components.join('::');
765};
766
767
768function ArgumentsProcessor(args) {
769 this.args_ = args;
770 this.result_ = ArgumentsProcessor.DEFAULTS;
771
772 this.argsDispatch_ = {
773 '-j': ['stateFilter', TickProcessor.VmStates.JS,
774 'Show only ticks from JS VM state'],
775 '-g': ['stateFilter', TickProcessor.VmStates.GC,
776 'Show only ticks from GC VM state'],
777 '-c': ['stateFilter', TickProcessor.VmStates.COMPILER,
778 'Show only ticks from COMPILER VM state'],
779 '-o': ['stateFilter', TickProcessor.VmStates.OTHER,
780 'Show only ticks from OTHER VM state'],
781 '-e': ['stateFilter', TickProcessor.VmStates.EXTERNAL,
782 'Show only ticks from EXTERNAL VM state'],
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100783 '--call-graph-size': ['callGraphSize', TickProcessor.CALL_GRAPH_SIZE,
784 'Set the call graph size'],
Steve Blocka7e24c12009-10-30 11:49:00 +0000785 '--ignore-unknown': ['ignoreUnknown', true,
786 'Exclude ticks of unknown code entries from processing'],
787 '--separate-ic': ['separateIc', true,
788 'Separate IC entries'],
789 '--unix': ['platform', 'unix',
790 'Specify that we are running on *nix platform'],
791 '--windows': ['platform', 'windows',
792 'Specify that we are running on Windows platform'],
793 '--mac': ['platform', 'mac',
794 'Specify that we are running on Mac OS X platform'],
795 '--nm': ['nm', 'nm',
Leon Clarkee46be812010-01-19 14:06:41 +0000796 'Specify the \'nm\' executable to use (e.g. --nm=/my_dir/nm)'],
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000797 '--target': ['targetRootFS', '',
798 'Specify the target root directory for cross environment'],
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000799 '--range': ['range', 'auto,auto',
800 'Specify the range limit as [start],[end]'],
801 '--distortion': ['distortion', 0,
802 'Specify the logging overhead in picoseconds'],
803 '--source-map': ['sourceMap', null,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000804 'Specify the source map that should be used for output'],
805 '--timed-range': ['timedRange', true,
806 'Ignore ticks before first and after last Date.now() call'],
807 '--pairwise-timed-range': ['pairwiseTimedRange', true,
808 'Ignore ticks outside pairs of Date.now() calls'],
809 '--only-summary': ['onlySummary', true,
810 'Print only tick summary, exclude other information']
Steve Blocka7e24c12009-10-30 11:49:00 +0000811 };
812 this.argsDispatch_['--js'] = this.argsDispatch_['-j'];
813 this.argsDispatch_['--gc'] = this.argsDispatch_['-g'];
814 this.argsDispatch_['--compiler'] = this.argsDispatch_['-c'];
815 this.argsDispatch_['--other'] = this.argsDispatch_['-o'];
816 this.argsDispatch_['--external'] = this.argsDispatch_['-e'];
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000817 this.argsDispatch_['--ptr'] = this.argsDispatch_['--pairwise-timed-range'];
Steve Blocka7e24c12009-10-30 11:49:00 +0000818};
819
820
821ArgumentsProcessor.DEFAULTS = {
822 logFileName: 'v8.log',
Steve Blocka7e24c12009-10-30 11:49:00 +0000823 platform: 'unix',
824 stateFilter: null,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100825 callGraphSize: 5,
Steve Blocka7e24c12009-10-30 11:49:00 +0000826 ignoreUnknown: false,
827 separateIc: false,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000828 targetRootFS: '',
829 nm: 'nm',
830 range: 'auto,auto',
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000831 distortion: 0,
832 timedRange: false,
833 pairwiseTimedRange: false,
834 onlySummary: false
Steve Blocka7e24c12009-10-30 11:49:00 +0000835};
836
837
838ArgumentsProcessor.prototype.parse = function() {
839 while (this.args_.length) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000840 var arg = this.args_.shift();
Steve Blocka7e24c12009-10-30 11:49:00 +0000841 if (arg.charAt(0) != '-') {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000842 this.result_.logFileName = arg;
843 continue;
Steve Blocka7e24c12009-10-30 11:49:00 +0000844 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000845 var userValue = null;
846 var eqPos = arg.indexOf('=');
847 if (eqPos != -1) {
848 userValue = arg.substr(eqPos + 1);
849 arg = arg.substr(0, eqPos);
850 }
851 if (arg in this.argsDispatch_) {
852 var dispatch = this.argsDispatch_[arg];
853 this.result_[dispatch[0]] = userValue == null ? dispatch[1] : userValue;
854 } else {
855 return false;
856 }
857 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000858 return true;
859};
860
861
862ArgumentsProcessor.prototype.result = function() {
863 return this.result_;
864};
865
866
867ArgumentsProcessor.prototype.printUsageAndExit = function() {
868
869 function padRight(s, len) {
870 s = s.toString();
871 if (s.length < len) {
872 s = s + (new Array(len - s.length + 1).join(' '));
873 }
874 return s;
875 }
876
877 print('Cmdline args: [options] [log-file-name]\n' +
878 'Default log file name is "' +
879 ArgumentsProcessor.DEFAULTS.logFileName + '".\n');
880 print('Options:');
881 for (var arg in this.argsDispatch_) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000882 var synonyms = [arg];
Steve Blocka7e24c12009-10-30 11:49:00 +0000883 var dispatch = this.argsDispatch_[arg];
884 for (var synArg in this.argsDispatch_) {
885 if (arg !== synArg && dispatch === this.argsDispatch_[synArg]) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000886 synonyms.push(synArg);
Steve Blocka7e24c12009-10-30 11:49:00 +0000887 delete this.argsDispatch_[synArg];
888 }
889 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000890 print(' ' + padRight(synonyms.join(', '), 20) + " " + dispatch[2]);
Steve Blocka7e24c12009-10-30 11:49:00 +0000891 }
892 quit(2);
893};