Title: [249726] branches/safari-608.2.11.1-branch/Source/bmalloc
Revision
249726
Author
alanc...@apple.com
Date
2019-09-10 12:01:39 -0700 (Tue, 10 Sep 2019)

Log Message

Cherry-pick r249670. rdar://problem/55225974

    Revert to pre-r243144 scavenging behavior for macOS
    https://bugs.webkit.org/show_bug.cgi?id=201555

    Reviewed by Saam Barati.

    The change in r243144 regressed internal power metrics for some Mac models.

    * bmalloc/Heap.cpp:
    (bmalloc::Heap::scavenge):
    (bmalloc::Heap::scavengeToHighWatermark):
    (bmalloc::Heap::allocateSmallChunk):
    (bmalloc::Heap::allocateSmallPage):
    (bmalloc::Heap::allocateLarge):
    * bmalloc/Heap.h:
    * bmalloc/IsoDirectory.h:
    * bmalloc/IsoDirectoryInlines.h:
    (bmalloc::passedNumPages>::takeFirstEligible):
    (bmalloc::passedNumPages>::scavenge):
    (bmalloc::passedNumPages>::scavengeToHighWatermark):
    * bmalloc/IsoHeapImpl.h:
    * bmalloc/IsoHeapImplInlines.h:
    (bmalloc::IsoHeapImpl<Config>::scavengeToHighWatermark):
    * bmalloc/LargeMap.cpp:
    (bmalloc::LargeMap::add):
    * bmalloc/LargeRange.h:
    (bmalloc::LargeRange::LargeRange):
    (bmalloc::merge):
    * bmalloc/Scavenger.cpp:
    (bmalloc::Scavenger::Scavenger):
    (bmalloc::Scavenger::timeSinceLastPartialScavenge):
    (bmalloc::Scavenger::scavenge):
    (bmalloc::Scavenger::partialScavenge):
    (bmalloc::Scavenger::threadRunLoop):
    * bmalloc/Scavenger.h:
    * bmalloc/SmallPage.h:

    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@249670 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Modified Paths

Diff

Modified: branches/safari-608.2.11.1-branch/Source/bmalloc/ChangeLog (249725 => 249726)


