Fake memory pressure handler should log detailed memory breakdown.
authorakling@apple.com <akling@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 18 Feb 2016 23:35:42 +0000 (23:35 +0000)
committerakling@apple.com <akling@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 18 Feb 2016 23:35:42 +0000 (23:35 +0000)
<https://webkit.org/b/154415>

Reviewed by Antti Koivisto.

Piggyback on the RESOURCE_USAGE code to implement some detailed memory footprint diffing
and have the fake memory handler dump before/after/diff after it runs.

* page/ResourceUsageThread.h:
(WebCore::TagInfo::TagInfo):
* page/cocoa/ResourceUsageThreadCocoa.mm:
(WebCore::logFootprintComparison):
(WebCore::displayNameForVMTag):
(WebCore::pagesPerVMTag):
(WebCore::TagInfo::TagInfo): Deleted.
* platform/cocoa/MemoryPressureHandlerCocoa.mm:
(WebCore::MemoryPressureHandler::install):

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

Source/WebCore/ChangeLog
Source/WebCore/page/ResourceUsageThread.h
Source/WebCore/page/cocoa/ResourceUsageThreadCocoa.mm
Source/WebCore/platform/cocoa/MemoryPressureHandlerCocoa.mm

index de0156e..625880a 100644 (file)
@@ -1,3 +1,23 @@
+2016-02-18  Andreas Kling  <akling@apple.com>
+
+        Fake memory pressure handler should log detailed memory breakdown.
+        <https://webkit.org/b/154415>
+
+        Reviewed by Antti Koivisto.
+
+        Piggyback on the RESOURCE_USAGE code to implement some detailed memory footprint diffing
+        and have the fake memory handler dump before/after/diff after it runs.
+
+        * page/ResourceUsageThread.h:
+        (WebCore::TagInfo::TagInfo):
+        * page/cocoa/ResourceUsageThreadCocoa.mm:
+        (WebCore::logFootprintComparison):
+        (WebCore::displayNameForVMTag):
+        (WebCore::pagesPerVMTag):
+        (WebCore::TagInfo::TagInfo): Deleted.
+        * platform/cocoa/MemoryPressureHandlerCocoa.mm:
+        (WebCore::MemoryPressureHandler::install):
+
 2016-02-18  Brady Eidson  <beidson@apple.com>
 
         Modern IDB: Implement server->client operations in WK2.
index 369bb9f..fd2581a 100644 (file)
@@ -29,6 +29,7 @@
 #if ENABLE(RESOURCE_USAGE)
 
 #include "ResourceUsageData.h"
+#include <array>
 #include <functional>
 #include <wtf/Condition.h>
 #include <wtf/HashMap.h>
@@ -72,6 +73,18 @@ private:
     JSC::VM* m_vm { nullptr };
 };
 
+#if PLATFORM(COCOA)
+struct TagInfo {
+    TagInfo() { }
+    size_t dirty { 0 };
+    size_t reclaimable { 0 };
+};
+
+const char* displayNameForVMTag(unsigned);
+std::array<TagInfo, 256> pagesPerVMTag();
+void logFootprintComparison(const std::array<TagInfo, 256>&, const std::array<TagInfo, 256>&);
+#endif
+
 } // namespace WebCore
 
 #endif // ENABLE(RESOURCE_USAGE)
index 5e364be..53ff877 100644 (file)
@@ -51,13 +51,59 @@ static size_t vmPageSize()
     return pageSize;
 }
 
