-
Notifications
You must be signed in to change notification settings - Fork 2
/
MonoMemoryGraphBuilder.cs
184 lines (155 loc) · 7.02 KB
/
MonoMemoryGraphBuilder.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
using Graphs;
using Microsoft.Diagnostics.Tracing;
using Microsoft.Diagnostics.Tracing.Parsers.Clr;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
#nullable enable
namespace MonoGCDump
{
class MonoMemoryGraphBuilder
{
record GCRootRangeData(long Start, long End, string Name);
record GCRootData(long ObjectId, long AddressId);
record GCTypeData(long VTableId, string ClassName, long ModuleId);
record GCObjectReferenceData(long ObjectId, long VTableId, int ObjectSize, long[] Children);
class GCRootRangeComparer : IComparer<GCRootRangeData>
{
public int Compare(GCRootRangeData? x, GCRootRangeData? y)
{
if (x == y)
return 0;
if (x == null)
return 1;
if (y == null)
return -1;
return x.Start.CompareTo(y.Start);
}
}
public static async Task<MemoryGraph> Build(EventPipeEventSource source, MonoGCRootRangeTracker? rootRangeTracker = null, Action? stop = null)
{
var moduleMap = new Dictionary<long, string>();
var typeData = new List<GCTypeData>();
var objectReferenceData = new List<GCObjectReferenceData>();
var rootData = new List<GCRootData>();
var vtableIdToTypeIndex = new Dictionary<long, NodeTypeIndex>();
var objectIdToNodeIndex = new Dictionary<long, NodeIndex>();
var monoProfiler = new MonoProfilerTraceEventParser(source);
var clrRundown = new ClrRundownTraceEventParser(source);
TaskCompletionSource heapDumpStart = new TaskCompletionSource();
rootRangeTracker ??= new MonoGCRootRangeTracker();
rootRangeTracker.Attach(monoProfiler);
clrRundown.LoaderModuleDCStop += delegate (ModuleLoadUnloadTraceData data)
{
moduleMap[data.ModuleID] = data.ModuleILPath;
};
monoProfiler.MonoProfilerGCHeapDumpStart += data => heapDumpStart.SetResult();
monoProfiler.MonoProfilerGCHeapDumpStop += data => stop?.Invoke();
monoProfiler.MonoProfilerGCHeapDumpObjectReferenceData += data =>
{
long[] children;
if (data.Count > 0)
{
children = new long[data.Count];
for (int i = 0; i < data.Count; i++)
{
children[i] = data.GetReferencesObjectId(i);
}
}
else
{
children = Array.Empty<long>();
}
objectReferenceData.Add(new GCObjectReferenceData(data.ObjectID, data.VTableID, (int)data.ObjectSize, children));
};
monoProfiler.MonoProfilerGCRoots += delegate (GCRootsData data)
{
for (int i = 0; i < data.Count; i++)
{
rootData.Add(new GCRootData(data.GetObjectID(i), data.GetAddressID(i)));
}
};
monoProfiler.MonoProfilerGCHeapDumpVTableClassReference += delegate (GCHeapDumpVTableClassReferenceData data)
{
typeData.Add(new GCTypeData(data.VTableID, data.ClassName, data.ModuleID));
};
var processTask = Task.Run(() => source.Process());
if (await Task.WhenAny(heapDumpStart.Task, Task.Delay(5000)) != heapDumpStart.Task)
{
source.StopProcessing();
throw new TimeoutException("Heap dump didn't start within 5 seconds");
}
await processTask;
rootRangeTracker.Detach(monoProfiler);
// TODO: Better estimate
var memoryGraph = new MemoryGraph(objectReferenceData.Count + 1024);
memoryGraph.Is64Bit = source.PointerSize == 8;
var rootBuilder = new MemoryNodeBuilder(memoryGraph, "[.NET Roots]");
foreach (var type in typeData)
{
if (!vtableIdToTypeIndex.TryGetValue(type.VTableId, out var typeIndex))
{
if (!moduleMap.TryGetValue(type.ModuleId, out string? moduleName))
moduleName = $"(Module 0x{type.ModuleId:x})";
typeIndex = memoryGraph.CreateType(type.ClassName, moduleName);
vtableIdToTypeIndex[type.VTableId] = typeIndex;
}
}
foreach (var objectReference in objectReferenceData)
{
if (!objectIdToNodeIndex.TryGetValue(objectReference.ObjectId, out var nodeIndex))
{
nodeIndex = memoryGraph.CreateNode();
objectIdToNodeIndex.Add(objectReference.ObjectId, nodeIndex);
}
if (!memoryGraph.IsDefined(nodeIndex))
{
var children = new GrowableArray<NodeIndex>(objectReference.Children.Length);
for (int i = 0; i < objectReference.Children.Length; i++)
{
var childObjectId = objectReference.Children[i];
if (!objectIdToNodeIndex.TryGetValue(childObjectId, out var childNodeIndex))
{
childNodeIndex = memoryGraph.CreateNode();
objectIdToNodeIndex.Add(childObjectId, childNodeIndex);
}
children.Add(childNodeIndex);
}
memoryGraph.SetNode(nodeIndex, vtableIdToTypeIndex[objectReference.VTableId], objectReference.ObjectSize, children);
}
else
{
Console.WriteLine($"Duplicate object ID: {objectReference.ObjectId:X}");
}
}
if (rootData.Count == 0)
{
Console.WriteLine($"Missing GC Roots data");
// Fallback to reporting every object as a root if we don't have GCRoots events
foreach (var objectReference in objectReferenceData)
{
if (objectIdToNodeIndex.TryGetValue(objectReference.ObjectId, out var nodeIndex))
{
rootBuilder.AddChild(nodeIndex);
}
}
}
else
{
foreach (var root in rootData)
{
if (objectIdToNodeIndex.TryGetValue(root.ObjectId, out var nodeIndex))
{
// Find
var rootRange = rootRangeTracker.FindRootRange(root.AddressId);
rootBuilder.FindOrCreateChild(rootRange?.Name ?? "Other Roots").AddChild(nodeIndex);
}
}
}
memoryGraph.RootIndex = rootBuilder.Build();
memoryGraph.AllowReading();
return memoryGraph;
}
}
}