2020-10-15 18:00:53 +03:00
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "js/SliceBudget.h"
|
2020-12-11 22:04:27 +03:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2020-12-04 19:03:07 +03:00
|
|
|
#include "mozilla/CycleCollectedJSContext.h"
|
2021-07-09 10:14:11 +03:00
|
|
|
#include "mozilla/IdleTaskRunner.h"
|
2020-12-04 19:02:52 +03:00
|
|
|
#include "mozilla/MainThreadIdlePeriod.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2020-11-13 22:38:02 +03:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2021-07-09 10:14:11 +03:00
|
|
|
#include "mozilla/ipc/IdleSchedulerChild.h"
|
2020-10-15 18:00:53 +03:00
|
|
|
#include "nsCycleCollector.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "nsJSEnvironment.h"
|
2021-10-14 21:15:39 +03:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2020-10-15 18:00:53 +03:00
|
|
|
|
2020-12-11 22:04:27 +03:00
|
|
|
namespace mozilla {
|
2020-12-04 19:02:59 +03:00
|
|
|
|
2020-12-16 03:11:06 +03:00
|
|
|
static const TimeDuration kOneMinute = TimeDuration::FromSeconds(60.0f);
|
2020-10-15 18:00:53 +03:00
|
|
|
|
|
|
|
// The amount of time we wait between a request to CC (after GC ran)
|
|
|
|
// and doing the actual CC.
|
2020-12-16 03:11:06 +03:00
|
|
|
static const TimeDuration kCCDelay = TimeDuration::FromSeconds(6);
|
2020-10-15 18:00:53 +03:00
|
|
|
|
2020-12-16 03:11:06 +03:00
|
|
|
static const TimeDuration kCCSkippableDelay =
|
|
|
|
TimeDuration::FromMilliseconds(250);
|
2020-10-15 18:00:53 +03:00
|
|
|
|
|
|
|
// In case the cycle collector isn't run at all, we don't want forget skippables
|
|
|
|
// to run too often. So limit the forget skippable cycle to start at earliest 2
|
|
|
|
// seconds after the end of the previous cycle.
|
2020-12-16 03:11:06 +03:00
|
|
|
static const TimeDuration kTimeBetweenForgetSkippableCycles =
|
|
|
|
TimeDuration::FromSeconds(2);
|
2020-10-15 18:00:53 +03:00
|
|
|
|
|
|
|
// ForgetSkippable is usually fast, so we can use small budgets.
|
|
|
|
// This isn't a real budget but a hint to IdleTaskRunner whether there
|
|
|
|
// is enough time to call ForgetSkippable.
|
2020-12-16 03:11:06 +03:00
|
|
|
static const TimeDuration kForgetSkippableSliceDuration =
|
|
|
|
TimeDuration::FromMilliseconds(2);
|
2020-10-15 18:00:53 +03:00
|
|
|
|
|
|
|
// Maximum amount of time that should elapse between incremental CC slices
|
2020-12-16 03:11:06 +03:00
|
|
|
static const TimeDuration kICCIntersliceDelay =
|
|
|
|
TimeDuration::FromMilliseconds(64);
|
2020-10-15 18:00:53 +03:00
|
|
|
|
|
|
|
// Time budget for an incremental CC slice when using timer to run it.
|
2020-12-16 03:11:06 +03:00
|
|
|
static const TimeDuration kICCSliceBudget = TimeDuration::FromMilliseconds(3);
|
2020-10-15 18:00:53 +03:00
|
|
|
// Minimum budget for an incremental CC slice when using idle time to run it.
|
2020-12-16 03:11:06 +03:00
|
|
|
static const TimeDuration kIdleICCSliceBudget =
|
|
|
|
TimeDuration::FromMilliseconds(2);
|
2020-10-15 18:00:53 +03:00
|
|
|
|
|
|
|
// Maximum total duration for an ICC
|
2020-12-16 03:11:06 +03:00
|
|
|
static const TimeDuration kMaxICCDuration = TimeDuration::FromSeconds(2);
|
2020-10-15 18:00:53 +03:00
|
|
|
|
|
|
|
// Force a CC after this long if there's more than NS_CC_FORCED_PURPLE_LIMIT
|
|
|
|
// objects in the purple buffer.
|
2020-12-16 03:11:06 +03:00
|
|
|
static const TimeDuration kCCForced = kOneMinute * 2;
|
2020-10-15 18:00:53 +03:00
|
|
|
static const uint32_t kCCForcedPurpleLimit = 10;
|
|
|
|
|
|
|
|
// Don't allow an incremental GC to lock out the CC for too long.
|
2020-12-16 03:11:06 +03:00
|
|
|
static const TimeDuration kMaxCCLockedoutTime = TimeDuration::FromSeconds(30);
|
2020-10-15 18:00:53 +03:00
|
|
|
|
|
|
|
// Trigger a CC if the purple buffer exceeds this size when we check it.
|
|
|
|
static const uint32_t kCCPurpleLimit = 200;
|
|
|
|
|
2021-04-28 19:37:32 +03:00
|
|
|
// How many cycle collected nodes to traverse between time checks.
|
|
|
|
static const int64_t kNumCCNodesBetweenTimeChecks = 1000;
|
|
|
|
|
2021-04-30 02:12:04 +03:00
|
|
|
// Actions performed by the GCRunner state machine.
|
|
|
|
enum class GCRunnerAction {
|
2021-05-20 02:26:17 +03:00
|
|
|
WaitToMajorGC, // We want to start a new major GC
|
|
|
|
StartMajorGC, // The parent says we may begin our major GC
|
|
|
|
GCSlice, // Run a single slice of a major GC
|
2021-04-30 02:12:04 +03:00
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
struct GCRunnerStep {
|
|
|
|
GCRunnerAction mAction;
|
|
|
|
JS::GCReason mReason;
|
|
|
|
};
|
|
|
|
|
2020-12-11 22:04:27 +03:00
|
|
|
enum class CCRunnerAction {
|
|
|
|
None,
|
|
|
|
ForgetSkippable,
|
|
|
|
CleanupContentUnbinder,
|
|
|
|
CleanupDeferred,
|
|
|
|
CycleCollect,
|
|
|
|
StopRunning
|
|
|
|
};
|
2020-10-15 18:00:53 +03:00
|
|
|
|
2020-12-11 22:04:27 +03:00
|
|
|
enum CCRunnerYield { Continue, Yield };
|
2020-11-12 22:05:28 +03:00
|
|
|
|
2020-12-11 22:04:27 +03:00
|
|
|
enum CCRunnerForgetSkippableRemoveChildless {
|
|
|
|
KeepChildless = false,
|
|
|
|
RemoveChildless = true
|
|
|
|
};
|
2020-11-12 22:05:28 +03:00
|
|
|
|
2020-12-11 22:04:27 +03:00
|
|
|
struct CCRunnerStep {
|
|
|
|
// The action to scheduler is instructing the caller to perform.
|
|
|
|
CCRunnerAction mAction;
|
|
|
|
|
|
|
|
// Whether to stop processing actions for this invocation of the timer
|
|
|
|
// callback.
|
|
|
|
CCRunnerYield mYield;
|
|
|
|
|
|
|
|
// If the action is ForgetSkippable, then whether to remove childless nodes
|
|
|
|
// or not. (ForgetSkippable is the only action requiring a parameter; if
|
|
|
|
// that changes, this will become a union.)
|
|
|
|
CCRunnerForgetSkippableRemoveChildless mRemoveChildless;
|
2021-10-14 21:15:40 +03:00
|
|
|
|
|
|
|
// If the action is CycleCollect, the reason for the collection.
|
|
|
|
CCReason mCCReason;
|
2020-12-11 22:04:27 +03:00
|
|
|
};
|
2020-10-15 18:01:24 +03:00
|
|
|
|
2020-10-15 18:00:53 +03:00
|
|
|
class CCGCScheduler {
|
2020-12-16 03:11:06 +03:00
|
|
|
public:
|
2021-07-09 10:14:11 +03:00
|
|
|
static bool CCRunnerFired(TimeStamp aDeadline);
|
|
|
|
|
2020-10-15 18:01:11 +03:00
|
|
|
// Parameter setting
|
|
|
|
|
2020-10-15 18:00:53 +03:00
|
|
|
void SetActiveIntersliceGCBudget(TimeDuration aDuration) {
|
|
|
|
mActiveIntersliceGCBudget = aDuration;
|
|
|
|
}
|
|
|
|
|
2020-10-15 18:01:11 +03:00
|
|
|
// State retrieval
|
|
|
|
|
2020-12-04 19:03:07 +03:00
|
|
|
TimeDuration GetCCBlockedTime(TimeStamp aNow) const {
|
|
|
|
MOZ_ASSERT(mInIncrementalGC);
|
|
|
|
MOZ_ASSERT(!mCCBlockStart.IsNull());
|
|
|
|
return aNow - mCCBlockStart;
|
2020-10-15 18:01:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool InIncrementalGC() const { return mInIncrementalGC; }
|
|
|
|
|
2020-10-15 18:01:48 +03:00
|
|
|
TimeStamp GetLastCCEndTime() const { return mLastCCEndTime; }
|
|
|
|
|
2020-10-15 18:02:12 +03:00
|
|
|
bool IsEarlyForgetSkippable(uint32_t aN = kMajorForgetSkippableCalls) const {
|
|
|
|
return mCleanupsSinceLastGC < aN;
|
|
|
|
}
|
|
|
|
|
2020-12-04 19:02:59 +03:00
|
|
|
bool NeedsFullGC() const { return mNeedsFullGC; }
|
|
|
|
|
2021-07-09 10:14:11 +03:00
|
|
|
// Requests
|
2021-07-13 04:42:32 +03:00
|
|
|
void PokeGC(JS::GCReason aReason, JSObject* aObj, TimeDuration aDelay = 0);
|
2021-07-09 10:14:11 +03:00
|
|
|
void PokeShrinkingGC();
|
|
|
|
void PokeFullGC();
|
2021-07-09 10:14:15 +03:00
|
|
|
void MaybePokeCC(TimeStamp aNow, uint32_t aSuspectedCCObjects);
|
2021-07-09 10:14:13 +03:00
|
|
|
|
2021-07-09 10:14:14 +03:00
|
|
|
void UserIsInactive();
|
|
|
|
void UserIsActive();
|
2021-08-30 03:04:56 +03:00
|
|
|
bool IsUserActive() const { return mUserIsActive; }
|
2021-07-09 10:14:14 +03:00
|
|
|
|
2021-07-09 10:14:11 +03:00
|
|
|
void KillShrinkingGCTimer();
|
|
|
|
void KillFullGCTimer();
|
|
|
|
void KillGCRunner();
|
2021-07-09 10:14:11 +03:00
|
|
|
void KillCCRunner();
|
2021-07-09 10:14:12 +03:00
|
|
|
void KillAllTimersAndRunners();
|
2021-07-09 10:14:11 +03:00
|
|
|
|
2021-07-09 10:14:13 +03:00
|
|
|
/*
|
|
|
|
* aDelay is the delay before the first time the idle task runner runs.
|
|
|
|
* Then it runs every
|
|
|
|
* StaticPrefs::javascript_options_gc_delay_interslice()
|
|
|
|
*/
|
2021-07-13 04:42:32 +03:00
|
|
|
void EnsureGCRunner(TimeDuration aDelay);
|
2021-07-09 10:14:13 +03:00
|
|
|
|
2021-07-09 10:14:11 +03:00
|
|
|
void EnsureCCRunner(TimeDuration aDelay, TimeDuration aBudget);
|
2021-07-09 10:14:11 +03:00
|
|
|
|
2020-10-15 18:01:11 +03:00
|
|
|
// State modification
|
|
|
|
|
2020-12-04 19:02:59 +03:00
|
|
|
void SetNeedsFullGC(bool aNeedGC = true) { mNeedsFullGC = aNeedGC; }
|
|
|
|
|
2021-04-30 02:12:04 +03:00
|
|
|
void SetWantMajorGC(JS::GCReason aReason) {
|
2021-08-05 12:13:31 +03:00
|
|
|
MOZ_ASSERT(aReason != JS::GCReason::NO_REASON);
|
|
|
|
|
2021-10-19 05:31:14 +03:00
|
|
|
// If the GC being requested is not a shrinking GC set this flag.
|
|
|
|
// If/when the shrinking GC timer fires but the user is active we check
|
|
|
|
// this flag before canceling the GC, so as not to cancel the
|
|
|
|
// non-shrinking GC being requested here.
|
|
|
|
if (aReason != JS::GCReason::USER_INACTIVE) {
|
2021-08-05 12:13:31 +03:00
|
|
|
mWantAtLeastRegularGC = true;
|
|
|
|
}
|
2021-04-30 02:12:04 +03:00
|
|
|
|
|
|
|
// Force full GCs when called from reftests so that we collect dead zones
|
|
|
|
// that have not been scheduled for collection.
|
|
|
|
if (aReason == JS::GCReason::DOM_WINDOW_UTILS) {
|
|
|
|
SetNeedsFullGC();
|
|
|
|
}
|
2021-10-19 05:31:15 +03:00
|
|
|
|
|
|
|
// USER_INACTIVE trumps everything,
|
|
|
|
// FULL_GC_TIMER trumps everything except USER_INACTIVE,
|
|
|
|
// all other reasons just use the latest reason.
|
|
|
|
switch (aReason) {
|
|
|
|
case JS::GCReason::USER_INACTIVE:
|
|
|
|
mMajorGCReason = aReason;
|
|
|
|
break;
|
|
|
|
case JS::GCReason::FULL_GC_TIMER:
|
|
|
|
if (mMajorGCReason != JS::GCReason::USER_INACTIVE) {
|
|
|
|
mMajorGCReason = aReason;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (mMajorGCReason != JS::GCReason::USER_INACTIVE &&
|
|
|
|
mMajorGCReason != JS::GCReason::FULL_GC_TIMER) {
|
|
|
|
mMajorGCReason = aReason;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-04-30 02:12:04 +03:00
|
|
|
}
|
2021-08-13 07:06:26 +03:00
|
|
|
|
2020-12-04 19:02:59 +03:00
|
|
|
// Ensure that the current runner does a cycle collection, and trigger a GC
|
|
|
|
// after it finishes.
|
2021-10-14 21:15:39 +03:00
|
|
|
void EnsureCCThenGC(CCReason aReason) {
|
2020-12-04 19:02:59 +03:00
|
|
|
MOZ_ASSERT(mCCRunnerState != CCRunnerState::Inactive);
|
2021-10-14 21:15:39 +03:00
|
|
|
MOZ_ASSERT(aReason != CCReason::NO_REASON);
|
|
|
|
mNeedsFullCC = aReason;
|
2020-12-04 19:02:59 +03:00
|
|
|
mNeedsGCAfterCC = true;
|
|
|
|
}
|
2020-10-15 18:02:12 +03:00
|
|
|
|
2021-05-20 02:26:17 +03:00
|
|
|
// Returns false if we started and finished a major GC while waiting for a
|
|
|
|
// response.
|
|
|
|
[[nodiscard]] bool NoteReadyForMajorGC() {
|
2021-08-13 07:06:25 +03:00
|
|
|
if (mMajorGCReason == JS::GCReason::NO_REASON || InIncrementalGC()) {
|
2021-05-20 02:26:17 +03:00
|
|
|
return false;
|
|
|
|
}
|
2021-05-12 09:46:08 +03:00
|
|
|
mReadyForMajorGC = true;
|
2021-05-20 02:26:17 +03:00
|
|
|
return true;
|
2021-05-12 09:46:08 +03:00
|
|
|
}
|
|
|
|
|
2021-10-14 21:15:40 +03:00
|
|
|
// Starting a major GC (incremental or non-incremental).
|
2021-08-05 12:13:30 +03:00
|
|
|
void NoteGCBegin();
|
2021-10-14 21:15:40 +03:00
|
|
|
|
|
|
|
// Major GC completed.
|
2021-08-05 12:13:30 +03:00
|
|
|
void NoteGCEnd();
|
2021-10-14 21:15:40 +03:00
|
|
|
|
2021-08-13 07:06:25 +03:00
|
|
|
// A timer fired, but then decided not to run a GC.
|
|
|
|
void NoteWontGC();
|
2020-10-15 18:00:53 +03:00
|
|
|
|
2021-10-14 21:15:38 +03:00
|
|
|
// This is invoked when we reach the actual cycle collection portion of the
|
|
|
|
// overall cycle collection.
|
2021-10-14 21:15:40 +03:00
|
|
|
void NoteCCBegin(CCReason aReason, TimeStamp aWhen);
|
2021-10-14 21:15:38 +03:00
|
|
|
|
|
|
|
// This is invoked when the whole process of collection is done -- i.e., CC
|
|
|
|
// preparation (eg ForgetSkippables) in addition to the CC itself. There
|
|
|
|
// really ought to be a separate name for the overall CC as opposed to the
|
|
|
|
// actual cycle collection portion.
|
|
|
|
void NoteCCEnd(TimeStamp aWhen);
|
|
|
|
|
2021-07-09 10:14:11 +03:00
|
|
|
void NoteGCSliceEnd(TimeDuration aSliceDuration) {
|
2021-05-20 02:26:17 +03:00
|
|
|
if (mMajorGCReason == JS::GCReason::NO_REASON) {
|
2021-06-02 17:07:18 +03:00
|
|
|
// Internally-triggered GCs do not wait for the parent's permission to
|
|
|
|
// proceed. This flag won't be checked during an incremental GC anyway,
|
|
|
|
// but it better reflects reality.
|
2021-05-20 02:26:17 +03:00
|
|
|
mReadyForMajorGC = true;
|
|
|
|
}
|
2021-06-02 17:07:18 +03:00
|
|
|
|
|
|
|
// Subsequent slices should be INTER_SLICE_GC unless they are triggered by
|
|
|
|
// something else that provides its own reason.
|
|
|
|
mMajorGCReason = JS::GCReason::INTER_SLICE_GC;
|
2021-07-09 10:14:11 +03:00
|
|
|
|
|
|
|
mGCUnnotifiedTotalTime += aSliceDuration;
|
2021-05-20 02:26:17 +03:00
|
|
|
}
|
|
|
|
|
2021-07-09 10:14:11 +03:00
|
|
|
bool GCRunnerFired(TimeStamp aDeadline);
|
2021-08-13 07:06:25 +03:00
|
|
|
bool GCRunnerFiredDoGC(TimeStamp aDeadline, const GCRunnerStep& aStep);
|
2021-07-09 10:14:11 +03:00
|
|
|
|
|
|
|
using MayGCPromise =
|
2021-08-13 07:06:25 +03:00
|
|
|
MozPromise<bool, mozilla::ipc::ResponseRejectReason, true>;
|
2021-07-09 10:14:11 +03:00
|
|
|
|
|
|
|
// Returns null if we shouldn't GC now (eg a GC is already running).
|
|
|
|
static RefPtr<MayGCPromise> MayGCNow(JS::GCReason reason);
|
|
|
|
|
2021-07-09 10:14:12 +03:00
|
|
|
// Check all of the various collector timers/runners and see if they are
|
|
|
|
// waiting to fire. This does not check the Full GC Timer, as that's a
|
|
|
|
// more expensive collection we run on a long timer.
|
|
|
|
void RunNextCollectorTimer(JS::GCReason aReason,
|
|
|
|
mozilla::TimeStamp aDeadline);
|
|
|
|
|
2020-10-15 18:01:11 +03:00
|
|
|
// When we decide to do a cycle collection but we're in the middle of an
|
|
|
|
// incremental GC, the CC is "locked out" until the GC completes -- unless
|
|
|
|
// the wait is too long, and we decide to finish the incremental GC early.
|
2020-12-04 19:03:07 +03:00
|
|
|
void BlockCC(TimeStamp aNow) {
|
2020-10-15 18:01:11 +03:00
|
|
|
MOZ_ASSERT(mInIncrementalGC);
|
2020-12-04 19:03:07 +03:00
|
|
|
MOZ_ASSERT(mCCBlockStart.IsNull());
|
2020-10-15 18:01:11 +03:00
|
|
|
mCCBlockStart = aNow;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UnblockCC() { mCCBlockStart = TimeStamp(); }
|
|
|
|
|
2020-10-15 18:02:12 +03:00
|
|
|
// Returns the number of purple buffer items that were processed and removed.
|
2021-07-09 10:14:15 +03:00
|
|
|
uint32_t NoteForgetSkippableComplete(TimeStamp aNow,
|
|
|
|
uint32_t aSuspectedBeforeForgetSkippable,
|
|
|
|
uint32_t aSuspectedCCObjects) {
|
2020-10-15 18:01:35 +03:00
|
|
|
mLastForgetSkippableEndTime = aNow;
|
2021-07-09 10:14:15 +03:00
|
|
|
mPreviousSuspectedCount = aSuspectedCCObjects;
|
2020-10-15 18:02:12 +03:00
|
|
|
mCleanupsSinceLastGC++;
|
2021-07-09 10:14:15 +03:00
|
|
|
return aSuspectedBeforeForgetSkippable - aSuspectedCCObjects;
|
2020-10-15 18:01:35 +03:00
|
|
|
}
|
|
|
|
|
2020-12-04 19:02:59 +03:00
|
|
|
// After collecting cycles, record the results that are used in scheduling
|
|
|
|
// decisions.
|
|
|
|
void NoteCycleCollected(const CycleCollectorResults& aResults) {
|
|
|
|
mCCollectedWaitingForGC += aResults.mFreedGCed;
|
|
|
|
mCCollectedZonesWaitingForGC += aResults.mFreedJSZones;
|
|
|
|
}
|
|
|
|
|
2021-10-14 21:15:38 +03:00
|
|
|
// Test if we are in the NoteCCBegin .. NoteCCEnd interval.
|
|
|
|
bool IsCollectingCycles() const { return mIsCollectingCycles; }
|
2020-10-15 18:01:48 +03:00
|
|
|
|
2020-10-15 18:02:12 +03:00
|
|
|
// The CC was abandoned without running a slice, so we only did forget
|
|
|
|
// skippables. Prevent running another cycle soon.
|
2021-07-09 10:14:15 +03:00
|
|
|
void NoteForgetSkippableOnlyCycle(TimeStamp aNow) {
|
|
|
|
mLastForgetSkippableCycleEndTime = aNow;
|
2020-10-15 18:01:48 +03:00
|
|
|
}
|
|
|
|
|
2021-07-09 10:14:14 +03:00
|
|
|
void Shutdown() {
|
|
|
|
mDidShutdown = true;
|
|
|
|
KillAllTimersAndRunners();
|
|
|
|
}
|
2020-12-04 19:02:59 +03:00
|
|
|
|
2020-10-15 18:01:11 +03:00
|
|
|
// Scheduling
|
2020-10-15 18:00:53 +03:00
|
|
|
|
2020-10-15 18:01:24 +03:00
|
|
|
// Return a budget along with a boolean saying whether to prefer to run short
|
|
|
|
// slices and stop rather than continuing to the next phase of cycle
|
|
|
|
// collection.
|
2021-07-09 10:14:15 +03:00
|
|
|
js::SliceBudget ComputeCCSliceBudget(TimeStamp aDeadline,
|
|
|
|
TimeStamp aCCBeginTime,
|
|
|
|
TimeStamp aPrevSliceEndTime,
|
|
|
|
TimeStamp aNow,
|
|
|
|
bool* aPreferShorterSlices) const;
|
2020-10-15 18:01:24 +03:00
|
|
|
|
2021-07-09 10:14:15 +03:00
|
|
|
TimeDuration ComputeInterSliceGCBudget(TimeStamp aDeadline,
|
|
|
|
TimeStamp aNow) const;
|
2020-11-13 00:59:07 +03:00
|
|
|
|
2021-07-09 10:14:15 +03:00
|
|
|
bool ShouldForgetSkippable(uint32_t aSuspectedCCObjects) const {
|
2020-10-15 18:02:12 +03:00
|
|
|
// Only do a forget skippable if there are more than a few new objects
|
|
|
|
// or we're doing the initial forget skippables.
|
2021-07-09 10:14:15 +03:00
|
|
|
return ((mPreviousSuspectedCount + 100) <= aSuspectedCCObjects) ||
|
2020-10-15 18:02:12 +03:00
|
|
|
mCleanupsSinceLastGC < kMajorForgetSkippableCalls;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is reason to suspect that there may be a significant amount of
|
|
|
|
// garbage to cycle collect: either we just finished a GC, or the purple
|
|
|
|
// buffer is getting really big, or it's getting somewhat big and it has been
|
|
|
|
// too long since the last CC.
|
2021-10-14 21:15:39 +03:00
|
|
|
CCReason IsCCNeeded(TimeStamp aNow, uint32_t aSuspectedCCObjects) const {
|
|
|
|
if (mNeedsFullCC != CCReason::NO_REASON) {
|
|
|
|
return mNeedsFullCC;
|
2021-10-14 01:32:58 +03:00
|
|
|
}
|
2021-10-14 21:15:39 +03:00
|
|
|
if (aSuspectedCCObjects > kCCPurpleLimit) {
|
|
|
|
return CCReason::MANY_SUSPECTED;
|
|
|
|
}
|
|
|
|
if (aSuspectedCCObjects > kCCForcedPurpleLimit && mLastCCEndTime &&
|
|
|
|
aNow - mLastCCEndTime > kCCForced) {
|
|
|
|
return CCReason::TIMED;
|
|
|
|
}
|
|
|
|
return CCReason::NO_REASON;
|
2020-10-15 18:02:12 +03:00
|
|
|
}
|
|
|
|
|
2021-10-14 21:15:39 +03:00
|
|
|
mozilla::CCReason ShouldScheduleCC(TimeStamp aNow,
|
|
|
|
uint32_t aSuspectedCCObjects) const;
|
2020-11-12 22:05:00 +03:00
|
|
|
|
2020-12-04 19:02:59 +03:00
|
|
|
// If we collected a substantial amount of cycles, poke the GC since more
|
|
|
|
// objects might be unreachable now.
|
|
|
|
bool NeedsGCAfterCC() const {
|
|
|
|
return mCCollectedWaitingForGC > 250 || mCCollectedZonesWaitingForGC > 0 ||
|
|
|
|
mLikelyShortLivingObjectsNeedingGC > 2500 || mNeedsGCAfterCC;
|
|
|
|
}
|
|
|
|
|
2020-12-04 19:03:07 +03:00
|
|
|
bool IsLastEarlyCCTimer(int32_t aCurrentFireCount) const {
|
2020-10-15 18:02:12 +03:00
|
|
|
int32_t numEarlyTimerFires =
|
|
|
|
std::max(int32_t(mCCDelay / kCCSkippableDelay) - 2, 1);
|
|
|
|
|
|
|
|
return aCurrentFireCount >= numEarlyTimerFires;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum class CCRunnerState {
|
|
|
|
Inactive,
|
2020-10-15 18:03:44 +03:00
|
|
|
ReducePurple,
|
|
|
|
CleanupChildless,
|
|
|
|
CleanupContentUnbinder,
|
|
|
|
CleanupDeferred,
|
2020-12-04 19:03:07 +03:00
|
|
|
StartCycleCollection,
|
2020-12-11 22:04:27 +03:00
|
|
|
CycleCollecting,
|
2020-12-11 22:04:58 +03:00
|
|
|
Canceled,
|
2020-12-11 22:04:27 +03:00
|
|
|
NumStates
|
2020-11-13 00:59:07 +03:00
|
|
|
};
|
2020-11-12 22:05:28 +03:00
|
|
|
|
2021-10-14 21:15:40 +03:00
|
|
|
void InitCCRunnerStateMachine(CCRunnerState initialState, CCReason aReason) {
|
2021-07-09 10:14:11 +03:00
|
|
|
if (mCCRunner) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-14 21:15:40 +03:00
|
|
|
MOZ_ASSERT(mCCReason == CCReason::NO_REASON);
|
|
|
|
mCCReason = aReason;
|
|
|
|
|
2020-12-11 22:04:58 +03:00
|
|
|
// The state machine should always have been deactivated after the previous
|
|
|
|
// collection, however far that collection may have gone.
|
|
|
|
MOZ_ASSERT(mCCRunnerState == CCRunnerState::Inactive,
|
|
|
|
"DeactivateCCRunner should have been called");
|
2020-12-04 19:03:07 +03:00
|
|
|
mCCRunnerState = initialState;
|
2020-12-11 22:04:58 +03:00
|
|
|
|
|
|
|
// Currently, there are only two entry points to the non-Inactive part of
|
|
|
|
// the state machine.
|
2020-12-04 19:03:07 +03:00
|
|
|
if (initialState == CCRunnerState::ReducePurple) {
|
|
|
|
mCCDelay = kCCDelay;
|
|
|
|
mCCRunnerEarlyFireCount = 0;
|
|
|
|
} else if (initialState == CCRunnerState::CycleCollecting) {
|
|
|
|
// Nothing needed.
|
|
|
|
} else {
|
|
|
|
MOZ_CRASH("Invalid initial state");
|
|
|
|
}
|
2020-11-12 22:05:28 +03:00
|
|
|
}
|
|
|
|
|
2021-10-14 21:15:40 +03:00
|
|
|
void DeactivateCCRunner() {
|
|
|
|
mCCRunnerState = CCRunnerState::Inactive;
|
|
|
|
mCCReason = CCReason::NO_REASON;
|
|
|
|
}
|
2020-11-12 22:05:28 +03:00
|
|
|
|
2021-08-05 12:13:31 +03:00
|
|
|
GCRunnerStep GetNextGCRunnerAction() const;
|
2021-04-30 02:12:04 +03:00
|
|
|
|
2021-07-09 10:14:15 +03:00
|
|
|
CCRunnerStep AdvanceCCRunner(TimeStamp aDeadline, TimeStamp aNow,
|
|
|
|
uint32_t aSuspectedCCObjects);
|
2020-11-12 22:05:28 +03:00
|
|
|
|
2020-11-13 00:59:07 +03:00
|
|
|
// aStartTimeStamp : when the ForgetSkippable timer fired. This may be some
|
|
|
|
// time ago, if an incremental GC needed to be finished.
|
|
|
|
js::SliceBudget ComputeForgetSkippableBudget(TimeStamp aStartTimeStamp,
|
2020-12-11 22:04:27 +03:00
|
|
|
TimeStamp aDeadline);
|
2020-11-12 22:05:28 +03:00
|
|
|
|
2020-12-04 19:03:07 +03:00
|
|
|
private:
|
2020-11-13 00:59:07 +03:00
|
|
|
// State
|
2020-11-12 22:05:28 +03:00
|
|
|
|
2020-11-13 00:59:07 +03:00
|
|
|
// An incremental GC is in progress, which blocks the CC from running for its
|
|
|
|
// duration (or until it goes too long and is finished synchronously.)
|
|
|
|
bool mInIncrementalGC = false;
|
2020-11-12 22:05:28 +03:00
|
|
|
|
2021-05-12 09:46:08 +03:00
|
|
|
// The parent process is ready for us to do a major GC.
|
|
|
|
bool mReadyForMajorGC = false;
|
|
|
|
|
2021-08-05 12:13:31 +03:00
|
|
|
// When a shrinking GC has been requested but we back-out, if this is true
|
|
|
|
// we run a non-shrinking GC.
|
|
|
|
bool mWantAtLeastRegularGC = false;
|
|
|
|
|
2020-11-13 00:59:07 +03:00
|
|
|
// When the CC started actually waiting for the GC to finish. This will be
|
|
|
|
// set to non-null at a later time than mCCLockedOut.
|
|
|
|
TimeStamp mCCBlockStart;
|
2020-12-04 19:02:59 +03:00
|
|
|
|
|
|
|
bool mDidShutdown = false;
|
|
|
|
|
2020-11-13 00:59:07 +03:00
|
|
|
TimeStamp mLastForgetSkippableEndTime;
|
|
|
|
uint32_t mForgetSkippableCounter = 0;
|
|
|
|
TimeStamp mForgetSkippableFrequencyStartTime;
|
|
|
|
TimeStamp mLastCCEndTime;
|
|
|
|
TimeStamp mLastForgetSkippableCycleEndTime;
|
2020-11-12 22:05:28 +03:00
|
|
|
|
2020-11-13 00:59:07 +03:00
|
|
|
CCRunnerState mCCRunnerState = CCRunnerState::Inactive;
|
|
|
|
int32_t mCCRunnerEarlyFireCount = 0;
|
|
|
|
TimeDuration mCCDelay = kCCDelay;
|
2020-12-04 19:02:59 +03:00
|
|
|
|
|
|
|
// Prevent the very first CC from running before we have GC'd and set the
|
|
|
|
// gray bits.
|
|
|
|
bool mHasRunGC = false;
|
|
|
|
|
2021-10-14 21:15:39 +03:00
|
|
|
mozilla::CCReason mNeedsFullCC = CCReason::NO_REASON;
|
2020-12-04 19:02:59 +03:00
|
|
|
bool mNeedsFullGC = true;
|
|
|
|
bool mNeedsGCAfterCC = false;
|
2020-11-13 00:59:07 +03:00
|
|
|
uint32_t mPreviousSuspectedCount = 0;
|
|
|
|
|
|
|
|
uint32_t mCleanupsSinceLastGC = UINT32_MAX;
|
|
|
|
|
2021-07-09 10:14:11 +03:00
|
|
|
TimeDuration mGCUnnotifiedTotalTime;
|
|
|
|
|
|
|
|
RefPtr<IdleTaskRunner> mGCRunner;
|
2021-07-09 10:14:11 +03:00
|
|
|
RefPtr<IdleTaskRunner> mCCRunner;
|
2021-07-09 10:14:11 +03:00
|
|
|
nsITimer* mShrinkingGCTimer = nullptr;
|
|
|
|
nsITimer* mFullGCTimer = nullptr;
|
|
|
|
|
2021-10-14 21:15:40 +03:00
|
|
|
mozilla::CCReason mCCReason = mozilla::CCReason::NO_REASON;
|
2021-05-20 02:26:17 +03:00
|
|
|
JS::GCReason mMajorGCReason = JS::GCReason::NO_REASON;
|
|
|
|
|
2021-07-09 10:14:14 +03:00
|
|
|
bool mIsCompactingOnUserInactive = false;
|
2021-10-14 21:15:38 +03:00
|
|
|
bool mIsCollectingCycles = false;
|
2021-07-09 10:14:14 +03:00
|
|
|
bool mUserIsActive = true;
|
|
|
|
|
2021-06-02 17:07:18 +03:00
|
|
|
public:
|
2020-12-04 19:02:59 +03:00
|
|
|
uint32_t mCCollectedWaitingForGC = 0;
|
|
|
|
uint32_t mCCollectedZonesWaitingForGC = 0;
|
|
|
|
uint32_t mLikelyShortLivingObjectsNeedingGC = 0;
|
|
|
|
|
2020-11-13 00:59:07 +03:00
|
|
|
// Configuration parameters
|
|
|
|
|
|
|
|
TimeDuration mActiveIntersliceGCBudget = TimeDuration::FromMilliseconds(5);
|
|
|
|
};
|
2020-11-12 22:05:28 +03:00
|
|
|
|
2020-10-15 18:00:53 +03:00
|
|
|
} // namespace mozilla
|