bmalloc should honor the FastMalloc statistics API
authorggaren@apple.com <ggaren@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 6 Sep 2014 00:16:18 +0000 (00:16 +0000)
committerggaren@apple.com <ggaren@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sat, 6 Sep 2014 00:16:18 +0000 (00:16 +0000)
https://bugs.webkit.org/show_bug.cgi?id=136592

Reviewed by Gavin Barraclough.

Source/bmalloc:

We do this by tracking "size" and "capacity" in the VM heap.

The VM heap's "capacity" is all the VM we ever allocated.

The VM heap's "size" the subset of VM currently held onto by the
VM heap (and therefore not in use by the regular heap).

Somewhat ironically, reducing the process's memory footprint, increases
the size of the VM heap, since the VM heap holds the pages that are
purely virtual and not physical.

* bmalloc/Heap.cpp:
(bmalloc::Heap::size):
(bmalloc::Heap::capacity):
* bmalloc/Heap.h:
* bmalloc/VMHeap.cpp:
(bmalloc::VMHeap::VMHeap):
(bmalloc::VMHeap::allocateSmallChunk):
(bmalloc::VMHeap::allocateMediumChunk):
(bmalloc::VMHeap::allocateLargeChunk):
* bmalloc/VMHeap.h:
(bmalloc::VMHeap::size):
(bmalloc::VMHeap::capacity):
(bmalloc::VMHeap::allocateSmallPage):
(bmalloc::VMHeap::allocateMediumPage):
(bmalloc::VMHeap::allocateLargeRange):
(bmalloc::VMHeap::deallocateSmallPage):
(bmalloc::VMHeap::deallocateMediumPage):
(bmalloc::VMHeap::deallocateLargeRange):
* bmalloc/bmalloc.h:
(bmalloc::api::heapSize):
(bmalloc::api::heapCapacity):

Source/WTF:

* wtf/FastMalloc.cpp:
(WTF::fastMallocStatistics):

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

Source/WTF/ChangeLog
Source/WTF/wtf/FastMalloc.cpp
Source/bmalloc/ChangeLog
Source/bmalloc/bmalloc/Heap.cpp
Source/bmalloc/bmalloc/Heap.h
Source/bmalloc/bmalloc/VMHeap.cpp
Source/bmalloc/bmalloc/VMHeap.h
Source/bmalloc/bmalloc/bmalloc.h

index 3163c1f1821f25b63fc041a51a1abdc3431c168c..93c89b77ec646078d79412f8a194b96586fd9a25 100644 (file)
@@ -1,3 +1,13 @@
+2014-09-05  Geoffrey Garen  <ggaren@apple.com>
+
+        bmalloc should honor the FastMalloc statistics API
+        https://bugs.webkit.org/show_bug.cgi?id=136592
+
+        Reviewed by Gavin Barraclough.
+
+        * wtf/FastMalloc.cpp:
+        (WTF::fastMallocStatistics):
+
 2014-09-05  Geoffrey Garen  <ggaren@apple.com>
 
         bmalloc should honor the FastMalloc scavenging API
index b62f2f2a9edff5fe1fc70e562311909f287e3632..798c1dca44f3156e40f58dd61199690c7f20bc17 100644 (file)
@@ -477,7 +477,11 @@ void releaseFastMallocFreeMemory()
 
 FastMallocStatistics fastMallocStatistics()
 {
-    FastMallocStatistics statistics = { 0, 0, 0 };
+    FastMallocStatistics statistics;
+    statistics.committedVMBytes = bmalloc::api::heapSize();
+    statistics.reservedVMBytes = bmalloc::api::heapCapacity();
+    statistics.freeListBytes = 0; // bmalloc doesn't really have free lists.
+
     return statistics;
 }
 