--- branches/safari-608.2.11.1-branch/Source/bmalloc/ChangeLog	2019-09-10 18:43:28 UTC (rev 249725)
+++ branches/safari-608.2.11.1-branch/Source/bmalloc/ChangeLog	2019-09-10 19:01:39 UTC (rev 249726)
@@ -1,3 +1,83 @@
+2019-09-10  Alan Coon  <alanc...@apple.com>
+
+        Cherry-pick r249670. rdar://problem/55225974
+
+    Revert to pre-r243144 scavenging behavior for macOS
+    https://bugs.webkit.org/show_bug.cgi?id=201555
+    
+    Reviewed by Saam Barati.
+    
+    The change in r243144 regressed internal power metrics for some Mac models.
+    
+    * bmalloc/Heap.cpp:
+    (bmalloc::Heap::scavenge):
+    (bmalloc::Heap::scavengeToHighWatermark):
+    (bmalloc::Heap::allocateSmallChunk):
+    (bmalloc::Heap::allocateSmallPage):
+    (bmalloc::Heap::allocateLarge):
+    * bmalloc/Heap.h:
+    * bmalloc/IsoDirectory.h:
+    * bmalloc/IsoDirectoryInlines.h:
+    (bmalloc::passedNumPages>::takeFirstEligible):
+    (bmalloc::passedNumPages>::scavenge):
+    (bmalloc::passedNumPages>::scavengeToHighWatermark):
+    * bmalloc/IsoHeapImpl.h:
+    * bmalloc/IsoHeapImplInlines.h:
+    (bmalloc::IsoHeapImpl<Config>::scavengeToHighWatermark):
+    * bmalloc/LargeMap.cpp:
+    (bmalloc::LargeMap::add):
+    * bmalloc/LargeRange.h:
+    (bmalloc::LargeRange::LargeRange):
+    (bmalloc::merge):
+    * bmalloc/Scavenger.cpp:
+    (bmalloc::Scavenger::Scavenger):
+    (bmalloc::Scavenger::timeSinceLastPartialScavenge):
+    (bmalloc::Scavenger::scavenge):
+    (bmalloc::Scavenger::partialScavenge):
+    (bmalloc::Scavenger::threadRunLoop):
+    * bmalloc/Scavenger.h:
+    * bmalloc/SmallPage.h:
+    
+    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@249670 268f45cc-cd09-0410-ab3c-d52691b4dbfc
+
+    2019-09-09  Michael Saboff  <msab...@apple.com>
+
+            Revert to pre-r243144 scavenging behavior for macOS
+            https://bugs.webkit.org/show_bug.cgi?id=201555
+
+            Reviewed by Saam Barati.
+
+            The change in r243144 regressed internal power metrics for some Mac models.
+
+            * bmalloc/Heap.cpp:
+            (bmalloc::Heap::scavenge):
+            (bmalloc::Heap::scavengeToHighWatermark):
+            (bmalloc::Heap::allocateSmallChunk):
+            (bmalloc::Heap::allocateSmallPage):
+            (bmalloc::Heap::allocateLarge):
+            * bmalloc/Heap.h:
+            * bmalloc/IsoDirectory.h:
+            * bmalloc/IsoDirectoryInlines.h:
+            (bmalloc::passedNumPages>::takeFirstEligible):
+            (bmalloc::passedNumPages>::scavenge):
+            (bmalloc::passedNumPages>::scavengeToHighWatermark):
+            * bmalloc/IsoHeapImpl.h:
+            * bmalloc/IsoHeapImplInlines.h:
+            (bmalloc::IsoHeapImpl<Config>::scavengeToHighWatermark):
+            * bmalloc/LargeMap.cpp:
+            (bmalloc::LargeMap::add):
+            * bmalloc/LargeRange.h:
+            (bmalloc::LargeRange::LargeRange):
+            (bmalloc::merge):
+            * bmalloc/Scavenger.cpp:
+            (bmalloc::Scavenger::Scavenger):
+            (bmalloc::Scavenger::timeSinceLastPartialScavenge):
+            (bmalloc::Scavenger::scavenge):
+            (bmalloc::Scavenger::partialScavenge):
+            (bmalloc::Scavenger::threadRunLoop):
+            * bmalloc/Scavenger.h:
+            * bmalloc/SmallPage.h:
+
 2019-07-29  Alan Coon  <alanc...@apple.com>
 
         Cherry-pick r247714. rdar://problem/53647616

Modified: branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/Heap.cpp (249725 => 249726)


--- branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/Heap.cpp	2019-09-10 18:43:28 UTC (rev 249725)
+++ branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/Heap.cpp	2019-09-10 19:01:39 UTC (rev 249726)
@@ -175,7 +175,11 @@
 #endif
 }
 
+#if BPLATFORM(MAC)
+void Heap::scavenge(std::lock_guard<Mutex>& lock, BulkDecommit& decommitter)
+#else
 void Heap::scavenge(std::lock_guard<Mutex>& lock, BulkDecommit& decommitter, size_t& deferredDecommits)
