forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap-snapshot-to-graphviz.js
executable file
·164 lines (139 loc) · 4.05 KB
/
heap-snapshot-to-graphviz.js
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
#!/usr/bin/env bun
// Usage: Tools/Scripts/heap-snapshot-to-graphviz.js snapshot output
// Produces output/GCDebugging.gv.txt, which you can open with GraphVIZ:
// dot -Tsvg ~/GCDebugging.gv.txt -O
// You will probably want to edit this script manually to adjust the layout.
import { parseArgs } from "util"
import { file } from "bun"
let HeapSnapshot = {}
let HeapSnapshotDiff = {}
const nodeFieldCount = 4;
const nodeIdOffset = 0;
const nodeSizeOffset = 1;
const nodeClassNameOffset = 2;
const nodeFlagsOffset = 3;
const gcDebuggingNodeFieldCount = 7;
// node flags
const internalFlagsMask = (1 << 0);
const objectTypeMask = (1 << 1);
// edges
// [<0:fromId>, <1:toId>, <2:typeTableIndex>, <3:edgeDataIndexOrEdgeNameIndex>]
const edgeFieldCount = 4;
const edgeFromIdOffset = 0;
const edgeToIdOffset = 1;
const edgeTypeOffset = 2;
const edgeDataOffset = 3;
const rootFieldCount = 3;
const rootNodeIdOffset = 0;
const rootReasonIndexOffset = 1;
// Other constants.
const rootNodeIndex = 0;
const rootNodeOrdinal = 0;
const rootNodeIdentifier = 0;
eval(await file('Source/WebInspectorUI/UserInterface/Workers/HeapSnapshot/HeapSnapshot.js').text())
const { values, positionals } = parseArgs({
args: Bun.argv,
options: {
},
strict: true,
allowPositionals: true,
})
if (positionals.length != 4)
throw "Usage: Tools/Scripts/heap-snapshot-to-graphviz.js snapshot output"
let fileName = positionals[2]
let outputName = positionals[3]
let json = await file(fileName).text()
let snapshot = new HeapSnapshot(0, json, "Snapshot", true)
console.log("Snapshot loaded")
function escapeOutput(unsafe) {
return unsafe.replace(/["]/g, function (c) {
switch (c) {
case '"': return '-'
}
})
}
let output = `digraph Heap {
fontname="Helvetica,Arial,sans-serif";
node [fontname="Helvetica,Arial,sans-serif"];
edge [fontname="Helvetica,Arial,sans-serif"]; ` +
/*`
layout=twopi;
ranksep=2;
ratio=auto;
overlap=false;
overlap_scaling=100;
`*/
`
layout=neato
center=""
overlap=false
`
output += `
n0[label="Root"];`
let {nodeOrdinalToPostOrderIndex, postOrderIndexToNodeOrdinal} = snapshot._buildPostOrderIndexes()
const rootPostOrderIndex = snapshot._nodeCount - 1;
for (let postOrderIndex = rootPostOrderIndex - 1; postOrderIndex >= 0; --postOrderIndex) {
let {
id,
className,
size,
retainedSize,
internal,
isObjectType,
gcRoot,
dead,
dominatorNodeIdentifier,
hasChildren,
} = snapshot.serializeNode(postOrderIndexToNodeOrdinal[postOrderIndex] * snapshot._nodeFieldCount)
output += `
n${id}[label=" ${escapeOutput(className)}" tooltip="${size}"];`
}
for (let edgeIndex = 0; edgeIndex < snapshot._edges.length; edgeIndex += edgeFieldCount) {
let {
from, to, type, data
} = snapshot.serializeEdge(edgeIndex)
output += `
n${from} -> n${to} [label=" ${escapeOutput(type + ":" + data)}"];`
}
for (let nodeIndex = 0; nodeIndex < snapshot._nodes.length; nodeIndex += snapshot._nodeFieldCount) {
let {
id,
className,
size,
retainedSize,
internal,
isObjectType,
gcRoot,
dead,
dominatorNodeIdentifier,
hasChildren,
} = snapshot.serializeNode(nodeIndex)
if (!gcRoot) {
continue
}
let rootReason = ""
if (gcRoot) {
for (let rootIndex = 0; rootIndex < snapshot._roots.length; rootIndex += rootFieldCount) {
let nodeID = snapshot._roots[rootIndex + rootNodeIdOffset]
let reason = snapshot._labels[snapshot._roots[rootIndex + rootReasonIndexOffset]]
if (nodeID != id)
continue
rootReason = reason
break
}
}
output += `
n0 -> n${id} [label=" ROOT: ${escapeOutput(rootReason)}"];`
}
output += `
}
`
if (true) {
await Bun.write(outputName + "/GCDebugging.gv.txt", output);
}
if (true) {
let id = 276208
console.log(snapshot.shortestGCRootPath(id))
console.log("----")
console.log(snapshot._determineGCRootPaths(id))
}