index 27d4c89660c77e250f4874e635bb64e1db7a9c06..e71eb8654caf5faddba4d26bc31b4751e15c1bf9 100644 (file)
@@ -1,3 +1,43 @@
+2014-09-05  Geoffrey Garen  <ggaren@apple.com>
+
+        bmalloc should honor the FastMalloc statistics API
+        https://bugs.webkit.org/show_bug.cgi?id=136592
+
+        Reviewed by Gavin Barraclough.
+
+        We do this by tracking "size" and "capacity" in the VM heap.
+
+        The VM heap's "capacity" is all the VM we ever allocated.
+
+        The VM heap's "size" the subset of VM currently held onto by the
+        VM heap (and therefore not in use by the regular heap).
+
+        Somewhat ironically, reducing the process's memory footprint, increases
+        the size of the VM heap, since the VM heap holds the pages that are
+        purely virtual and not physical.
+
+        * bmalloc/Heap.cpp:
+        (bmalloc::Heap::size):
+        (bmalloc::Heap::capacity):
+        * bmalloc/Heap.h:
+        * bmalloc/VMHeap.cpp:
+        (bmalloc::VMHeap::VMHeap):
+        (bmalloc::VMHeap::allocateSmallChunk):
+        (bmalloc::VMHeap::allocateMediumChunk):
+        (bmalloc::VMHeap::allocateLargeChunk):
+        * bmalloc/VMHeap.h:
+        (bmalloc::VMHeap::size):
+        (bmalloc::VMHeap::capacity):
+        (bmalloc::VMHeap::allocateSmallPage):
+        (bmalloc::VMHeap::allocateMediumPage):
+        (bmalloc::VMHeap::allocateLargeRange):
+        (bmalloc::VMHeap::deallocateSmallPage):
+        (bmalloc::VMHeap::deallocateMediumPage):
+        (bmalloc::VMHeap::deallocateLargeRange):
+        * bmalloc/bmalloc.h:
+        (bmalloc::api::heapSize):
+        (bmalloc::api::heapCapacity):
+
 2014-09-02  Geoffrey Garen  <ggaren@apple.com>
 
         bmalloc crashes on the EWS bots (due to bad large object allocation)
index 3fdd83d1bd7171601d9be66b409bf4be6bbe0daa..d4293f0e30a042d41e69a61c80f9c1a257b2a995 100644 (file)
@@ -210,4 +210,14 @@ void Heap::deallocateLarge(std::lock_guard<StaticMutex>&, void* object)
     m_scavenger.run();
 }
 
+size_t Heap::size(std::lock_guard<StaticMutex>&)
+{
+    return m_vmHeap.capacity() - m_vmHeap.size();
+}
+
+size_t Heap::capacity(std::lock_guard<StaticMutex>&)
+{
+    return m_vmHeap.capacity();
+}
+
 } // namespace bmalloc
index 2c417d20cb989a01b47bb76921af25f529495cc2..6d160e3539d15c655c3b4176c043a4789f605ce9 100644 (file)
@@ -62,7 +62,10 @@ public:
     void deallocateXLarge(std::lock_guard<StaticMutex>&, void*);
 
     void scavenge(std::unique_lock<StaticMutex>&, std::chrono::milliseconds sleepDuration);
-    
+
+    size_t size(std::lock_guard<StaticMutex>&);
+    size_t capacity(std::lock_guard<StaticMutex>&);
+
 private:
     ~Heap() = delete;
 
index 4464bd1d4a726d2637650900d6f116e7b346e037..d039c089b9ef6e35ef505b803b4ea1c90d5a13a6 100644 (file)
@@ -33,6 +33,8 @@
 namespace bmalloc {
 
 VMHeap::VMHeap()
+    : m_size(0)
+    , m_capacity(0)
 {
 }
 
@@ -41,6 +43,9 @@ void VMHeap::allocateSmallChunk()
     SmallChunk* chunk = SmallChunk::create();
     for (auto* it = chunk->begin(); it != chunk->end(); ++it)
         m_smallPages.push(it);
+
+    m_size += smallChunkSize;
+    m_capacity += smallChunkSize;
 }
 
 void VMHeap::allocateMediumChunk()
