GCActivityCallbackCF.cpp [plain text]
#include "config.h"
#include "GCActivityCallback.h"
#include "APIShims.h"
#include "Heap.h"
#include "JSGlobalData.h"
#include "JSLock.h"
#include "JSObject.h"
#include "ScopeChain.h"
#include <wtf/RetainPtr.h>
#include <wtf/WTFThreadData.h>
#if !USE(CF)
#error "This file should only be used on CF platforms."
#endif
namespace JSC {
struct DefaultGCActivityCallbackPlatformData {
static void timerDidFire(CFRunLoopTimerRef, void *info);
RetainPtr<CFRunLoopTimerRef> timer;
RetainPtr<CFRunLoopRef> runLoop;
CFRunLoopTimerContext context;
double delay;
};
const double gcTimeSlicePerMB = 0.01; const double maxGCTimeSlice = 0.05; const double timerSlop = 2.0; const double pagingTimeOut = 0.1; const CFTimeInterval decade = 60 * 60 * 24 * 365 * 10;
const CFTimeInterval hour = 60 * 60;
void DefaultGCActivityCallbackPlatformData::timerDidFire(CFRunLoopTimerRef, void *info)
{
Heap* heap = static_cast<Heap*>(info);
APIEntryShim shim(heap->globalData());
#if !PLATFORM(IOS)
double startTime = WTF::monotonicallyIncreasingTime();
if (heap->isPagedOut(startTime + pagingTimeOut)) {
heap->activityCallback()->cancel();
heap->increaseLastGCLength(pagingTimeOut);
return;
}
#endif
heap->collectAllGarbage();
}
DefaultGCActivityCallback::DefaultGCActivityCallback(Heap* heap)
{
commonConstructor(heap, CFRunLoopGetCurrent());
}
DefaultGCActivityCallback::DefaultGCActivityCallback(Heap* heap, CFRunLoopRef runLoop)
{
commonConstructor(heap, runLoop);
}
DefaultGCActivityCallback::~DefaultGCActivityCallback()
{
CFRunLoopRemoveTimer(d->runLoop.get(), d->timer.get(), kCFRunLoopCommonModes);
CFRunLoopTimerInvalidate(d->timer.get());
}
void DefaultGCActivityCallback::commonConstructor(Heap* heap, CFRunLoopRef runLoop)
{
d = adoptPtr(new DefaultGCActivityCallbackPlatformData);
memset(&d->context, 0, sizeof(CFRunLoopTimerContext));
d->context.info = heap;
d->runLoop = runLoop;
d->timer.adoptCF(CFRunLoopTimerCreate(0, decade, decade, 0, 0, DefaultGCActivityCallbackPlatformData::timerDidFire, &d->context));
d->delay = decade;
CFRunLoopAddTimer(d->runLoop.get(), d->timer.get(), kCFRunLoopCommonModes);
}
static void scheduleTimer(DefaultGCActivityCallbackPlatformData* d, double newDelay)
{
if (newDelay * timerSlop > d->delay)
return;
double delta = d->delay - newDelay;
d->delay = newDelay;
CFRunLoopTimerSetNextFireDate(d->timer.get(), CFRunLoopTimerGetNextFireDate(d->timer.get()) - delta);
}
static void cancelTimer(DefaultGCActivityCallbackPlatformData* d)
{
d->delay = decade;
CFRunLoopTimerSetNextFireDate(d->timer.get(), CFAbsoluteTimeGetCurrent() + decade);
}
void DefaultGCActivityCallback::didAllocate(size_t bytes)
{
if (!bytes)
bytes = 1;
Heap* heap = static_cast<Heap*>(d->context.info);
double gcTimeSlice = std::min((static_cast<double>(bytes) / MB) * gcTimeSlicePerMB, maxGCTimeSlice);
double newDelay = heap->lastGCLength() / gcTimeSlice;
scheduleTimer(d.get(), newDelay);
}
void DefaultGCActivityCallback::willCollect()
{
cancelTimer(d.get());
}
void DefaultGCActivityCallback::synchronize()
{
if (CFRunLoopGetCurrent() == d->runLoop.get())
return;
CFRunLoopRemoveTimer(d->runLoop.get(), d->timer.get(), kCFRunLoopCommonModes);
d->runLoop = CFRunLoopGetCurrent();
CFRunLoopAddTimer(d->runLoop.get(), d->timer.get(), kCFRunLoopCommonModes);
}
void DefaultGCActivityCallback::cancel()
{
cancelTimer(d.get());
}
}