OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 // Print a summary of a profile trace. |
| 6 |
| 7 import 'dart:convert'; |
| 8 import 'dart:io'; |
| 9 |
| 10 class TraceSymbol { |
| 11 final String name; |
| 12 int exclusive = 0; |
| 13 int inclusive = 0; |
| 14 TraceSymbol(this.name); |
| 15 } |
| 16 |
| 17 class TraceSummary { |
| 18 Map<String, TraceSymbol> _symbols = {}; |
| 19 List _events = []; |
| 20 List<TraceSymbol> _stack = []; |
| 21 List<TraceSymbol> _topExclusive = []; |
| 22 List<TraceSymbol> _topInclusive = []; |
| 23 |
| 24 void _processEventsFromFile(String name) { |
| 25 var file = new File(name); |
| 26 var events = []; |
| 27 try { |
| 28 var contents = file.readAsStringSync(); |
| 29 events = JSON.decode(contents); |
| 30 } catch (e) { |
| 31 print('Exception for $name $e'); |
| 32 } |
| 33 _processEvents(events); |
| 34 } |
| 35 |
| 36 void _processBegin(Map event) { |
| 37 var name = event['name']; |
| 38 if (name == '<no frame>') { |
| 39 return; |
| 40 } |
| 41 var symbol = _symbols[name]; |
| 42 if (symbol == null) { |
| 43 symbol = new TraceSymbol(name); |
| 44 _symbols[name] = symbol; |
| 45 } |
| 46 // Start at 1 because 0 will always be the isolate. |
| 47 for (var i = 1; i < _stack.length; i++) { |
| 48 // Bump inclusive count for all frames. |
| 49 symbol.inclusive++; |
| 50 } |
| 51 _stack.add(symbol); |
| 52 if (_stack.length > 1) { |
| 53 // Only if we aren't the isolate. |
| 54 symbol.exclusive++; |
| 55 } |
| 56 } |
| 57 |
| 58 void _processEnd(Map event) { |
| 59 var name = event['name']; |
| 60 if (name == '<no frame>') { |
| 61 return; |
| 62 } |
| 63 var symbol = _stack.last; |
| 64 if (symbol.name != name) { |
| 65 throw new StateError('$name not found at top of stack.'); |
| 66 } |
| 67 _stack.removeLast(); |
| 68 } |
| 69 |
| 70 void _processEvents(List events) { |
| 71 for (var i = 0; i < events.length; i++) { |
| 72 Map event = events[i]; |
| 73 if (event['ph'] == 'M') { |
| 74 // Ignore. |
| 75 } else if (event['ph'] == 'B') { |
| 76 _processBegin(event); |
| 77 } else if (event['ph'] == 'E') { |
| 78 _processEnd(event); |
| 79 } |
| 80 } |
| 81 } |
| 82 |
| 83 |
| 84 static const NUM_SYMBOLS = 10; |
| 85 |
| 86 void _findTopExclusive() { |
| 87 _topExclusive = _symbols.values.toList(); |
| 88 _topExclusive.sort((a, b) { |
| 89 return b.exclusive - a.exclusive; |
| 90 }); |
| 91 } |
| 92 |
| 93 |
| 94 void _findTopInclusive() { |
| 95 _topInclusive = _symbols.values.toList(); |
| 96 _topInclusive.sort((a, b) { |
| 97 return b.inclusive - a.inclusive; |
| 98 }); |
| 99 } |
| 100 |
| 101 void summarize(String input) { |
| 102 _processEventsFromFile(input); |
| 103 _findTopExclusive(); |
| 104 _findTopInclusive(); |
| 105 _print(); |
| 106 } |
| 107 |
| 108 void _print() { |
| 109 print('Top ${NUM_SYMBOLS} exlusive symbols:'); |
| 110 _topExclusive.getRange(0, NUM_SYMBOLS).forEach((a) { |
| 111 print('${a.exclusive} ${a.name}'); |
| 112 }); |
| 113 print(''); |
| 114 print('Top ${NUM_SYMBOLS} inclusive symbols:'); |
| 115 _topInclusive.getRange(0, NUM_SYMBOLS).forEach((a) { |
| 116 print('${a.inclusive} ${a.name}'); |
| 117 }); |
| 118 } |
| 119 } |
| 120 |
| 121 main(List<String> arguments) { |
| 122 if (arguments.length < 1) { |
| 123 print('${Platform.executable} ${Platform.script} <input>'); |
| 124 return; |
| 125 } |
| 126 String input = arguments[0]; |
| 127 TraceSummary ts = new TraceSummary(); |
| 128 ts.summarize(input); |
| 129 } |
OLD | NEW |