@@ -48,12 +53,19 @@ void VMHeap::allocateMediumChunk()
     MediumChunk* chunk = MediumChunk::create();
     for (auto* it = chunk->begin(); it != chunk->end(); ++it)
         m_mediumPages.push(it);
+
+    m_size += mediumChunkSize;
+    m_capacity += mediumChunkSize;
 }
 
 Range VMHeap::allocateLargeChunk()
 {
     LargeChunk* chunk = LargeChunk::create();
     Range result = BoundaryTag::init(chunk);
+
+    m_size += largeChunkSize;
+    m_capacity += largeChunkSize;
+
     return result;
 }
 
index d14cc8fa3a872143b4b5a9781c1cb31836da8df7..1e1155098086a19a0b0522bb49cafd218127e292 100644 (file)
@@ -45,6 +45,9 @@ class VMHeap {
 public:
     VMHeap();
 
+    size_t size() { return m_size; }
+    size_t capacity() { return m_capacity; }
+
     SmallPage* allocateSmallPage();
     MediumPage* allocateMediumPage();
     Range allocateLargeRange(size_t);
@@ -57,7 +60,10 @@ private:
     void allocateSmallChunk();
     void allocateMediumChunk();
     Range allocateLargeChunk();
-    
+
+    size_t m_size;
+    size_t m_capacity;
+
     Vector<SmallPage*> m_smallPages;
     Vector<MediumPage*> m_mediumPages;
     SegregatedFreeList m_largeRanges;
@@ -68,6 +74,7 @@ inline SmallPage* VMHeap::allocateSmallPage()
     if (!m_smallPages.size())
         allocateSmallChunk();
 
+    m_size -= vmPageSize;
     return m_smallPages.pop();
 }
 
@@ -76,6 +83,7 @@ inline MediumPage* VMHeap::allocateMediumPage()
     if (!m_mediumPages.size())
         allocateMediumChunk();
 
+    m_size -= vmPageSize;
     return m_mediumPages.pop();
 }
 
@@ -84,6 +92,7 @@ inline Range VMHeap::allocateLargeRange(size_t size)
     Range range = m_largeRanges.take(size);
     if (!range)
         range = allocateLargeChunk();
+    m_size -= range.size();
     return range;
 }
 
@@ -93,6 +102,7 @@ inline void VMHeap::deallocateSmallPage(std::unique_lock<StaticMutex>& lock, Sma
     vmDeallocatePhysicalPages(page->begin()->begin(), vmPageSize);
     lock.lock();
     
+    m_size += vmPageSize;
     m_smallPages.push(page);
 }
 
@@ -102,6 +112,7 @@ inline void VMHeap::deallocateMediumPage(std::unique_lock<StaticMutex>& lock, Me
     vmDeallocatePhysicalPages(page->begin()->begin(), vmPageSize);
     lock.lock();
     
+    m_size += vmPageSize;
     m_mediumPages.push(page);
 }
 
@@ -125,6 +136,7 @@ inline void VMHeap::deallocateLargeRange(std::unique_lock<StaticMutex>& lock, Ra
     beginTag->setHasPhysicalPages(false);
     endTag->setHasPhysicalPages(false);
 
+    m_size += range.size();
     m_largeRanges.insert(range);
 }
 
index 972393f145990102fb19f36b8a92482b4b38ddb3..b1b18c81a2e386363a4f86cd27345bec9f86fe18 100644 (file)
@@ -89,5 +89,17 @@ inline void scavenge()
     PerProcess<Heap>::get()->scavenge(lock, std::chrono::milliseconds(0));
 }
 
+inline size_t heapSize()
+{
+    std::lock_guard<StaticMutex> lock(PerProcess<Heap>::mutex());
+    return PerProcess<Heap>::get()->size(lock);
+}
+
+inline size_t heapCapacity()
+{
+    std::lock_guard<StaticMutex> lock(PerProcess<Heap>::mutex());
+    return PerProcess<Heap>::get()->capacity(lock);
+}
+
 } // namespace api
 } // namespace bmalloc