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 final int numSymbols; | |
19 | |
20 Map<String, TraceSymbol> _symbols = {}; | |
21 List _events = []; | |
22 List<TraceSymbol> _stack = []; | |
23 List<TraceSymbol> _topExclusive = []; | |
24 List<TraceSymbol> _topInclusive = []; | |
25 bool _marked = false; | |
26 int _totalSamples = 0; | |
27 | |
28 TraceSummary(this.numSymbols); | |
29 | |
30 void _processEventsFromFile(String name) { | |
31 var file = new File(name); | |
32 var events = []; | |
33 try { | |
34 var contents = file.readAsStringSync(); | |
35 events = JSON.decode(contents); | |
36 } catch (e) { | |
37 print('Exception for $name $e'); | |
38 } | |
39 _processEvents(events); | |
40 } | |
41 | |
42 void _processBegin(Map event) { | |
43 var name = event['name']; | |
44 if (name == '<no frame>') { | |
45 return; | |
46 } | |
47 var symbol = _symbols[name]; | |
48 if (symbol == null) { | |
49 symbol = new TraceSymbol(name); | |
50 _symbols[name] = symbol; | |
51 } | |
52 _stack.add(symbol); | |
53 _marked = false; | |
54 } | |
55 | |
56 void _processEnd(Map event) { | |
57 var name = event['name']; | |
58 if (name == '<no frame>') { | |
59 return; | |
60 } | |
61 var symbol = _stack.last; | |
62 if (symbol.name != name) { | |
63 throw new StateError('$name not found at top of stack.'); | |
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; i++) { | |
72 _stack[i].inclusive++; | |
73 } | |
74 _stack.last.exclusive++; | |
75 } | |
76 _stack.removeLast(); | |
77 } | |
78 | |
79 void _processEvents(List events) { | |
80 for (var i = 0; i < events.length; i++) { | |
81 Map event = events[i]; | |
82 if (event['ph'] == 'M') { | |
83 // Ignore. | |
84 } else if (event['ph'] == 'B') { | |
85 _processBegin(event); | |
86 } else if (event['ph'] == 'E') { | |
87 _processEnd(event); | |
88 } | |
89 } | |
90 } | |
91 | |
92 void _findTopExclusive() { | |
93 _topExclusive = _symbols.values.toList(); | |
94 _topExclusive.sort((a, b) { | |
95 return b.exclusive - a.exclusive; | |
96 }); | |
97 } | |
98 | |
99 | |
100 void _findTopInclusive() { | |
101 _topInclusive = _symbols.values.toList(); | |
102 _topInclusive.sort((a, b) { | |
103 return b.inclusive - a.inclusive; | |
104 }); | |
105 } | |
106 | |
107 void summarize(String input) { | |
108 _processEventsFromFile(input); | |
109 _findTopExclusive(); | |
110 _findTopInclusive(); | |
111 _print(); | |
112 } | |
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 | |
134 void _print() { | |
135 print('Top ${numSymbols} inclusive symbols'); | |
136 print('--------------------------'); | |
137 print(' ticks percent name'); | |
138 _topInclusive.getRange(0, numSymbols).forEach((a) { | |
139 _printSymbol(a.inclusive, a.name); | |
140 }); | |
141 print(''); | |
142 print('Top ${numSymbols} exclusive symbols'); | |
143 print('--------------------------'); | |
144 print(' ticks percent name'); | |
145 _topExclusive.getRange(0, numSymbols).forEach((a) { | |
146 _printSymbol(a.exclusive, a.name); | |
147 }); | |
148 } | |
149 } | |
150 | |
151 main(List<String> arguments) { | |
152 if (arguments.length < 1) { | |
153 print('${Platform.executable} ${Platform.script} <input> [symbol count]'); | |
154 return; | |
155 } | |
156 String input = arguments[0]; | |
157 int numSymbols = 10; | |
158 if (arguments.length >= 2) { | |
159 numSymbols = int.parse(arguments[1]); | |
160 } | |
161 TraceSummary ts = new TraceSummary(numSymbols); | |
162 ts.summarize(input); | |
163 } | |
OLD | NEW |