OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, 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 part of heap_snapshot; |
| 6 |
| 7 class HeapSnapshot implements M.HeapSnapshot { |
| 8 ObjectGraph graph; |
| 9 DateTime timestamp; |
| 10 int get objects => graph.vertexCount; |
| 11 int get references => graph.edgeCount; |
| 12 int get size => graph.size; |
| 13 HeapSnapshotDominatorNode dominatorTree; |
| 14 List<MergedVertex> classReferences; |
| 15 |
| 16 static Future sleep([Duration duration = const Duration(microseconds: 0)]) { |
| 17 final Completer completer = new Completer(); |
| 18 new Timer(duration, () => completer.complete() ); |
| 19 return completer.future; |
| 20 } |
| 21 |
| 22 Stream<List> loadProgress(S.Isolate isolate, S.RawHeapSnapshot raw) { |
| 23 final progress = new StreamController<List>.broadcast(); |
| 24 progress.add(['', 0.0]); |
| 25 graph = new ObjectGraph(raw.chunks, raw.count); |
| 26 var signal = (String description, double p) { |
| 27 progress.add([description, p]); |
| 28 return sleep(); |
| 29 }; |
| 30 (() async { |
| 31 timestamp = new DateTime.now(); |
| 32 final stream = graph.process(); |
| 33 stream.listen((status) { |
| 34 status[1] /= 2.0; |
| 35 progress.add(status); |
| 36 }); |
| 37 await stream.last; |
| 38 dominatorTree = new HeapSnapshotDominatorNode(isolate, graph.root); |
| 39 classReferences = await buildMergedVertices(isolate, graph, signal); |
| 40 progress.close(); |
| 41 }()); |
| 42 return progress.stream; |
| 43 } |
| 44 |
| 45 Future<List<MergedVertex>> buildMergedVertices(S.Isolate isolate, |
| 46 ObjectGraph graph, |
| 47 signal) async { |
| 48 final cidToMergedVertex = {}; |
| 49 |
| 50 int count = 0; |
| 51 final Stopwatch watch = new Stopwatch(); |
| 52 watch.start(); |
| 53 var needToUpdate = () { |
| 54 count++; |
| 55 if (((count % 256) == 0) && (watch.elapsedMilliseconds > 16)) { |
| 56 watch.reset(); |
| 57 return true; |
| 58 } |
| 59 return false; |
| 60 }; |
| 61 final length = graph.vertices.length; |
| 62 for (final vertex in graph.vertices) { |
| 63 if (vertex.vmCid == 0) { |
| 64 continue; |
| 65 } |
| 66 if (needToUpdate()) { |
| 67 await signal('', count * 50.0 / length + 50); |
| 68 } |
| 69 final cid = vertex.vmCid; |
| 70 MergedVertex source = cidToMergedVertex[cid]; |
| 71 if (source == null) { |
| 72 cidToMergedVertex[cid] = source = new MergedVertex(isolate, cid); |
| 73 } |
| 74 |
| 75 source.instances++; |
| 76 source.shallowSize += vertex.shallowSize ?? 0; |
| 77 |
| 78 for (final vertex2 in vertex.successors) { |
| 79 if (vertex2.vmCid == 0) { |
| 80 continue; |
| 81 } |
| 82 final cid2 = vertex2.vmCid; |
| 83 MergedEdge edge = source.outgoingEdges[cid2]; |
| 84 if (edge == null) { |
| 85 MergedVertex target = cidToMergedVertex[cid2]; |
| 86 if (target == null) { |
| 87 cidToMergedVertex[cid2] = target = new MergedVertex(isolate, cid2); |
| 88 } |
| 89 edge = new MergedEdge(source, target); |
| 90 source.outgoingEdges[cid2] = edge; |
| 91 target.incomingEdges.add(edge); |
| 92 } |
| 93 edge.count++; |
| 94 // An over-estimate if there are multiple references to the same object. |
| 95 edge.shallowSize += vertex2.shallowSize ?? 0; |
| 96 } |
| 97 } |
| 98 return cidToMergedVertex.values.toList(); |
| 99 } |
| 100 |
| 101 List<Future<S.ServiceObject>> getMostRetained(S.Isolate isolate, |
| 102 {int classId, int limit}) { |
| 103 var result = []; |
| 104 for (ObjectVertex v in graph.getMostRetained(classId: classId, |
| 105 limit: limit)) { |
| 106 result.add(isolate.getObjectByAddress(v.address) |
| 107 .then((S.ServiceObject obj) { |
| 108 if (obj is S.HeapObject) { |
| 109 obj.retainedSize = v.retainedSize; |
| 110 } else { |
| 111 print("${obj.runtimeType} should be a HeapObject"); |
| 112 } |
| 113 return obj; |
| 114 })); |
| 115 } |
| 116 return result; |
| 117 } |
| 118 } |
| 119 |
| 120 class HeapSnapshotDominatorNode implements M.HeapSnapshotDominatorNode { |
| 121 final ObjectVertex v; |
| 122 final S.Isolate isolate; |
| 123 S.HeapObject _preloaded; |
| 124 |
| 125 Future<S.HeapObject> get object { |
| 126 if (_preloaded != null) { |
| 127 return new Future.value(_preloaded); |
| 128 } else { |
| 129 return isolate.getObjectByAddress(v.address).then((S.HeapObject obj) { |
| 130 return _preloaded = obj; |
| 131 }); |
| 132 } |
| 133 } |
| 134 Iterable<HeapSnapshotDominatorNode> _children; |
| 135 Iterable<HeapSnapshotDominatorNode> get children { |
| 136 if (_children != null) { |
| 137 return _children; |
| 138 } else { |
| 139 return _children = new List.unmodifiable( |
| 140 v.dominatorTreeChildren().map((v) { |
| 141 return new HeapSnapshotDominatorNode(isolate, v); |
| 142 }) |
| 143 ); |
| 144 } |
| 145 } |
| 146 int get retainedSize => v.retainedSize; |
| 147 int get shallowSize => v.shallowSize; |
| 148 |
| 149 HeapSnapshotDominatorNode(S.Isolate isolate, ObjectVertex vertex) |
| 150 : isolate = isolate, |
| 151 v = vertex; |
| 152 } |
| 153 |
| 154 class MergedEdge { |
| 155 final MergedVertex sourceVertex; |
| 156 final MergedVertex targetVertex; |
| 157 int count = 0; |
| 158 int shallowSize = 0; |
| 159 int retainedSize = 0; |
| 160 |
| 161 MergedEdge(this.sourceVertex, this.targetVertex); |
| 162 } |
| 163 |
| 164 class MergedVertex implements M.HeapSnapshotClassReferences { |
| 165 final int cid; |
| 166 final S.Isolate isolate; |
| 167 S.Class get clazz => isolate.getClassByCid(cid); |
| 168 int instances = 0; |
| 169 int shallowSize = 0; |
| 170 int retainedSize = 0; |
| 171 |
| 172 List<MergedEdge> incomingEdges = new List<MergedEdge>(); |
| 173 Map<int, MergedEdge> outgoingEdges = new Map<int, MergedEdge>(); |
| 174 |
| 175 Iterable<HeapSnapshotClassInbound> _inbounds; |
| 176 Iterable<HeapSnapshotClassInbound> get inbounds { |
| 177 if (_inbounds != null) { |
| 178 return _inbounds; |
| 179 } else { |
| 180 return _inbounds = new List.unmodifiable( |
| 181 incomingEdges.map((edge) { |
| 182 return new HeapSnapshotClassInbound(this, edge); |
| 183 }) |
| 184 ); |
| 185 } |
| 186 } |
| 187 |
| 188 Iterable<HeapSnapshotClassOutbound> _outbounds; |
| 189 Iterable<HeapSnapshotClassOutbound> get outbounds { |
| 190 if (_outbounds != null) { |
| 191 return _outbounds; |
| 192 } else { |
| 193 return _outbounds = new List.unmodifiable( |
| 194 outgoingEdges.values.map((edge) { |
| 195 return new HeapSnapshotClassInbound(this, edge); |
| 196 }) |
| 197 ); |
| 198 } |
| 199 } |
| 200 |
| 201 MergedVertex(this.isolate, this.cid); |
| 202 } |
| 203 |
| 204 class HeapSnapshotClassInbound implements M.HeapSnapshotClassInbound { |
| 205 final MergedVertex vertex; |
| 206 final MergedEdge edge; |
| 207 S.Class get source => edge.sourceVertex != vertex ? edge.sourceVertex.clazz |
| 208 : edge.targetVertex.clazz; |
| 209 int get count => edge.count; |
| 210 int get shallowSize => edge.shallowSize; |
| 211 int get retainedSize => edge.retainedSize; |
| 212 |
| 213 HeapSnapshotClassInbound(this.vertex, this.edge); |
| 214 } |
| 215 |
| 216 class HeapSnapshotClassOutbound implements M.HeapSnapshotClassOutbound { |
| 217 final MergedVertex vertex; |
| 218 final MergedEdge edge; |
| 219 S.Class get target => edge.sourceVertex != vertex ? edge.sourceVertex.clazz |
| 220 : edge.targetVertex.clazz; |
| 221 int get count => edge.count; |
| 222 int get shallowSize => edge.shallowSize; |
| 223 int get retainedSize => edge.retainedSize; |
| 224 |
| 225 HeapSnapshotClassOutbound(this.vertex, this.edge); |
| 226 } |
OLD | NEW |