-struct TagInfo {
-    TagInfo() { }
-    size_t dirty { 0 };
-    size_t reclaimable { 0 };
-};
+void logFootprintComparison(const std::array<TagInfo, 256>& before, const std::array<TagInfo, 256>& after)
+{
+    const size_t pageSize = vmPageSize();
+
+    WTFLogAlways("Per-tag breakdown of memory reclaimed by pressure handler:");
+    WTFLogAlways("  ## %16s %10s %10s %10s", "VM Tag", "Before", "After", "Diff");
+    for (unsigned i = 0; i < 256; ++i) {
+        ssize_t dirtyBefore = before[i].dirty * pageSize;
+        ssize_t dirtyAfter = after[i].dirty * pageSize;
+        ssize_t dirtyDiff = dirtyAfter - dirtyBefore;
+        if (!dirtyBefore && !dirtyAfter)
+            continue;
+        String tagName = displayNameForVMTag(i);
+        if (!tagName)
+            tagName = String::format("Tag %u", i);
+        WTFLogAlways("  %02X %16s %10ld %10ld %10ld",
+            i,
+            tagName.ascii().data(),
+            dirtyBefore,
+            dirtyAfter,
+            dirtyDiff
+        );
+    }
+}
+
+const char* displayNameForVMTag(unsigned tag)
+{
+    switch (tag) {
+    case VM_MEMORY_IOKIT: return "IOKit";
+    case VM_MEMORY_LAYERKIT: return "CoreAnimation";
+    case VM_MEMORY_IMAGEIO: return "ImageIO";
+    case VM_MEMORY_CGIMAGE: return "CG image";
+    case VM_MEMORY_JAVASCRIPT_JIT_EXECUTABLE_ALLOCATOR: return "JSC JIT";
+    case VM_MEMORY_MALLOC: return "malloc";
+    case VM_MEMORY_MALLOC_HUGE: return "malloc (huge)";
+    case VM_MEMORY_MALLOC_LARGE: return "malloc (large)";
+    case VM_MEMORY_MALLOC_SMALL: return "malloc (small)";
+    case VM_MEMORY_MALLOC_TINY: return "malloc (tiny)";
+    case VM_MEMORY_MALLOC_NANO: return "malloc (nano)";
+    case VM_MEMORY_TCMALLOC: return "bmalloc";
+    case VM_MEMORY_FOUNDATION: return "Foundation";
+    case VM_MEMORY_STACK: return "Stack";
+    case VM_MEMORY_SQLITE: return "SQLite";
+    case VM_MEMORY_UNSHARED_PMAP: return "pmap (unshared)";
+    case VM_MEMORY_DYLIB: return "dylib";
+    case VM_MEMORY_CORESERVICES: return "CoreServices";
+    case VM_MEMORY_OS_ALLOC_ONCE: return "os_alloc_once";
+    case VM_MEMORY_LIBDISPATCH: return "libdispatch";
+    default: return nullptr;
+    }
+}
 
-static std::array<TagInfo, 256> pagesPerVMTag()
+std::array<TagInfo, 256> pagesPerVMTag()
 {
     std::array<TagInfo, 256> tags;
     task_t task = mach_task_self();
index 6789f7b..40a367b 100644 (file)
 #import "JSDOMWindowBase.h"
 #import "LayerPool.h"
 #import "Logging.h"
+#import "ResourceUsageThread.h"
 #import "WebCoreSystemInterface.h"
 #import <mach/mach.h>
 #import <mach/task_info.h>
 #import <malloc/malloc.h>
 #import <notify.h>
 #import <wtf/CurrentTime.h>
+#import <sys/sysctl.h>
 
 #if PLATFORM(IOS)
 #import "SystemMemory.h"
@@ -128,6 +130,10 @@ void MemoryPressureHandler::install()
 
     // Allow simulation of memory pressure with "notifyutil -p org.WebKit.lowMemory"
     notify_register_dispatch("org.WebKit.lowMemory", &_notifyToken, dispatch_get_main_queue(), ^(int) {
+#if ENABLE(RESOURCE_USAGE)
+        auto footprintBefore = pagesPerVMTag();
+#endif
+
         bool wasUnderMemoryPressure = m_underMemoryPressure;
         m_underMemoryPressure = true;
 
@@ -137,6 +143,11 @@ void MemoryPressureHandler::install()
 
         malloc_zone_pressure_relief(nullptr, 0);
 
+#if ENABLE(RESOURCE_USAGE)
+        auto footprintAfter = pagesPerVMTag();
+        logFootprintComparison(footprintBefore, footprintAfter);
+#endif
+
         // Since this is a simulation, unset the "under memory pressure" flag on next runloop.
         dispatch_async(dispatch_get_main_queue(), ^{
             MemoryPressureHandler::singleton().setUnderMemoryPressure(wasUnderMemoryPressure);