SpaceTimeMutatorScheduler.cpp [plain text]
#include "config.h"
#include "SpaceTimeMutatorScheduler.h"
#include "JSCInlines.h"
namespace JSC {
class SpaceTimeMutatorScheduler::Snapshot {
public:
Snapshot(SpaceTimeMutatorScheduler& scheduler)
{
m_now = MonotonicTime::now();
m_bytesAllocatedThisCycle = scheduler.bytesAllocatedThisCycleImpl();
}
MonotonicTime now() const { return m_now; }
double bytesAllocatedThisCycle() const { return m_bytesAllocatedThisCycle; }
private:
MonotonicTime m_now;
double m_bytesAllocatedThisCycle;
};
SpaceTimeMutatorScheduler::SpaceTimeMutatorScheduler(Heap& heap)
: m_heap(heap)
, m_period(Seconds::fromMilliseconds(Options::concurrentGCPeriodMS()))
{
}
SpaceTimeMutatorScheduler::~SpaceTimeMutatorScheduler()
{
}
MutatorScheduler::State SpaceTimeMutatorScheduler::state() const
{
return m_state;
}
void SpaceTimeMutatorScheduler::beginCollection()
{
RELEASE_ASSERT(m_state == Normal);
m_state = Stopped;
m_startTime = MonotonicTime::now();
m_bytesAllocatedThisCycleAtTheBeginning = m_heap.m_bytesAllocatedThisCycle;
m_bytesAllocatedThisCycleAtTheEnd =
Options::concurrentGCMaxHeadroom() *
std::max<double>(m_bytesAllocatedThisCycleAtTheBeginning, m_heap.m_maxEdenSize);
}
void SpaceTimeMutatorScheduler::didStop()
{
RELEASE_ASSERT(m_state == Stopped || m_state == Resumed);
m_state = Stopped;
}
void SpaceTimeMutatorScheduler::willResume()
{
RELEASE_ASSERT(m_state == Stopped || m_state == Resumed);
m_state = Resumed;
}
void SpaceTimeMutatorScheduler::didExecuteConstraints()
{
m_startTime = MonotonicTime::now();
}
MonotonicTime SpaceTimeMutatorScheduler::timeToStop()
{
switch (m_state) {
case Normal:
return MonotonicTime::infinity();
case Stopped:
return MonotonicTime::now();
case Resumed: {
Snapshot snapshot(*this);
if (!shouldBeResumed(snapshot))
return snapshot.now();
return snapshot.now() - elapsedInPeriod(snapshot) + m_period;
} }
RELEASE_ASSERT_NOT_REACHED();
return MonotonicTime();
}
MonotonicTime SpaceTimeMutatorScheduler::timeToResume()
{
switch (m_state) {
case Normal:
case Resumed:
return MonotonicTime::now();
case Stopped: {
Snapshot snapshot(*this);
if (shouldBeResumed(snapshot))
return snapshot.now();
return snapshot.now() - elapsedInPeriod(snapshot) + m_period * collectorUtilization(snapshot);
} }
RELEASE_ASSERT_NOT_REACHED();
return MonotonicTime();
}
void SpaceTimeMutatorScheduler::log()
{
ASSERT(Options::logGC());
Snapshot snapshot(*this);
dataLog(
"a=", format("%.0lf", bytesSinceBeginningOfCycle(snapshot) / 1024), "kb ",
"hf=", format("%.3lf", headroomFullness(snapshot)), " ",
"mu=", format("%.3lf", mutatorUtilization(snapshot)), " ");
}
void SpaceTimeMutatorScheduler::endCollection()
{
m_state = Normal;
m_startTime = MonotonicTime::now();
}
double SpaceTimeMutatorScheduler::bytesAllocatedThisCycleImpl()
{
return m_heap.m_bytesAllocatedThisCycle;
}
double SpaceTimeMutatorScheduler::bytesSinceBeginningOfCycle(const Snapshot& snapshot)
{
return snapshot.bytesAllocatedThisCycle() - m_bytesAllocatedThisCycleAtTheBeginning;
}
double SpaceTimeMutatorScheduler::maxHeadroom()
{
return m_bytesAllocatedThisCycleAtTheEnd - m_bytesAllocatedThisCycleAtTheBeginning;
}
double SpaceTimeMutatorScheduler::headroomFullness(const Snapshot& snapshot)
{
double result = bytesSinceBeginningOfCycle(snapshot) / maxHeadroom();
if (!(result >= 0))
result = 0;
if (!(result <= 1))
result = 1;
return result;
}
double SpaceTimeMutatorScheduler::mutatorUtilization(const Snapshot& snapshot)
{
double mutatorUtilization = 1 - headroomFullness(snapshot);
mutatorUtilization =
Options::minimumMutatorUtilization() +
mutatorUtilization * (
Options::maximumMutatorUtilization() -
Options::minimumMutatorUtilization());
return mutatorUtilization;
}
double SpaceTimeMutatorScheduler::collectorUtilization(const Snapshot& snapshot)
{
return 1 - mutatorUtilization(snapshot);
}
Seconds SpaceTimeMutatorScheduler::elapsedInPeriod(const Snapshot& snapshot)
{
return (snapshot.now() - m_startTime) % m_period;
}
double SpaceTimeMutatorScheduler::phase(const Snapshot& snapshot)
{
return elapsedInPeriod(snapshot) / m_period;
}
bool SpaceTimeMutatorScheduler::shouldBeResumed(const Snapshot& snapshot)
{
return phase(snapshot) > collectorUtilization(snapshot);
}
}