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 library isolate_profile_element; | |
6 | |
7 import 'dart:html'; | |
8 import 'observatory_element.dart'; | |
9 import 'package:logging/logging.dart'; | |
10 import 'package:observatory/service.dart'; | |
11 import 'package:observatory/app.dart'; | |
12 import 'package:polymer/polymer.dart'; | |
13 | |
14 class ProfileCodeTrieNodeTreeRow extends TableTreeRow { | |
15 final ServiceMap profile; | |
16 @reflectable final CodeTrieNode root; | |
17 @reflectable final CodeTrieNode node; | |
18 @reflectable Code get code => node.code; | |
19 | |
20 @reflectable String tipKind = ''; | |
21 @reflectable String tipParent = ''; | |
22 @reflectable String tipExclusive = ''; | |
23 @reflectable String tipTicks = ''; | |
24 @reflectable String tipTime = ''; | |
25 | |
26 ProfileCodeTrieNodeTreeRow(this.profile, this.root, this.node, | |
27 ProfileCodeTrieNodeTreeRow parent) | |
28 : super(parent) { | |
29 assert(root != null); | |
30 assert(node != null); | |
31 tipTicks = '${node.count}'; | |
32 var period = profile['period']; | |
33 var MICROSECONDS_PER_SECOND = 1000000.0; | |
34 var seconds = (period * node.count) / MICROSECONDS_PER_SECOND; // seconds | |
35 tipTime = Utils.formatTimePrecise(seconds); | |
36 if (code.kind == CodeKind.Tag) { | |
37 tipKind = 'Tag (category)'; | |
38 if (parent == null) { | |
39 tipParent = Utils.formatPercent(node.count, root.count); | |
40 } else { | |
41 tipParent = Utils.formatPercent(node.count, parent.node.count); | |
42 } | |
43 tipExclusive = Utils.formatPercent(node.count, root.count); | |
44 } else { | |
45 if ((code.kind == CodeKind.Collected) || | |
46 (code.kind == CodeKind.Reused)) { | |
47 tipKind = 'Garbage Collected Code'; | |
48 } else { | |
49 tipKind = '${code.kind} (Function)'; | |
50 } | |
51 if (parent == null) { | |
52 tipParent = Utils.formatPercent(node.count, root.count); | |
53 } else { | |
54 tipParent = Utils.formatPercent(node.count, parent.node.count); | |
55 } | |
56 tipExclusive = Utils.formatPercent(node.code.exclusiveTicks, root.count); | |
57 } | |
58 columns.add(tipParent); | |
59 columns.add(tipExclusive); | |
60 } | |
61 | |
62 bool shouldDisplayChild(CodeTrieNode childNode, double threshold) { | |
63 return ((childNode.count / node.count) > threshold) || | |
64 ((childNode.code.exclusiveTicks / root.count) > threshold); | |
65 } | |
66 | |
67 void onShow() { | |
68 var threshold = profile['threshold']; | |
69 if (children.length > 0) { | |
70 // Child rows already created. | |
71 return; | |
72 } | |
73 for (var childNode in node.children) { | |
74 if (!shouldDisplayChild(childNode, threshold)) { | |
75 continue; | |
76 } | |
77 var row = new ProfileCodeTrieNodeTreeRow(profile, root, childNode, this); | |
78 children.add(row); | |
79 } | |
80 } | |
81 | |
82 void onHide() { | |
83 } | |
84 | |
85 bool hasChildren() { | |
86 return node.children.length > 0; | |
87 } | |
88 } | |
89 | |
90 /// Displays an IsolateProfile | |
91 @CustomTag('isolate-profile') | |
92 class IsolateProfileElement extends ObservatoryElement { | |
93 IsolateProfileElement.created() : super.created(); | |
94 @published ServiceMap profile; | |
95 @observable bool hideTagsChecked; | |
96 @observable String sampleCount = ''; | |
97 @observable String refreshTime = ''; | |
98 @observable String sampleRate = ''; | |
99 @observable String sampleDepth = ''; | |
100 @observable String displayCutoff = ''; | |
101 @observable String timeSpan = ''; | |
102 @reflectable double displayThreshold = 0.0002; // 0.02%. | |
103 | |
104 @observable String tagSelector = 'uv'; | |
105 | |
106 final _id = '#tableTree'; | |
107 TableTree tree; | |
108 | |
109 static const MICROSECONDS_PER_SECOND = 1000000.0; | |
110 | |
111 void profileChanged(oldValue) { | |
112 if (profile == null) { | |
113 return; | |
114 } | |
115 var totalSamples = profile['samples']; | |
116 var now = new DateTime.now(); | |
117 sampleCount = totalSamples.toString(); | |
118 refreshTime = now.toString(); | |
119 sampleDepth = profile['depth'].toString(); | |
120 var period = profile['period']; | |
121 sampleRate = (MICROSECONDS_PER_SECOND / period).toStringAsFixed(0); | |
122 timeSpan = formatTime(profile['timeSpan']); | |
123 displayCutoff = '${(displayThreshold * 100.0).toString()}%'; | |
124 profile.isolate.processProfile(profile); | |
125 profile['threshold'] = displayThreshold; | |
126 _update(); | |
127 } | |
128 | |
129 | |
130 @override | |
131 void attached() { | |
132 super.attached(); | |
133 tree = new TableTree(); | |
134 _update(); | |
135 } | |
136 | |
137 void tagSelectorChanged(oldValue) { | |
138 refresh(null); | |
139 } | |
140 | |
141 void refresh(var done) { | |
142 var request = 'profile?tags=$tagSelector'; | |
143 profile.isolate.get(request).then((ServiceMap m) { | |
144 // Assert we got back the a profile. | |
145 assert(m.type == 'Profile'); | |
146 profile = m; | |
147 }).whenComplete(done); | |
148 } | |
149 | |
150 void _update() { | |
151 if (profile == null) { | |
152 return; | |
153 } | |
154 _buildTree(); | |
155 } | |
156 | |
157 void _buildStackTree() { | |
158 var root = profile.isolate.profileTrieRoot; | |
159 if (root == null) { | |
160 return; | |
161 } | |
162 try { | |
163 tree.initialize( | |
164 new ProfileCodeTrieNodeTreeRow(profile, root, root, null)); | |
165 } catch (e, stackTrace) { | |
166 Logger.root.warning('_buildStackTree', e, stackTrace); | |
167 } | |
168 // Check if we only have one node at the root and expand it. | |
169 if (tree.rows.length == 1) { | |
170 tree.toggle(0); | |
171 } | |
172 notifyPropertyChange(#tree, null, tree); | |
173 } | |
174 | |
175 void _buildTree() { | |
176 _buildStackTree(); | |
177 } | |
178 | |
179 @observable String padding(TableTreeRow row) { | |
180 return 'padding-left: ${row.depth * 16}px;'; | |
181 } | |
182 | |
183 @observable String coloring(TableTreeRow row) { | |
184 const colors = const ['rowColor0', 'rowColor1', 'rowColor2', 'rowColor3', | |
185 'rowColor4', 'rowColor5', 'rowColor6', 'rowColor7', | |
186 'rowColor8']; | |
187 var index = (row.depth - 1) % colors.length; | |
188 return colors[index]; | |
189 } | |
190 | |
191 @observable void toggleExpanded(Event e, var detail, Element target) { | |
192 // We only want to expand a tree row if the target of the click is | |
193 // the table cell (passed in as target) or the span containing the | |
194 // expander symbol (#expand). | |
195 var eventTarget = e.target; | |
196 if ((eventTarget.id != 'expand') && (e.target != target)) { | |
197 // Target of click was not the expander span or the table cell. | |
198 return; | |
199 } | |
200 var row = target.parent; | |
201 if (row is TableRowElement) { | |
202 // Subtract 1 to get 0 based indexing. | |
203 try { | |
204 tree.toggle(row.rowIndex - 1); | |
205 } catch (e, stackTrace) { | |
206 Logger.root.warning('toggleExpanded', e, stackTrace); | |
207 } | |
208 } | |
209 } | |
210 } | |
OLD | NEW |