+#endif
 {
     for (auto& list : m_freePages) {
         for (auto* chunk : list) {
@@ -182,11 +186,13 @@
             for (auto* page : chunk->freePages()) {
                 if (!page->hasPhysicalPages())
                     continue;
+#if !BPLATFORM(MAC)
                 if (page->usedSinceLastScavenge()) {
                     page->clearUsedSinceLastScavenge();
                     deferredDecommits++;
                     continue;
                 }
+#endif
 
                 size_t pageSize = bmalloc::pageSize(&list - &m_freePages[0]);
                 size_t decommitSize = physicalPageSizeSloppy(page->begin()->begin(), pageSize);
@@ -207,15 +213,37 @@
     }
 
     for (LargeRange& range : m_largeFree) {
+#if BPLATFORM(MAC)
+        m_highWatermark = std::min(m_highWatermark, static_cast<void*>(range.begin()));
+#else
         if (range.usedSinceLastScavenge()) {
             range.clearUsedSinceLastScavenge();
             deferredDecommits++;
             continue;
         }
+#endif
         decommitLargeRange(lock, range, decommitter);
     }
+
+#if BPLATFORM(MAC)
+    m_freeableMemory = 0;
+#endif
 }
 
+#if BPLATFORM(MAC)
+void Heap::scavengeToHighWatermark(std::lock_guard<Mutex>& lock, BulkDecommit& decommitter)
+{
+    void* newHighWaterMark = nullptr;
+    for (LargeRange& range : m_largeFree) {
+        if (range.begin() <= m_highWatermark)
+            newHighWaterMark = std::min(newHighWaterMark, static_cast<void*>(range.begin()));
+        else
+            decommitLargeRange(lock, range, decommitter);
+    }
+    m_highWatermark = newHighWaterMark;
+}
+#endif
+
 void Heap::deallocateLineCache(std::unique_lock<Mutex>&, LineCache& lineCache)
 {
     for (auto& list : lineCache) {
@@ -244,7 +272,9 @@
 
         forEachPage(chunk, pageSize, [&](SmallPage* page) {
             page->setHasPhysicalPages(true);
+#if !BPLATFORM(MAC)
             page->setUsedSinceLastScavenge();
+#endif
             page->setHasFreeLines(lock, true);
             chunk->freePages().push(page);
         });
@@ -324,7 +354,9 @@
             m_physicalPageMap.commit(page->begin()->begin(), pageSize);
 #endif
         }
+#if !BPLATFORM(MAC)
         page->setUsedSinceLastScavenge();
+#endif
 
         return page;
     }();
@@ -582,6 +614,9 @@
     m_freeableMemory -= range.totalPhysicalSize();
 
     void* result = splitAndAllocate(lock, range, alignment, size).begin();
+#if BPLATFORM(MAC)
+    m_highWatermark = std::max(m_highWatermark, result);
+#endif
     return result;
 }
 

Modified: branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/Heap.h (249725 => 249726)


--- branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/Heap.h	2019-09-10 18:43:28 UTC (rev 249725)
+++ branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/Heap.h	2019-09-10 19:01:39 UTC (rev 249726)
@@ -76,7 +76,12 @@
     size_t largeSize(std::unique_lock<Mutex>&, void*);
     void shrinkLarge(std::unique_lock<Mutex>&, const Range&, size_t);
 
+#if BPLATFORM(MAC)
+    void scavengeToHighWatermark(std::lock_guard<Mutex>&, BulkDecommit&);
+    void scavenge(std::lock_guard<Mutex>&, BulkDecommit&);
+#else
     void scavenge(std::lock_guard<Mutex>&, BulkDecommit&, size_t& deferredDecommits);
+#endif
     void scavenge(std::lock_guard<Mutex>&, BulkDecommit&, size_t& freed, size_t goal);
 
     size_t freeableMemory(std::lock_guard<Mutex>&);
@@ -152,6 +157,10 @@
 #if ENABLE_PHYSICAL_PAGE_MAP 
     PhysicalPageMap m_physicalPageMap;
 #endif
+    
+#if BPLATFORM(MAC)
+    void* m_highWatermark { nullptr };
+#endif
 };
 
 inline void Heap::allocateSmallBumpRanges(

Modified: branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/IsoDirectory.h (249725 => 249726)


--- branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/IsoDirectory.h	2019-09-10 18:43:28 UTC (rev 249725)
+++ branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/IsoDirectory.h	2019-09-10 19:01:39 UTC (rev 249726)
@@ -75,6 +75,9 @@
     // Iterate over all empty and committed pages, and put them into the vector. This also records the
     // pages as being decommitted. It's the caller's job to do the actual decommitting.
     void scavenge(Vector<DeferredDecommit>&);
+#if BPLATFORM(MAC)
+    void scavengeToHighWatermark(Vector<DeferredDecommit>&);
+#endif
 
     template<typename Func>
     void forEachCommittedPage(const Func&);
@@ -89,6 +92,9 @@
     Bits<numPages> m_committed;
     std::array<IsoPage<Config>*, numPages> m_pages;
     unsigned m_firstEligibleOrDecommitted { 0 };
+#if BPLATFORM(MAC)
+    unsigned m_highWatermark { 0 };
+#endif
 };
 
 } // namespace bmalloc

