Bug 98084

Summary: Block freeing thread should sleep indefinitely when there's no work to do
Product: WebKit Reporter: Mark Hahnenberg <mhahnenberg>
Component: JavaScriptCoreAssignee: Mark Hahnenberg <mhahnenberg>
Status: RESOLVED FIXED    
Severity: Normal CC: ggaren, levin+threading, webkit.review.bot, yong.li.webkit
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch
none
Patch
none
Patch none

Description Mark Hahnenberg 2012-10-01 15:44:24 PDT
Current the block freeing thread wakes up once a second to check if there are any blocks for it to release back to the OS. This is wasteful. We should change it to sleep when it realizes there are no more blocks to free. Any thread that returns a block to the BlockAllocator should then notify the block freeing thread that there is more work to do now.
Comment 1 Mark Hahnenberg 2012-10-01 15:59:24 PDT
Created attachment 166562 [details]
Patch
Comment 2 Geoffrey Garen 2012-10-01 16:24:59 PDT
Comment on attachment 166562 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=166562&action=review

This fix looks correct, but I think it can get better.

> Source/JavaScriptCore/heap/BlockAllocator.h:111
> +        m_numberOfFreeBlocks++;

Can we just use m_numberOfFreeBlocks != 0, instead of a separate m_blocksNeedFreeing boolean, to tell if we need to free anything?

> Source/JavaScriptCore/heap/BlockAllocator.h:118
> +    MutexLocker mutexLocker(m_freeBlockConditionLock);
> +    if (!m_blocksNeedFreeing) {
> +        m_blocksNeedFreeing = true;
> +        m_freeBlockCondition.signal();
> +    }

Rather than doing this unconditionally, I'd prefer to see this done only if m_numberOfFreeBlocks was 0 when we acquired the lock.
Comment 3 Mark Hahnenberg 2012-10-01 18:29:46 PDT
Created attachment 166587 [details]
Patch
Comment 4 Geoffrey Garen 2012-10-01 21:11:55 PDT
Comment on attachment 166587 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=166587&action=review

r=me

> Source/JavaScriptCore/heap/BlockAllocator.h:115
> +    MutexLocker mutexLocker(m_freeBlockConditionLock);
> +    if (!numberOfFreeBlocks)

"mutexLocker" should be scoped inside "if (!numberOfFreeBlocks)", so we only acquire the mutex on the transition from zero to non-zero. Please make this change before landing.
Comment 5 Mark Hahnenberg 2012-10-02 14:24:04 PDT
Committed r130212: <http://trac.webkit.org/changeset/130212>
Comment 6 Mark Hahnenberg 2012-10-02 14:24:59 PDT
<rdar://problem/12350667>
Comment 7 Yong Li 2012-10-05 08:40:21 PDT
*** Bug 90622 has been marked as a duplicate of this bug. ***
Comment 8 Mark Hahnenberg 2012-10-16 23:40:46 PDT
Reopening because this wasn't completely fixed.
Comment 9 Mark Hahnenberg 2012-10-17 09:45:24 PDT
Created attachment 169199 [details]
Patch
Comment 10 Geoffrey Garen 2012-10-17 10:18:12 PDT
Comment on attachment 169199 [details]
Patch

r=me
Comment 11 WebKit Review Bot 2012-10-17 10:30:41 PDT
Comment on attachment 169199 [details]
Patch

Clearing flags on attachment: 169199

Committed r131619: <http://trac.webkit.org/changeset/131619>
Comment 12 WebKit Review Bot 2012-10-17 10:30:44 PDT
All reviewed patches have been landed.  Closing bug.