1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "gc/Statistics.h"
9 #include "mozilla/DebugOnly.h"
10 #include "mozilla/Sprintf.h"
11 #include "mozilla/TimeStamp.h"
16 #include <type_traits>
19 #include "gc/GCInternals.h"
20 #include "gc/Memory.h"
21 #include "js/Printer.h"
22 #include "util/GetPidProvider.h"
23 #include "util/Text.h"
24 #include "vm/JSONPrinter.h"
25 #include "vm/Runtime.h"
28 #include "gc/PrivateIterators-inl.h"
31 using namespace js::gc
;
32 using namespace js::gcstats
;
34 using mozilla::DebugOnly
;
35 using mozilla::EnumeratedArray
;
37 using mozilla::TimeDuration
;
38 using mozilla::TimeStamp
;
40 static const size_t BYTES_PER_MB
= 1024 * 1024;
43 * If this fails, then you can either delete this assertion and allow all
44 * larger-numbered reasons to pile up in the last telemetry bucket, or switch
45 * to GC_REASON_3 and bump the max value.
47 static_assert(JS::GCReason::NUM_TELEMETRY_REASONS
>= JS::GCReason::NUM_REASONS
);
49 static inline auto AllPhaseKinds() {
50 return mozilla::MakeEnumeratedRange(PhaseKind::FIRST
, PhaseKind::LIMIT
);
53 static inline auto MajorGCPhaseKinds() {
54 return mozilla::MakeEnumeratedRange(PhaseKind::GC_BEGIN
,
55 PhaseKind(size_t(PhaseKind::GC_END
) + 1));
58 static const char* ExplainGCOptions(JS::GCOptions options
) {
60 case JS::GCOptions::Normal
:
62 case JS::GCOptions::Shrink
:
64 case JS::GCOptions::Shutdown
:
68 MOZ_CRASH("Unexpected GCOptions value");
71 JS_PUBLIC_API
const char* JS::ExplainGCReason(JS::GCReason reason
) {
73 #define SWITCH_REASON(name, _) \
74 case JS::GCReason::name: \
76 GCREASONS(SWITCH_REASON
)
79 case JS::GCReason::NO_REASON
:
83 MOZ_CRASH("bad GC reason");
87 JS_PUBLIC_API
bool JS::InternalGCReason(JS::GCReason reason
) {
88 return reason
< JS::GCReason::FIRST_FIREFOX_REASON
;
91 const char* js::gcstats::ExplainAbortReason(GCAbortReason reason
) {
93 #define SWITCH_REASON(name, _) \
94 case GCAbortReason::name: \
96 GC_ABORT_REASONS(SWITCH_REASON
)
99 MOZ_CRASH("bad GC abort reason");
104 static FILE* MaybeOpenFileFromEnv(const char* env
,
105 FILE* defaultFile
= nullptr) {
106 const char* value
= getenv(env
);
112 if (strcmp(value
, "none") == 0) {
114 } else if (strcmp(value
, "stdout") == 0) {
116 } else if (strcmp(value
, "stderr") == 0) {
120 if (value
[0] != '/') {
121 const char* dir
= getenv("MOZ_UPLOAD_DIR");
123 SprintfLiteral(path
, "%s/%s", dir
, value
);
128 file
= fopen(value
, "a");
129 if (!file
|| setvbuf(file
, nullptr, _IOLBF
, 256) != 0) {
130 perror("Error opening log file");
131 MOZ_CRASH("Failed to open log file.");
138 struct PhaseKindInfo
{
140 uint8_t telemetryBucket
;
144 // PhaseInfo objects form a tree.
149 Phase nextWithPhaseKind
;
156 // A table of PhaseInfo indexed by Phase.
157 using PhaseTable
= EnumeratedArray
<Phase
, Phase::LIMIT
, PhaseInfo
>;
159 // A table of PhaseKindInfo indexed by PhaseKind.
160 using PhaseKindTable
=
161 EnumeratedArray
<PhaseKind
, PhaseKind::LIMIT
, PhaseKindInfo
>;
163 #include "gc/StatsPhasesGenerated.inc"
165 // Iterate the phases in a phase kind.
170 explicit PhaseIter(PhaseKind kind
) : phase(phaseKinds
[kind
].firstPhase
) {}
171 bool done() const { return phase
== Phase::NONE
; }
172 void next() { phase
= phases
[phase
].nextWithPhaseKind
; }
173 Phase
get() const { return phase
; }
174 operator Phase() const { return phase
; }
177 static double t(TimeDuration duration
) { return duration
.ToMilliseconds(); }
179 static TimeDuration
TimeBetween(TimeStamp start
, TimeStamp end
) {
181 MOZ_ASSERT(end
>= start
);
183 // Sadly this happens sometimes.
185 return TimeDuration::Zero();
191 inline JSContext
* Statistics::context() {
192 return gc
->rt
->mainContextFromOwnThread();
195 inline Phase
Statistics::currentPhase() const {
196 return phaseStack
.empty() ? Phase::NONE
: phaseStack
.back();
199 PhaseKind
Statistics::currentPhaseKind() const {
200 // Public API to get the current phase kind, suppressing the synthetic
201 // PhaseKind::MUTATOR phase.
203 Phase phase
= currentPhase();
204 MOZ_ASSERT_IF(phase
== Phase::MUTATOR
, phaseStack
.length() == 1);
205 if (phase
== Phase::NONE
|| phase
== Phase::MUTATOR
) {
206 return PhaseKind::NONE
;
209 return phases
[phase
].phaseKind
;
212 static Phase
LookupPhaseWithParent(PhaseKind phaseKind
, Phase parentPhase
) {
213 for (PhaseIter
phase(phaseKind
); !phase
.done(); phase
.next()) {
214 if (phases
[phase
].parent
== parentPhase
) {
222 static const char* PhaseKindName(PhaseKind kind
) {
223 if (kind
== PhaseKind::NONE
) {
227 return phaseKinds
[kind
].name
;
230 Phase
Statistics::lookupChildPhase(PhaseKind phaseKind
) const {
231 if (phaseKind
== PhaseKind::IMPLICIT_SUSPENSION
) {
232 return Phase::IMPLICIT_SUSPENSION
;
234 if (phaseKind
== PhaseKind::EXPLICIT_SUSPENSION
) {
235 return Phase::EXPLICIT_SUSPENSION
;
238 MOZ_ASSERT(phaseKind
< PhaseKind::LIMIT
);
240 // Search all expanded phases that correspond to the required
241 // phase to find the one whose parent is the current expanded phase.
242 Phase phase
= LookupPhaseWithParent(phaseKind
, currentPhase());
244 if (phase
== Phase::NONE
) {
245 MOZ_CRASH_UNSAFE_PRINTF(
246 "Child phase kind %s not found under current phase kind %s",
247 PhaseKindName(phaseKind
), PhaseKindName(currentPhaseKind()));
253 inline auto AllPhases() {
254 return mozilla::MakeEnumeratedRange(Phase::FIRST
, Phase::LIMIT
);
257 void Statistics::gcDuration(TimeDuration
* total
, TimeDuration
* maxPause
) const {
258 *total
= *maxPause
= TimeDuration::Zero();
259 for (const auto& slice
: slices_
) {
260 *total
+= slice
.duration();
261 if (slice
.duration() > *maxPause
) {
262 *maxPause
= slice
.duration();
265 if (*maxPause
> maxPauseInInterval
) {
266 maxPauseInInterval
= *maxPause
;
270 void Statistics::sccDurations(TimeDuration
* total
,
271 TimeDuration
* maxPause
) const {
272 *total
= *maxPause
= TimeDuration::Zero();
273 for (const auto& duration
: sccTimes
) {
275 *maxPause
= std::max(*maxPause
, duration
);
279 using FragmentVector
= Vector
<UniqueChars
, 8, SystemAllocPolicy
>;
281 static UniqueChars
Join(const FragmentVector
& fragments
,
282 const char* separator
= "") {
283 const size_t separatorLength
= strlen(separator
);
285 for (size_t i
= 0; i
< fragments
.length(); ++i
) {
286 length
+= fragments
[i
] ? strlen(fragments
[i
].get()) : 0;
287 if (i
< (fragments
.length() - 1)) {
288 length
+= separatorLength
;
292 char* joined
= js_pod_malloc
<char>(length
+ 1);
294 return UniqueChars();
297 joined
[length
] = '\0';
298 char* cursor
= joined
;
299 for (size_t i
= 0; i
< fragments
.length(); ++i
) {
301 strcpy(cursor
, fragments
[i
].get());
303 cursor
+= fragments
[i
] ? strlen(fragments
[i
].get()) : 0;
304 if (i
< (fragments
.length() - 1)) {
305 if (separatorLength
) {
306 strcpy(cursor
, separator
);
308 cursor
+= separatorLength
;
312 return UniqueChars(joined
);
315 static TimeDuration
SumChildTimes(Phase phase
,
316 const Statistics::PhaseTimes
& phaseTimes
) {
318 for (phase
= phases
[phase
].firstChild
; phase
!= Phase::NONE
;
319 phase
= phases
[phase
].nextSibling
) {
320 total
+= phaseTimes
[phase
];
325 UniqueChars
Statistics::formatCompactSliceMessage() const {
326 // Skip if we OOM'ed.
327 if (slices_
.length() == 0) {
328 return UniqueChars(nullptr);
331 const size_t index
= slices_
.length() - 1;
332 const SliceData
& slice
= slices_
.back();
334 char budgetDescription
[200];
335 slice
.budget
.describe(budgetDescription
, sizeof(budgetDescription
) - 1);
338 "GC Slice %u - Pause: %.3fms of %s budget (@ %.3fms); Reason: %s; Reset: "
341 SprintfLiteral(buffer
, format
, index
, t(slice
.duration()), budgetDescription
,
342 t(slice
.start
- slices_
[0].start
),
343 ExplainGCReason(slice
.reason
),
344 slice
.wasReset() ? "yes - " : "no",
345 slice
.wasReset() ? ExplainAbortReason(slice
.resetReason
) : "");
347 FragmentVector fragments
;
348 if (!fragments
.append(DuplicateString(buffer
)) ||
350 formatCompactSlicePhaseTimes(slices_
[index
].phaseTimes
))) {
351 return UniqueChars(nullptr);
353 return Join(fragments
);
356 UniqueChars
Statistics::formatCompactSummaryMessage() const {
357 FragmentVector fragments
;
358 if (!fragments
.append(DuplicateString("Summary - "))) {
359 return UniqueChars(nullptr);
362 TimeDuration total
, longest
;
363 gcDuration(&total
, &longest
);
365 const double mmu20
= computeMMU(TimeDuration::FromMilliseconds(20));
366 const double mmu50
= computeMMU(TimeDuration::FromMilliseconds(50));
369 if (!nonincremental()) {
370 SprintfLiteral(buffer
,
371 "Max Pause: %.3fms; MMU 20ms: %.1f%%; MMU 50ms: %.1f%%; "
373 t(longest
), mmu20
* 100., mmu50
* 100., t(total
));
375 SprintfLiteral(buffer
, "Non-Incremental: %.3fms (%s); ", t(total
),
376 ExplainAbortReason(nonincrementalReason_
));
378 if (!fragments
.append(DuplicateString(buffer
))) {
379 return UniqueChars(nullptr);
382 SprintfLiteral(buffer
,
383 "Zones: %zu of %zu (-%zu); Compartments: %zu of %zu (-%zu); "
384 "HeapSize: %.3f MiB; "
385 "HeapChange (abs): %+d (%u); ",
386 zoneStats
.collectedZoneCount
, zoneStats
.zoneCount
,
387 zoneStats
.sweptZoneCount
, zoneStats
.collectedCompartmentCount
,
388 zoneStats
.compartmentCount
, zoneStats
.sweptCompartmentCount
,
389 double(preTotalHeapBytes
) / BYTES_PER_MB
,
390 int32_t(counts
[COUNT_NEW_CHUNK
] - counts
[COUNT_DESTROY_CHUNK
]),
391 counts
[COUNT_NEW_CHUNK
] + counts
[COUNT_DESTROY_CHUNK
]);
392 if (!fragments
.append(DuplicateString(buffer
))) {
393 return UniqueChars(nullptr);
396 MOZ_ASSERT_IF(counts
[COUNT_ARENA_RELOCATED
],
397 gcOptions
== JS::GCOptions::Shrink
);
398 if (gcOptions
== JS::GCOptions::Shrink
) {
400 buffer
, "Kind: %s; Relocated: %.3f MiB; ", ExplainGCOptions(gcOptions
),
401 double(ArenaSize
* counts
[COUNT_ARENA_RELOCATED
]) / BYTES_PER_MB
);
402 if (!fragments
.append(DuplicateString(buffer
))) {
403 return UniqueChars(nullptr);
407 return Join(fragments
);
410 UniqueChars
Statistics::formatCompactSlicePhaseTimes(
411 const PhaseTimes
& phaseTimes
) const {
412 static const TimeDuration MaxUnaccountedTime
=
413 TimeDuration::FromMicroseconds(100);
415 FragmentVector fragments
;
417 for (auto phase
: AllPhases()) {
418 DebugOnly
<uint8_t> level
= phases
[phase
].depth
;
419 MOZ_ASSERT(level
< 4);
421 TimeDuration ownTime
= phaseTimes
[phase
];
422 TimeDuration childTime
= SumChildTimes(phase
, phaseTimes
);
423 if (ownTime
> MaxUnaccountedTime
) {
424 SprintfLiteral(buffer
, "%s: %.3fms", phases
[phase
].name
, t(ownTime
));
425 if (!fragments
.append(DuplicateString(buffer
))) {
426 return UniqueChars(nullptr);
429 if (childTime
&& (ownTime
- childTime
) > MaxUnaccountedTime
) {
430 MOZ_ASSERT(level
< 3);
431 SprintfLiteral(buffer
, "%s: %.3fms", "Other", t(ownTime
- childTime
));
432 if (!fragments
.append(DuplicateString(buffer
))) {
433 return UniqueChars(nullptr);
438 return Join(fragments
, ", ");
441 UniqueChars
Statistics::formatDetailedMessage() const {
442 FragmentVector fragments
;
444 if (!fragments
.append(formatDetailedDescription())) {
445 return UniqueChars(nullptr);
448 if (!slices_
.empty()) {
449 for (unsigned i
= 0; i
< slices_
.length(); i
++) {
450 if (!fragments
.append(formatDetailedSliceDescription(i
, slices_
[i
]))) {
451 return UniqueChars(nullptr);
453 if (!fragments
.append(formatDetailedPhaseTimes(slices_
[i
].phaseTimes
))) {
454 return UniqueChars(nullptr);
458 if (!fragments
.append(formatDetailedTotals())) {
459 return UniqueChars(nullptr);
461 if (!fragments
.append(formatDetailedPhaseTimes(phaseTimes
))) {
462 return UniqueChars(nullptr);
465 return Join(fragments
);
468 UniqueChars
Statistics::formatDetailedDescription() const {
469 TimeDuration sccTotal
, sccLongest
;
470 sccDurations(&sccTotal
, &sccLongest
);
472 const double mmu20
= computeMMU(TimeDuration::FromMilliseconds(20));
473 const double mmu50
= computeMMU(TimeDuration::FromMilliseconds(50));
476 "=================================================================\n\
477 Invocation Kind: %s\n\
480 Zones Collected: %d of %d (-%d)\n\
481 Compartments Collected: %d of %d (-%d)\n\
482 MinorGCs since last GC: %d\n\
483 Store Buffer Overflows: %d\n\
484 MMU 20ms:%.1f%%; 50ms:%.1f%%\n\
485 SCC Sweep Total (MaxPause): %.3fms (%.3fms)\n\
486 HeapSize: %.3f MiB\n\
487 Chunk Delta (magnitude): %+d (%d)\n\
488 Arenas Relocated: %.3f MiB\n\
493 buffer
, format
, ExplainGCOptions(gcOptions
),
494 ExplainGCReason(slices_
[0].reason
), nonincremental() ? "no - " : "yes",
495 nonincremental() ? ExplainAbortReason(nonincrementalReason_
) : "",
496 zoneStats
.collectedZoneCount
, zoneStats
.zoneCount
,
497 zoneStats
.sweptZoneCount
, zoneStats
.collectedCompartmentCount
,
498 zoneStats
.compartmentCount
, zoneStats
.sweptCompartmentCount
,
499 getCount(COUNT_MINOR_GC
), getCount(COUNT_STOREBUFFER_OVERFLOW
),
500 mmu20
* 100., mmu50
* 100., t(sccTotal
), t(sccLongest
),
501 double(preTotalHeapBytes
) / BYTES_PER_MB
,
502 getCount(COUNT_NEW_CHUNK
) - getCount(COUNT_DESTROY_CHUNK
),
503 getCount(COUNT_NEW_CHUNK
) + getCount(COUNT_DESTROY_CHUNK
),
504 double(ArenaSize
* getCount(COUNT_ARENA_RELOCATED
)) / BYTES_PER_MB
);
506 return DuplicateString(buffer
);
509 UniqueChars
Statistics::formatDetailedSliceDescription(
510 unsigned i
, const SliceData
& slice
) const {
511 char budgetDescription
[200];
512 slice
.budget
.describe(budgetDescription
, sizeof(budgetDescription
) - 1);
516 ---- Slice %u ----\n\
521 Page Faults: %" PRIu64
523 Pause: %.3fms of %s budget (@ %.3fms)\n\
526 char triggerBuffer
[100] = "n/a";
528 Trigger trigger
= slice
.trigger
.value();
529 SprintfLiteral(triggerBuffer
, "%.3f MiB of %.3f MiB threshold\n",
530 double(trigger
.amount
) / BYTES_PER_MB
,
531 double(trigger
.threshold
) / BYTES_PER_MB
);
536 buffer
, format
, i
, ExplainGCReason(slice
.reason
), triggerBuffer
,
537 slice
.wasReset() ? "yes - " : "no",
538 slice
.wasReset() ? ExplainAbortReason(slice
.resetReason
) : "",
539 gc::StateName(slice
.initialState
), gc::StateName(slice
.finalState
),
540 uint64_t(slice
.endFaults
- slice
.startFaults
), t(slice
.duration()),
541 budgetDescription
, t(slice
.start
- slices_
[0].start
));
542 return DuplicateString(buffer
);
545 static bool IncludePhase(TimeDuration duration
) {
546 // Don't include durations that will print as "0.000ms".
547 return duration
.ToMilliseconds() >= 0.001;
550 UniqueChars
Statistics::formatDetailedPhaseTimes(
551 const PhaseTimes
& phaseTimes
) const {
552 static const TimeDuration MaxUnaccountedChildTime
=
553 TimeDuration::FromMicroseconds(50);
555 FragmentVector fragments
;
557 for (auto phase
: AllPhases()) {
558 uint8_t level
= phases
[phase
].depth
;
559 TimeDuration ownTime
= phaseTimes
[phase
];
560 TimeDuration childTime
= SumChildTimes(phase
, phaseTimes
);
561 if (IncludePhase(ownTime
)) {
562 SprintfLiteral(buffer
, " %*s%s: %.3fms\n", level
* 2, "",
563 phases
[phase
].name
, t(ownTime
));
564 if (!fragments
.append(DuplicateString(buffer
))) {
565 return UniqueChars(nullptr);
568 if (childTime
&& (ownTime
- childTime
) > MaxUnaccountedChildTime
) {
569 SprintfLiteral(buffer
, " %*s%s: %.3fms\n", (level
+ 1) * 2, "",
570 "Other", t(ownTime
- childTime
));
571 if (!fragments
.append(DuplicateString(buffer
))) {
572 return UniqueChars(nullptr);
577 return Join(fragments
);
580 UniqueChars
Statistics::formatDetailedTotals() const {
581 TimeDuration total
, longest
;
582 gcDuration(&total
, &longest
);
587 Total Time: %.3fms\n\
591 SprintfLiteral(buffer
, format
, t(total
), t(longest
));
592 return DuplicateString(buffer
);
595 void Statistics::formatJsonSlice(size_t sliceNum
, JSONPrinter
& json
) const {
597 * We number each of the slice properties to keep the code in
598 * GCTelemetry.jsm in sync. See MAX_SLICE_KEYS.
601 formatJsonSliceDescription(sliceNum
, slices_
[sliceNum
], json
); // # 1-11
603 json
.beginObjectProperty("times"); // # 12
604 formatJsonPhaseTimes(slices_
[sliceNum
].phaseTimes
, json
);
610 UniqueChars
Statistics::renderJsonSlice(size_t sliceNum
) const {
611 Sprinter
printer(nullptr, false);
612 if (!printer
.init()) {
613 return UniqueChars(nullptr);
615 JSONPrinter
json(printer
, false);
617 formatJsonSlice(sliceNum
, json
);
618 return printer
.release();
621 UniqueChars
Statistics::renderNurseryJson() const {
622 Sprinter
printer(nullptr, false);
623 if (!printer
.init()) {
624 return UniqueChars(nullptr);
626 JSONPrinter
json(printer
, false);
627 gc
->nursery().renderProfileJSON(json
);
628 return printer
.release();
632 void Statistics::log(const char* fmt
, ...) {
636 TimeDuration sinceStart
=
637 TimeBetween(TimeStamp::FirstTimeStamp(), TimeStamp::Now());
638 fprintf(gcDebugFile
, "%12.3f: ", sinceStart
.ToMicroseconds());
639 vfprintf(gcDebugFile
, fmt
, args
);
640 fprintf(gcDebugFile
, "\n");
647 UniqueChars
Statistics::renderJsonMessage() const {
649 * The format of the JSON message is specified by the GCMajorMarkerPayload
650 * type in profiler.firefox.com
651 * https://github.com/firefox-devtools/profiler/blob/master/src/types/markers.js#L62
653 * All the properties listed here are created within the timings property
654 * of the GCMajor marker.
657 return DuplicateString("{status:\"aborted\"}"); // May return nullptr
660 Sprinter
printer(nullptr, false);
661 if (!printer
.init()) {
662 return UniqueChars(nullptr);
664 JSONPrinter
json(printer
, false);
667 json
.property("status", "completed");
668 formatJsonDescription(json
);
670 json
.beginObjectProperty("totals");
671 formatJsonPhaseTimes(phaseTimes
, json
);
676 return printer
.release();
679 void Statistics::formatJsonDescription(JSONPrinter
& json
) const {
680 // If you change JSON properties here, please update:
682 // https://github.com/firefox-devtools/profiler
684 TimeDuration total
, longest
;
685 gcDuration(&total
, &longest
);
686 json
.property("max_pause", longest
, JSONPrinter::MILLISECONDS
);
687 json
.property("total_time", total
, JSONPrinter::MILLISECONDS
);
688 // We might be able to omit reason if profiler.firefox.com was able to retrive
689 // it from the first slice. But it doesn't do this yet.
690 json
.property("reason", ExplainGCReason(slices_
[0].reason
));
691 json
.property("zones_collected", zoneStats
.collectedZoneCount
);
692 json
.property("total_zones", zoneStats
.zoneCount
);
693 json
.property("total_compartments", zoneStats
.compartmentCount
);
694 json
.property("minor_gcs", getCount(COUNT_MINOR_GC
));
695 json
.property("minor_gc_number", gc
->minorGCCount());
696 json
.property("major_gc_number", gc
->majorGCCount());
697 uint32_t storebufferOverflows
= getCount(COUNT_STOREBUFFER_OVERFLOW
);
698 if (storebufferOverflows
) {
699 json
.property("store_buffer_overflows", storebufferOverflows
);
701 json
.property("slices", slices_
.length());
703 const double mmu20
= computeMMU(TimeDuration::FromMilliseconds(20));
704 const double mmu50
= computeMMU(TimeDuration::FromMilliseconds(50));
705 json
.property("mmu_20ms", int(mmu20
* 100));
706 json
.property("mmu_50ms", int(mmu50
* 100));
708 TimeDuration sccTotal
, sccLongest
;
709 sccDurations(&sccTotal
, &sccLongest
);
710 json
.property("scc_sweep_total", sccTotal
, JSONPrinter::MILLISECONDS
);
711 json
.property("scc_sweep_max_pause", sccLongest
, JSONPrinter::MILLISECONDS
);
713 if (nonincrementalReason_
!= GCAbortReason::None
) {
714 json
.property("nonincremental_reason",
715 ExplainAbortReason(nonincrementalReason_
));
717 json
.property("allocated_bytes", preTotalHeapBytes
);
718 json
.property("post_heap_size", postTotalHeapBytes
);
720 uint32_t addedChunks
= getCount(COUNT_NEW_CHUNK
);
722 json
.property("added_chunks", addedChunks
);
724 uint32_t removedChunks
= getCount(COUNT_DESTROY_CHUNK
);
726 json
.property("removed_chunks", removedChunks
);
728 json
.property("major_gc_number", startingMajorGCNumber
);
729 json
.property("minor_gc_number", startingMinorGCNumber
);
730 json
.property("slice_number", startingSliceNumber
);
733 void Statistics::formatJsonSliceDescription(unsigned i
, const SliceData
& slice
,
734 JSONPrinter
& json
) const {
735 // If you change JSON properties here, please update:
737 // https://github.com/firefox-devtools/profiler
739 char budgetDescription
[200];
740 slice
.budget
.describe(budgetDescription
, sizeof(budgetDescription
) - 1);
741 TimeStamp originTime
= TimeStamp::ProcessCreation();
743 json
.property("slice", i
);
744 json
.property("pause", slice
.duration(), JSONPrinter::MILLISECONDS
);
745 json
.property("reason", ExplainGCReason(slice
.reason
));
746 json
.property("initial_state", gc::StateName(slice
.initialState
));
747 json
.property("final_state", gc::StateName(slice
.finalState
));
748 json
.property("budget", budgetDescription
);
749 json
.property("major_gc_number", startingMajorGCNumber
);
751 Trigger trigger
= slice
.trigger
.value();
752 json
.property("trigger_amount", trigger
.amount
);
753 json
.property("trigger_threshold", trigger
.threshold
);
755 MOZ_ASSERT(slice
.endFaults
>= slice
.startFaults
);
756 size_t numFaults
= slice
.endFaults
- slice
.startFaults
;
757 if (numFaults
!= 0) {
758 json
.property("page_faults", numFaults
);
760 json
.property("start_timestamp", TimeBetween(originTime
, slice
.start
),
761 JSONPrinter::SECONDS
);
764 void Statistics::formatJsonPhaseTimes(const PhaseTimes
& phaseTimes
,
765 JSONPrinter
& json
) const {
766 for (auto phase
: AllPhases()) {
767 TimeDuration ownTime
= phaseTimes
[phase
];
768 if (!ownTime
.IsZero()) {
769 json
.property(phases
[phase
].path
, ownTime
, JSONPrinter::MILLISECONDS
);
774 Statistics::Statistics(GCRuntime
* gc
)
776 gcTimerFile(nullptr),
777 gcDebugFile(nullptr),
778 nonincrementalReason_(GCAbortReason::None
),
779 creationTime_(TimeStamp::Now()),
780 tenuredAllocsSinceMinorGC(0),
781 preTotalHeapBytes(0),
782 postTotalHeapBytes(0),
783 preCollectedHeapBytes(0),
784 startingMinorGCNumber(0),
785 startingMajorGCNumber(0),
786 startingSliceNumber(0),
787 sliceCallback(nullptr),
788 nurseryCollectionCallback(nullptr),
790 enableProfiling_(false),
792 for (auto& count
: counts
) {
796 for (auto& stat
: stats
) {
801 for (const auto& duration
: totalTimes_
) {
802 using ElementType
= std::remove_reference_t
<decltype(duration
)>;
803 static_assert(!std::is_trivially_constructible_v
<ElementType
>,
804 "Statistics::Statistics will only initialize "
805 "totalTimes_'s elements if their default constructor is "
807 MOZ_ASSERT(duration
.IsZero(),
808 "totalTimes_ default-initialization should have "
809 "default-initialized every element of totalTimes_ to zero");
813 MOZ_ALWAYS_TRUE(phaseStack
.reserve(MAX_PHASE_NESTING
));
814 MOZ_ALWAYS_TRUE(suspendedPhases
.reserve(MAX_SUSPENDED_PHASES
));
816 gcTimerFile
= MaybeOpenFileFromEnv("MOZ_GCTIMER");
817 gcDebugFile
= MaybeOpenFileFromEnv("JS_GC_DEBUG");
818 gcProfileFile
= MaybeOpenFileFromEnv("JS_GC_PROFILE_FILE", stderr
);
820 gc::ReadProfileEnv("JS_GC_PROFILE",
821 "Report major GCs taking more than N milliseconds for "
822 "all or just the main runtime\n",
823 &enableProfiling_
, &profileWorkers_
, &profileThreshold_
);
826 Statistics::~Statistics() {
827 if (gcTimerFile
&& gcTimerFile
!= stdout
&& gcTimerFile
!= stderr
) {
830 if (gcDebugFile
&& gcDebugFile
!= stdout
&& gcDebugFile
!= stderr
) {
836 bool Statistics::initialize() {
838 // Sanity check generated tables.
839 for (auto i
: AllPhases()) {
840 auto parent
= phases
[i
].parent
;
841 if (parent
!= Phase::NONE
) {
842 MOZ_ASSERT(phases
[i
].depth
== phases
[parent
].depth
+ 1);
844 auto firstChild
= phases
[i
].firstChild
;
845 if (firstChild
!= Phase::NONE
) {
846 MOZ_ASSERT(i
== phases
[firstChild
].parent
);
847 MOZ_ASSERT(phases
[i
].depth
== phases
[firstChild
].depth
- 1);
849 auto nextSibling
= phases
[i
].nextSibling
;
850 if (nextSibling
!= Phase::NONE
) {
851 MOZ_ASSERT(parent
== phases
[nextSibling
].parent
);
852 MOZ_ASSERT(phases
[i
].depth
== phases
[nextSibling
].depth
);
854 auto nextWithPhaseKind
= phases
[i
].nextWithPhaseKind
;
855 if (nextWithPhaseKind
!= Phase::NONE
) {
856 MOZ_ASSERT(phases
[i
].phaseKind
== phases
[nextWithPhaseKind
].phaseKind
);
857 MOZ_ASSERT(parent
!= phases
[nextWithPhaseKind
].parent
);
860 for (auto i
: AllPhaseKinds()) {
861 MOZ_ASSERT(phases
[phaseKinds
[i
].firstPhase
].phaseKind
== i
);
862 for (auto j
: AllPhaseKinds()) {
863 MOZ_ASSERT_IF(i
!= j
, phaseKinds
[i
].telemetryBucket
!=
864 phaseKinds
[j
].telemetryBucket
);
872 JS::GCSliceCallback
Statistics::setSliceCallback(
873 JS::GCSliceCallback newCallback
) {
874 JS::GCSliceCallback oldCallback
= sliceCallback
;
875 sliceCallback
= newCallback
;
879 JS::GCNurseryCollectionCallback
Statistics::setNurseryCollectionCallback(
880 JS::GCNurseryCollectionCallback newCallback
) {
881 auto oldCallback
= nurseryCollectionCallback
;
882 nurseryCollectionCallback
= newCallback
;
886 TimeDuration
Statistics::clearMaxGCPauseAccumulator() {
887 TimeDuration prior
= maxPauseInInterval
;
888 maxPauseInInterval
= TimeDuration::Zero();
892 TimeDuration
Statistics::getMaxGCPauseSinceClear() {
893 return maxPauseInInterval
;
896 // Sum up the time for a phase, including instances of the phase with different
898 static TimeDuration
SumPhase(PhaseKind phaseKind
,
899 const Statistics::PhaseTimes
& times
) {
901 for (PhaseIter
phase(phaseKind
); !phase
.done(); phase
.next()) {
907 static bool CheckSelfTime(Phase parent
, Phase child
,
908 const Statistics::PhaseTimes
& times
,
909 const Statistics::PhaseTimes
& selfTimes
,
910 TimeDuration childTime
) {
911 if (selfTimes
[parent
] < childTime
) {
914 "Parent %s time = %.3fms with %.3fms remaining, child %s time %.3fms\n",
915 phases
[parent
].name
, times
[parent
].ToMilliseconds(),
916 selfTimes
[parent
].ToMilliseconds(), phases
[child
].name
,
917 childTime
.ToMilliseconds());
925 static PhaseKind
FindLongestPhaseKind(const Statistics::PhaseKindTimes
& times
) {
926 TimeDuration longestTime
;
927 PhaseKind phaseKind
= PhaseKind::NONE
;
928 for (auto i
: MajorGCPhaseKinds()) {
929 if (times
[i
] > longestTime
) {
930 longestTime
= times
[i
];
938 static PhaseKind
LongestPhaseSelfTimeInMajorGC(
939 const Statistics::PhaseTimes
& times
) {
940 // Start with total times per expanded phase, including children's times.
941 Statistics::PhaseTimes
selfTimes(times
);
943 // We have the total time spent in each phase, including descendant times.
944 // Loop over the children and subtract their times from their parent's self
946 for (auto i
: AllPhases()) {
947 Phase parent
= phases
[i
].parent
;
948 if (parent
!= Phase::NONE
) {
949 bool ok
= CheckSelfTime(parent
, i
, times
, selfTimes
, times
[i
]);
951 // This happens very occasionally in release builds and frequently
952 // in Windows debug builds. Skip collecting longest phase telemetry
955 MOZ_ASSERT(ok
, "Inconsistent time data; see bug 1400153");
958 return PhaseKind::NONE
;
961 selfTimes
[parent
] -= times
[i
];
965 // Sum expanded phases corresponding to the same phase.
966 Statistics::PhaseKindTimes phaseKindTimes
;
967 for (auto i
: AllPhaseKinds()) {
968 phaseKindTimes
[i
] = SumPhase(i
, selfTimes
);
971 return FindLongestPhaseKind(phaseKindTimes
);
974 void Statistics::printStats() {
977 "OOM during GC statistics collection. The report is unavailable "
980 UniqueChars msg
= formatDetailedMessage();
982 double secSinceStart
=
983 TimeBetween(TimeStamp::ProcessCreation(), slices_
[0].start
)
985 fprintf(gcTimerFile
, "GC(T+%.3fs) %s\n", secSinceStart
, msg
.get());
991 void Statistics::beginGC(JS::GCOptions options
, const TimeStamp
& currentTime
) {
992 slices_
.clearAndFree();
993 sccTimes
.clearAndFree();
995 nonincrementalReason_
= GCAbortReason::None
;
997 preTotalHeapBytes
= gc
->heapSize
.bytes();
999 preCollectedHeapBytes
= 0;
1001 startingMajorGCNumber
= gc
->majorGCCount();
1002 startingSliceNumber
= gc
->gcNumber();
1004 if (gc
->lastGCEndTime()) {
1005 timeSinceLastGC
= TimeBetween(gc
->lastGCEndTime(), currentTime
);
1008 totalGCTime_
= TimeDuration::Zero();
1011 void Statistics::measureInitialHeapSize() {
1012 MOZ_ASSERT(preCollectedHeapBytes
== 0);
1013 for (GCZonesIter
zone(gc
); !zone
.done(); zone
.next()) {
1014 preCollectedHeapBytes
+= zone
->gcHeapSize
.bytes();
1018 void Statistics::endGC() {
1019 postTotalHeapBytes
= gc
->heapSize
.bytes();
1024 TimeDuration
Statistics::sumTotalParallelTime(PhaseKind phaseKind
) const {
1026 for (const SliceData
& slice
: slices_
) {
1027 total
+= slice
.totalParallelTimes
[phaseKind
];
1032 void Statistics::sendGCTelemetry() {
1033 JSRuntime
* runtime
= gc
->rt
;
1034 // NOTE: "Compartmental" is term that was deprecated with the
1035 // introduction of zone-based GC, but the old telemetry probe
1036 // continues to be used.
1037 runtime
->metrics().GC_IS_COMPARTMENTAL(!gc
->fullGCRequested
);
1038 runtime
->metrics().GC_ZONE_COUNT(zoneStats
.zoneCount
);
1039 runtime
->metrics().GC_ZONES_COLLECTED(zoneStats
.collectedZoneCount
);
1041 TimeDuration prepareTotal
= phaseTimes
[Phase::PREPARE
];
1042 TimeDuration markTotal
= SumPhase(PhaseKind::MARK
, phaseTimes
);
1043 TimeDuration markRootsTotal
= SumPhase(PhaseKind::MARK_ROOTS
, phaseTimes
);
1045 // Gray and weak marking time is counted under MARK_WEAK and not MARK_GRAY.
1046 TimeDuration markWeakTotal
= SumPhase(PhaseKind::MARK_WEAK
, phaseTimes
);
1047 TimeDuration markGrayNotWeak
=
1048 SumPhase(PhaseKind::MARK_GRAY
, phaseTimes
) +
1049 SumPhase(PhaseKind::MARK_INCOMING_GRAY
, phaseTimes
);
1050 TimeDuration markGrayWeak
= SumPhase(PhaseKind::MARK_GRAY_WEAK
, phaseTimes
);
1051 TimeDuration markGrayTotal
= markGrayNotWeak
+ markGrayWeak
;
1052 TimeDuration markNotGrayOrWeak
= markTotal
- markGrayNotWeak
- markWeakTotal
;
1053 if (markNotGrayOrWeak
< TimeDuration::FromMilliseconds(0)) {
1054 markNotGrayOrWeak
= TimeDuration::Zero();
1057 size_t markCount
= getCount(COUNT_CELLS_MARKED
);
1059 runtime
->metrics().GC_PREPARE_MS(prepareTotal
);
1060 runtime
->metrics().GC_MARK_MS(markNotGrayOrWeak
);
1061 if (markTotal
>= TimeDuration::FromMicroseconds(1)) {
1062 double markRate
= double(markCount
) / t(markTotal
);
1063 runtime
->metrics().GC_MARK_RATE_2(uint32_t(markRate
));
1065 runtime
->metrics().GC_SWEEP_MS(phaseTimes
[Phase::SWEEP
]);
1066 if (gc
->didCompactZones()) {
1067 runtime
->metrics().GC_COMPACT_MS(phaseTimes
[Phase::COMPACT
]);
1069 runtime
->metrics().GC_MARK_ROOTS_US(markRootsTotal
);
1070 runtime
->metrics().GC_MARK_GRAY_MS_2(markGrayTotal
);
1071 runtime
->metrics().GC_MARK_WEAK_MS(markWeakTotal
);
1072 runtime
->metrics().GC_NON_INCREMENTAL(nonincremental());
1073 if (nonincremental()) {
1074 runtime
->metrics().GC_NON_INCREMENTAL_REASON(
1075 uint32_t(nonincrementalReason_
));
1079 // Reset happens non-incrementally, so only the last slice can be reset.
1080 for (size_t i
= 0; i
< slices_
.length() - 1; i
++) {
1081 MOZ_ASSERT(!slices_
[i
].wasReset());
1084 const auto& lastSlice
= slices_
.back();
1085 runtime
->metrics().GC_RESET(lastSlice
.wasReset());
1086 if (lastSlice
.wasReset()) {
1087 runtime
->metrics().GC_RESET_REASON(uint32_t(lastSlice
.resetReason
));
1090 TimeDuration total
, longest
;
1091 gcDuration(&total
, &longest
);
1093 runtime
->metrics().GC_MS(total
);
1094 runtime
->metrics().GC_MAX_PAUSE_MS_2(longest
);
1096 const double mmu50
= computeMMU(TimeDuration::FromMilliseconds(50));
1097 runtime
->metrics().GC_MMU_50(mmu50
* 100.0);
1099 // Record scheduling telemetry for the main runtime but not for workers, which
1100 // are scheduled differently.
1101 if (!runtime
->parentRuntime
&& timeSinceLastGC
) {
1102 runtime
->metrics().GC_TIME_BETWEEN_S(timeSinceLastGC
);
1103 if (!nonincremental()) {
1104 runtime
->metrics().GC_SLICE_COUNT(slices_
.length());
1108 if (!lastSlice
.wasReset() && preCollectedHeapBytes
!= 0) {
1109 size_t bytesSurvived
= 0;
1110 for (ZonesIter
zone(runtime
, WithAtoms
); !zone
.done(); zone
.next()) {
1111 if (zone
->wasCollected()) {
1112 bytesSurvived
+= zone
->gcHeapSize
.retainedBytes();
1116 MOZ_ASSERT(preCollectedHeapBytes
>= bytesSurvived
);
1117 double survivalRate
=
1118 100.0 * double(bytesSurvived
) / double(preCollectedHeapBytes
);
1119 runtime
->metrics().GC_TENURED_SURVIVAL_RATE(survivalRate
);
1121 // Calculate 'effectiveness' in MB / second, on main thread only for now.
1122 if (!runtime
->parentRuntime
) {
1123 size_t bytesFreed
= preCollectedHeapBytes
- bytesSurvived
;
1124 TimeDuration clampedTotal
=
1125 TimeDuration::Max(total
, TimeDuration::FromMilliseconds(1));
1126 double effectiveness
=
1127 (double(bytesFreed
) / BYTES_PER_MB
) / clampedTotal
.ToSeconds();
1128 runtime
->metrics().GC_EFFECTIVENESS(uint32_t(effectiveness
));
1132 // Parallel marking stats.
1133 if (gc
->isParallelMarkingEnabled()) {
1134 TimeDuration wallTime
= SumPhase(PhaseKind::PARALLEL_MARK
, phaseTimes
);
1135 TimeDuration parallelRunTime
=
1136 sumTotalParallelTime(PhaseKind::PARALLEL_MARK
) -
1137 sumTotalParallelTime(PhaseKind::PARALLEL_MARK_WAIT
);
1138 TimeDuration parallelMarkTime
=
1139 sumTotalParallelTime(PhaseKind::PARALLEL_MARK_MARK
);
1140 if (wallTime
&& parallelMarkTime
) {
1141 uint32_t threadCount
= gc
->markers
.length();
1142 double speedup
= parallelMarkTime
/ wallTime
;
1143 double utilization
= parallelRunTime
/ (wallTime
* threadCount
);
1144 MOZ_ASSERT(utilization
<= 1.0);
1145 runtime
->metrics().GC_PARALLEL_MARK_SPEEDUP(uint32_t(speedup
* 100.0));
1146 runtime
->metrics().GC_PARALLEL_MARK_UTILIZATION(
1147 std::clamp(utilization
* 100.0, 0.0, 100.0));
1148 runtime
->metrics().GC_PARALLEL_MARK_INTERRUPTIONS(
1149 getCount(COUNT_PARALLEL_MARK_INTERRUPTIONS
));
1154 void Statistics::beginNurseryCollection(JS::GCReason reason
) {
1155 count(COUNT_MINOR_GC
);
1156 startingMinorGCNumber
= gc
->minorGCCount();
1157 if (nurseryCollectionCallback
) {
1158 (*nurseryCollectionCallback
)(
1159 context(), JS::GCNurseryProgress::GC_NURSERY_COLLECTION_START
, reason
);
1163 void Statistics::endNurseryCollection(JS::GCReason reason
) {
1164 if (nurseryCollectionCallback
) {
1165 (*nurseryCollectionCallback
)(
1166 context(), JS::GCNurseryProgress::GC_NURSERY_COLLECTION_END
, reason
);
1169 tenuredAllocsSinceMinorGC
= 0;
1172 Statistics::SliceData::SliceData(const SliceBudget
& budget
,
1173 Maybe
<Trigger
> trigger
, JS::GCReason reason
,
1174 TimeStamp start
, size_t startFaults
,
1175 gc::State initialState
)
1179 initialState(initialState
),
1181 startFaults(startFaults
) {}
1183 TimeDuration
Statistics::SliceData::duration() const {
1184 return TimeBetween(start
, end
);
1187 void Statistics::beginSlice(const ZoneGCStats
& zoneStats
, JS::GCOptions options
,
1188 const SliceBudget
& budget
, JS::GCReason reason
,
1189 bool budgetWasIncreased
) {
1190 MOZ_ASSERT(phaseStack
.empty() ||
1191 (phaseStack
.length() == 1 && phaseStack
[0] == Phase::MUTATOR
));
1193 this->zoneStats
= zoneStats
;
1195 TimeStamp currentTime
= TimeStamp::Now();
1197 bool first
= !gc
->isIncrementalGCInProgress();
1199 beginGC(options
, currentTime
);
1202 JSRuntime
* runtime
= gc
->rt
;
1203 if (!runtime
->parentRuntime
&& !slices_
.empty()) {
1204 TimeDuration timeSinceLastSlice
=
1205 TimeBetween(slices_
.back().end
, currentTime
);
1206 runtime
->metrics().GC_TIME_BETWEEN_SLICES_MS(timeSinceLastSlice
);
1209 Maybe
<Trigger
> trigger
= recordedTrigger
;
1210 recordedTrigger
.reset();
1212 if (!slices_
.emplaceBack(budget
, trigger
, reason
, currentTime
,
1213 GetPageFaultCount(), gc
->state())) {
1214 // If we are OOM, set a flag to indicate we have missing slice data.
1219 runtime
->metrics().GC_REASON_2(uint32_t(reason
));
1220 runtime
->metrics().GC_BUDGET_WAS_INCREASED(budgetWasIncreased
);
1222 // Slice callbacks should only fire for the outermost level.
1223 if (sliceCallback
) {
1224 JSContext
* cx
= context();
1225 JS::GCDescription
desc(!gc
->fullGCRequested
, false, options
, reason
);
1227 (*sliceCallback
)(cx
, JS::GC_CYCLE_BEGIN
, desc
);
1229 (*sliceCallback
)(cx
, JS::GC_SLICE_BEGIN
, desc
);
1235 void Statistics::endSlice() {
1236 MOZ_ASSERT(phaseStack
.empty() ||
1237 (phaseStack
.length() == 1 && phaseStack
[0] == Phase::MUTATOR
));
1240 auto& slice
= slices_
.back();
1241 slice
.end
= TimeStamp::Now();
1242 slice
.endFaults
= GetPageFaultCount();
1243 slice
.finalState
= gc
->state();
1247 sendSliceTelemetry(slice
);
1251 totalGCTime_
+= slice
.duration();
1254 bool last
= !gc
->isIncrementalGCInProgress();
1266 ShouldPrintProfile(gc
->rt
, enableProfiling_
, profileWorkers_
,
1267 profileThreshold_
, slices_
.back().duration())) {
1268 printSliceProfile();
1271 // Slice callbacks should only fire for the outermost level.
1273 if (sliceCallback
) {
1274 JSContext
* cx
= context();
1275 JS::GCDescription
desc(!gc
->fullGCRequested
, last
, gcOptions
,
1276 slices_
.back().reason
);
1277 (*sliceCallback
)(cx
, JS::GC_SLICE_END
, desc
);
1279 (*sliceCallback
)(cx
, JS::GC_CYCLE_END
, desc
);
1284 // Do this after the slice callback since it uses these values.
1286 for (auto& count
: counts
) {
1290 // Clear the timers at the end of a GC, preserving the data for
1291 // PhaseKind::MUTATOR.
1292 auto mutatorStartTime
= phaseStartTimes
[Phase::MUTATOR
];
1293 auto mutatorTime
= phaseTimes
[Phase::MUTATOR
];
1295 phaseStartTimes
= PhaseTimeStamps();
1297 phaseEndTimes
= PhaseTimeStamps();
1299 phaseTimes
= PhaseTimes();
1301 phaseStartTimes
[Phase::MUTATOR
] = mutatorStartTime
;
1302 phaseTimes
[Phase::MUTATOR
] = mutatorTime
;
1308 void Statistics::sendSliceTelemetry(const SliceData
& slice
) {
1309 JSRuntime
* runtime
= gc
->rt
;
1310 TimeDuration sliceTime
= slice
.duration();
1311 runtime
->metrics().GC_SLICE_MS(sliceTime
);
1313 if (slice
.budget
.isTimeBudget()) {
1314 TimeDuration budgetDuration
= slice
.budget
.timeBudgetDuration();
1315 runtime
->metrics().GC_BUDGET_MS_2(budgetDuration
);
1317 if (IsCurrentlyAnimating(runtime
->lastAnimationTime
, slice
.end
)) {
1318 runtime
->metrics().GC_ANIMATION_MS(sliceTime
);
1321 bool wasLongSlice
= false;
1322 if (sliceTime
> budgetDuration
) {
1323 // Record how long we went over budget.
1324 TimeDuration overrun
= sliceTime
- budgetDuration
;
1325 runtime
->metrics().GC_BUDGET_OVERRUN(overrun
);
1327 // Long GC slices are those that go 50% or 5ms over their budget.
1328 wasLongSlice
= (overrun
> TimeDuration::FromMilliseconds(5)) ||
1329 (overrun
> (budgetDuration
/ int64_t(2)));
1331 // Record the longest phase in any long slice.
1333 PhaseKind longest
= LongestPhaseSelfTimeInMajorGC(slice
.phaseTimes
);
1334 reportLongestPhaseInMajorGC(longest
, [runtime
](auto sample
) {
1335 runtime
->metrics().GC_SLOW_PHASE(sample
);
1338 // If the longest phase was waiting for parallel tasks then record the
1340 if (longest
== PhaseKind::JOIN_PARALLEL_TASKS
) {
1341 PhaseKind longestParallel
=
1342 FindLongestPhaseKind(slice
.maxParallelTimes
);
1343 reportLongestPhaseInMajorGC(longestParallel
, [runtime
](auto sample
) {
1344 runtime
->metrics().GC_SLOW_TASK(sample
);
1350 // Record `wasLongSlice` for all TimeBudget slices.
1351 runtime
->metrics().GC_SLICE_WAS_LONG(wasLongSlice
);
1355 template <typename Fn
>
1356 void Statistics::reportLongestPhaseInMajorGC(PhaseKind longest
, Fn reportFn
) {
1357 if (longest
!= PhaseKind::NONE
) {
1358 uint8_t bucket
= phaseKinds
[longest
].telemetryBucket
;
1363 bool Statistics::startTimingMutator() {
1364 if (phaseStack
.length() != 0) {
1365 // Should only be called from outside of GC.
1366 MOZ_ASSERT(phaseStack
.length() == 1);
1367 MOZ_ASSERT(phaseStack
[0] == Phase::MUTATOR
);
1371 MOZ_ASSERT(suspendedPhases
.empty());
1373 timedGCTime
= TimeDuration::Zero();
1374 phaseStartTimes
[Phase::MUTATOR
] = TimeStamp();
1375 phaseTimes
[Phase::MUTATOR
] = TimeDuration::Zero();
1376 timedGCStart
= TimeStamp();
1378 beginPhase(PhaseKind::MUTATOR
);
1382 bool Statistics::stopTimingMutator(double& mutator_ms
, double& gc_ms
) {
1383 // This should only be called from outside of GC, while timing the mutator.
1384 if (phaseStack
.length() != 1 || phaseStack
[0] != Phase::MUTATOR
) {
1388 endPhase(PhaseKind::MUTATOR
);
1389 mutator_ms
= t(phaseTimes
[Phase::MUTATOR
]);
1390 gc_ms
= t(timedGCTime
);
1395 void Statistics::suspendPhases(PhaseKind suspension
) {
1396 MOZ_ASSERT(suspension
== PhaseKind::EXPLICIT_SUSPENSION
||
1397 suspension
== PhaseKind::IMPLICIT_SUSPENSION
);
1398 while (!phaseStack
.empty()) {
1399 MOZ_ASSERT(suspendedPhases
.length() < MAX_SUSPENDED_PHASES
);
1400 Phase parent
= phaseStack
.back();
1401 suspendedPhases
.infallibleAppend(parent
);
1402 recordPhaseEnd(parent
);
1404 suspendedPhases
.infallibleAppend(lookupChildPhase(suspension
));
1407 void Statistics::resumePhases() {
1408 MOZ_ASSERT(suspendedPhases
.back() == Phase::EXPLICIT_SUSPENSION
||
1409 suspendedPhases
.back() == Phase::IMPLICIT_SUSPENSION
);
1410 suspendedPhases
.popBack();
1412 while (!suspendedPhases
.empty() &&
1413 suspendedPhases
.back() != Phase::EXPLICIT_SUSPENSION
&&
1414 suspendedPhases
.back() != Phase::IMPLICIT_SUSPENSION
) {
1415 Phase resumePhase
= suspendedPhases
.popCopy();
1416 if (resumePhase
== Phase::MUTATOR
) {
1417 timedGCTime
+= TimeBetween(timedGCStart
, TimeStamp::Now());
1419 recordPhaseBegin(resumePhase
);
1423 void Statistics::beginPhase(PhaseKind phaseKind
) {
1424 // No longer timing these phases. We should never see these.
1425 MOZ_ASSERT(phaseKind
!= PhaseKind::GC_BEGIN
&&
1426 phaseKind
!= PhaseKind::GC_END
);
1428 // PhaseKind::MUTATOR is suspended while performing GC.
1429 if (currentPhase() == Phase::MUTATOR
) {
1430 suspendPhases(PhaseKind::IMPLICIT_SUSPENSION
);
1433 recordPhaseBegin(lookupChildPhase(phaseKind
));
1436 void Statistics::recordPhaseBegin(Phase phase
) {
1437 MOZ_ASSERT(CurrentThreadCanAccessRuntime(gc
->rt
));
1439 // Guard against any other re-entry.
1440 MOZ_ASSERT(!phaseStartTimes
[phase
]);
1442 MOZ_ASSERT(phaseStack
.length() < MAX_PHASE_NESTING
);
1444 Phase current
= currentPhase();
1445 MOZ_ASSERT(phases
[phase
].parent
== current
);
1447 TimeStamp now
= TimeStamp::Now();
1449 if (current
!= Phase::NONE
) {
1450 MOZ_ASSERT(now
>= phaseStartTimes
[currentPhase()],
1451 "Inconsistent time data; see bug 1400153");
1452 if (now
< phaseStartTimes
[currentPhase()]) {
1453 now
= phaseStartTimes
[currentPhase()];
1458 phaseStack
.infallibleAppend(phase
);
1459 phaseStartTimes
[phase
] = now
;
1460 log("begin: %s", phases
[phase
].path
);
1463 void Statistics::recordPhaseEnd(Phase phase
) {
1464 MOZ_ASSERT(CurrentThreadCanAccessRuntime(gc
->rt
));
1466 MOZ_ASSERT(phaseStartTimes
[phase
]);
1468 TimeStamp now
= TimeStamp::Now();
1470 // Make sure this phase ends after it starts.
1471 MOZ_ASSERT(now
>= phaseStartTimes
[phase
],
1472 "Inconsistent time data; see bug 1400153");
1475 // Make sure this phase ends after all of its children. Note that some
1476 // children might not have run in this instance, in which case they will
1477 // have run in a previous instance of this parent or not at all.
1478 for (Phase kid
= phases
[phase
].firstChild
; kid
!= Phase::NONE
;
1479 kid
= phases
[kid
].nextSibling
) {
1480 if (phaseEndTimes
[kid
].IsNull()) {
1483 if (phaseEndTimes
[kid
] > now
) {
1485 "Parent %s ended at %.3fms, before child %s ended at %.3fms?\n",
1487 t(TimeBetween(TimeStamp::FirstTimeStamp(), now
)),
1489 t(TimeBetween(TimeStamp::FirstTimeStamp(), phaseEndTimes
[kid
])));
1491 MOZ_ASSERT(phaseEndTimes
[kid
] <= now
,
1492 "Inconsistent time data; see bug 1400153");
1496 if (now
< phaseStartTimes
[phase
]) {
1497 now
= phaseStartTimes
[phase
];
1501 if (phase
== Phase::MUTATOR
) {
1505 phaseStack
.popBack();
1507 TimeDuration t
= TimeBetween(phaseStartTimes
[phase
], now
);
1508 if (!slices_
.empty()) {
1509 slices_
.back().phaseTimes
[phase
] += t
;
1511 phaseTimes
[phase
] += t
;
1512 phaseStartTimes
[phase
] = TimeStamp();
1515 phaseEndTimes
[phase
] = now
;
1516 log("end: %s", phases
[phase
].path
);
1520 void Statistics::endPhase(PhaseKind phaseKind
) {
1521 Phase phase
= currentPhase();
1522 MOZ_ASSERT(phase
!= Phase::NONE
);
1523 MOZ_ASSERT(phases
[phase
].phaseKind
== phaseKind
);
1525 recordPhaseEnd(phase
);
1527 // When emptying the stack, we may need to return to timing the mutator
1528 // (PhaseKind::MUTATOR).
1529 if (phaseStack
.empty() && !suspendedPhases
.empty() &&
1530 suspendedPhases
.back() == Phase::IMPLICIT_SUSPENSION
) {
1535 void Statistics::recordParallelPhase(PhaseKind phaseKind
,
1536 TimeDuration duration
) {
1537 MOZ_ASSERT(CurrentThreadCanAccessRuntime(gc
->rt
));
1543 slices_
.back().totalParallelTimes
[phaseKind
] += duration
;
1545 // Also record the maximum task time for each phase. Don't record times for
1547 TimeDuration
& maxTime
= slices_
.back().maxParallelTimes
[phaseKind
];
1548 maxTime
= std::max(maxTime
, duration
);
1551 TimeStamp
Statistics::beginSCC() { return TimeStamp::Now(); }
1553 void Statistics::endSCC(unsigned scc
, TimeStamp start
) {
1554 if (scc
>= sccTimes
.length() && !sccTimes
.resize(scc
+ 1)) {
1558 sccTimes
[scc
] += TimeBetween(start
, TimeStamp::Now());
1562 * Calculate minimum mutator utilization for previous incremental GC.
1564 * MMU (minimum mutator utilization) is a measure of how much garbage collection
1565 * is affecting the responsiveness of the system. MMU measurements are given
1566 * with respect to a certain window size. If we report MMU(50ms) = 80%, then
1567 * that means that, for any 50ms window of time, at least 80% of the window is
1568 * devoted to the mutator. In other words, the GC is running for at most 20% of
1569 * the window, or 10ms. The GC can run multiple slices during the 50ms window
1570 * as long as the total time it spends is at most 10ms.
1572 double Statistics::computeMMU(TimeDuration window
) const {
1573 MOZ_ASSERT(!slices_
.empty());
1575 TimeDuration gc
= slices_
[0].duration();
1576 TimeDuration gcMax
= gc
;
1583 for (size_t endIndex
= 1; endIndex
< slices_
.length(); endIndex
++) {
1584 const auto* startSlice
= &slices_
[startIndex
];
1585 const auto& endSlice
= slices_
[endIndex
];
1586 gc
+= endSlice
.duration();
1588 while (TimeBetween(startSlice
->end
, endSlice
.end
) >= window
) {
1589 gc
-= startSlice
->duration();
1590 startSlice
= &slices_
[++startIndex
];
1593 TimeDuration cur
= gc
;
1594 TimeDuration sliceRange
= TimeBetween(startSlice
->start
, endSlice
.end
);
1595 if (sliceRange
> window
) {
1596 cur
-= (sliceRange
- window
);
1604 MOZ_ASSERT(gcMax
> TimeDuration::Zero());
1605 MOZ_ASSERT(gcMax
<= window
);
1606 return (window
- gcMax
) / window
;
1609 void Statistics::maybePrintProfileHeaders() {
1610 static int printedHeader
= 0;
1611 if ((printedHeader
++ % 200) == 0) {
1612 printProfileHeader();
1613 if (gc
->nursery().enableProfiling()) {
1614 gc
->nursery().printProfileHeader();
1619 // The following macros define GC profile metadata fields that are printed
1620 // before the timing information defined by FOR_EACH_GC_PROFILE_TIME.
1622 #define FOR_EACH_GC_PROFILE_COMMON_METADATA(_) \
1623 _("PID", 7, "%7zu", pid) \
1624 _("Runtime", 14, "0x%12p", runtime)
1626 #define FOR_EACH_GC_PROFILE_SLICE_METADATA(_) \
1627 _("Timestamp", 10, "%10.6f", timestamp.ToSeconds()) \
1628 _("Reason", 20, "%-20.20s", reason) \
1629 _("States", 6, "%6s", formatGCStates(slice)) \
1630 _("FSNR", 4, "%4s", formatGCFlags(slice)) \
1631 _("SizeKB", 8, "%8zu", sizeKB) \
1632 _("Zs", 3, "%3zu", zoneCount) \
1633 _("Cs", 3, "%3zu", compartmentCount) \
1634 _("Rs", 3, "%3zu", realmCount) \
1635 _("Budget", 6, "%6s", formatBudget(slice))
1637 #define FOR_EACH_GC_PROFILE_METADATA(_) \
1638 FOR_EACH_GC_PROFILE_COMMON_METADATA(_) \
1639 FOR_EACH_GC_PROFILE_SLICE_METADATA(_)
1641 void Statistics::printProfileHeader() {
1642 if (!enableProfiling_
) {
1647 if (!sprinter
.init() || !sprinter
.put(MajorGCProfilePrefix
)) {
1651 #define PRINT_METADATA_NAME(name, width, _1, _2) \
1652 if (!sprinter.jsprintf(" %-*s", width, name)) { \
1655 FOR_EACH_GC_PROFILE_METADATA(PRINT_METADATA_NAME
)
1656 #undef PRINT_METADATA_NAME
1658 #define PRINT_PROFILE_NAME(_1, text, _2) \
1659 if (!sprinter.jsprintf(" %-6.6s", text)) { \
1662 FOR_EACH_GC_PROFILE_TIME(PRINT_PROFILE_NAME
)
1663 #undef PRINT_PROFILE_NAME
1665 if (!sprinter
.put("\n")) {
1669 fputs(sprinter
.string(), profileFile());
1672 static TimeDuration
SumAllPhaseKinds(const Statistics::PhaseKindTimes
& times
) {
1674 for (PhaseKind kind
: AllPhaseKinds()) {
1680 void Statistics::printSliceProfile() {
1681 maybePrintProfileHeaders();
1683 const SliceData
& slice
= slices_
.back();
1684 ProfileDurations times
= getProfileTimes(slice
);
1685 updateTotalProfileTimes(times
);
1688 if (!sprinter
.init() || !sprinter
.put(MajorGCProfilePrefix
)) {
1692 size_t pid
= getpid();
1693 JSRuntime
* runtime
= gc
->rt
;
1694 TimeDuration timestamp
= TimeBetween(creationTime(), slice
.end
);
1695 const char* reason
= ExplainGCReason(slice
.reason
);
1696 size_t sizeKB
= gc
->heapSize
.bytes() / 1024;
1697 size_t zoneCount
= zoneStats
.zoneCount
;
1698 size_t compartmentCount
= zoneStats
.compartmentCount
;
1699 size_t realmCount
= zoneStats
.realmCount
;
1701 #define PRINT_FIELD_VALUE(_1, _2, format, value) \
1702 if (!sprinter.jsprintf(" " format, value)) { \
1705 FOR_EACH_GC_PROFILE_METADATA(PRINT_FIELD_VALUE
)
1706 #undef PRINT_FIELD_VALUE
1708 if (!printProfileTimes(times
, sprinter
)) {
1712 fputs(sprinter
.string(), profileFile());
1715 Statistics::ProfileDurations
Statistics::getProfileTimes(
1716 const SliceData
& slice
) const {
1717 ProfileDurations times
;
1719 times
[ProfileKey::Total
] = slice
.duration();
1720 times
[ProfileKey::Background
] = SumAllPhaseKinds(slice
.totalParallelTimes
);
1722 #define GET_PROFILE_TIME(name, text, phase) \
1723 if (phase != PhaseKind::NONE) { \
1724 times[ProfileKey::name] = SumPhase(phase, slice.phaseTimes); \
1726 FOR_EACH_GC_PROFILE_TIME(GET_PROFILE_TIME
)
1727 #undef GET_PROFILE_TIME
1732 void Statistics::updateTotalProfileTimes(const ProfileDurations
& times
) {
1733 #define UPDATE_PROFILE_TIME(name, _, phase) \
1734 totalTimes_[ProfileKey::name] += times[ProfileKey::name];
1735 FOR_EACH_GC_PROFILE_TIME(UPDATE_PROFILE_TIME
)
1736 #undef UPDATE_PROFILE_TIME
1739 const char* Statistics::formatGCStates(const SliceData
& slice
) {
1741 SprintfLiteral(formatBuffer_
, "%1d -> %1d", int(slice
.initialState
),
1742 int(slice
.finalState
));
1743 MOZ_ASSERT(r
> 0 && r
< FormatBufferLength
);
1744 return formatBuffer_
;
1747 const char* Statistics::formatGCFlags(const SliceData
& slice
) {
1748 bool fullGC
= gc
->fullGCRequested
;
1749 bool shrinkingGC
= gcOptions
== JS::GCOptions::Shrink
;
1750 bool nonIncrementalGC
= nonincrementalReason_
!= GCAbortReason::None
;
1751 bool wasReset
= slice
.resetReason
!= GCAbortReason::None
;
1753 MOZ_ASSERT(FormatBufferLength
>= 5);
1754 formatBuffer_
[0] = fullGC
? 'F' : ' ';
1755 formatBuffer_
[1] = shrinkingGC
? 'S' : ' ';
1756 formatBuffer_
[2] = nonIncrementalGC
? 'N' : ' ';
1757 formatBuffer_
[3] = wasReset
? 'R' : ' ';
1758 formatBuffer_
[4] = '\0';
1760 return formatBuffer_
;
1763 const char* Statistics::formatBudget(const SliceData
& slice
) {
1764 if (nonincrementalReason_
!= GCAbortReason::None
||
1765 !slice
.budget
.isTimeBudget()) {
1766 formatBuffer_
[0] = '\0';
1767 return formatBuffer_
;
1771 SprintfLiteral(formatBuffer_
, "%6" PRIi64
, slice
.budget
.timeBudget());
1772 MOZ_ASSERT(r
> 0 && r
< FormatBufferLength
);
1773 return formatBuffer_
;
1777 bool Statistics::printProfileTimes(const ProfileDurations
& times
,
1778 Sprinter
& sprinter
) {
1779 for (auto time
: times
) {
1780 int64_t millis
= int64_t(time
.ToMilliseconds());
1781 if (!sprinter
.jsprintf(" %6" PRIi64
, millis
)) {
1786 return sprinter
.put("\n");
1789 constexpr size_t SliceMetadataFormatWidth() {
1790 size_t fieldCount
= 0;
1791 size_t totalWidth
= 0;
1793 #define UPDATE_COUNT_AND_WIDTH(_1, width, _2, _3) \
1795 totalWidth += width;
1796 FOR_EACH_GC_PROFILE_SLICE_METADATA(UPDATE_COUNT_AND_WIDTH
)
1797 #undef UPDATE_COUNT_AND_WIDTH
1799 // Add padding between fields.
1800 totalWidth
+= fieldCount
- 1;
1805 void Statistics::printTotalProfileTimes() {
1806 if (!enableProfiling_
) {
1811 if (!sprinter
.init() || !sprinter
.put(MajorGCProfilePrefix
)) {
1815 size_t pid
= getpid();
1816 JSRuntime
* runtime
= gc
->rt
;
1818 #define PRINT_FIELD_VALUE(_1, _2, format, value) \
1819 if (!sprinter.jsprintf(" " format, value)) { \
1822 FOR_EACH_GC_PROFILE_COMMON_METADATA(PRINT_FIELD_VALUE
)
1823 #undef PRINT_FIELD_VALUE
1825 // Use whole width of per-slice metadata to print total slices so the profile
1826 // totals that follow line up.
1827 size_t width
= SliceMetadataFormatWidth();
1828 if (!sprinter
.jsprintf(" %-*s", int(width
), formatTotalSlices())) {
1832 if (!printProfileTimes(totalTimes_
, sprinter
)) {
1836 fputs(sprinter
.string(), profileFile());
1839 const char* Statistics::formatTotalSlices() {
1840 DebugOnly
<int> r
= SprintfLiteral(
1841 formatBuffer_
, "TOTALS: %7" PRIu64
" slices:", sliceCount_
);
1842 MOZ_ASSERT(r
> 0 && r
< FormatBufferLength
);
1843 return formatBuffer_
;