Modified: branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/IsoDirectoryInlines.h (249725 => 249726)


--- branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/IsoDirectoryInlines.h	2019-09-10 18:43:28 UTC (rev 249725)
+++ branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/IsoDirectoryInlines.h	2019-09-10 19:01:39 UTC (rev 249726)
@@ -52,6 +52,10 @@
     if (pageIndex >= numPages)
         return EligibilityKind::Full;
 
+#if BPLATFORM(MAC)
+    m_highWatermark = std::max(pageIndex, m_highWatermark);
+#endif
+
     Scavenger& scavenger = *Scavenger::get();
     scavenger.didStartGrowing();
     
@@ -144,9 +148,25 @@
         [&] (size_t index) {
             scavengePage(index, decommits);
         });
+#if BPLATFORM(MAC)
+    m_highWatermark = 0;
+#endif
 }
 
+#if BPLATFORM(MAC)
 template<typename Config, unsigned passedNumPages>
+void IsoDirectory<Config, passedNumPages>::scavengeToHighWatermark(Vector<DeferredDecommit>& decommits)
+{
+    (m_empty & m_committed).forEachSetBit(
+        [&] (size_t index) {
+            if (index > m_highWatermark)
+                scavengePage(index, decommits);
+        });
+    m_highWatermark = 0;
+}
+#endif
+
+template<typename Config, unsigned passedNumPages>
 template<typename Func>
 void IsoDirectory<Config, passedNumPages>::forEachCommittedPage(const Func& func)
 {

Modified: branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/IsoHeapImpl.h (249725 => 249726)


--- branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/IsoHeapImpl.h	2019-09-10 18:43:28 UTC (rev 249725)
+++ branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/IsoHeapImpl.h	2019-09-10 19:01:39 UTC (rev 249726)
@@ -46,6 +46,9 @@
     virtual ~IsoHeapImplBase();
     
     virtual void scavenge(Vector<DeferredDecommit>&) = 0;
+#if BPLATFORM(MAC)
+    virtual void scavengeToHighWatermark(Vector<DeferredDecommit>&) = 0;
+#endif
     virtual size_t freeableMemory() = 0;
     virtual size_t footprint() = 0;
     
@@ -84,6 +87,9 @@
     void didBecomeEligibleOrDecommited(IsoDirectory<Config, IsoDirectoryPage<Config>::numPages>*);
     
     void scavenge(Vector<DeferredDecommit>&) override;
+#if BPLATFORM(MAC)
+    void scavengeToHighWatermark(Vector<DeferredDecommit>&) override;
+#endif
 
     size_t freeableMemory() override;
 

Modified: branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/IsoHeapImplInlines.h (249725 => 249726)


--- branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/IsoHeapImplInlines.h	2019-09-10 18:43:28 UTC (rev 249725)
+++ branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/IsoHeapImplInlines.h	2019-09-10 19:01:39 UTC (rev 249726)
@@ -111,7 +111,22 @@
     m_directoryHighWatermark = 0;
 }
 
+#if BPLATFORM(MAC)
 template<typename Config>
