2012-12-10 05:50:24 -08:00
|
|
|
/* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 8; -*- */
|
|
|
|
/* vim: set sw=2 sts=2 ts=8 et tw=80 : */
|
|
|
|
/* 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 "TaskThrottler.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace layers {
|
|
|
|
|
2013-07-09 16:26:08 -07:00
|
|
|
TaskThrottler::TaskThrottler(const TimeStamp& aTimeStamp)
|
2012-12-10 05:50:24 -08:00
|
|
|
: mOutstanding(false)
|
|
|
|
, mQueuedTask(nullptr)
|
2013-07-09 16:26:08 -07:00
|
|
|
, mStartTime(aTimeStamp)
|
2012-12-10 05:50:24 -08:00
|
|
|
{ }
|
|
|
|
|
|
|
|
void
|
|
|
|
TaskThrottler::PostTask(const tracked_objects::Location& aLocation,
|
2013-07-09 16:26:08 -07:00
|
|
|
CancelableTask* aTask, const TimeStamp& aTimeStamp)
|
2012-12-10 05:50:24 -08:00
|
|
|
{
|
|
|
|
aTask->SetBirthPlace(aLocation);
|
|
|
|
|
|
|
|
if (mOutstanding) {
|
|
|
|
if (mQueuedTask) {
|
|
|
|
mQueuedTask->Cancel();
|
|
|
|
}
|
|
|
|
mQueuedTask = aTask;
|
|
|
|
} else {
|
2013-07-09 16:26:08 -07:00
|
|
|
mStartTime = aTimeStamp;
|
2012-12-10 05:50:24 -08:00
|
|
|
aTask->Run();
|
|
|
|
delete aTask;
|
|
|
|
mOutstanding = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-09 16:26:08 -07:00
|
|
|
void
|
|
|
|
TaskThrottler::TaskComplete(const TimeStamp& aTimeStamp)
|
2012-12-10 05:50:24 -08:00
|
|
|
{
|
2013-07-09 16:26:08 -07:00
|
|
|
if (!mOutstanding) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the oldest sample we have if adding a new sample takes us over our
|
|
|
|
// desired number of samples.
|
2013-07-19 18:20:11 -07:00
|
|
|
if (mMaxDurations > 0) {
|
|
|
|
if (mDurations.Length() >= mMaxDurations) {
|
|
|
|
mDurations.RemoveElementAt(0);
|
|
|
|
}
|
|
|
|
mDurations.AppendElement(aTimeStamp - mStartTime);
|
2013-07-09 16:26:08 -07:00
|
|
|
}
|
|
|
|
|
2012-12-10 05:50:24 -08:00
|
|
|
if (mQueuedTask) {
|
2013-07-09 16:26:08 -07:00
|
|
|
mStartTime = aTimeStamp;
|
2012-12-10 05:50:24 -08:00
|
|
|
mQueuedTask->Run();
|
|
|
|
mQueuedTask = nullptr;
|
2013-07-09 16:26:08 -07:00
|
|
|
} else {
|
|
|
|
mOutstanding = false;
|
2012-12-10 05:50:24 -08:00
|
|
|
}
|
2013-07-09 16:26:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
TimeDuration
|
|
|
|
TaskThrottler::AverageDuration()
|
|
|
|
{
|
|
|
|
if (!mDurations.Length()) {
|
|
|
|
return TimeDuration();
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeDuration durationSum;
|
|
|
|
for (uint32_t i = 0; i < mDurations.Length(); i++) {
|
|
|
|
durationSum += mDurations[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return durationSum / mDurations.Length();
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeDuration
|
|
|
|
TaskThrottler::TimeSinceLastRequest(const TimeStamp& aTimeStamp)
|
|
|
|
{
|
|
|
|
return aTimeStamp - mStartTime;
|
2012-12-10 05:50:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|