-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathCompareSnapshotsControl.cs
More file actions
389 lines (318 loc) · 12.9 KB
/
CompareSnapshotsControl.cs
File metadata and controls
389 lines (318 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
//
// Heap Explorer for Unity. Copyright (c) 2019-2020 Peter Schraut (www.console-dev.de). See LICENSE.md
// https://github.com/pschraut/UnityHeapExplorer/
//
using System.Collections;
using System.Collections.Generic;
using HeapExplorer.Utilities;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
namespace HeapExplorer
{
public class CompareSnapshotsControl : AbstractTreeView
{
//public System.Action<GotoCommand> gotoCB;
enum EColumn
{
Type,
SizeA,
SizeB,
SizeDiff,
CountA,
CountB,
CountDiff
}
public CompareSnapshotsControl(HeapExplorerWindow window, string editorPrefsKey, TreeViewState state)
: base(window, editorPrefsKey, state, new MultiColumnHeader(
new MultiColumnHeaderState(new[]
{
new MultiColumnHeaderState.Column() { headerContent = new GUIContent("Type"), width = 300, autoResize = true },
new MultiColumnHeaderState.Column() { headerContent = new GUIContent("Size (A)"), width = 100, autoResize = true },
new MultiColumnHeaderState.Column() { headerContent = new GUIContent("Size (B)"), width = 100, autoResize = true },
new MultiColumnHeaderState.Column() { headerContent = new GUIContent("Size delta (B-A)"), width = 120, autoResize = true },
new MultiColumnHeaderState.Column() { headerContent = new GUIContent("Count (A)"), width = 100, autoResize = true },
new MultiColumnHeaderState.Column() { headerContent = new GUIContent("Count (B)"), width = 100, autoResize = true },
new MultiColumnHeaderState.Column() { headerContent = new GUIContent("Count delta (B-A)"), width = 120, autoResize = true },
})))
{
multiColumnHeader.canSort = true;
Reload();
}
protected override int OnSortItem(TreeViewItem aa, TreeViewItem bb)
{
var sortingColumn = multiColumnHeader.sortedColumnIndex;
var ascending = multiColumnHeader.IsSortedAscending(sortingColumn);
var itemA = (ascending ? aa : bb) as Item;
var itemB = (ascending ? bb : aa) as Item;
switch ((EColumn)sortingColumn)
{
case EColumn.Type:
return string.Compare(itemB.displayName, itemA.displayName, true);
case EColumn.SizeA:
return itemA.size[0].CompareTo(itemB.size[0]);
case EColumn.SizeB:
return itemA.size[1].CompareTo(itemB.size[1]);
case EColumn.SizeDiff:
return itemA.sizeDiff.CompareTo(itemB.sizeDiff);
case EColumn.CountA:
return itemA.count[0].CompareTo(itemB.count[0]);
case EColumn.CountB:
return itemA.count[1].CompareTo(itemB.count[1]);
case EColumn.CountDiff:
return itemA.countDiff.CompareTo(itemB.countDiff);
}
return 0;
}
public void SwapAB()
{
if (!rootItem.hasChildren)
return;
foreach(var r in rootItem.children)
SwapR(r);
}
void SwapR(TreeViewItem i)
{
var item = i as Item;
if (item != null)
item.Swap();
if (i != null && i.hasChildren)
{
for (int n = 0; n < i.children.Count; ++n)
SwapR(i.children[n]);
}
}
public TreeViewItem BuildTree(PackedMemorySnapshot snapshotA, PackedMemorySnapshot snapshotB)
{
var root = new TreeViewItem { id = 0, depth = -1, displayName = "Root" };
if (snapshotA == null || snapshotB == null)
{
root.AddChild(new TreeViewItem { id = 1, depth = -1, displayName = "" });
return root;
}
var uniqueId = 1;
var snapshots = new[] { snapshotA, snapshotB };
BuildNativeTree(snapshots, ref uniqueId, root);
BuildManagedTree(snapshots, ref uniqueId, root);
BuildMemoryTree(snapshots, ref uniqueId, root);
BuildGCHandleTree(snapshots, ref uniqueId, root);
return root;
}
void BuildMemoryTree(PackedMemorySnapshot[] snapshots, ref int uniqueId, TreeViewItem root)
{
var parent = new Item
{
id = uniqueId++,
displayName = "C# Memory Sections",
depth = root.depth + 1,
icon = HeEditorStyles.csImage
};
root.AddChild(parent);
for (int k = 0, kend = snapshots.Length; k < kend; ++k)
{
if (window.isClosing) // the window is closing
break;
var snapshot = snapshots[k];
foreach (var section in snapshot.managedHeapSections)
{
parent.size[k] += section.size;
}
parent.count[k] += snapshot.managedHeapSections.Length;
}
}
void BuildGCHandleTree(PackedMemorySnapshot[] snapshots, ref int uniqueId, TreeViewItem root)
{
var parent = new Item
{
id = uniqueId++,
displayName = "GC Handles",
depth = root.depth + 1,
icon = HeEditorStyles.gcHandleImage
};
root.AddChild(parent);
for (int k = 0, kend = snapshots.Length; k < kend; ++k)
{
if (window.isClosing) // the window is closing
break;
var snapshot = snapshots[k];
parent.size[k] += (
snapshot.gcHandles.Length * snapshot.virtualMachineInformation.pointerSize.sizeInBytes()
).ToUIntClamped();
parent.count[k] += snapshot.gcHandles.Length;
}
}
void BuildNativeTree(PackedMemorySnapshot[] snapshots, ref int uniqueId, TreeViewItem root)
{
var parent = new Item
{
id = uniqueId++,
displayName = "C++ Objects",
depth = root.depth + 1,
icon = HeEditorStyles.cppImage
};
root.AddChild(parent);
var items = new List<Item>(1024);
var table = new Dictionary<string, Item>(1024); // TypeName, Item
for (int k = 0, kend = snapshots.Length; k < kend; ++k)
{
if (window.isClosing) // the window is closing
break;
var snapshot = snapshots[k];
for (int n = 0, nend = snapshot.nativeTypes.Length; n < nend; ++n)
{
var type = snapshot.nativeTypes[n];
var item = default(Item);
if (!table.TryGetValue(type.name, out item))
{
item = new Item
{
id = uniqueId++,
displayName = type.name,
depth = parent.depth + 1,
icon = HeEditorStyles.cppImage
};
items.Add(item);
table[type.name] = item;
}
item.size[k] = type.totalObjectSize;
item.count[k] = type.totalObjectCount;
}
}
// Remove all items that are identical between both snapshots
for (int n = 0, nend = items.Count; n < nend; ++n)
{
if (items[n].countDiff != 0 && items[n].sizeDiff != 0)
parent.AddChild(items[n]);
}
Sum(parent);
}
void Sum(Item parent)
{
if (parent == null || !parent.hasChildren)
return;
for (int n = 0, nend = parent.children.Count; n < nend; ++n)
{
if (window.isClosing) // the window is closing
break;
var item = parent.children[n] as Item;
if (item == null)
continue;
parent.size[0] += item.size[0];
parent.size[1] += item.size[1];
parent.count[0] += item.count[0];
parent.count[1] += item.count[1];
}
}
void BuildManagedTree(PackedMemorySnapshot[] snapshots, ref int uniqueId, TreeViewItem root)
{
var parent = new Item
{
id = uniqueId++,
displayName = "C# Objects",
depth = root.depth + 1,
icon = HeEditorStyles.csImage
};
root.AddChild(parent);
var items = new List<Item>(1024);
var table = new Dictionary<string, Item>(1024); // TypeName, Item
for (int k = 0, kend = snapshots.Length; k < kend; ++k)
{
if (window.isClosing) // the window is closing
break;
var snapshot = snapshots[k];
for (int n = 0, nend = snapshot.managedTypes.Length; n < nend; ++n)
{
var type = snapshot.managedTypes[n];
var item = default(Item);
if (!table.TryGetValue(type.name, out item))
{
item = new Item
{
id = uniqueId++,
displayName = type.name,
depth = parent.depth + 1,
icon = HeEditorStyles.csImage
};
items.Add(item);
table[type.name] = item;
}
item.size[k] = type.totalObjectSize;
item.count[k] = type.totalObjectCount;
}
}
// Remove all items that are identical between both snapshots
for (int n = 0, nend = items.Count; n < nend; ++n)
{
if (items[n].countDiff != 0 && items[n].sizeDiff != 0)
parent.AddChild(items[n]);
}
Sum(parent);
}
class Item : AbstractTreeViewItem
{
public ulong[] size = new ulong[2];
public long[] count = new long[2];
public long sizeDiff
{
get
{
return size[1].ToLongClamped() - size[0].ToLongClamped();
}
}
public long countDiff
{
get
{
return count[1] - count[0];
}
}
const string k_UnknownTypeString = "<unknown type>";
public void Swap()
{
var s = size[0];
size[0] = size[1];
size[1] = s;
var c = count[0];
count[0] = count[1];
count[1] = c;
}
public override void GetItemSearchString(string[] target, out int count, out string type, out string label)
{
base.GetItemSearchString(target, out count, out type, out label);
type = displayName ?? k_UnknownTypeString;
target[count++] = type;
}
public override void OnGUI(Rect position, int column)
{
if (column == 0 && this.icon != null)
{
GUI.Box(HeEditorGUI.SpaceL(ref position, position.height), this.icon, HeEditorStyles.iconStyle);
}
switch((EColumn)column)
{
case EColumn.Type:
HeEditorGUI.TypeName(position, displayName ?? k_UnknownTypeString);
break;
case EColumn.SizeA:
HeEditorGUI.Size(position, size[0].ToLongClamped());
break;
case EColumn.SizeB:
HeEditorGUI.Size(position, size[1].ToLongClamped());
break;
case EColumn.SizeDiff:
HeEditorGUI.SizeDiff(position, sizeDiff);
break;
case EColumn.CountA:
HeEditorGUI.Count(position, count[0]);
break;
case EColumn.CountB:
HeEditorGUI.Count(position, count[1]);
break;
case EColumn.CountDiff:
HeEditorGUI.CountDiff(position, countDiff);
break;
}
}
}
}
}