+void IsoHeapImpl<Config>::scavengeToHighWatermark(Vector<DeferredDecommit>& decommits)
+{
+    std::lock_guard<Mutex> locker(this->lock);
+    if (!m_directoryHighWatermark)
+        m_inlineDirectory.scavengeToHighWatermark(decommits);
+    for (IsoDirectoryPage<Config>* page = m_headDirectory; page; page = page->next) {
+        if (page->index() >= m_directoryHighWatermark)
+            page->payload.scavengeToHighWatermark(decommits);
+    }
+    m_directoryHighWatermark = 0;
+}
+#endif
+
+template<typename Config>
 size_t IsoHeapImpl<Config>::freeableMemory()
 {
     return m_freeableMemory;

Modified: branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/LargeMap.cpp (249725 => 249726)


--- branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/LargeMap.cpp	2019-09-10 18:43:28 UTC (rev 249725)
+++ branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/LargeMap.cpp	2019-09-10 19:01:39 UTC (rev 249726)
@@ -76,7 +76,9 @@
         merged = merge(merged, m_free.pop(i--));
     }
 
+#if !BPLATFORM(MAC)
     merged.setUsedSinceLastScavenge();
+#endif
     m_free.push(merged);
 }
 

Modified: branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/LargeRange.h (249725 => 249726)


--- branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/LargeRange.h	2019-09-10 18:43:28 UTC (rev 249725)
+++ branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/LargeRange.h	2019-09-10 19:01:39 UTC (rev 249726)
@@ -37,8 +37,10 @@
         : Range()
         , m_startPhysicalSize(0)
         , m_totalPhysicalSize(0)
+#if !BPLATFORM(MAC)
         , m_isEligible(true)
         , m_usedSinceLastScavenge(false)
+#endif
     {
     }
 
@@ -46,13 +48,25 @@
         : Range(other)
         , m_startPhysicalSize(startPhysicalSize)
         , m_totalPhysicalSize(totalPhysicalSize)
+#if !BPLATFORM(MAC)
         , m_isEligible(true)
         , m_usedSinceLastScavenge(false)
+#endif
     {
         BASSERT(this->size() >= this->totalPhysicalSize());
         BASSERT(this->totalPhysicalSize() >= this->startPhysicalSize());
     }
 
+#if BPLATFORM(MAC)
+    LargeRange(void* begin, size_t size, size_t startPhysicalSize, size_t totalPhysicalSize)
+        : Range(begin, size)
+        , m_startPhysicalSize(startPhysicalSize)
+        , m_totalPhysicalSize(totalPhysicalSize)
+    {
+        BASSERT(this->size() >= this->totalPhysicalSize());
+        BASSERT(this->totalPhysicalSize() >= this->startPhysicalSize());
+    }
+#else
     LargeRange(void* begin, size_t size, size_t startPhysicalSize, size_t totalPhysicalSize, bool usedSinceLastScavenge = false)
         : Range(begin, size)
         , m_startPhysicalSize(startPhysicalSize)
@@ -63,6 +77,7 @@
         BASSERT(this->size() >= this->totalPhysicalSize());
         BASSERT(this->totalPhysicalSize() >= this->startPhysicalSize());
     }
+#endif
 
     // Returns a lower bound on physical size at the start of the range. Ranges that
     // span non-physical fragments use this number to remember the physical size of
@@ -89,9 +104,11 @@
     void setEligible(bool eligible) { m_isEligible = eligible; }
     bool isEligibile() const { return m_isEligible; }
 
+#if !BPLATFORM(MAC)
     bool usedSinceLastScavenge() const { return m_usedSinceLastScavenge; }
     void clearUsedSinceLastScavenge() { m_usedSinceLastScavenge = false; }
     void setUsedSinceLastScavenge() { m_usedSinceLastScavenge = true; }
+#endif
 
     bool operator<(const void* other) const { return begin() < other; }
     bool operator<(const LargeRange& other) const { return begin() < other.begin(); }
