| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library object_graph; | 5 library object_graph; |
| 6 | 6 |
| 7 import 'dart:typed_data'; | 7 import 'dart:typed_data'; |
| 8 | 8 |
| 9 import 'dominator_tree.dart'; | 9 import 'dominator_tree.dart'; |
| 10 | 10 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 } | 31 } |
| 32 | 32 |
| 33 static const int dataBitsPerByte = 7; | 33 static const int dataBitsPerByte = 7; |
| 34 static const int byteMask = (1 << dataBitsPerByte) - 1; | 34 static const int byteMask = (1 << dataBitsPerByte) - 1; |
| 35 static const int maxUnsignedDataPerByte = byteMask; | 35 static const int maxUnsignedDataPerByte = byteMask; |
| 36 } | 36 } |
| 37 | 37 |
| 38 class ObjectVertex { | 38 class ObjectVertex { |
| 39 // Never null. The isolate root has id 0. | 39 // Never null. The isolate root has id 0. |
| 40 final int _id; | 40 final int _id; |
| 41 bool get isRoot => _id == 0; |
| 42 // TODO(koda): Include units in object graph metadata. |
| 43 int addressForWordSize(int bytesPerWord) => _id * 2 * bytesPerWord; |
| 41 // null for VM-heap objects. | 44 // null for VM-heap objects. |
| 42 int _shallowSize; | 45 int _shallowSize; |
| 43 int get shallowSize => _shallowSize; | 46 int get shallowSize => _shallowSize; |
| 44 int _retainedSize; | 47 int _retainedSize; |
| 45 int get retainedSize => _retainedSize; | 48 int get retainedSize => _retainedSize; |
| 46 // null for VM-heap objects. | 49 // null for VM-heap objects. |
| 47 int _classId; | 50 int _classId; |
| 48 int get classId => _classId; | 51 int get classId => _classId; |
| 49 final List<ObjectVertex> succ = new List<ObjectVertex>(); | 52 final List<ObjectVertex> succ = new List<ObjectVertex>(); |
| 50 ObjectVertex(this._id) : _retainedSize = 0; | 53 ObjectVertex(this._id) : _retainedSize = 0; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 68 obj.succ.add(_asVertex(last)); | 71 obj.succ.add(_asVertex(last)); |
| 69 last = stream.readUnsigned(); | 72 last = stream.readUnsigned(); |
| 70 } | 73 } |
| 71 } | 74 } |
| 72 | 75 |
| 73 ObjectGraph(ReadStream reader) { | 76 ObjectGraph(ReadStream reader) { |
| 74 while (reader.pendingBytes > 0) { | 77 while (reader.pendingBytes > 0) { |
| 75 _addFrom(reader); | 78 _addFrom(reader); |
| 76 } | 79 } |
| 77 _computeRetainedSizes(); | 80 _computeRetainedSizes(); |
| 81 _mostRetained = new List<ObjectVertex>.from( |
| 82 vertices.where((u) => !u.isRoot)); |
| 83 _mostRetained.sort((u, v) => v.retainedSize - u.retainedSize); |
| 78 } | 84 } |
| 79 | 85 |
| 80 Iterable<ObjectVertex> get vertices => _idToVertex.values; | 86 Iterable<ObjectVertex> get vertices => _idToVertex.values; |
| 87 List<ObjectVertex> _mostRetained; |
| 81 | 88 |
| 82 ObjectVertex get root => _asVertex(0); | 89 ObjectVertex get root => _asVertex(0); |
| 83 | 90 |
| 91 Iterable<ObjectVertex> getMostRetained({int classId, int limit}) { |
| 92 var result = _mostRetained; |
| 93 if (classId != null) { |
| 94 result = result.where((u) => u.classId == classId); |
| 95 } |
| 96 if (limit != null) { |
| 97 result = result.take(limit); |
| 98 } |
| 99 return result; |
| 100 } |
| 101 |
| 84 void _computeRetainedSizes() { | 102 void _computeRetainedSizes() { |
| 85 // The retained size for an object is the sum of the shallow sizes of | 103 // The retained size for an object is the sum of the shallow sizes of |
| 86 // all its descendants in the dominator tree (including itself). | 104 // all its descendants in the dominator tree (including itself). |
| 87 var d = new Dominator(); | 105 var d = new Dominator(); |
| 88 for (ObjectVertex u in vertices) { | 106 for (ObjectVertex u in vertices) { |
| 89 if (u.shallowSize != null) { | 107 if (u.shallowSize != null) { |
| 90 u._retainedSize = u.shallowSize; | 108 u._retainedSize = u.shallowSize; |
| 91 d.addEdges(u, u.succ.where((ObjectVertex v) => v.shallowSize != null)); | 109 d.addEdges(u, u.succ.where((ObjectVertex v) => v.shallowSize != null)); |
| 92 } | 110 } |
| 93 } | 111 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 111 var v = leaves.removeLast(); | 129 var v = leaves.removeLast(); |
| 112 var u = d.dominator(v); | 130 var u = d.dominator(v); |
| 113 if (u == null) continue; | 131 if (u == null) continue; |
| 114 u._retainedSize += v._retainedSize; | 132 u._retainedSize += v._retainedSize; |
| 115 if (--degree[u] == 0) { | 133 if (--degree[u] == 0) { |
| 116 leaves.add(u); | 134 leaves.add(u); |
| 117 } | 135 } |
| 118 } | 136 } |
| 119 } | 137 } |
| 120 } | 138 } |
| OLD | NEW |