-
Notifications
You must be signed in to change notification settings - Fork 0
/
Snapshot.cs
452 lines (387 loc) · 16.9 KB
/
Snapshot.cs
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Unicode;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
using System.Xml.Linq;
using static MonoHeapSnapshotViewer.Utils;
using System.Diagnostics.CodeAnalysis;
namespace MonoHeapSnapshotViewer {
public unsafe class Snapshot : IDisposable {
public MemoryMappedFile File;
public MemoryMappedViewAccessor View;
public byte* Data;
public Dictionary<string, double> Counters = new ();
public Dictionary<UInt32, string> StringTable = new() {
{ 0, "" }
};
public SnapshotClass[] Classes;
public SnapshotObject[] Objects;
public SnapshotRoot[] Roots;
public Task Initializing;
public Snapshot (string path) {
File = MemoryMappedFile.CreateFromFile(path, FileMode.Open);
View = File.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read);
Data = null;
View.SafeMemoryMappedViewHandle.AcquirePointer(ref Data);
Initializing = Task.Run(Initialize);
// Nullable fields are initialized by the async handler
}
internal void Initialize () {
var data = new ReadOnlySpan<byte>(Data, (int)View.Capacity);
int i = 0;
// Load counters (there may be more than one chunk)
while (i < (View.Capacity - 8)) {
var chunk = new RiffChunk(data, ref i);
var chunkId = Encoding.UTF8.GetString(chunk.ChunkId);
switch (chunkId) {
case "CNTR":
LoadCounters(chunk);
break;
}
}
Classes = new SnapshotClass[(int)Counters["snapshot/num-classes"]];
Objects = new SnapshotObject[(int)Counters["snapshot/num-objects"]];
Roots = new SnapshotRoot[(int)Counters["snapshot/num-roots"]];
var refAddresses = new ArraySegment<UInt32>(new UInt32[(int)Counters["snapshot/num-refs"]]);
int classOffset = 0, objectOffset = 0, rootOffset = 0;
// First decoding pass
i = 0;
while (i < (View.Capacity - 8)) {
var chunk = new RiffChunk(data, ref i);
var chunkId = Encoding.UTF8.GetString(chunk.ChunkId);
switch (chunkId) {
case "STBL":
DecodeStringTable(chunk);
break;
case "TYPE":
DecodeClasses(chunk, ref classOffset);
break;
case "OBJH":
DecodeObjectHeaders(chunk, ref objectOffset);
break;
default:
Debug.WriteLine($"{chunkId} {chunk.Data.Length}b");
break;
}
}
Array.Sort(Classes, 0, classOffset, SnapshotClass.AddressComparer.Instance);
Array.Sort(Objects, 0, objectOffset, SnapshotObject.AddressComparer.Instance);
// Ref scanning pass
i = 0;
while (i < (View.Capacity - 8)) {
var chunk = new RiffChunk(data, ref i);
var chunkId = Encoding.UTF8.GetString(chunk.ChunkId);
switch (chunkId) {
case "REFS":
// Determine how many refs total each object has so we can reserve enough space to store them
DecodeRefsPass1(chunk);
break;
case "ROOT":
DecodeRootsPass1(chunk, ref rootOffset);
break;
default:
Debug.WriteLine($"{chunkId} {chunk.Data.Length}b");
break;
}
}
// Ref assembly pass
i = 0;
while (i < (View.Capacity - 8)) {
var chunk = new RiffChunk(data, ref i);
var chunkId = Encoding.UTF8.GetString(chunk.ChunkId);
switch (chunkId) {
case "REFS":
// Now build lists of refs for each object
DecodeRefsPass2(chunk, ref refAddresses);
break;
default:
Debug.WriteLine($"{chunkId} {chunk.Data.Length}b");
break;
}
}
BuildSummaryData();
}
private void DecodeRootsPass1 (RiffChunk chunk, ref int rootOffset) {
var data = chunk.Data;
while (data.Length > 0) {
Assert(ReadLEBUInt(ref data, out var kind));
Assert(ReadLEBUInt(ref data, out var rootCount));
for (uint i = 0; i < rootCount; i++) {
ref var root = ref Roots[rootOffset++];
root = new SnapshotRoot();
root.Address = ReadValue<UInt32>(ref data);
root.Object = ReadValue<UInt32>(ref data);
ref var obj = ref Object(root.Object);
obj.DirectRootCount++;
}
}
}
private void BuildSummaryData () {
for (int i = 0; i < Objects.Length; i++) {
ref var obj = ref Objects[i];
ref var klass = ref Class(obj.Klass);
klass.Count += 1;
klass.ShallowSizeSum += obj.ShallowSize;
}
for (int i = 0; i < Objects.Length; i++) {
ref var obj = ref Objects[i];
ref var klass = ref Class(obj.Klass);
UpdateSummaryDataForObject(ref klass, ref obj, 0);
}
for (int i = 0; i < Classes.Length; i++) {
ref var klass = ref Classes[i];
klass.Depth2HashSet = null;
klass.ReachableHashSet = null;
}
}
private void UpdateSummaryDataForObject (ref SnapshotClass klass, ref SnapshotObject obj, int depth) {
UpdateSummaryDataForObjectRefs(ref klass, ref obj, 0);
}
private void UpdateSummaryDataForObjectRefs (
ref SnapshotClass klass, ref SnapshotObject obj, int depth
) {
klass.Depth2HashSet ??= new ();
klass.ReachableHashSet ??= new ();
if (depth <= 1) {
if (!klass.Depth2HashSet.Contains(obj.Object)) {
klass.Depth2HashSet.Add(obj.Object);
klass.Depth2SizeSum += obj.ShallowSize;
}
}
if (!klass.ReachableHashSet.Contains(obj.Object)) {
klass.ReachableHashSet.Add(obj.Object);
klass.ReachableSizeSum += obj.ShallowSize;
}
if (depth == 0)
obj.Depth2Size = obj.ShallowSize;
if (obj.RefCount < 1)
return;
foreach (var r in obj.Refs) {
ref var refTarget = ref Object(r);
if (depth == 0)
obj.Depth2Size += refTarget.ShallowSize;
if (!klass.ReachableHashSet.Contains(refTarget.Object))
UpdateSummaryDataForObjectRefs(ref klass, ref refTarget, depth + 1);
}
}
private void DecodeStringTable (RiffChunk chunk) {
var data = chunk.Data;
while (data.Length > 0) {
Assert(ReadLEBUInt(ref data, out var index));
var value = DecodePString(ref data);
StringTable[(uint)index] = value;
}
}
private void DecodeClasses (RiffChunk chunk, ref int classOffset) {
var data = chunk.Data;
while (data.Length > 0) {
ref var klass = ref Classes[classOffset++];
klass = new SnapshotClass();
klass.Klass = ReadValue<UInt32>(ref data);
klass.ElementKlass = ReadValue<UInt32>(ref data);
klass.NestingKlass = ReadValue<UInt32>(ref data);
klass.Assembly = ReadValue<UInt32>(ref data);
Assert(ReadLEBUInt(ref data, out var rank));
klass.Rank = (int)rank;
Assert(ReadLEBUInt(ref data, out var kindName));
klass.Kind = kindName;
Assert(ReadLEBUInt(ref data, out var ns));
klass.Namespace = ns;
Assert(ReadLEBUInt(ref data, out var name));
klass.Name = name;
Assert(ReadLEBUInt(ref data, out var numGps));
// FIXME: Slices of a single big array for better density
// ALTERNATELY: On-demand constructed ReadOnlySpan over the mmap'd view
var gps = new UInt32[numGps];
for (uint i = 0; i < numGps; i++)
gps[i] = ReadValue<UInt32>(ref data);
klass.GenericParameters = gps;
}
}
private void DecodeObjectHeaders (RiffChunk chunk, ref int objectOffset) {
var data = chunk.Data;
while (data.Length > 0) {
ref var header = ref Objects[objectOffset++];
header = new SnapshotObject();
header.Object = ReadValue<UInt32>(ref data);
header.Klass = ReadValue<UInt32>(ref data);
Assert(ReadLEBUInt(ref data, out var shallowSize));
header.ShallowSize = (uint)shallowSize;
}
}
private void DecodeRefsPass1 (RiffChunk chunk) {
var data = chunk.Data;
while (data.Length > 0) {
var obj = ReadValue<UInt32>(ref data);
ref var header = ref Object(obj);
Assert(ReadLEBUInt(ref data, out var count));
header.RefCount += (uint)count;
// Skip the actual refs, we'll handle them in pass 2
data = data.Slice((int)count * 4);
}
}
private void DecodeRefsPass2 (RiffChunk chunk, ref ArraySegment<uint> refAddresses) {
var data = chunk.Data;
while (data.Length > 0) {
var obj = ReadValue<UInt32>(ref data);
ref var header = ref Object(obj);
Assert(ReadLEBUInt(ref data, out var count));
int j = header.Refs.Count;
if (header.Refs.Count == 0) {
header.Refs = new ArraySegment<uint>(refAddresses.Array!, refAddresses.Offset, (int)count);
refAddresses = new ArraySegment<uint>(refAddresses.Array!, refAddresses.Offset + (int)header.RefCount, refAddresses.Count - (int)header.RefCount);
} else {
header.Refs = new ArraySegment<uint>(header.Refs.Array!, header.Refs.Offset, header.Refs.Count + (int)count);
}
for (int i = 0; i < (int)count; i++)
header.Refs[j + i] = ReadValue<UInt32>(ref data);
}
}
internal void LoadCounters (RiffChunk chunk) {
var data = chunk.Data;
while (data.Length > 0) {
var name = DecodePString(ref data);
var value = ReadValue<double>(ref data);
Counters[name] = value;
}
}
public ref SnapshotClass Class (uint klass) {
var needle = new SnapshotClass {
Klass = klass,
};
var index = Array.BinarySearch(Classes, needle, SnapshotClass.AddressComparer.Instance);
if (index < 0)
throw new Exception($"Class not found: {klass}");
return ref Classes[index];
}
public ref SnapshotObject Object (uint obj) {
var needle = new SnapshotObject {
Object = obj,
};
var index = Array.BinarySearch(Objects, needle, SnapshotObject.AddressComparer.Instance);
if (index < 0)
throw new Exception($"Object not found: {obj}");
return ref Objects[index];
}
public void Dispose () {
Data = null;
View.SafeMemoryMappedViewHandle.ReleasePointer();
View.Dispose();
File.Dispose();
}
}
public struct Counter {
public string Name;
public double Value;
}
public ref struct RiffChunk {
public ReadOnlySpan<byte> ChunkId;
public ReadOnlySpan<byte> Data;
public RiffChunk (ReadOnlySpan<byte> data, ref int offset) {
ChunkId = data.Slice(offset, 4);
var lengthSpan = data.Slice(offset + 4, 4);
var length = MemoryMarshal.Read<UInt32>(lengthSpan);
Data = data.Slice(offset + 8, (int)length);
offset += ((int)length + 8);
}
}
public struct StringTableKey {
public UInt32 Index;
public string Get (Snapshot snapshot) =>
snapshot.StringTable.TryGetValue(Index, out var value)
? value
: "missing";
public static implicit operator StringTableKey (uint value) =>
new StringTableKey { Index = value };
public static implicit operator StringTableKey (ulong value) =>
new StringTableKey { Index = (uint)value };
public override string ToString () => Index.ToString();
public override int GetHashCode () => Index.GetHashCode();
public bool Equals (StringTableKey rhs) =>
Index == rhs.Index;
public override bool Equals ([NotNullWhen(true)] object? obj) {
if (obj is StringTableKey stk)
return Equals(stk);
else
return false;
}
}
public struct SnapshotClass {
public class AddressComparer : IComparer<SnapshotClass> {
public static readonly AddressComparer Instance = new();
public int Compare (SnapshotClass x, SnapshotClass y) =>
x.Klass.CompareTo(y.Klass);
}
public UInt32 Klass, ElementKlass, NestingKlass, Assembly;
public int Rank;
public StringTableKey Kind, Namespace, Name;
public ArraySegment<UInt32> GenericParameters;
public HashSet<UInt32>? Depth2HashSet, ReachableHashSet;
public uint Count, ShallowSizeSum, Depth2SizeSum, ReachableSizeSum;
private string? _FullName;
// FIXME: Use a stringbuilder
public string GetFullName (Snapshot snapshot) {
if (_FullName != null)
return _FullName;
if (Namespace.Index > 0)
_FullName = $"{Namespace.Get(snapshot)}.{Name.Get(snapshot)}";
else
_FullName = Name.Get(snapshot);
if (NestingKlass > 0)
_FullName = $"{snapshot.Class(NestingKlass).GetFullName(snapshot)}.{_FullName}";
// FIXME: Optimize this
if (GenericParameters.Count > 0)
_FullName += $"<{string.Join(", ", from gp in GenericParameters select snapshot.Class(gp).GetFullName(snapshot))}>";
if (Rank > 0)
_FullName += "[]";
return _FullName;
}
}
public struct SnapshotObject {
public class AddressComparer : IComparer<SnapshotObject> {
public static readonly AddressComparer Instance = new();
public int Compare (SnapshotObject x, SnapshotObject y) =>
x.Object.CompareTo(y.Object);
}
public UInt32 Object, Klass;
public uint ShallowSize, RefCount, Depth2Size, DirectRootCount;
public ArraySegment<UInt32> Refs;
private uint _ReachableSize;
public uint GetReachableSize (Snapshot snapshot) {
if (_ReachableSize > 0)
return _ReachableSize;
if (Refs.Count <= 0)
return ShallowSize;
var set = new HashSet<UInt32> { Object };
var result = ShallowSize;
if (RefCount > 0)
AccumulateRefs(snapshot, set, Refs, ref result);
_ReachableSize = result;
return result;
}
private void AccumulateRefs (Snapshot snapshot, HashSet<UInt32> set, ArraySegment<uint> refs, ref uint result) {
foreach (var r in refs) {
if (set.Contains(r))
continue;
ref var refTarget = ref snapshot.Object(r);
set.Add(r);
result += refTarget.ShallowSize;
if (refTarget.RefCount > 0)
AccumulateRefs(snapshot, set, refTarget.Refs, ref result);
}
}
}
public struct SnapshotRoot {
public StringTableKey Kind;
public UInt32 Address, Object;
}
}