@@ -99,8 +116,12 @@
 private:
     size_t m_startPhysicalSize;
     size_t m_totalPhysicalSize;
+#if BPLATFORM(MAC)
+    bool m_isEligible { true };
+#else
     unsigned m_isEligible: 1;
     unsigned m_usedSinceLastScavenge: 1;
+#endif
 };
 
 inline bool canMerge(const LargeRange& a, const LargeRange& b)
@@ -123,14 +144,20 @@
 inline LargeRange merge(const LargeRange& a, const LargeRange& b)
 {
     const LargeRange& left = std::min(a, b);
+#if !BPLATFORM(MAC)
     bool mergedUsedSinceLastScavenge = a.usedSinceLastScavenge() || b.usedSinceLastScavenge();
+#endif
     if (left.size() == left.startPhysicalSize()) {
         return LargeRange(
             left.begin(),
             a.size() + b.size(),
             a.startPhysicalSize() + b.startPhysicalSize(),
-            a.totalPhysicalSize() + b.totalPhysicalSize(),
-            mergedUsedSinceLastScavenge);
+            a.totalPhysicalSize() + b.totalPhysicalSize()
+#if !BPLATFORM(MAC)
+            , mergedUsedSinceLastScavenge
+#endif
+        );
+        
     }
 
     return LargeRange(
@@ -137,8 +164,11 @@
         left.begin(),
         a.size() + b.size(),
         left.startPhysicalSize(),
-        a.totalPhysicalSize() + b.totalPhysicalSize(),
-        mergedUsedSinceLastScavenge);
+        a.totalPhysicalSize() + b.totalPhysicalSize()
+#if !BPLATFORM(MAC)
+        , mergedUsedSinceLastScavenge
+#endif
+    );
 }
 
 inline std::pair<LargeRange, LargeRange> LargeRange::split(size_t leftSize) const

Modified: branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/Scavenger.cpp (249725 => 249726)


--- branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/Scavenger.cpp	2019-09-10 18:43:28 UTC (rev 249725)
+++ branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/Scavenger.cpp	2019-09-10 19:01:39 UTC (rev 249726)
@@ -80,7 +80,11 @@
     dispatch_resume(m_pressureHandlerDispatchSource);
     dispatch_release(queue);
 #endif
+#if BPLATFORM(MAC)
+    m_waitTime = std::chrono::milliseconds(m_isInMiniMode ? 200 : 2000);
+#else
     m_waitTime = std::chrono::milliseconds(10);
+#endif
 
     m_thread = std::thread(&threadEntryPoint, this);
 }
@@ -178,6 +182,14 @@
     return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - m_lastFullScavengeTime);
 }
 
+#if BPLATFORM(MAC)
+std::chrono::milliseconds Scavenger::timeSinceLastPartialScavenge()
+{
+    std::unique_lock<Mutex> lock(mutex());
+    return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - m_lastPartialScavengeTime);
+}
+#endif
+
 void Scavenger::enableMiniMode()
 {
     m_isInMiniMode = true; // We just store to this racily. The scavenger thread will eventually pick up the right value.
@@ -200,17 +212,25 @@
 
         {
             PrintTime printTime("\nfull scavenge under lock time");
+#if !BPLATFORM(MAC)
             size_t deferredDecommits = 0;
+#endif
             std::lock_guard<Mutex> lock(Heap::mutex());
             for (unsigned i = numHeaps; i--;) {
                 if (!isActiveHeapKind(static_cast<HeapKind>(i)))
                     continue;
+#if BPLATFORM(MAC)
+                PerProcess<PerHeapKind<Heap>>::get()->at(i).scavenge(lock, decommitter);
+#else
                 PerProcess<PerHeapKind<Heap>>::get()->at(i).scavenge(lock, decommitter, deferredDecommits);
+#endif
             }
             decommitter.processEager();
 
+#if !BPLATFORM(MAC)
             if (deferredDecommits)
                 m_state = State::RunSoon;
+#endif
         }
 
         {
@@ -251,6 +271,75 @@
     }
 }
 
