Bug 926678 - Ensure GC gets triggered when gcMallocBytes drops below zero r=billm

This commit is contained in:
Jon Coppeard 2013-10-16 09:45:27 +01:00
Родитель 31536f0773
Коммит df837cc40e
5 изменённых файлов: 41 добавлений и 26 удалений

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

@ -127,7 +127,14 @@ Zone::setGCMaxMallocBytes(size_t value)
void
Zone::onTooMuchMalloc()
{
TriggerZoneGC(this, gcreason::TOO_MUCH_MALLOC);
if (TriggerZoneGC(this, gcreason::TOO_MUCH_MALLOC)) {
/*
* Set gcMallocBytes to stop updateMallocCounter() calling this method
* again before the counter is reset by GC.
*/
gcMallocBytes = PTRDIFF_MAX;
}
}
void

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

@ -272,10 +272,8 @@ struct Zone : public JS::shadow::Zone,
* Note: this code may be run from worker threads. We
* tolerate any thread races when updating gcMallocBytes.
*/
ptrdiff_t oldCount = gcMallocBytes;
ptrdiff_t newCount = oldCount - ptrdiff_t(nbytes);
gcMallocBytes = newCount;
if (JS_UNLIKELY(newCount <= 0 && oldCount > 0))
gcMallocBytes -= ptrdiff_t(nbytes);
if (JS_UNLIKELY(isTooMuchMalloc()))
onTooMuchMalloc();
}

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

@ -1930,29 +1930,31 @@ TriggerOperationCallback(JSRuntime *rt, JS::gcreason::Reason reason)
rt->triggerOperationCallback(JSRuntime::TriggerCallbackMainThread);
}
void
bool
js::TriggerGC(JSRuntime *rt, JS::gcreason::Reason reason)
{
/* Wait till end of parallel section to trigger GC. */
if (InParallelSection()) {
ForkJoinSlice::Current()->requestGC(reason);
return;
return true;
}
/* Don't trigger GCs when allocating under the operation callback lock. */
if (rt->currentThreadOwnsOperationCallbackLock())
return;
return false;
JS_ASSERT(CurrentThreadCanAccessRuntime(rt));
if (rt->isHeapBusy())
return;
/* GC is already running. */
if (rt->isHeapCollecting())
return true;
JS::PrepareForFullGC(rt);
TriggerOperationCallback(rt, reason);
return true;
}
void
bool
js::TriggerZoneGC(Zone *zone, JS::gcreason::Reason reason)
{
/*
@ -1961,35 +1963,37 @@ js::TriggerZoneGC(Zone *zone, JS::gcreason::Reason reason)
*/
if (InParallelSection()) {
ForkJoinSlice::Current()->requestZoneGC(zone, reason);
return;
return true;
}
/* Zones in use by a thread with an exclusive context can't be collected. */
if (zone->usedByExclusiveThread)
return;
return false;
JSRuntime *rt = zone->runtimeFromMainThread();
/* Don't trigger GCs when allocating under the operation callback lock. */
if (rt->currentThreadOwnsOperationCallbackLock())
return;
return false;
if (rt->isHeapBusy())
return;
/* GC is already running. */
if (rt->isHeapCollecting())
return true;
if (rt->gcZeal() == ZealAllocValue) {
TriggerGC(rt, reason);
return;
return true;
}
if (rt->isAtomsZone(zone)) {
/* We can't do a zone GC of the atoms compartment. */
TriggerGC(rt, reason);
return;
return true;
}
PrepareZoneForGC(zone);
TriggerOperationCallback(rt, reason);
return true;
}
void

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

@ -687,11 +687,11 @@ extern void
TraceRuntime(JSTracer *trc);
/* Must be called with GC lock taken. */
extern void
extern bool
TriggerGC(JSRuntime *rt, JS::gcreason::Reason reason);
/* Must be called with GC lock taken. */
extern void
extern bool
TriggerZoneGC(Zone *zone, JS::gcreason::Reason reason);
extern void

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

@ -724,10 +724,8 @@ void
JSRuntime::updateMallocCounter(JS::Zone *zone, size_t nbytes)
{
/* We tolerate any thread races when updating gcMallocBytes. */
ptrdiff_t oldCount = gcMallocBytes;
ptrdiff_t newCount = oldCount - ptrdiff_t(nbytes);
gcMallocBytes = newCount;
if (JS_UNLIKELY(newCount <= 0 && oldCount > 0))
gcMallocBytes -= ptrdiff_t(nbytes);
if (JS_UNLIKELY(gcMallocBytes <= 0))
onTooMuchMalloc();
else if (zone)
zone->updateMallocCounter(nbytes);
@ -736,8 +734,16 @@ JSRuntime::updateMallocCounter(JS::Zone *zone, size_t nbytes)
JS_FRIEND_API(void)
JSRuntime::onTooMuchMalloc()
{
if (CurrentThreadCanAccessRuntime(this))
TriggerGC(this, JS::gcreason::TOO_MUCH_MALLOC);
if (!CurrentThreadCanAccessRuntime(this))
return;
if (TriggerGC(this, JS::gcreason::TOO_MUCH_MALLOC)) {
/*
* Set gcMallocBytes to stop updateMallocCounter() calling this method
* again before the counter is reset by GC.
*/
gcMallocBytes = PTRDIFF_MAX;
}
}
JS_FRIEND_API(void *)