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;
DefaultGCActivityCallback* thisObject;
JSGlobalData* globalData;
Mutex shutdownMutex;
};
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)
{
DefaultGCActivityCallbackPlatformData* d = static_cast<DefaultGCActivityCallbackPlatformData*>(info);
d->shutdownMutex.lock();
if (!d->globalData) {
d->shutdownMutex.unlock();
delete d->thisObject;
return;
}
{
APIEntryShim shim(d->globalData, APIEntryShimWithoutLock::DontRefGlobalData);
Heap* heap = &d->globalData->heap;
#if !PLATFORM(IOS)
double startTime = WTF::monotonicallyIncreasingTime();
if (heap->isPagedOut(startTime + pagingTimeOut)) {
heap->activityCallback()->cancel();
heap->increaseLastGCLength(pagingTimeOut);
d->shutdownMutex.unlock();
return;
}
#endif
heap->collectAllGarbage();
}
d->shutdownMutex.unlock();
}
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 = d.get();
d->thisObject = this;
d->globalData = heap->globalData();
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);
}
void DefaultGCActivityCallback::invalidate()
{
d->globalData = 0;
CFRunLoopTimerSetNextFireDate(d->timer.get(), CFAbsoluteTimeGetCurrent() - decade);
}
void DefaultGCActivityCallback::didStartVMShutdown()
{
if (CFRunLoopGetCurrent() == d->runLoop.get()) {
invalidate();
delete this;
return;
}
ASSERT(!d->globalData->apiLock().currentThreadIsHoldingLock());
MutexLocker locker(d->shutdownMutex);
invalidate();
}
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<DefaultGCActivityCallbackPlatformData*>(d->context.info)->globalData->heap;
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());
}
}