+#if BPLATFORM(MAC)
+void Scavenger::partialScavenge()
+{
+    std::unique_lock<Mutex> lock(m_scavengingMutex);
+
+    if (verbose) {
+        fprintf(stderr, "--------------------------------\n");
+        fprintf(stderr, "--before partial scavenging--\n");
+        dumpStats();
+    }
+
+    {
+        BulkDecommit decommitter;
+        {
+            PrintTime printTime("\npartialScavenge under lock time");
+            std::lock_guard<Mutex> lock(Heap::mutex());
+            for (unsigned i = numHeaps; i--;) {
+                if (!isActiveHeapKind(static_cast<HeapKind>(i)))
+                    continue;
+                Heap& heap = PerProcess<PerHeapKind<Heap>>::get()->at(i);
+                size_t freeableMemory = heap.freeableMemory(lock);
+                if (freeableMemory < 4 * MB)
+                    continue;
+                heap.scavengeToHighWatermark(lock, decommitter);
+            }
+
+            decommitter.processEager();
+        }
+
+        {
+            PrintTime printTime("partialScavenge lazy decommit time");
+            decommitter.processLazy();
+        }
+
+        {
+            PrintTime printTime("partialScavenge mark all as eligible time");
+            std::lock_guard<Mutex> lock(Heap::mutex());
+            for (unsigned i = numHeaps; i--;) {
+                if (!isActiveHeapKind(static_cast<HeapKind>(i)))
+                    continue;
+                Heap& heap = PerProcess<PerHeapKind<Heap>>::get()->at(i);
+                heap.markAllLargeAsEligibile(lock);
+            }
+        }
+    }
+
+    {
+        RELEASE_BASSERT(!m_deferredDecommits.size());
+        AllIsoHeaps::get()->forEach(
+            [&] (IsoHeapImplBase& heap) {
+                heap.scavengeToHighWatermark(m_deferredDecommits);
+            });
+        IsoHeapImplBase::finishScavenging(m_deferredDecommits);
+        m_deferredDecommits.shrink(0);
+    }
+
+    if (verbose) {
+        fprintf(stderr, "--after partial scavenging--\n");
+        dumpStats();
+        fprintf(stderr, "--------------------------------\n");
+    }
+
+    {
+        std::unique_lock<Mutex> lock(mutex());
+        m_lastPartialScavengeTime = std::chrono::steady_clock::now();
+    }
+}
+#endif
+
 size_t Scavenger::freeableMemory()
 {
     size_t result = 0;
@@ -332,6 +421,69 @@
             fprintf(stderr, "--------------------------------\n");
         }
 
+#if BPLATFORM(MAC)
+        enum class ScavengeMode {
+            None,
+            Partial,
+            Full
+        };
+
+        size_t freeableMemory = this->freeableMemory();
+
+        ScavengeMode scavengeMode = [&] {
+            auto timeSinceLastFullScavenge = this->timeSinceLastFullScavenge();
+            auto timeSinceLastPartialScavenge = this->timeSinceLastPartialScavenge();
+            auto timeSinceLastScavenge = std::min(timeSinceLastPartialScavenge, timeSinceLastFullScavenge);
+
+            if (isUnderMemoryPressure() && freeableMemory > 1 * MB && timeSinceLastScavenge > std::chrono::milliseconds(5))
+                return ScavengeMode::Full;
+
+            if (!m_isProbablyGrowing) {
+                if (timeSinceLastFullScavenge < std::chrono::milliseconds(1000) && !m_isInMiniMode)
+                    return ScavengeMode::Partial;
+                return ScavengeMode::Full;
+            }
+
+            if (m_isInMiniMode) {
+                if (timeSinceLastFullScavenge < std::chrono::milliseconds(200))
+                    return ScavengeMode::Partial;
+                return ScavengeMode::Full;
+            }
+
+#if BCPU(X86_64)
+            auto partialScavengeInterval = std::chrono::milliseconds(12000);
+#else
+            auto partialScavengeInterval = std::chrono::milliseconds(8000);
+#endif
+            if (timeSinceLastScavenge < partialScavengeInterval) {
+                // Rate limit partial scavenges.
+                return ScavengeMode::None;
+            }
+            if (freeableMemory < 25 * MB)
+                return ScavengeMode::None;
+            if (5 * freeableMemory < footprint())
+                return ScavengeMode::None;
+            return ScavengeMode::Partial;
+        }();
+
+        m_isProbablyGrowing = false;
+
+        switch (scavengeMode) {
+        case ScavengeMode::None: {
+            runSoon();
+            break;
+        }
+        case ScavengeMode::Partial: {
+            partialScavenge();
+            runSoon();
+            break;
+        }
+        case ScavengeMode::Full: {
+            scavenge();
+            break;
+        }
+        }
+#else
         std::chrono::steady_clock::time_point start { std::chrono::steady_clock::now() };
         
         scavenge();
