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
|
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# flake8: noqa: F821
# Generate graph structures for GC statistics recording.
#
# Stats phases are nested and form a directed acyclic graph starting
# from a set of root phases. Importantly, a phase may appear under more
# than one parent phase.
#
# For example, the following arrangement is possible:
#
# +---+
# | A |
# +---+
# |
# +-------+-------+
# | | |
# v v v
# +---+ +---+ +---+
# | B | | C | | D |
# +---+ +---+ +---+
# | |
# +---+---+
# |
# v
# +---+
# | E |
# +---+
#
# This graph is expanded into a tree (or really a forest) and phases
# with multiple parents are duplicated.
#
# For example, the input example above would be expanded to:
#
# +---+
# | A |
# +---+
# |
# +-------+-------+
# | | |
# v v v
# +---+ +---+ +---+
# | B | | C | | D |
# +---+ +---+ +---+
# | |
# v v
# +---+ +---+
# | E | | E'|
# +---+ +---+
# NOTE: If you add new phases here the current next phase kind number can be
# found at the end of js/src/gc/StatsPhasesGenerated.inc
import collections
import re
class PhaseKind:
def __init__(self, name, descr, bucket, children=[]):
self.name = name
self.descr = descr
# For telemetry
self.bucket = bucket
self.children = children
AllPhaseKinds = []
PhaseKindsByName = dict()
def addPhaseKind(name, descr, bucket, children=[]):
assert name not in PhaseKindsByName
phaseKind = PhaseKind(name, descr, bucket, children)
AllPhaseKinds.append(phaseKind)
PhaseKindsByName[name] = phaseKind
return phaseKind
def getPhaseKind(name):
return PhaseKindsByName[name]
PhaseKindGraphRoots = [
addPhaseKind("MUTATOR", "Mutator Running", 0),
addPhaseKind("GC_BEGIN", "Begin Callback", 1),
addPhaseKind(
"EVICT_NURSERY_FOR_MAJOR_GC",
"Evict Nursery For Major GC",
70,
[
addPhaseKind(
"MARK_ROOTS",
"Mark Roots",
48,
[
addPhaseKind("MARK_CCWS", "Mark Cross Compartment Wrappers", 50),
addPhaseKind("MARK_STACK", "Mark C and JS stacks", 51),
addPhaseKind("MARK_RUNTIME_DATA", "Mark Runtime-wide Data", 52),
addPhaseKind("MARK_EMBEDDING", "Mark Embedding", 53),
],
)
],
),
addPhaseKind("WAIT_BACKGROUND_THREAD", "Wait Background Thread", 2),
addPhaseKind(
"PREPARE",
"Prepare For Collection",
69,
[
addPhaseKind("UNMARK", "Unmark", 7),
addPhaseKind("UNMARK_WEAKMAPS", "Unmark WeakMaps", 76),
addPhaseKind("MARK_DISCARD_CODE", "Mark Discard Code", 3),
addPhaseKind("RELAZIFY_FUNCTIONS", "Relazify Functions", 4),
addPhaseKind("PURGE", "Purge", 5),
addPhaseKind("PURGE_PROP_MAP_TABLES", "Purge PropMapTables", 60),
addPhaseKind("PURGE_SOURCE_URLS", "Purge Source URLs", 73),
addPhaseKind("JOIN_PARALLEL_TASKS", "Join Parallel Tasks", 67),
],
),
addPhaseKind(
"MARK",
"Mark",
6,
[
getPhaseKind("MARK_ROOTS"),
addPhaseKind("MARK_DELAYED", "Mark Delayed", 8),
addPhaseKind(
"MARK_WEAK",
"Mark Weak",
13,
[
getPhaseKind("MARK_DELAYED"),
addPhaseKind("MARK_GRAY_WEAK", "Mark Gray and Weak", 16),
],
),
addPhaseKind("MARK_INCOMING_GRAY", "Mark Incoming Gray Pointers", 14),
addPhaseKind("MARK_GRAY", "Mark Gray", 15),
addPhaseKind(
"PARALLEL_MARK",
"Parallel marking",
78,
[
getPhaseKind("JOIN_PARALLEL_TASKS"),
# The following are only used for parallel phase times:
addPhaseKind("PARALLEL_MARK_MARK", "Parallel marking work", 79),
addPhaseKind("PARALLEL_MARK_WAIT", "Waiting for work", 80),
],
),
],
),
addPhaseKind(
"SWEEP",
"Sweep",
9,
[
getPhaseKind("MARK"),
addPhaseKind(
"FINALIZE_START",
"Finalize Start Callbacks",
17,
[
addPhaseKind("WEAK_ZONES_CALLBACK", "Per-Slice Weak Callback", 57),
addPhaseKind(
"WEAK_COMPARTMENT_CALLBACK", "Per-Compartment Weak Callback", 58
),
],
),
addPhaseKind("UPDATE_ATOMS_BITMAP", "Sweep Atoms Bitmap", 68),
addPhaseKind("SWEEP_ATOMS_TABLE", "Sweep Atoms Table", 18),
addPhaseKind(
"SWEEP_COMPARTMENTS",
"Sweep Compartments",
20,
[
addPhaseKind("SWEEP_DISCARD_CODE", "Sweep Discard Code", 21),
addPhaseKind("SWEEP_INNER_VIEWS", "Sweep Inner Views", 22),
addPhaseKind(
"SWEEP_CC_WRAPPER", "Sweep Cross Compartment Wrappers", 23
),
addPhaseKind("SWEEP_BASE_SHAPE", "Sweep Base Shapes", 24),
addPhaseKind("SWEEP_INITIAL_SHAPE", "Sweep Initial Shapes", 25),
addPhaseKind("SWEEP_REGEXP", "Sweep Regexps", 28),
addPhaseKind("SWEEP_COMPRESSION", "Sweep Compression Tasks", 62),
addPhaseKind("SWEEP_WEAKMAPS", "Sweep WeakMaps", 63),
addPhaseKind("SWEEP_UNIQUEIDS", "Sweep Unique IDs", 64),
addPhaseKind(
"SWEEP_FINALIZATION_OBSERVERS",
"Sweep FinalizationRegistries and WeakRefs",
74,
),
addPhaseKind("SWEEP_JIT_DATA", "Sweep JIT Data", 65),
addPhaseKind("SWEEP_WEAK_CACHES", "Sweep Weak Caches", 66),
addPhaseKind("SWEEP_MISC", "Sweep Miscellaneous", 29),
getPhaseKind("JOIN_PARALLEL_TASKS"),
],
),
addPhaseKind("FINALIZE_OBJECT", "Finalize Objects", 33),
addPhaseKind("FINALIZE_NON_OBJECT", "Finalize Non-objects", 34),
addPhaseKind("SWEEP_PROP_MAP", "Sweep PropMap Tree", 77),
addPhaseKind("FINALIZE_END", "Finalize End Callback", 38),
addPhaseKind("DESTROY", "Deallocate", 39),
getPhaseKind("JOIN_PARALLEL_TASKS"),
addPhaseKind("FIND_DEAD_COMPARTMENTS", "Find Dead Compartments", 54),
],
),
addPhaseKind(
"COMPACT",
"Compact",
40,
[
addPhaseKind("COMPACT_MOVE", "Compact Move", 41),
addPhaseKind(
"COMPACT_UPDATE",
"Compact Update",
42,
[
getPhaseKind("MARK_ROOTS"),
addPhaseKind("COMPACT_UPDATE_CELLS", "Compact Update Cells", 43),
getPhaseKind("JOIN_PARALLEL_TASKS"),
],
),
],
),
addPhaseKind("DECOMMIT", "Decommit", 72),
addPhaseKind("GC_END", "End Callback", 44),
addPhaseKind(
"MINOR_GC",
"All Minor GCs",
45,
[
getPhaseKind("MARK_ROOTS"),
],
),
addPhaseKind(
"EVICT_NURSERY",
"Minor GCs to Evict Nursery",
46,
[
getPhaseKind("MARK_ROOTS"),
],
),
addPhaseKind(
"TRACE_HEAP",
"Trace Heap",
47,
[
getPhaseKind("MARK_ROOTS"),
],
),
]
class Phase:
# Expand the DAG into a tree, duplicating phases which have more than
# one parent.
def __init__(self, phaseKind, parent):
self.phaseKind = phaseKind
self.parent = parent
self.depth = parent.depth + 1 if parent else 0
self.children = []
self.nextSibling = None
self.nextInPhaseKind = None
self.path = re.sub(r"\W+", "_", phaseKind.name.lower())
if parent is not None:
self.path = parent.path + "." + self.path
def expandPhases():
phases = []
phasesForKind = collections.defaultdict(list)
def traverse(phaseKind, parent):
ep = Phase(phaseKind, parent)
phases.append(ep)
# Update list of expanded phases for this phase kind.
if phasesForKind[phaseKind]:
phasesForKind[phaseKind][-1].nextInPhaseKind = ep
phasesForKind[phaseKind].append(ep)
# Recurse over children.
for child in phaseKind.children:
child_ep = traverse(child, ep)
if ep.children:
ep.children[-1].nextSibling = child_ep
ep.children.append(child_ep)
return ep
for phaseKind in PhaseKindGraphRoots:
traverse(phaseKind, None)
return phases, phasesForKind
AllPhases, PhasesForPhaseKind = expandPhases()
# Name phases based on phase kind name and index if there are multiple phases
# corresponding to a single phase kind.
for phaseKind in AllPhaseKinds:
phases = PhasesForPhaseKind[phaseKind]
if len(phases) == 1:
phases[0].name = "%s" % phaseKind.name
else:
for index, phase in enumerate(phases):
phase.name = "%s_%d" % (phaseKind.name, index + 1)
# Find the maximum phase nesting.
MaxPhaseNesting = max(phase.depth for phase in AllPhases) + 1
# And the maximum bucket number.
MaxBucket = max(kind.bucket for kind in AllPhaseKinds)
# Generate code.
def writeList(out, items):
if items:
out.write(",\n".join(" " + item for item in items) + "\n")
def writeEnumClass(out, name, type, items, extraItems):
items = ["FIRST"] + list(items) + ["LIMIT"] + list(extraItems)
items[1] += " = " + items[0]
out.write("enum class %s : %s {\n" % (name, type))
writeList(out, items)
out.write("};\n")
def generateHeader(out):
#
# Generate PhaseKind enum.
#
phaseKindNames = map(lambda phaseKind: phaseKind.name, AllPhaseKinds)
extraPhaseKinds = [
"NONE = LIMIT",
"EXPLICIT_SUSPENSION = LIMIT",
"IMPLICIT_SUSPENSION",
]
writeEnumClass(out, "PhaseKind", "uint8_t", phaseKindNames, extraPhaseKinds)
out.write("\n")
#
# Generate Phase enum.
#
phaseNames = map(lambda phase: phase.name, AllPhases)
extraPhases = ["NONE = LIMIT", "EXPLICIT_SUSPENSION = LIMIT", "IMPLICIT_SUSPENSION"]
writeEnumClass(out, "Phase", "uint8_t", phaseNames, extraPhases)
out.write("\n")
#
# Generate MAX_PHASE_NESTING constant.
#
out.write("static const size_t MAX_PHASE_NESTING = %d;\n" % MaxPhaseNesting)
def generateCpp(out):
#
# Generate the PhaseKindInfo table.
#
out.write("static constexpr PhaseKindTable phaseKinds = {\n")
for phaseKind in AllPhaseKinds:
phase = PhasesForPhaseKind[phaseKind][0]
out.write(
' /* PhaseKind::%s */ PhaseKindInfo { Phase::%s, %d, "%s" },\n'
% (phaseKind.name, phase.name, phaseKind.bucket, phaseKind.name)
)
out.write("};\n")
out.write("\n")
#
# Generate the PhaseInfo tree.
#
def name(phase):
return "Phase::" + phase.name if phase else "Phase::NONE"
out.write("static constexpr PhaseTable phases = {\n")
for phase in AllPhases:
firstChild = phase.children[0] if phase.children else None
phaseKind = phase.phaseKind
out.write(
' /* %s */ PhaseInfo { %s, %s, %s, %s, PhaseKind::%s, %d, "%s", "%s" },\n'
% ( # NOQA: E501
name(phase),
name(phase.parent),
name(firstChild),
name(phase.nextSibling),
name(phase.nextInPhaseKind),
phaseKind.name,
phase.depth,
phaseKind.descr,
phase.path,
)
)
out.write("};\n")
#
# Print in a comment the next available phase kind number.
#
out.write("// The next available phase kind number is: %d\n" % (MaxBucket + 1))
|