| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Print a summary of a profile trace. | 5 // Print a summary of a profile trace. |
| 6 | 6 |
| 7 import 'dart:convert'; | 7 import 'dart:convert'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 | 9 |
| 10 class TraceSymbol { | 10 class TraceSymbol { |
| 11 final String name; | 11 final String name; |
| 12 int exclusive = 0; | 12 int exclusive = 0; |
| 13 int inclusive = 0; | 13 int inclusive = 0; |
| 14 TraceSymbol(this.name); | 14 TraceSymbol(this.name); |
| 15 } | 15 } |
| 16 | 16 |
| 17 class TraceSummary { | 17 class TraceSummary { |
| 18 final int numSymbols; |
| 19 |
| 18 Map<String, TraceSymbol> _symbols = {}; | 20 Map<String, TraceSymbol> _symbols = {}; |
| 19 List _events = []; | 21 List _events = []; |
| 20 List<TraceSymbol> _stack = []; | 22 List<TraceSymbol> _stack = []; |
| 21 List<TraceSymbol> _topExclusive = []; | 23 List<TraceSymbol> _topExclusive = []; |
| 22 List<TraceSymbol> _topInclusive = []; | 24 List<TraceSymbol> _topInclusive = []; |
| 25 bool _marked = false; |
| 26 int _totalSamples = 0; |
| 27 |
| 28 TraceSummary(this.numSymbols); |
| 23 | 29 |
| 24 void _processEventsFromFile(String name) { | 30 void _processEventsFromFile(String name) { |
| 25 var file = new File(name); | 31 var file = new File(name); |
| 26 var events = []; | 32 var events = []; |
| 27 try { | 33 try { |
| 28 var contents = file.readAsStringSync(); | 34 var contents = file.readAsStringSync(); |
| 29 events = JSON.decode(contents); | 35 events = JSON.decode(contents); |
| 30 } catch (e) { | 36 } catch (e) { |
| 31 print('Exception for $name $e'); | 37 print('Exception for $name $e'); |
| 32 } | 38 } |
| 33 _processEvents(events); | 39 _processEvents(events); |
| 34 } | 40 } |
| 35 | 41 |
| 36 void _processBegin(Map event) { | 42 void _processBegin(Map event) { |
| 37 var name = event['name']; | 43 var name = event['name']; |
| 38 if (name == '<no frame>') { | 44 if (name == '<no frame>') { |
| 39 return; | 45 return; |
| 40 } | 46 } |
| 41 var symbol = _symbols[name]; | 47 var symbol = _symbols[name]; |
| 42 if (symbol == null) { | 48 if (symbol == null) { |
| 43 symbol = new TraceSymbol(name); | 49 symbol = new TraceSymbol(name); |
| 44 _symbols[name] = symbol; | 50 _symbols[name] = symbol; |
| 45 } | 51 } |
| 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 _stack.add(symbol); |
| 52 if (_stack.length > 1) { | 53 _marked = false; |
| 53 // Only if we aren't the isolate. | |
| 54 symbol.exclusive++; | |
| 55 } | |
| 56 } | 54 } |
| 57 | 55 |
| 58 void _processEnd(Map event) { | 56 void _processEnd(Map event) { |
| 59 var name = event['name']; | 57 var name = event['name']; |
| 60 if (name == '<no frame>') { | 58 if (name == '<no frame>') { |
| 61 return; | 59 return; |
| 62 } | 60 } |
| 63 var symbol = _stack.last; | 61 var symbol = _stack.last; |
| 64 if (symbol.name != name) { | 62 if (symbol.name != name) { |
| 65 throw new StateError('$name not found at top of stack.'); | 63 throw new StateError('$name not found at top of stack.'); |
| 66 } | 64 } |
| 65 if ((_stack.length > 1) && (_marked == false)) { |
| 66 // We are transitioning from the sequence of begins to the sequence |
| 67 // of ends. Mark the symbols on the stack. |
| 68 _marked = true; |
| 69 _totalSamples++; |
| 70 // Mark all symbols except the top with an inclusive tick. |
| 71 for (int i = 1; i < _stack.length - 1; i++) { |
| 72 _stack[i].inclusive++; |
| 73 } |
| 74 _stack.last.exclusive++; |
| 75 } |
| 67 _stack.removeLast(); | 76 _stack.removeLast(); |
| 68 } | 77 } |
| 69 | 78 |
| 70 void _processEvents(List events) { | 79 void _processEvents(List events) { |
| 71 for (var i = 0; i < events.length; i++) { | 80 for (var i = 0; i < events.length; i++) { |
| 72 Map event = events[i]; | 81 Map event = events[i]; |
| 73 if (event['ph'] == 'M') { | 82 if (event['ph'] == 'M') { |
| 74 // Ignore. | 83 // Ignore. |
| 75 } else if (event['ph'] == 'B') { | 84 } else if (event['ph'] == 'B') { |
| 76 _processBegin(event); | 85 _processBegin(event); |
| 77 } else if (event['ph'] == 'E') { | 86 } else if (event['ph'] == 'E') { |
| 78 _processEnd(event); | 87 _processEnd(event); |
| 79 } | 88 } |
| 80 } | 89 } |
| 81 } | 90 } |
| 82 | 91 |
| 83 | |
| 84 static const NUM_SYMBOLS = 10; | |
| 85 | |
| 86 void _findTopExclusive() { | 92 void _findTopExclusive() { |
| 87 _topExclusive = _symbols.values.toList(); | 93 _topExclusive = _symbols.values.toList(); |
| 88 _topExclusive.sort((a, b) { | 94 _topExclusive.sort((a, b) { |
| 89 return b.exclusive - a.exclusive; | 95 return b.exclusive - a.exclusive; |
| 90 }); | 96 }); |
| 91 } | 97 } |
| 92 | 98 |
| 93 | 99 |
| 94 void _findTopInclusive() { | 100 void _findTopInclusive() { |
| 95 _topInclusive = _symbols.values.toList(); | 101 _topInclusive = _symbols.values.toList(); |
| 96 _topInclusive.sort((a, b) { | 102 _topInclusive.sort((a, b) { |
| 97 return b.inclusive - a.inclusive; | 103 return b.inclusive - a.inclusive; |
| 98 }); | 104 }); |
| 99 } | 105 } |
| 100 | 106 |
| 101 void summarize(String input) { | 107 void summarize(String input) { |
| 102 _processEventsFromFile(input); | 108 _processEventsFromFile(input); |
| 103 _findTopExclusive(); | 109 _findTopExclusive(); |
| 104 _findTopInclusive(); | 110 _findTopInclusive(); |
| 105 _print(); | 111 _print(); |
| 106 } | 112 } |
| 107 | 113 |
| 114 String _pad(String input, int minLength) { |
| 115 int length = input.length; |
| 116 for (int i = 0; i < minLength - length; i++) { |
| 117 input = ' $input'; |
| 118 } |
| 119 return input; |
| 120 } |
| 121 |
| 122 static const TICKS_LENGTH = 10; |
| 123 static const PERCENT_LENGTH = 7; |
| 124 |
| 125 void _printSymbol(int t, String name) { |
| 126 String ticks = t.toString(); |
| 127 ticks = _pad(ticks, TICKS_LENGTH); |
| 128 double total = (t / _totalSamples); |
| 129 String percent = (total * 100.0).toStringAsFixed(2); |
| 130 percent = _pad(percent, PERCENT_LENGTH); |
| 131 print('$ticks $percent $name'); |
| 132 } |
| 133 |
| 108 void _print() { | 134 void _print() { |
| 109 print('Top ${NUM_SYMBOLS} exlusive symbols:'); | 135 print('Top ${numSymbols} inclusive symbols'); |
| 110 _topExclusive.getRange(0, NUM_SYMBOLS).forEach((a) { | 136 print('--------------------------'); |
| 111 print('${a.exclusive} ${a.name}'); | 137 print(' ticks percent name'); |
| 138 _topInclusive.getRange(0, numSymbols).forEach((a) { |
| 139 _printSymbol(a.inclusive, a.name); |
| 112 }); | 140 }); |
| 113 print(''); | 141 print(''); |
| 114 print('Top ${NUM_SYMBOLS} inclusive symbols:'); | 142 print('Top ${numSymbols} exclusive symbols'); |
| 115 _topInclusive.getRange(0, NUM_SYMBOLS).forEach((a) { | 143 print('--------------------------'); |
| 116 print('${a.inclusive} ${a.name}'); | 144 print(' ticks percent name'); |
| 145 _topExclusive.getRange(0, numSymbols).forEach((a) { |
| 146 _printSymbol(a.exclusive, a.name); |
| 117 }); | 147 }); |
| 118 } | 148 } |
| 119 } | 149 } |
| 120 | 150 |
| 121 main(List<String> arguments) { | 151 main(List<String> arguments) { |
| 122 if (arguments.length < 1) { | 152 if (arguments.length < 1) { |
| 123 print('${Platform.executable} ${Platform.script} <input>'); | 153 print('${Platform.executable} ${Platform.script} <input> [symbol count]'); |
| 124 return; | 154 return; |
| 125 } | 155 } |
| 126 String input = arguments[0]; | 156 String input = arguments[0]; |
| 127 TraceSummary ts = new TraceSummary(); | 157 int numSymbols = 10; |
| 158 if (arguments.length >= 2) { |
| 159 numSymbols = int.parse(arguments[1]); |
| 160 } |
| 161 TraceSummary ts = new TraceSummary(numSymbols); |
| 128 ts.summarize(input); | 162 ts.summarize(input); |
| 129 } | 163 } |
| OLD | NEW |