@@ -357,6 +509,7 @@
 
         if (verbose)
             fprintf(stderr, "new wait time %lldms\n", static_cast<long long int>(m_waitTime.count()));
+#endif
     }
 }
 

Modified: branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/Scavenger.h (249725 => 249726)


--- branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/Scavenger.h	2019-09-10 18:43:28 UTC (rev 249725)
+++ branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/Scavenger.h	2019-09-10 19:01:39 UTC (rev 249726)
@@ -89,6 +89,10 @@
     void setThreadName(const char*);
 
     std::chrono::milliseconds timeSinceLastFullScavenge();
+#if BPLATFORM(MAC)
+    std::chrono::milliseconds timeSinceLastPartialScavenge();
+    void partialScavenge();
+#endif
 
     std::atomic<State> m_state { State::Sleep };
     size_t m_scavengerBytes { 0 };
@@ -101,7 +105,10 @@
 
     std::thread m_thread;
     std::chrono::steady_clock::time_point m_lastFullScavengeTime { std::chrono::steady_clock::now() };
-    
+#if BPLATFORM(MAC)
+    std::chrono::steady_clock::time_point m_lastPartialScavengeTime { std::chrono::steady_clock::now() };
+#endif
+
 #if BOS(DARWIN)
     dispatch_source_t m_pressureHandlerDispatchSource;
     qos_class_t m_requestedScavengerThreadQOSClass { QOS_CLASS_USER_INITIATED };

Modified: branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/SmallPage.h (249725 => 249726)


--- branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/SmallPage.h	2019-09-10 18:43:28 UTC (rev 249725)
+++ branches/safari-608.2.11.1-branch/Source/bmalloc/bmalloc/SmallPage.h	2019-09-10 19:01:39 UTC (rev 249726)
@@ -50,10 +50,12 @@
     
     bool hasPhysicalPages() { return m_hasPhysicalPages; }
     void setHasPhysicalPages(bool hasPhysicalPages) { m_hasPhysicalPages = hasPhysicalPages; }
-    
+
+#if !BPLATFORM(MAC)
     bool usedSinceLastScavenge() { return m_usedSinceLastScavenge; }
     void clearUsedSinceLastScavenge() { m_usedSinceLastScavenge = false; }
     void setUsedSinceLastScavenge() { m_usedSinceLastScavenge = true; }
+#endif
 
     SmallLine* begin();
 
@@ -63,7 +65,9 @@
 private:
     unsigned char m_hasFreeLines: 1;
     unsigned char m_hasPhysicalPages: 1;
+#if !BPLATFORM(MAC)
     unsigned char m_usedSinceLastScavenge: 1;
+#endif
     unsigned char m_refCount: 7;
     unsigned char m_sizeClass;
     unsigned char m_slide;
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to