| 1 |
# This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 |
|
| 5 |
# flake8: noqa: F821 |
| 6 |
|
| 7 |
# Generate graph structures for GC statistics recording. |
| 8 |
# |
| 9 |
# Stats phases are nested and form a directed acyclic graph starting |
| 10 |
# from a set of root phases. Importantly, a phase may appear under more |
| 11 |
# than one parent phase. |
| 12 |
# |
| 13 |
# For example, the following arrangement is possible: |
| 14 |
# |
| 15 |
# +---+ |
| 16 |
# | A | |
| 17 |
# +---+ |
| 18 |
# | |
| 19 |
# +-------+-------+ |
| 20 |
# | | | |
| 21 |
# v v v |
| 22 |
# +---+ +---+ +---+ |
| 23 |
# | B | | C | | D | |
| 24 |
# +---+ +---+ +---+ |
| 25 |
# | | |
| 26 |
# +---+---+ |
| 27 |
# | |
| 28 |
# v |
| 29 |
# +---+ |
| 30 |
# | E | |
| 31 |
# +---+ |
| 32 |
# |
| 33 |
# This graph is expanded into a tree (or really a forest) and phases |
| 34 |
# with multiple parents are duplicated. |
| 35 |
# |
| 36 |
# For example, the input example above would be expanded to: |
| 37 |
# |
| 38 |
# +---+ |
| 39 |
# | A | |
| 40 |
# +---+ |
| 41 |
# | |
| 42 |
# +-------+-------+ |
| 43 |
# | | | |
| 44 |
# v v v |
| 45 |
# +---+ +---+ +---+ |
| 46 |
# | B | | C | | D | |
| 47 |
# +---+ +---+ +---+ |
| 48 |
# | | |
| 49 |
# v v |
| 50 |
# +---+ +---+ |
| 51 |
# | E | | E'| |
| 52 |
# +---+ +---+ |
| 53 |
|
| 54 |
# NOTE: If you add new phases here the current next phase kind number can be |
| 55 |
# found at the end of js/src/gc/StatsPhasesGenerated.inc |
| 56 |
|
| 57 |
import collections |
| 58 |
import re |
| 59 |
|
| 60 |
|
| 61 |
class PhaseKind: |
| 62 |
def __init__(self, name, descr, bucket, children=[]): |
| 63 |
self.name = name |
| 64 |
self.descr = descr |
| 65 |
# For telemetry |
| 66 |
self.bucket = bucket |
| 67 |
self.children = children |
| 68 |
|
| 69 |
|
| 70 |
AllPhaseKinds = [] |
| 71 |
PhaseKindsByName = dict() |
| 72 |
|
| 73 |
|
| 74 |
def addPhaseKind(name, descr, bucket, children=[]): |
| 75 |
assert name not in PhaseKindsByName |
| 76 |
phaseKind = PhaseKind(name, descr, bucket, children) |
| 77 |
AllPhaseKinds.append(phaseKind) |
| 78 |
PhaseKindsByName[name] = phaseKind |
| 79 |
return phaseKind |
| 80 |
|
| 81 |
|
| 82 |
def getPhaseKind(name): |
| 83 |
return PhaseKindsByName[name] |
| 84 |
|
| 85 |
|
| 86 |
PhaseKindGraphRoots = [ |
| 87 |
addPhaseKind("MUTATOR", "Mutator Running", 0), |
| 88 |
addPhaseKind("GC_BEGIN", "Begin Callback", 1), |
| 89 |
addPhaseKind( |
| 90 |
"EVICT_NURSERY_FOR_MAJOR_GC", |
| 91 |
"Evict Nursery For Major GC", |
| 92 |
70, |
| 93 |
[ |
| 94 |
addPhaseKind( |
| 95 |
"MARK_ROOTS", |
| 96 |
"Mark Roots", |
| 97 |
48, |
| 98 |
[ |
| 99 |
addPhaseKind("MARK_CCWS", "Mark Cross Compartment Wrappers", 50), |
| 100 |
addPhaseKind("MARK_STACK", "Mark C and JS stacks", 51), |
| 101 |
addPhaseKind("MARK_RUNTIME_DATA", "Mark Runtime-wide Data", 52), |
| 102 |
addPhaseKind("MARK_EMBEDDING", "Mark Embedding", 53), |
| 103 |
], |
| 104 |
) |
| 105 |
], |
| 106 |
), |
| 107 |
addPhaseKind("WAIT_BACKGROUND_THREAD", "Wait Background Thread", 2), |
| 108 |
addPhaseKind( |
| 109 |
"PREPARE", |
| 110 |
"Prepare For Collection", |
| 111 |
69, |
| 112 |
[ |
| 113 |
addPhaseKind("UNMARK", "Unmark", 7), |
| 114 |
addPhaseKind("UNMARK_WEAKMAPS", "Unmark WeakMaps", 76), |
| 115 |
addPhaseKind("MARK_DISCARD_CODE", "Mark Discard Code", 3), |
| 116 |
addPhaseKind("RELAZIFY_FUNCTIONS", "Relazify Functions", 4), |
| 117 |
addPhaseKind("PURGE", "Purge", 5), |
| 118 |
addPhaseKind("PURGE_PROP_MAP_TABLES", "Purge PropMapTables", 60), |
| 119 |
addPhaseKind("PURGE_SOURCE_URLS", "Purge Source URLs", 73), |
| 120 |
addPhaseKind("JOIN_PARALLEL_TASKS", "Join Parallel Tasks", 67), |
| 121 |
], |
| 122 |
), |
| 123 |
addPhaseKind( |
| 124 |
"MARK", |
| 125 |
"Mark", |
| 126 |
6, |
| 127 |
[ |
| 128 |
getPhaseKind("MARK_ROOTS"), |
| 129 |
addPhaseKind("MARK_DELAYED", "Mark Delayed", 8), |
| 130 |
addPhaseKind( |
| 131 |
"MARK_WEAK", |
| 132 |
"Mark Weak", |
| 133 |
13, |
| 134 |
[ |
| 135 |
getPhaseKind("MARK_DELAYED"), |
| 136 |
addPhaseKind("MARK_GRAY_WEAK", "Mark Gray and Weak", 16), |
| 137 |
], |
| 138 |
), |
| 139 |
addPhaseKind("MARK_INCOMING_GRAY", "Mark Incoming Gray Pointers", 14), |
| 140 |
addPhaseKind("MARK_GRAY", "Mark Gray", 15), |
| 141 |
addPhaseKind( |
| 142 |
"PARALLEL_MARK", |
| 143 |
"Parallel marking", |
| 144 |
78, |
| 145 |
[ |
| 146 |
getPhaseKind("JOIN_PARALLEL_TASKS"), |
| 147 |
# The following are only used for parallel phase times: |
| 148 |
addPhaseKind("PARALLEL_MARK_MARK", "Parallel marking work", 79), |
| 149 |
addPhaseKind("PARALLEL_MARK_WAIT", "Waiting for work", 80), |
| 150 |
addPhaseKind( |
| 151 |
"PARALLEL_MARK_OTHER", "Parallel marking overhead", 82 |
| 152 |
), |
| 153 |
], |
| 154 |
), |
| 155 |
], |
| 156 |
), |
| 157 |
addPhaseKind( |
| 158 |
"SWEEP", |
| 159 |
"Sweep", |
| 160 |
9, |
| 161 |
[ |
| 162 |
getPhaseKind("MARK"), |
| 163 |
addPhaseKind( |
| 164 |
"FINALIZE_START", |
| 165 |
"Finalize Start Callbacks", |
| 166 |
17, |
| 167 |
[ |
| 168 |
addPhaseKind("WEAK_ZONES_CALLBACK", "Per-Slice Weak Callback", 57), |
| 169 |
addPhaseKind( |
| 170 |
"WEAK_COMPARTMENT_CALLBACK", "Per-Compartment Weak Callback", 58 |
| 171 |
), |
| 172 |
], |
| 173 |
), |
| 174 |
addPhaseKind("UPDATE_ATOMS_BITMAP", "Sweep Atoms Bitmap", 68), |
| 175 |
addPhaseKind("SWEEP_ATOMS_TABLE", "Sweep Atoms Table", 18), |
| 176 |
addPhaseKind( |
| 177 |
"SWEEP_COMPARTMENTS", |
| 178 |
"Sweep Compartments", |
| 179 |
20, |
| 180 |
[ |
| 181 |
addPhaseKind("SWEEP_JIT_SCRIPTS", "Sweep JitScripts", 21), |
| 182 |
addPhaseKind("SWEEP_INNER_VIEWS", "Sweep Inner Views", 22), |
| 183 |
addPhaseKind( |
| 184 |
"SWEEP_CC_WRAPPER", "Sweep Cross Compartment Wrappers", 23 |
| 185 |
), |
| 186 |
addPhaseKind("SWEEP_BASE_SHAPE", "Sweep Base Shapes", 24), |
| 187 |
addPhaseKind("SWEEP_INITIAL_SHAPE", "Sweep Initial Shapes", 25), |
| 188 |
addPhaseKind("SWEEP_REGEXP", "Sweep Regexps", 28), |
| 189 |
addPhaseKind("SWEEP_COMPRESSION", "Sweep Compression Tasks", 62), |
| 190 |
addPhaseKind("SWEEP_WEAKMAPS", "Sweep WeakMaps", 63), |
| 191 |
addPhaseKind("SWEEP_UNIQUEIDS", "Sweep Unique IDs", 64), |
| 192 |
addPhaseKind("SWEEP_WEAK_POINTERS", "Sweep Weak Pointers", 81), |
| 193 |
addPhaseKind( |
| 194 |
"SWEEP_FINALIZATION_OBSERVERS", |
| 195 |
"Sweep FinalizationRegistries and WeakRefs", |
| 196 |
74, |
| 197 |
), |
| 198 |
addPhaseKind("SWEEP_JIT_DATA", "Sweep JIT Data", 65), |
| 199 |
addPhaseKind("SWEEP_WEAK_CACHES", "Sweep Weak Caches", 66), |
| 200 |
addPhaseKind("SWEEP_MISC", "Sweep Miscellaneous", 29), |
| 201 |
getPhaseKind("JOIN_PARALLEL_TASKS"), |
| 202 |
], |
| 203 |
), |
| 204 |
addPhaseKind("SWEEP_PROP_MAP", "Sweep PropMap Tree", 77), |
| 205 |
addPhaseKind("FINALIZE_END", "Finalize End Callback", 38), |
| 206 |
addPhaseKind("DESTROY", "Deallocate", 39), |
| 207 |
getPhaseKind("JOIN_PARALLEL_TASKS"), |
| 208 |
addPhaseKind("FIND_DEAD_COMPARTMENTS", "Find Dead Compartments", 54), |
| 209 |
], |
| 210 |
), |
| 211 |
addPhaseKind( |
| 212 |
"COMPACT", |
| 213 |
"Compact", |
| 214 |
40, |
| 215 |
[ |
| 216 |
addPhaseKind("COMPACT_MOVE", "Compact Move", 41), |
| 217 |
addPhaseKind( |
| 218 |
"COMPACT_UPDATE", |
| 219 |
"Compact Update", |
| 220 |
42, |
| 221 |
[ |
| 222 |
getPhaseKind("MARK_ROOTS"), |
| 223 |
addPhaseKind("COMPACT_UPDATE_CELLS", "Compact Update Cells", 43), |
| 224 |
getPhaseKind("JOIN_PARALLEL_TASKS"), |
| 225 |
], |
| 226 |
), |
| 227 |
], |
| 228 |
), |
| 229 |
addPhaseKind("DECOMMIT", "Decommit", 72), |
| 230 |
addPhaseKind("GC_END", "End Callback", 44), |
| 231 |
addPhaseKind( |
| 232 |
"MINOR_GC", |
| 233 |
"All Minor GCs", |
| 234 |
45, |
| 235 |
[ |
| 236 |
getPhaseKind("MARK_ROOTS"), |
| 237 |
], |
| 238 |
), |
| 239 |
addPhaseKind( |
| 240 |
"EVICT_NURSERY", |
| 241 |
"Minor GCs to Evict Nursery", |
| 242 |
46, |
| 243 |
[ |
| 244 |
getPhaseKind("MARK_ROOTS"), |
| 245 |
], |
| 246 |
), |
| 247 |
addPhaseKind( |
| 248 |
"TRACE_HEAP", |
| 249 |
"Trace Heap", |
| 250 |
47, |
| 251 |
[ |
| 252 |
getPhaseKind("MARK_ROOTS"), |
| 253 |
], |
| 254 |
), |
| 255 |
] |
| 256 |
|
| 257 |
|
| 258 |
class Phase: |
| 259 |
# Expand the DAG into a tree, duplicating phases which have more than |
| 260 |
# one parent. |
| 261 |
def __init__(self, phaseKind, parent): |
| 262 |
self.phaseKind = phaseKind |
| 263 |
self.parent = parent |
| 264 |
self.depth = parent.depth + 1 if parent else 0 |
| 265 |
self.children = [] |
| 266 |
self.nextSibling = None |
| 267 |
self.nextInPhaseKind = None |
| 268 |
|
| 269 |
self.path = re.sub(r"\W+", "_", phaseKind.name.lower()) |
| 270 |
if parent is not None: |
| 271 |
self.path = parent.path + "." + self.path |
| 272 |
|
| 273 |
|
| 274 |
def expandPhases(): |
| 275 |
phases = [] |
| 276 |
phasesForKind = collections.defaultdict(list) |
| 277 |
|
| 278 |
def traverse(phaseKind, parent): |
| 279 |
ep = Phase(phaseKind, parent) |
| 280 |
phases.append(ep) |
| 281 |
|
| 282 |
# Update list of expanded phases for this phase kind. |
| 283 |
if phasesForKind[phaseKind]: |
| 284 |
phasesForKind[phaseKind][-1].nextInPhaseKind = ep |
| 285 |
phasesForKind[phaseKind].append(ep) |
| 286 |
|
| 287 |
# Recurse over children. |
| 288 |
for child in phaseKind.children: |
| 289 |
child_ep = traverse(child, ep) |
| 290 |
if ep.children: |
| 291 |
ep.children[-1].nextSibling = child_ep |
| 292 |
ep.children.append(child_ep) |
| 293 |
return ep |
| 294 |
|
| 295 |
for phaseKind in PhaseKindGraphRoots: |
| 296 |
traverse(phaseKind, None) |
| 297 |
|
| 298 |
return phases, phasesForKind |
| 299 |
|
| 300 |
|
| 301 |
AllPhases, PhasesForPhaseKind = expandPhases() |
| 302 |
|
| 303 |
# Name phases based on phase kind name and index if there are multiple phases |
| 304 |
# corresponding to a single phase kind. |
| 305 |
|
| 306 |
for phaseKind in AllPhaseKinds: |
| 307 |
phases = PhasesForPhaseKind[phaseKind] |
| 308 |
if len(phases) == 1: |
| 309 |
phases[0].name = "%s" % phaseKind.name |
| 310 |
else: |
| 311 |
for index, phase in enumerate(phases): |
| 312 |
phase.name = "%s_%d" % (phaseKind.name, index + 1) |
| 313 |
|
| 314 |
# Find the maximum phase nesting. |
| 315 |
MaxPhaseNesting = max(phase.depth for phase in AllPhases) + 1 |
| 316 |
|
| 317 |
# And the maximum bucket number. |
| 318 |
MaxBucket = max(kind.bucket for kind in AllPhaseKinds) |
| 319 |
|
| 320 |
# Generate code. |
| 321 |
|
| 322 |
|
| 323 |
def writeList(out, items): |
| 324 |
if items: |
| 325 |
out.write(",\n".join(" " + item for item in items) + "\n") |
| 326 |
|
| 327 |
|
| 328 |
def writeEnumClass(out, name, type, items, extraItems): |
| 329 |
items = ["FIRST"] + list(items) + ["LIMIT"] + list(extraItems) |
| 330 |
items[1] += " = " + items[0] |
| 331 |
out.write("enum class %s : %s {\n" % (name, type)) |
| 332 |
writeList(out, items) |
| 333 |
out.write("};\n") |
| 334 |
|
| 335 |
|
| 336 |
def generateHeader(out): |
| 337 |
# |
| 338 |
# Generate PhaseKind enum. |
| 339 |
# |
| 340 |
phaseKindNames = map(lambda phaseKind: phaseKind.name, AllPhaseKinds) |
| 341 |
extraPhaseKinds = [ |
| 342 |
"NONE = LIMIT", |
| 343 |
"EXPLICIT_SUSPENSION = LIMIT", |
| 344 |
"IMPLICIT_SUSPENSION", |
| 345 |
] |
| 346 |
writeEnumClass(out, "PhaseKind", "uint8_t", phaseKindNames, extraPhaseKinds) |
| 347 |
out.write("\n") |
| 348 |
|
| 349 |
# |
| 350 |
# Generate Phase enum. |
| 351 |
# |
| 352 |
phaseNames = map(lambda phase: phase.name, AllPhases) |
| 353 |
extraPhases = ["NONE = LIMIT", "EXPLICIT_SUSPENSION = LIMIT", "IMPLICIT_SUSPENSION"] |
| 354 |
writeEnumClass(out, "Phase", "uint8_t", phaseNames, extraPhases) |
| 355 |
out.write("\n") |
| 356 |
|
| 357 |
# |
| 358 |
# Generate MAX_PHASE_NESTING constant. |
| 359 |
# |
| 360 |
out.write("static const size_t MAX_PHASE_NESTING = %d;\n" % MaxPhaseNesting) |
| 361 |
|
| 362 |
|
| 363 |
def generateCpp(out): |
| 364 |
# |
| 365 |
# Generate the PhaseKindInfo table. |
| 366 |
# |
| 367 |
out.write("static constexpr PhaseKindTable phaseKinds = {\n") |
| 368 |
for phaseKind in AllPhaseKinds: |
| 369 |
phase = PhasesForPhaseKind[phaseKind][0] |
| 370 |
out.write( |
| 371 |
' /* PhaseKind::%s */ PhaseKindInfo { Phase::%s, %d, "%s" },\n' |
| 372 |
% (phaseKind.name, phase.name, phaseKind.bucket, phaseKind.name) |
| 373 |
) |
| 374 |
out.write("};\n") |
| 375 |
out.write("\n") |
| 376 |
|
| 377 |
# |
| 378 |
# Generate the PhaseInfo tree. |
| 379 |
# |
| 380 |
def name(phase): |
| 381 |
return "Phase::" + phase.name if phase else "Phase::NONE" |
| 382 |
|
| 383 |
out.write("static constexpr PhaseTable phases = {\n") |
| 384 |
for phase in AllPhases: |
| 385 |
firstChild = phase.children[0] if phase.children else None |
| 386 |
phaseKind = phase.phaseKind |
| 387 |
out.write( |
| 388 |
' /* %s */ PhaseInfo { %s, %s, %s, %s, PhaseKind::%s, %d, "%s", "%s" },\n' |
| 389 |
% ( # NOQA: E501 |
| 390 |
name(phase), |
| 391 |
name(phase.parent), |
| 392 |
name(firstChild), |
| 393 |
name(phase.nextSibling), |
| 394 |
name(phase.nextInPhaseKind), |
| 395 |
phaseKind.name, |
| 396 |
phase.depth, |
| 397 |
phaseKind.descr, |
| 398 |
phase.path, |
| 399 |
) |
| 400 |
) |
| 401 |
out.write("};\n") |
| 402 |
|
| 403 |
# |
| 404 |
# Print in a comment the next available phase kind number. |
| 405 |
# |
| 406 |
out.write("// The next available phase kind number is: %d\n" % (MaxBucket + 1)) |