<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[196780] trunk/Source/WebCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/196780">196780</a></dd>
<dt>Author</dt> <dd>akling@apple.com</dd>
<dt>Date</dt> <dd>2016-02-18 15:35:42 -0800 (Thu, 18 Feb 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>Fake memory pressure handler should log detailed memory breakdown.
&lt;https://webkit.org/b/154415&gt;

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):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorepageResourceUsageThreadh">trunk/Source/WebCore/page/ResourceUsageThread.h</a></li>
<li><a href="#trunkSourceWebCorepagecocoaResourceUsageThreadCocoamm">trunk/Source/WebCore/page/cocoa/ResourceUsageThreadCocoa.mm</a></li>
<li><a href="#trunkSourceWebCoreplatformcocoaMemoryPressureHandlerCocoamm">trunk/Source/WebCore/platform/cocoa/MemoryPressureHandlerCocoa.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (196779 => 196780)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2016-02-18 22:42:54 UTC (rev 196779)
+++ trunk/Source/WebCore/ChangeLog        2016-02-18 23:35:42 UTC (rev 196780)
</span><span class="lines">@@ -1,3 +1,23 @@
</span><ins>+2016-02-18  Andreas Kling  &lt;akling@apple.com&gt;
+
+        Fake memory pressure handler should log detailed memory breakdown.
+        &lt;https://webkit.org/b/154415&gt;
+
+        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):
+
</ins><span class="cx"> 2016-02-18  Brady Eidson  &lt;beidson@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Modern IDB: Implement server-&gt;client operations in WK2.
</span></span></pre></div>
<a id="trunkSourceWebCorepageResourceUsageThreadh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/ResourceUsageThread.h (196779 => 196780)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/page/ResourceUsageThread.h        2016-02-18 22:42:54 UTC (rev 196779)
+++ trunk/Source/WebCore/page/ResourceUsageThread.h        2016-02-18 23:35:42 UTC (rev 196780)
</span><span class="lines">@@ -29,6 +29,7 @@
</span><span class="cx"> #if ENABLE(RESOURCE_USAGE)
</span><span class="cx"> 
</span><span class="cx"> #include &quot;ResourceUsageData.h&quot;
</span><ins>+#include &lt;array&gt;
</ins><span class="cx"> #include &lt;functional&gt;
</span><span class="cx"> #include &lt;wtf/Condition.h&gt;
</span><span class="cx"> #include &lt;wtf/HashMap.h&gt;
</span><span class="lines">@@ -72,6 +73,18 @@
</span><span class="cx">     JSC::VM* m_vm { nullptr };
</span><span class="cx"> };
</span><span class="cx"> 
</span><ins>+#if PLATFORM(COCOA)
+struct TagInfo {
+    TagInfo() { }
+    size_t dirty { 0 };
+    size_t reclaimable { 0 };
+};
+
+const char* displayNameForVMTag(unsigned);
+std::array&lt;TagInfo, 256&gt; pagesPerVMTag();
+void logFootprintComparison(const std::array&lt;TagInfo, 256&gt;&amp;, const std::array&lt;TagInfo, 256&gt;&amp;);
+#endif
+
</ins><span class="cx"> } // namespace WebCore
</span><span class="cx"> 
</span><span class="cx"> #endif // ENABLE(RESOURCE_USAGE)
</span></span></pre></div>
<a id="trunkSourceWebCorepagecocoaResourceUsageThreadCocoamm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/cocoa/ResourceUsageThreadCocoa.mm (196779 => 196780)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/page/cocoa/ResourceUsageThreadCocoa.mm        2016-02-18 22:42:54 UTC (rev 196779)
+++ trunk/Source/WebCore/page/cocoa/ResourceUsageThreadCocoa.mm        2016-02-18 23:35:42 UTC (rev 196780)
</span><span class="lines">@@ -51,14 +51,60 @@
</span><span class="cx">     return pageSize;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-struct TagInfo {
-    TagInfo() { }
-    size_t dirty { 0 };
-    size_t reclaimable { 0 };
-};
</del><ins>+void logFootprintComparison(const std::array&lt;TagInfo, 256&gt;&amp; before, const std::array&lt;TagInfo, 256&gt;&amp; after)
+{
+    const size_t pageSize = vmPageSize();
</ins><span class="cx"> 
</span><del>-static std::array&lt;TagInfo, 256&gt; pagesPerVMTag()
</del><ins>+    WTFLogAlways(&quot;Per-tag breakdown of memory reclaimed by pressure handler:&quot;);
+    WTFLogAlways(&quot;  ## %16s %10s %10s %10s&quot;, &quot;VM Tag&quot;, &quot;Before&quot;, &quot;After&quot;, &quot;Diff&quot;);
+    for (unsigned i = 0; i &lt; 256; ++i) {
+        ssize_t dirtyBefore = before[i].dirty * pageSize;
+        ssize_t dirtyAfter = after[i].dirty * pageSize;
+        ssize_t dirtyDiff = dirtyAfter - dirtyBefore;
+        if (!dirtyBefore &amp;&amp; !dirtyAfter)
+            continue;
+        String tagName = displayNameForVMTag(i);
+        if (!tagName)
+            tagName = String::format(&quot;Tag %u&quot;, i);
+        WTFLogAlways(&quot;  %02X %16s %10ld %10ld %10ld&quot;,
+            i,
+            tagName.ascii().data(),
+            dirtyBefore,
+            dirtyAfter,
+            dirtyDiff
+        );
+    }
+}
+
+const char* displayNameForVMTag(unsigned tag)
</ins><span class="cx"> {
</span><ins>+    switch (tag) {
+    case VM_MEMORY_IOKIT: return &quot;IOKit&quot;;
+    case VM_MEMORY_LAYERKIT: return &quot;CoreAnimation&quot;;
+    case VM_MEMORY_IMAGEIO: return &quot;ImageIO&quot;;
+    case VM_MEMORY_CGIMAGE: return &quot;CG image&quot;;
+    case VM_MEMORY_JAVASCRIPT_JIT_EXECUTABLE_ALLOCATOR: return &quot;JSC JIT&quot;;
+    case VM_MEMORY_MALLOC: return &quot;malloc&quot;;
+    case VM_MEMORY_MALLOC_HUGE: return &quot;malloc (huge)&quot;;
+    case VM_MEMORY_MALLOC_LARGE: return &quot;malloc (large)&quot;;
+    case VM_MEMORY_MALLOC_SMALL: return &quot;malloc (small)&quot;;
+    case VM_MEMORY_MALLOC_TINY: return &quot;malloc (tiny)&quot;;
+    case VM_MEMORY_MALLOC_NANO: return &quot;malloc (nano)&quot;;
+    case VM_MEMORY_TCMALLOC: return &quot;bmalloc&quot;;
+    case VM_MEMORY_FOUNDATION: return &quot;Foundation&quot;;
+    case VM_MEMORY_STACK: return &quot;Stack&quot;;
+    case VM_MEMORY_SQLITE: return &quot;SQLite&quot;;
+    case VM_MEMORY_UNSHARED_PMAP: return &quot;pmap (unshared)&quot;;
+    case VM_MEMORY_DYLIB: return &quot;dylib&quot;;
+    case VM_MEMORY_CORESERVICES: return &quot;CoreServices&quot;;
+    case VM_MEMORY_OS_ALLOC_ONCE: return &quot;os_alloc_once&quot;;
+    case VM_MEMORY_LIBDISPATCH: return &quot;libdispatch&quot;;
+    default: return nullptr;
+    }
+}
+
+std::array&lt;TagInfo, 256&gt; pagesPerVMTag()
+{
</ins><span class="cx">     std::array&lt;TagInfo, 256&gt; tags;
</span><span class="cx">     task_t task = mach_task_self();
</span><span class="cx">     mach_vm_size_t size;
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformcocoaMemoryPressureHandlerCocoamm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/cocoa/MemoryPressureHandlerCocoa.mm (196779 => 196780)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/cocoa/MemoryPressureHandlerCocoa.mm        2016-02-18 22:42:54 UTC (rev 196779)
+++ trunk/Source/WebCore/platform/cocoa/MemoryPressureHandlerCocoa.mm        2016-02-18 23:35:42 UTC (rev 196780)
</span><span class="lines">@@ -32,12 +32,14 @@
</span><span class="cx"> #import &quot;JSDOMWindowBase.h&quot;
</span><span class="cx"> #import &quot;LayerPool.h&quot;
</span><span class="cx"> #import &quot;Logging.h&quot;
</span><ins>+#import &quot;ResourceUsageThread.h&quot;
</ins><span class="cx"> #import &quot;WebCoreSystemInterface.h&quot;
</span><span class="cx"> #import &lt;mach/mach.h&gt;
</span><span class="cx"> #import &lt;mach/task_info.h&gt;
</span><span class="cx"> #import &lt;malloc/malloc.h&gt;
</span><span class="cx"> #import &lt;notify.h&gt;
</span><span class="cx"> #import &lt;wtf/CurrentTime.h&gt;
</span><ins>+#import &lt;sys/sysctl.h&gt;
</ins><span class="cx"> 
</span><span class="cx"> #if PLATFORM(IOS)
</span><span class="cx"> #import &quot;SystemMemory.h&quot;
</span><span class="lines">@@ -128,6 +130,10 @@
</span><span class="cx"> 
</span><span class="cx">     // Allow simulation of memory pressure with &quot;notifyutil -p org.WebKit.lowMemory&quot;
</span><span class="cx">     notify_register_dispatch(&quot;org.WebKit.lowMemory&quot;, &amp;_notifyToken, dispatch_get_main_queue(), ^(int) {
</span><ins>+#if ENABLE(RESOURCE_USAGE)
+        auto footprintBefore = pagesPerVMTag();
+#endif
+
</ins><span class="cx">         bool wasUnderMemoryPressure = m_underMemoryPressure;
</span><span class="cx">         m_underMemoryPressure = true;
</span><span class="cx"> 
</span><span class="lines">@@ -137,6 +143,11 @@
</span><span class="cx"> 
</span><span class="cx">         malloc_zone_pressure_relief(nullptr, 0);
</span><span class="cx"> 
</span><ins>+#if ENABLE(RESOURCE_USAGE)
+        auto footprintAfter = pagesPerVMTag();
+        logFootprintComparison(footprintBefore, footprintAfter);
+#endif
+
</ins><span class="cx">         // Since this is a simulation, unset the &quot;under memory pressure&quot; flag on next runloop.
</span><span class="cx">         dispatch_async(dispatch_get_main_queue(), ^{
</span><span class="cx">             MemoryPressureHandler::singleton().setUnderMemoryPressure(wasUnderMemoryPressure);
</span></span></pre>
</div>
</div>

</body>
</html>