| 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 import 'dart:async'; |
| 6 import 'dart:html'; |
| 7 import 'dart:math' as Math; |
| 8 import 'package:observatory/models.dart' as M; |
| 9 import 'package:observatory/src/elements/stack_trace_tree_config.dart' |
| 10 show ProfileTreeMode; |
| 11 import 'package:observatory/src/elements/code_ref.dart'; |
| 12 import 'package:observatory/src/elements/containers/virtual_tree.dart'; |
| 13 import 'package:observatory/src/elements/function_ref.dart'; |
| 14 import 'package:observatory/src/elements/helpers/rendering_scheduler.dart'; |
| 15 import 'package:observatory/src/elements/helpers/tag.dart'; |
| 16 import 'package:observatory/utils.dart'; |
| 17 |
| 18 class CpuProfileVirtualTreeElement extends HtmlElement implements Renderable { |
| 19 static const tag = |
| 20 const Tag<CpuProfileVirtualTreeElement>('cpu-profile-virtual-tree'); |
| 21 |
| 22 RenderingScheduler<CpuProfileVirtualTreeElement> _r; |
| 23 |
| 24 Stream<RenderedEvent<CpuProfileVirtualTreeElement>> get onRendered => |
| 25 _r.onRendered; |
| 26 |
| 27 M.ProfileTreeDirection _direction; |
| 28 ProfileTreeMode _mode; |
| 29 M.Isolate _isolate; |
| 30 M.SampleProfile _profile; |
| 31 M.CallTreeNodeFilter _filter; |
| 32 |
| 33 M.ProfileTreeDirection get direction => _direction; |
| 34 ProfileTreeMode get mode => _mode; |
| 35 M.Isolate get isolate => _isolate; |
| 36 M.SampleProfile get profile => _profile; |
| 37 M.CallTreeNodeFilter get filter => _filter; |
| 38 |
| 39 set direction(M.ProfileTreeDirection value) => |
| 40 _direction = _r.checkAndReact(_direction, value); |
| 41 set mode(ProfileTreeMode value) => _mode = _r.checkAndReact(_mode, value); |
| 42 set filter(M.CallTreeNodeFilter value) => |
| 43 _filter = _r.checkAndReact(_filter, value); |
| 44 |
| 45 factory CpuProfileVirtualTreeElement(M.IsolateRef isolate, |
| 46 M.SampleProfile profile, {ProfileTreeMode mode: ProfileTreeMode.function, |
| 47 M.ProfileTreeDirection direction: M.ProfileTreeDirection.exclusive, |
| 48 RenderingQueue queue}) { |
| 49 assert(isolate != null); |
| 50 assert(profile != null); |
| 51 assert(mode != null); |
| 52 assert(direction != null); |
| 53 CpuProfileVirtualTreeElement e = document.createElement(tag.name); |
| 54 e._r = new RenderingScheduler(e, queue: queue); |
| 55 e._isolate = isolate; |
| 56 e._profile = profile; |
| 57 e._mode = mode; |
| 58 e._direction = direction; |
| 59 return e; |
| 60 } |
| 61 |
| 62 |
| 63 CpuProfileVirtualTreeElement.created() : super.created(); |
| 64 |
| 65 @override |
| 66 attached() { |
| 67 super.attached(); |
| 68 _r.enable(); |
| 69 } |
| 70 |
| 71 @override |
| 72 detached() { |
| 73 super.detached(); _r.disable(notify: true); |
| 74 children = []; |
| 75 } |
| 76 |
| 77 VirtualTreeElement _tree; |
| 78 |
| 79 void render() { |
| 80 children = const []; |
| 81 var tree; |
| 82 var update; |
| 83 switch (mode) { |
| 84 case ProfileTreeMode.code: |
| 85 tree = _profile.loadCodeTree(_direction); |
| 86 update = _updateCodeRow; |
| 87 break; |
| 88 case ProfileTreeMode.function: |
| 89 tree = _profile.loadFunctionTree(_direction); |
| 90 update = _updateFunctionRow; |
| 91 break; |
| 92 default: |
| 93 throw new Exception('Unknown ProfileTreeMode'); |
| 94 } |
| 95 if (tree == null) { |
| 96 return; |
| 97 } |
| 98 if (filter != null) { |
| 99 tree = tree.filtered(filter); |
| 100 } |
| 101 _tree = new VirtualTreeElement(_createRow, update, _getChildren, |
| 102 items: tree.root.children, queue: _r.queue); |
| 103 if (tree.root.children.length == 1) { |
| 104 _tree.expand(tree.root.children.first, autoExpandSingleChildNodes: true); |
| 105 } |
| 106 children = [_tree]; |
| 107 } |
| 108 |
| 109 static Element _createRow(toggle) { |
| 110 return new DivElement() |
| 111 ..classes = const ['tmp-collection-item'] |
| 112 ..children = [ |
| 113 new SpanElement()..classes = const ['inclusive'] |
| 114 ..title = 'global % on stack', |
| 115 new SpanElement()..classes = const ['exclusive'] |
| 116 ..title = 'global % executing', |
| 117 new SpanElement()..classes = const ['lines'], |
| 118 new SpanElement()..classes = const ['expander'] |
| 119 ..onClick.listen((_) => toggle(autoToggleSingleChildNodes: true)), |
| 120 new SpanElement()..classes = const ['percentage'] |
| 121 ..title = 'tree node %', |
| 122 new SpanElement()..classes = const ['name'] |
| 123 ]; |
| 124 } |
| 125 |
| 126 static _getChildren(M.CallTreeNode node) => node.children; |
| 127 |
| 128 void _updateFunctionRow(HtmlElement element, M.FunctionCallTreeNode item, |
| 129 int depth) { |
| 130 element.children[0].text = Utils.formatPercentNormalized( |
| 131 item.profileFunction.normalizedInclusiveTicks); |
| 132 element.children[1].text = Utils.formatPercentNormalized( |
| 133 item.profileFunction.normalizedExclusiveTicks); |
| 134 _updateLines(element.children[2].children, depth); |
| 135 if (item.children.isNotEmpty) { |
| 136 element.children[3].text = _tree.isExpanded(item) ? '▼' : '►'; |
| 137 } else { |
| 138 element.children[3].text = ''; |
| 139 } |
| 140 element.children[4].text = Utils.formatPercentNormalized( |
| 141 item.percentage); |
| 142 element.children[5] = new FunctionRefElement(_isolate, |
| 143 item.profileFunction.function, queue: _r.queue) |
| 144 ..classes = const ['name']; |
| 145 } |
| 146 |
| 147 void _updateCodeRow(HtmlElement element, M.CodeCallTreeNode item, int depth) { |
| 148 element.children[0].text = Utils.formatPercentNormalized( |
| 149 item.profileCode.normalizedInclusiveTicks); |
| 150 element.children[1].text = Utils.formatPercentNormalized( |
| 151 item.profileCode.normalizedExclusiveTicks); |
| 152 _updateLines(element.children[2].children, depth); |
| 153 if (item.children.isNotEmpty) { |
| 154 element.children[3].text = _tree.isExpanded(item) ? '▼' : '►'; |
| 155 } else { |
| 156 element.children[3].text = ''; |
| 157 } |
| 158 element.children[4].text = Utils.formatPercentNormalized( |
| 159 item.percentage); |
| 160 element.children[5] = new CodeRefElement(_isolate, |
| 161 item.profileCode.code, queue: _r.queue) |
| 162 ..classes = const ['name']; |
| 163 } |
| 164 |
| 165 static _updateLines(List<Element> lines, int n) { |
| 166 n = Math.max(0, n); |
| 167 while (lines.length > n) { |
| 168 lines.removeLast(); |
| 169 } |
| 170 while (lines.length < n) { |
| 171 lines.add(new SpanElement()); |
| 172 } |
| 173 } |
| 174 } |
| OLD | NEW |