OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, 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 heap_profile_element; |
| 6 |
| 7 import 'dart:html'; |
| 8 import 'package:logging/logging.dart'; |
| 9 import 'package:polymer/polymer.dart'; |
| 10 import 'observatory_element.dart'; |
| 11 |
| 12 /// Displays an Error response. |
| 13 @CustomTag('heap-profile') |
| 14 class HeapProfileElement extends ObservatoryElement { |
| 15 // Indexes into VM provided map. |
| 16 static const ALLOCATED_BEFORE_GC = 0; |
| 17 static const ALLOCATED_BEFORE_GC_SIZE = 1; |
| 18 static const LIVE_AFTER_GC = 2; |
| 19 static const LIVE_AFTER_GC_SIZE = 3; |
| 20 static const ALLOCATED_SINCE_GC = 4; |
| 21 static const ALLOCATED_SINCE_GC_SIZE = 5; |
| 22 |
| 23 @published Map profile; |
| 24 @published List sortedProfile; |
| 25 int _sortColumnIndex = 1; |
| 26 HeapProfileElement.created() : super.created(); |
| 27 |
| 28 // Display columns. |
| 29 @observable final List<String> columns = [ |
| 30 'Class', |
| 31 'Current (new)', |
| 32 'Allocated Since GC (new)', |
| 33 'Total before GC (new)', |
| 34 'Survivors (new)', |
| 35 'Current (old)', |
| 36 'Allocated Since GC (old)', |
| 37 'Total before GC (old)', |
| 38 'Survivors (old)', |
| 39 ]; |
| 40 |
| 41 dynamic _columnValue(Map v, int index) { |
| 42 assert(columns.length == 9); |
| 43 switch (index) { |
| 44 case 0: |
| 45 return v['class']['user_name']; |
| 46 case 1: |
| 47 return v['new'][LIVE_AFTER_GC_SIZE] + v['new'][ALLOCATED_SINCE_GC_SIZE]; |
| 48 case 2: |
| 49 return v['new'][ALLOCATED_SINCE_GC_SIZE]; |
| 50 case 3: |
| 51 return v['new'][ALLOCATED_BEFORE_GC_SIZE]; |
| 52 case 4: |
| 53 return v['new'][LIVE_AFTER_GC_SIZE]; |
| 54 case 5: |
| 55 return v['old'][LIVE_AFTER_GC_SIZE] + v['old'][ALLOCATED_SINCE_GC_SIZE]; |
| 56 case 6: |
| 57 return v['old'][ALLOCATED_SINCE_GC_SIZE]; |
| 58 case 7: |
| 59 return v['old'][ALLOCATED_BEFORE_GC_SIZE]; |
| 60 case 8: |
| 61 return v['old'][LIVE_AFTER_GC_SIZE]; |
| 62 } |
| 63 } |
| 64 |
| 65 int _sortColumn(Map a, Map b, int index) { |
| 66 var aValue = _columnValue(a, index); |
| 67 var bValue = _columnValue(b, index); |
| 68 return Comparable.compare(bValue, aValue); |
| 69 } |
| 70 |
| 71 _sort() { |
| 72 if ((profile == null) || (profile['members'] is! List) || |
| 73 (profile['members'].length == 0)) { |
| 74 sortedProfile = toObservable([]); |
| 75 return; |
| 76 } |
| 77 sortedProfile = profile['members'].toList(); |
| 78 sortedProfile.sort((a, b) => _sortColumn(a, b, _sortColumnIndex)); |
| 79 sortedProfile = toObservable(sortedProfile); |
| 80 notifyPropertyChange(#sortedProfile, [], sortedProfile); |
| 81 notifyPropertyChange(#current, 0, 1); |
| 82 notifyPropertyChange(#allocated, 0, 1); |
| 83 notifyPropertyChange(#beforeGC, 0, 1); |
| 84 notifyPropertyChange(#afterGC, 0, 1); |
| 85 } |
| 86 |
| 87 void changeSortColumn(Event e, var detail, Element target) { |
| 88 var message = target.attributes['data-msg']; |
| 89 var index; |
| 90 try { |
| 91 index = int.parse(message); |
| 92 } catch (e) { |
| 93 return; |
| 94 } |
| 95 assert(index is int); |
| 96 assert(index > 0); |
| 97 assert(index < columns.length); |
| 98 _sortColumnIndex = index; |
| 99 _sort(); |
| 100 } |
| 101 |
| 102 void refreshData(Event e, var detail, Node target) { |
| 103 var isolateId = app.locationManager.currentIsolateId(); |
| 104 var isolate = app.isolateManager.getIsolate(isolateId); |
| 105 if (isolate == null) { |
| 106 Logger.root.info('No isolate found.'); |
| 107 return; |
| 108 } |
| 109 var request = '/$isolateId/allocationprofile'; |
| 110 app.requestManager.requestMap(request).then((Map response) { |
| 111 assert(response['type'] == 'AllocationProfile'); |
| 112 profile = response; |
| 113 }).catchError((e, st) { |
| 114 Logger.root.info('$e $st'); |
| 115 }); |
| 116 } |
| 117 |
| 118 void profileChanged(oldValue) { |
| 119 _sort(); |
| 120 notifyPropertyChange(#status, [], status); |
| 121 } |
| 122 |
| 123 String status(bool new_space) { |
| 124 if (profile == null) { |
| 125 return ''; |
| 126 } |
| 127 String space = new_space ? 'new' : 'old'; |
| 128 Map heap = profile['heaps'][space]; |
| 129 var usage = '${ObservatoryApplication.scaledSizeUnits(heap['used'])} / ' |
| 130 '${ObservatoryApplication.scaledSizeUnits(heap['capacity'])}'; |
| 131 var timings = '${ObservatoryApplication.timeUnits(heap['time'])} secs'; |
| 132 var collections = '${heap['collections']} collections'; |
| 133 var avgTime = '${(heap['time'] * 1000.0) / heap['collections']} ms'; |
| 134 return '$usage ($timings) [$collections] $avgTime'; |
| 135 } |
| 136 |
| 137 String current(Map cls, bool new_space, [bool instances = false]) { |
| 138 if (cls is !Map) { |
| 139 return ''; |
| 140 } |
| 141 List data = cls[new_space ? 'new' : 'old']; |
| 142 if (data == null) { |
| 143 return ''; |
| 144 } |
| 145 int current = data[instances ? LIVE_AFTER_GC : LIVE_AFTER_GC_SIZE] + |
| 146 data[instances ? ALLOCATED_SINCE_GC : ALLOCATED_SINCE_GC_SIZE]; |
| 147 if (instances) { |
| 148 return '$current'; |
| 149 } |
| 150 return ObservatoryApplication.scaledSizeUnits(current); |
| 151 } |
| 152 |
| 153 String allocated(Map cls, bool new_space, [bool instances = false]) { |
| 154 if (cls is !Map) { |
| 155 return ''; |
| 156 } |
| 157 List data = cls[new_space ? 'new' : 'old']; |
| 158 if (data == null) { |
| 159 return ''; |
| 160 } |
| 161 int current = |
| 162 data[instances ? ALLOCATED_SINCE_GC : ALLOCATED_SINCE_GC_SIZE]; |
| 163 if (instances) { |
| 164 return '$current'; |
| 165 } |
| 166 return ObservatoryApplication.scaledSizeUnits(current); |
| 167 } |
| 168 |
| 169 String beforeGC(Map cls, bool new_space, [bool instances = false]) { |
| 170 if (cls is! Map) { |
| 171 return ''; |
| 172 } |
| 173 List data = cls[new_space ? 'new' : 'old']; |
| 174 if (data == null) { |
| 175 return ''; |
| 176 } |
| 177 int current = |
| 178 data[instances ? ALLOCATED_BEFORE_GC : ALLOCATED_BEFORE_GC_SIZE]; |
| 179 if (instances) { |
| 180 return '$current'; |
| 181 } |
| 182 return ObservatoryApplication.scaledSizeUnits(current); |
| 183 } |
| 184 |
| 185 String afterGC(Map cls, bool new_space, [bool instances = false]) { |
| 186 if (cls is! Map) { |
| 187 return ''; |
| 188 } |
| 189 List data = cls[new_space ? 'new' : 'old']; |
| 190 if (data == null) { |
| 191 return ''; |
| 192 } |
| 193 int current = data[instances ? LIVE_AFTER_GC : LIVE_AFTER_GC_SIZE]; |
| 194 if (instances) { |
| 195 return '$current'; |
| 196 } |
| 197 return ObservatoryApplication.scaledSizeUnits(current); |
| 198 } |
| 199 } |
OLD | NEW |