Bug 1678416 - Implement IdleTaskRunner::SetBudget() and add some comments. r=smaug

Differential Revision: https://phabricator.services.mozilla.com/D97670
This commit is contained in:
Steve Fink 2020-12-04 16:02:52 +00:00
Родитель 50dfc2cb3a
Коммит 68bc625380
3 изменённых файлов: 42 добавлений и 17 удалений

Просмотреть файл

@ -3,9 +3,9 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "js/SliceBudget.h"
#include "mozilla/MainThreadIdlePeriod.h"
#include "mozilla/Telemetry.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/MainThreadIdlePeriod.h"
#include "nsCycleCollector.h"
#include "nsJSEnvironment.h"
@ -200,7 +200,7 @@ class CCGCScheduler {
}
const TimeDuration maxSlice = TimeDuration::FromMilliseconds(
MainThreadIdlePeriod::GetLongIdlePeriod());
mozilla::MainThreadIdlePeriod::GetLongIdlePeriod());
// Try to make up for a delay in running this slice.
double sliceDelayMultiplier =

Просмотреть файл

@ -11,28 +11,28 @@
namespace mozilla {
already_AddRefed<IdleTaskRunner> IdleTaskRunner::Create(
const CallbackType& aCallback, const char* aRunnableName, uint32_t aDelay,
int64_t aBudget, bool aRepeating,
const CallbackType& aCallback, const char* aRunnableName,
uint32_t aMaxDelay, int64_t aNonIdleBudget, bool aRepeating,
const MayStopProcessingCallbackType& aMayStopProcessing) {
if (aMayStopProcessing && aMayStopProcessing()) {
return nullptr;
}
RefPtr<IdleTaskRunner> runner =
new IdleTaskRunner(aCallback, aRunnableName, aDelay, aBudget, aRepeating,
aMayStopProcessing);
new IdleTaskRunner(aCallback, aRunnableName, aMaxDelay, aNonIdleBudget,
aRepeating, aMayStopProcessing);
runner->Schedule(false); // Initial scheduling shouldn't use idle dispatch.
return runner.forget();
}
IdleTaskRunner::IdleTaskRunner(
const CallbackType& aCallback, const char* aRunnableName, uint32_t aDelay,
int64_t aBudget, bool aRepeating,
const CallbackType& aCallback, const char* aRunnableName,
uint32_t aMaxDelay, int64_t aNonIdleBudget, bool aRepeating,
const MayStopProcessingCallbackType& aMayStopProcessing)
: CancelableIdleRunnable(aRunnableName),
mCallback(aCallback),
mDelay(aDelay),
mBudget(TimeDuration::FromMilliseconds(aBudget)),
mDelay(aMaxDelay),
mBudget(TimeDuration::FromMilliseconds(aNonIdleBudget)),
mRepeating(aRepeating),
mTimerActive(false),
mMayStopProcessing(aMayStopProcessing),
@ -76,7 +76,11 @@ static void TimedOut(nsITimer* aTimer, void* aClosure) {
void IdleTaskRunner::SetDeadline(mozilla::TimeStamp aDeadline) {
mDeadline = aDeadline;
};
}
void IdleTaskRunner::SetBudget(int64_t aBudget) {
mBudget = TimeDuration::FromMilliseconds(aBudget);
}
void IdleTaskRunner::SetTimer(uint32_t aDelay, nsIEventTarget* aTarget) {
MOZ_ASSERT(NS_IsMainThread());
@ -136,8 +140,8 @@ void IdleTaskRunner::Schedule(bool aAllowIdleDispatch) {
// We weren't allowed to do idle dispatch immediately, do it after a
// short timeout.
mScheduleTimer->InitWithNamedFuncCallback(
ScheduleTimedOut, this, 16, nsITimer::TYPE_ONE_SHOT_LOW_PRIORITY,
mName);
ScheduleTimedOut, this, 16 /* ms */,
nsITimer::TYPE_ONE_SHOT_LOW_PRIORITY, mName);
}
}
}

Просмотреть файл

@ -30,23 +30,34 @@ class IdleTaskRunner final : public CancelableIdleRunnable {
using MayStopProcessingCallbackType = std::function<bool()>;
public:
// An IdleTaskRunner will attempt to run in idle time, with a budget computed
// based on a (capped) estimate for how much idle time is available. If there
// is no idle time within `aMaxDelay` ms, it will fall back to running using
// a specified `aNonIdleBudget`.
static already_AddRefed<IdleTaskRunner> Create(
const CallbackType& aCallback, const char* aRunnableName, uint32_t aDelay,
int64_t aBudget, bool aRepeating,
const CallbackType& aCallback, const char* aRunnableName,
uint32_t aMaxDelay, int64_t aNonIdleBudget, bool aRepeating,
const MayStopProcessingCallbackType& aMayStopProcessing);
NS_IMETHOD Run() override;
// (Used by the task triggering code.) Record the end of the current idle
// period, or null if not running during idle time.
void SetDeadline(mozilla::TimeStamp aDeadline) override;
void SetTimer(uint32_t aDelay, nsIEventTarget* aTarget) override;
// Update the non-idle time budgeted for this callback. This really only
// makes sense for a repeating runner.
void SetBudget(int64_t aBudget);
nsresult Cancel() override;
void Schedule(bool aAllowIdleDispatch);
private:
explicit IdleTaskRunner(
const CallbackType& aCallback, const char* aRunnableName, uint32_t aDelay,
int64_t aBudget, bool aRepeating,
const CallbackType& aCallback, const char* aRunnableName,
uint32_t aMaxDelay, int64_t aNonIdleBudget, bool aRepeating,
const MayStopProcessingCallbackType& aMayStopProcessing);
~IdleTaskRunner();
void CancelTimer();
@ -55,9 +66,19 @@ class IdleTaskRunner final : public CancelableIdleRunnable {
nsCOMPtr<nsITimer> mTimer;
nsCOMPtr<nsITimer> mScheduleTimer;
CallbackType mCallback;
// Wait this long for idle time before giving up and running a non-idle
// callback.
uint32_t mDelay;
// If running during idle time, the expected end of the current idle period.
// The null timestamp when the run is triggered by aMaxDelay instead of idle.
TimeStamp mDeadline;
// The expected amount of time the callback will run for, when not running
// during idle time.
TimeDuration mBudget;
bool mRepeating;
bool mTimerActive;
MayStopProcessingCallbackType mMayStopProcessing;