<!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>[211622] trunk/Source</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/211622">211622</a></dd>
<dt>Author</dt> <dd>akling@apple.com</dd>
<dt>Date</dt> <dd>2017-02-02 23:25:24 -0800 (Thu, 02 Feb 2017)</dd>
</dl>

<h3>Log Message</h3>
<pre>[Mac] In-process memory pressure monitor for WebContent processes AKA websam
&lt;https://webkit.org/b/167491&gt;
&lt;rdar://problem/30116072&gt;

Reviewed by Antti Koivisto.

Source/JavaScriptCore:

Remove the sloppy &quot;max live heap size&quot; mechanism from JSC in favor of the new
WebCore-side memory footprint monitor.

* heap/Heap.cpp:
(JSC::Heap::updateAllocationLimits):
(JSC::Heap::didExceedMaxLiveSize): Deleted.
* heap/Heap.h:
(JSC::Heap::setMaxLiveSize): Deleted.

Source/WebCore:

Add a new timer-based memory pressure monitor that checks the process memory
footprint every 30 seconds and reacts to changes by setting a MemoryUsagePolicy.

There are four MemoryUsagePolicy values:

    - Unrestricted (below 1GB)
    - Conservative (above 1GB)
    - Strict (above 2GB)
    - Panic (above 4GB, or 3GB if 32-bit)

For Strict and above, the old-style &quot;isUnderMemoryPressure()&quot; API will return true.

Transitioning to a higher policy will cause memory pressure handlers to run:

At Strict, we run the &quot;non-critical&quot; memory pressure handler, then carry on.

At Panic, we run the &quot;critical&quot; memory pressure handler. If that fails to recover
enough memory to bring us back below 4GB, we may kill the process:

A process is eligible to get killed for using too much memory if:

    - It's not visible on screen (i.e it's a background tab.)
    - It's not playing audio.
    - It has not performed a main frame navigation in the last hour.

Before killing the process, an exit-time callback will run. This patch installs such
a callback that prints out some time-of-death statistics about C++ and JavaScript memory
usage to hopefully help understand what was soaking up all the memory.

* bindings/js/CommonVM.cpp:
(WebCore::commonVMSlow):
* loader/FrameLoader.cpp:
(WebCore::FrameLoader::setState):
* page/MainFrame.cpp:
(WebCore::MainFrame::didCompleteLoad):
* page/MainFrame.h:
* page/MemoryRelease.cpp:
(WebCore::pageCount):
(WebCore::logMemoryStatisticsAtTimeOfDeath):
(WebCore::didExceedMemoryLimitAndFailedToRecover):
(WebCore::processIsEligibleForMemoryKill):
* page/MemoryRelease.h:
* page/ResourceUsageThread.h:
* page/cocoa/ResourceUsageThreadCocoa.mm:
(WebCore::vmPageSize):
* platform/MemoryPressureHandler.cpp:
(WebCore::MemoryPressureHandler::MemoryPressureHandler):
(WebCore::MemoryPressureHandler::setShouldUsePeriodicMemoryMonitor):
(WebCore::toString):
(WebCore::thresholdForPolicy):
(WebCore::policyForFootprint):
(WebCore::MemoryPressureHandler::measurementTimerFired):
* platform/MemoryPressureHandler.h:
(WebCore::MemoryPressureHandler::setMemoryKillCallback):
(WebCore::MemoryPressureHandler::setProcessIsEligibleForMemoryKillCallback):
(WebCore::MemoryPressureHandler::isUnderMemoryPressure):

Source/WebKit2:

Enable the in-process memory monitor for WebContent processes on macOS 10.12+

* WebProcess/WebProcess.cpp:
(WebKit::WebProcess::initializeWebProcess):

Source/WTF:

Add a WTF helper function for getting the current process's memory footprint.

* WTF.xcodeproj/project.pbxproj:
* wtf/CMakeLists.txt:
* wtf/MemoryFootprint.cpp:
(WTF::memoryFootprint):
* wtf/MemoryFootprint.h:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreheapHeapcpp">trunk/Source/JavaScriptCore/heap/Heap.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreheapHeaph">trunk/Source/JavaScriptCore/heap/Heap.h</a></li>
<li><a href="#trunkSourceWTFChangeLog">trunk/Source/WTF/ChangeLog</a></li>
<li><a href="#trunkSourceWTFWTFxcodeprojprojectpbxproj">trunk/Source/WTF/WTF.xcodeproj/project.pbxproj</a></li>
<li><a href="#trunkSourceWTFwtfCMakeListstxt">trunk/Source/WTF/wtf/CMakeLists.txt</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorebindingsjsCommonVMcpp">trunk/Source/WebCore/bindings/js/CommonVM.cpp</a></li>
<li><a href="#trunkSourceWebCoreloaderFrameLoadercpp">trunk/Source/WebCore/loader/FrameLoader.cpp</a></li>
<li><a href="#trunkSourceWebCorepageMainFramecpp">trunk/Source/WebCore/page/MainFrame.cpp</a></li>
<li><a href="#trunkSourceWebCorepageMainFrameh">trunk/Source/WebCore/page/MainFrame.h</a></li>
<li><a href="#trunkSourceWebCorepageMemoryReleasecpp">trunk/Source/WebCore/page/MemoryRelease.cpp</a></li>
<li><a href="#trunkSourceWebCorepageMemoryReleaseh">trunk/Source/WebCore/page/MemoryRelease.h</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="#trunkSourceWebCoreplatformMemoryPressureHandlercpp">trunk/Source/WebCore/platform/MemoryPressureHandler.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformMemoryPressureHandlerh">trunk/Source/WebCore/platform/MemoryPressureHandler.h</a></li>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2WebProcessWebProcesscpp">trunk/Source/WebKit2/WebProcess/WebProcess.cpp</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkSourceWTFwtfMemoryFootprintcpp">trunk/Source/WTF/wtf/MemoryFootprint.cpp</a></li>
<li><a href="#trunkSourceWTFwtfMemoryFootprinth">trunk/Source/WTF/wtf/MemoryFootprint.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (211621 => 211622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2017-02-03 06:30:30 UTC (rev 211621)
+++ trunk/Source/JavaScriptCore/ChangeLog        2017-02-03 07:25:24 UTC (rev 211622)
</span><span class="lines">@@ -1,3 +1,20 @@
</span><ins>+2017-02-02  Andreas Kling  &lt;akling@apple.com&gt;
+
+        [Mac] In-process memory pressure monitor for WebContent processes AKA websam
+        &lt;https://webkit.org/b/167491&gt;
+        &lt;rdar://problem/30116072&gt;
+
+        Reviewed by Antti Koivisto.
+
+        Remove the sloppy &quot;max live heap size&quot; mechanism from JSC in favor of the new
+        WebCore-side memory footprint monitor.
+
+        * heap/Heap.cpp:
+        (JSC::Heap::updateAllocationLimits):
+        (JSC::Heap::didExceedMaxLiveSize): Deleted.
+        * heap/Heap.h:
+        (JSC::Heap::setMaxLiveSize): Deleted.
+
</ins><span class="cx"> 2017-02-02  Mark Lam  &lt;mark.lam@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Add a SIGILL crash analyzer to make debugging SIGILLs easier.
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreheapHeapcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/heap/Heap.cpp (211621 => 211622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/heap/Heap.cpp        2017-02-03 06:30:30 UTC (rev 211621)
+++ trunk/Source/JavaScriptCore/heap/Heap.cpp        2017-02-03 07:25:24 UTC (rev 211622)
</span><span class="lines">@@ -1726,11 +1726,6 @@
</span><span class="cx">     m_sweeper-&gt;startSweeping();
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-NEVER_INLINE void Heap::didExceedMaxLiveSize()
-{
-    CRASH();
-}
-
</del><span class="cx"> void Heap::updateAllocationLimits()
</span><span class="cx"> {
</span><span class="cx">     static const bool verbose = false;
</span><span class="lines">@@ -1762,9 +1757,6 @@
</span><span class="cx"> 
</span><span class="cx">     if (verbose)
</span><span class="cx">         dataLog(&quot;extraMemorySize() = &quot;, extraMemorySize(), &quot;, currentHeapSize = &quot;, currentHeapSize, &quot;\n&quot;);
</span><del>-
-    if (m_maxLiveSize &amp;&amp; currentHeapSize &gt; m_maxLiveSize)
-        didExceedMaxLiveSize();
</del><span class="cx">     
</span><span class="cx">     if (Options::gcMaxHeapSize() &amp;&amp; currentHeapSize &gt; Options::gcMaxHeapSize())
</span><span class="cx">         HeapStatistics::exitWithFailure();
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreheapHeaph"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/heap/Heap.h (211621 => 211622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/heap/Heap.h        2017-02-03 06:30:30 UTC (rev 211621)
+++ trunk/Source/JavaScriptCore/heap/Heap.h        2017-02-03 07:25:24 UTC (rev 211622)
</span><span class="lines">@@ -132,9 +132,6 @@
</span><span class="cx"> 
</span><span class="cx">     VM* vm() const;
</span><span class="cx"> 
</span><del>-    // Set a hard limit where JSC will crash if live heap size exceeds it.
-    void setMaxLiveSize(size_t size) { m_maxLiveSize = size; }
-
</del><span class="cx">     MarkedSpace&amp; objectSpace() { return m_objectSpace; }
</span><span class="cx">     MachineThreads&amp; machineThreads() { return m_machineThreads; }
</span><span class="cx"> 
</span><span class="lines">@@ -605,9 +602,6 @@
</span><span class="cx">     size_t m_blockBytesAllocated { 0 };
</span><span class="cx">     size_t m_externalMemorySize { 0 };
</span><span class="cx"> #endif
</span><del>-
-    NO_RETURN_DUE_TO_CRASH void didExceedMaxLiveSize();
-    size_t m_maxLiveSize { 0 };
</del><span class="cx">     
</span><span class="cx">     std::unique_ptr&lt;MutatorScheduler&gt; m_scheduler;
</span><span class="cx">     
</span></span></pre></div>
<a id="trunkSourceWTFChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/ChangeLog (211621 => 211622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/ChangeLog        2017-02-03 06:30:30 UTC (rev 211621)
+++ trunk/Source/WTF/ChangeLog        2017-02-03 07:25:24 UTC (rev 211622)
</span><span class="lines">@@ -1,3 +1,19 @@
</span><ins>+2017-02-02  Andreas Kling  &lt;akling@apple.com&gt;
+
+        [Mac] In-process memory pressure monitor for WebContent processes AKA websam
+        &lt;https://webkit.org/b/167491&gt;
+        &lt;rdar://problem/30116072&gt;
+
+        Reviewed by Antti Koivisto.
+
+        Add a WTF helper function for getting the current process's memory footprint.
+
+        * WTF.xcodeproj/project.pbxproj:
+        * wtf/CMakeLists.txt:
+        * wtf/MemoryFootprint.cpp:
+        (WTF::memoryFootprint):
+        * wtf/MemoryFootprint.h:
+
</ins><span class="cx"> 2017-02-02  Mark Lam  &lt;mark.lam@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Add a SIGILL crash analyzer to make debugging SIGILLs easier.
</span></span></pre></div>
<a id="trunkSourceWTFWTFxcodeprojprojectpbxproj"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/WTF.xcodeproj/project.pbxproj (211621 => 211622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/WTF.xcodeproj/project.pbxproj        2017-02-03 06:30:30 UTC (rev 211621)
+++ trunk/Source/WTF/WTF.xcodeproj/project.pbxproj        2017-02-03 07:25:24 UTC (rev 211622)
</span><span class="lines">@@ -337,6 +337,8 @@
</span><span class="cx">                 A8A47487151A825B004123FF /* WTFThreadData.h in Headers */ = {isa = PBXBuildFile; fileRef = A8A4737B151A825B004123FF /* WTFThreadData.h */; };
</span><span class="cx">                 A8A4748C151A8264004123FF /* config.h in Headers */ = {isa = PBXBuildFile; fileRef = A8A4748B151A8264004123FF /* config.h */; };
</span><span class="cx">                 AD7C434B1DD2A4A70026888B /* Expected.h in Headers */ = {isa = PBXBuildFile; fileRef = AD7C434A1DD2A4A70026888B /* Expected.h */; };
</span><ins>+                ADF2CE661E39F106006889DB /* MemoryFootprint.h in Headers */ = {isa = PBXBuildFile; fileRef = ADF2CE641E39F106006889DB /* MemoryFootprint.h */; settings = {ATTRIBUTES = (Private, ); }; };
+                ADF2CE671E39F106006889DB /* MemoryFootprint.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ADF2CE651E39F106006889DB /* MemoryFootprint.cpp */; };
</ins><span class="cx">                 B38FD7BD168953E80065C969 /* FeatureDefines.h in Headers */ = {isa = PBXBuildFile; fileRef = B38FD7BC168953E80065C969 /* FeatureDefines.h */; };
</span><span class="cx">                 C4F8A93719C65EB400B2B15D /* Stopwatch.h in Headers */ = {isa = PBXBuildFile; fileRef = C4F8A93619C65EB400B2B15D /* Stopwatch.h */; };
</span><span class="cx">                 C8B0E1A1E01A486EB95E0D11 /* IndexSet.h in Headers */ = {isa = PBXBuildFile; fileRef = 3137E1D7DBD84AC38FAE4D34 /* IndexSet.h */; };
</span><span class="lines">@@ -719,6 +721,8 @@
</span><span class="cx">                 A8A4737B151A825B004123FF /* WTFThreadData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WTFThreadData.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 A8A4748B151A8264004123FF /* config.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = config.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 AD7C434A1DD2A4A70026888B /* Expected.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Expected.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><ins>+                ADF2CE641E39F106006889DB /* MemoryFootprint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MemoryFootprint.h; sourceTree = &quot;&lt;group&gt;&quot;; };
+                ADF2CE651E39F106006889DB /* MemoryFootprint.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MemoryFootprint.cpp; sourceTree = &quot;&lt;group&gt;&quot;; };
</ins><span class="cx">                 B38FD7BC168953E80065C969 /* FeatureDefines.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FeatureDefines.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 C4F8A93619C65EB400B2B15D /* Stopwatch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Stopwatch.h; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="cx">                 CD5497AA15857D0300B5BC30 /* MediaTime.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MediaTime.cpp; sourceTree = &quot;&lt;group&gt;&quot;; };
</span><span class="lines">@@ -1005,6 +1009,8 @@
</span><span class="cx">                                 A8A472CB151A825B004123FF /* MD5.h */,
</span><span class="cx">                                 CD5497AA15857D0300B5BC30 /* MediaTime.cpp */,
</span><span class="cx">                                 CD5497AB15857D0300B5BC30 /* MediaTime.h */,
</span><ins>+                                ADF2CE641E39F106006889DB /* MemoryFootprint.h */,
+                                ADF2CE651E39F106006889DB /* MemoryFootprint.cpp */,
</ins><span class="cx">                                 A8A472CC151A825B004123FF /* MessageQueue.h */,
</span><span class="cx">                                 A8A472CD151A825B004123FF /* MetaAllocator.cpp */,
</span><span class="cx">                                 A8A472CE151A825B004123FF /* MetaAllocator.h */,
</span><span class="lines">@@ -1494,6 +1500,7 @@
</span><span class="cx">                                 A8A4741C151A825B004123FF /* RefPtr.h in Headers */,
</span><span class="cx">                                 A8A4741E151A825B004123FF /* RetainPtr.h in Headers */,
</span><span class="cx">                                 2CDED0F418115C85004DBA70 /* RunLoop.h in Headers */,
</span><ins>+                                ADF2CE661E39F106006889DB /* MemoryFootprint.h in Headers */,
</ins><span class="cx">                                 1469419216EAAF6D0024E146 /* RunLoopTimer.h in Headers */,
</span><span class="cx">                                 A5098B001C169E0700087797 /* SandboxSPI.h in Headers */,
</span><span class="cx">                                 14F3B0F715E45E4600210069 /* SaturatedArithmetic.h in Headers */,
</span><span class="lines">@@ -1701,6 +1708,7 @@
</span><span class="cx">                                 1ACADD841884480100D8B71D /* DeprecatedSymbolsUsedBySafari.mm in Sources */,
</span><span class="cx">                                 A8A473AE151A825B004123FF /* diy-fp.cc in Sources */,
</span><span class="cx">                                 A8A473B0151A825B004123FF /* double-conversion.cc in Sources */,
</span><ins>+                                ADF2CE671E39F106006889DB /* MemoryFootprint.cpp in Sources */,
</ins><span class="cx">                                 A8A473BA151A825B004123FF /* dtoa.cpp in Sources */,
</span><span class="cx">                                 A8A473B3151A825B004123FF /* fast-dtoa.cc in Sources */,
</span><span class="cx">                                 0F7C5FB61D885CF20044F5E2 /* FastBitVector.cpp in Sources */,
</span></span></pre></div>
<a id="trunkSourceWTFwtfCMakeListstxt"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/CMakeLists.txt (211621 => 211622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/CMakeLists.txt        2017-02-03 06:30:30 UTC (rev 211621)
+++ trunk/Source/WTF/wtf/CMakeLists.txt        2017-02-03 07:25:24 UTC (rev 211622)
</span><span class="lines">@@ -64,6 +64,7 @@
</span><span class="cx">     MallocPtr.h
</span><span class="cx">     MathExtras.h
</span><span class="cx">     MediaTime.h
</span><ins>+    MemoryFootprint.h
</ins><span class="cx">     MessageQueue.h
</span><span class="cx">     MetaAllocator.h
</span><span class="cx">     MetaAllocatorHandle.h
</span><span class="lines">@@ -204,6 +205,7 @@
</span><span class="cx">     MD5.cpp
</span><span class="cx">     MainThread.cpp
</span><span class="cx">     MediaTime.cpp
</span><ins>+    MemoryFootprint.cpp
</ins><span class="cx">     MetaAllocator.cpp
</span><span class="cx">     MonotonicTime.cpp
</span><span class="cx">     NumberOfCores.cpp
</span></span></pre></div>
<a id="trunkSourceWTFwtfMemoryFootprintcppfromrev211621trunkSourceWebCorebindingsjsCommonVMcpp"></a>
<div class="copfile"><h4>Copied: trunk/Source/WTF/wtf/MemoryFootprint.cpp (from rev 211621, trunk/Source/WebCore/bindings/js/CommonVM.cpp) (0 => 211622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/MemoryFootprint.cpp                                (rev 0)
+++ trunk/Source/WTF/wtf/MemoryFootprint.cpp        2017-02-03 07:25:24 UTC (rev 211622)
</span><span class="lines">@@ -0,0 +1,50 @@
</span><ins>+/*
+ * Copyright (C) 2017 Apple Inc. All Rights Reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include &quot;config.h&quot;
+#include &quot;MemoryFootprint.h&quot;
+
+#if PLATFORM(COCOA)
+#include &lt;mach/mach.h&gt;
+#include &lt;mach/task_info.h&gt;
+#endif
+
+namespace WTF {
+
+std::optional&lt;size_t&gt; memoryFootprint()
+{
+#if PLATFORM(COCOA)
+    task_vm_info_data_t vmInfo;
+    mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
+    kern_return_t result = task_info(mach_task_self(), TASK_VM_INFO, (task_info_t) &amp;vmInfo, &amp;count);
+    if (result != KERN_SUCCESS)
+        return std::nullopt;
+    return static_cast&lt;size_t&gt;(vmInfo.phys_footprint);
+#else
+    return std::nullopt;
+#endif
+}
+
+}
</ins></span></pre></div>
<a id="trunkSourceWTFwtfMemoryFootprinthfromrev211621trunkSourceWebCorepageMemoryReleaseh"></a>
<div class="copfile"><h4>Copied: trunk/Source/WTF/wtf/MemoryFootprint.h (from rev 211621, trunk/Source/WebCore/page/MemoryRelease.h) (0 => 211622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/MemoryFootprint.h                                (rev 0)
+++ trunk/Source/WTF/wtf/MemoryFootprint.h        2017-02-03 07:25:24 UTC (rev 211622)
</span><span class="lines">@@ -0,0 +1,37 @@
</span><ins>+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include &lt;wtf/Optional.h&gt;
+
+namespace WTF {
+
+WTF_EXPORT_PRIVATE std::optional&lt;size_t&gt; memoryFootprint();
+
+}
+
+using WTF::memoryFootprint;
+
</ins></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (211621 => 211622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2017-02-03 06:30:30 UTC (rev 211621)
+++ trunk/Source/WebCore/ChangeLog        2017-02-03 07:25:24 UTC (rev 211622)
</span><span class="lines">@@ -1,3 +1,68 @@
</span><ins>+2017-02-02  Andreas Kling  &lt;akling@apple.com&gt;
+
+        [Mac] In-process memory pressure monitor for WebContent processes AKA websam
+        &lt;https://webkit.org/b/167491&gt;
+        &lt;rdar://problem/30116072&gt;
+
+        Reviewed by Antti Koivisto.
+
+        Add a new timer-based memory pressure monitor that checks the process memory
+        footprint every 30 seconds and reacts to changes by setting a MemoryUsagePolicy.
+
+        There are four MemoryUsagePolicy values:
+
+            - Unrestricted (below 1GB)
+            - Conservative (above 1GB)
+            - Strict (above 2GB)
+            - Panic (above 4GB, or 3GB if 32-bit)
+
+        For Strict and above, the old-style &quot;isUnderMemoryPressure()&quot; API will return true.
+
+        Transitioning to a higher policy will cause memory pressure handlers to run:
+
+        At Strict, we run the &quot;non-critical&quot; memory pressure handler, then carry on.
+
+        At Panic, we run the &quot;critical&quot; memory pressure handler. If that fails to recover
+        enough memory to bring us back below 4GB, we may kill the process:
+
+        A process is eligible to get killed for using too much memory if:
+
+            - It's not visible on screen (i.e it's a background tab.)
+            - It's not playing audio.
+            - It has not performed a main frame navigation in the last hour.
+
+        Before killing the process, an exit-time callback will run. This patch installs such
+        a callback that prints out some time-of-death statistics about C++ and JavaScript memory
+        usage to hopefully help understand what was soaking up all the memory.
+
+        * bindings/js/CommonVM.cpp:
+        (WebCore::commonVMSlow):
+        * loader/FrameLoader.cpp:
+        (WebCore::FrameLoader::setState):
+        * page/MainFrame.cpp:
+        (WebCore::MainFrame::didCompleteLoad):
+        * page/MainFrame.h:
+        * page/MemoryRelease.cpp:
+        (WebCore::pageCount):
+        (WebCore::logMemoryStatisticsAtTimeOfDeath):
+        (WebCore::didExceedMemoryLimitAndFailedToRecover):
+        (WebCore::processIsEligibleForMemoryKill):
+        * page/MemoryRelease.h:
+        * page/ResourceUsageThread.h:
+        * page/cocoa/ResourceUsageThreadCocoa.mm:
+        (WebCore::vmPageSize):
+        * platform/MemoryPressureHandler.cpp:
+        (WebCore::MemoryPressureHandler::MemoryPressureHandler):
+        (WebCore::MemoryPressureHandler::setShouldUsePeriodicMemoryMonitor):
+        (WebCore::toString):
+        (WebCore::thresholdForPolicy):
+        (WebCore::policyForFootprint):
+        (WebCore::MemoryPressureHandler::measurementTimerFired):
+        * platform/MemoryPressureHandler.h:
+        (WebCore::MemoryPressureHandler::setMemoryKillCallback):
+        (WebCore::MemoryPressureHandler::setProcessIsEligibleForMemoryKillCallback):
+        (WebCore::MemoryPressureHandler::isUnderMemoryPressure):
+
</ins><span class="cx"> 2017-02-02  Alex Christensen  &lt;achristensen@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         URLParser: Fix parsing invalid IPv4 addresses with non-ASCII characters
</span></span></pre></div>
<a id="trunkSourceWebCorebindingsjsCommonVMcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/bindings/js/CommonVM.cpp (211621 => 211622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/bindings/js/CommonVM.cpp        2017-02-03 06:30:30 UTC (rev 211621)
+++ trunk/Source/WebCore/bindings/js/CommonVM.cpp        2017-02-03 07:25:24 UTC (rev 211622)
</span><span class="lines">@@ -46,10 +46,6 @@
</span><span class="cx">     
</span><span class="cx">     ScriptController::initializeThreading();
</span><span class="cx">     g_commonVMOrNull = &amp;VM::createLeaked(LargeHeap).leakRef();
</span><del>-#if CPU(X86_64) || CPU(ARM64)
-    static const size_t maxGCHeapSize = 4 * GB;
-    g_commonVMOrNull-&gt;heap.setMaxLiveSize(maxGCHeapSize);
-#endif
</del><span class="cx">     g_commonVMOrNull-&gt;heap.acquireAccess(); // At any time, we may do things that affect the GC.
</span><span class="cx"> #if !PLATFORM(IOS)
</span><span class="cx">     g_commonVMOrNull-&gt;setExclusiveThread(std::this_thread::get_id());
</span></span></pre></div>
<a id="trunkSourceWebCoreloaderFrameLoadercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/loader/FrameLoader.cpp (211621 => 211622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/loader/FrameLoader.cpp        2017-02-03 06:30:30 UTC (rev 211621)
+++ trunk/Source/WebCore/loader/FrameLoader.cpp        2017-02-03 07:25:24 UTC (rev 211622)
</span><span class="lines">@@ -1742,7 +1742,7 @@
</span><span class="cx">         if (m_documentLoader)
</span><span class="cx">             m_documentLoader-&gt;stopRecordingResponses();
</span><span class="cx">         if (m_frame.isMainFrame() &amp;&amp; oldState != newState)
</span><del>-            static_cast&lt;MainFrame&amp;&gt;(m_frame).performanceLogging().didReachPointOfInterest(PerformanceLogging::MainFrameLoadCompleted);
</del><ins>+            static_cast&lt;MainFrame&amp;&gt;(m_frame).didCompleteLoad();
</ins><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCorepageMainFramecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/MainFrame.cpp (211621 => 211622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/page/MainFrame.cpp        2017-02-03 06:30:30 UTC (rev 211621)
+++ trunk/Source/WebCore/page/MainFrame.cpp        2017-02-03 07:25:24 UTC (rev 211622)
</span><span class="lines">@@ -95,6 +95,12 @@
</span><span class="cx">         tree().removeChild(*child);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void MainFrame::didCompleteLoad()
+{
+    m_timeOfLastCompletedLoad = MonotonicTime::now();
+    performanceLogging().didReachPointOfInterest(PerformanceLogging::MainFrameLoadCompleted);
+}
+
</ins><span class="cx"> #if PLATFORM(MAC)
</span><span class="cx"> ScrollLatchingState* MainFrame::latchingState()
</span><span class="cx"> {
</span></span></pre></div>
<a id="trunkSourceWebCorepageMainFrameh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/MainFrame.h (211621 => 211622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/page/MainFrame.h        2017-02-03 06:30:30 UTC (rev 211621)
+++ trunk/Source/WebCore/page/MainFrame.h        2017-02-03 07:25:24 UTC (rev 211622)
</span><span class="lines">@@ -69,6 +69,9 @@
</span><span class="cx"> 
</span><span class="cx">     PerformanceLogging&amp; performanceLogging() const { return *m_performanceLogging; }
</span><span class="cx"> 
</span><ins>+    void didCompleteLoad();
+    MonotonicTime timeOfLastCompletedLoad() const { return m_timeOfLastCompletedLoad; }
+
</ins><span class="cx"> private:
</span><span class="cx">     MainFrame(Page&amp;, PageConfiguration&amp;);
</span><span class="cx"> 
</span><span class="lines">@@ -91,6 +94,8 @@
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx">     std::unique_ptr&lt;PerformanceLogging&gt; m_performanceLogging;
</span><ins>+
+    MonotonicTime m_timeOfLastCompletedLoad;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> } // namespace WebCore
</span></span></pre></div>
<a id="trunkSourceWebCorepageMemoryReleasecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/MemoryRelease.cpp (211621 => 211622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/page/MemoryRelease.cpp        2017-02-03 06:30:30 UTC (rev 211621)
+++ trunk/Source/WebCore/page/MemoryRelease.cpp        2017-02-03 07:25:24 UTC (rev 211622)
</span><span class="lines">@@ -29,6 +29,7 @@
</span><span class="cx"> #include &quot;CSSValuePool.h&quot;
</span><span class="cx"> #include &quot;Chrome.h&quot;
</span><span class="cx"> #include &quot;ChromeClient.h&quot;
</span><ins>+#include &quot;CommonVM.h&quot;
</ins><span class="cx"> #include &quot;Document.h&quot;
</span><span class="cx"> #include &quot;FontCache.h&quot;
</span><span class="cx"> #include &quot;GCController.h&quot;
</span><span class="lines">@@ -35,6 +36,8 @@
</span><span class="cx"> #include &quot;HTMLMediaElement.h&quot;
</span><span class="cx"> #include &quot;InlineStyleSheetOwner.h&quot;
</span><span class="cx"> #include &quot;InspectorInstrumentation.h&quot;
</span><ins>+#include &quot;Logging.h&quot;
+#include &quot;MainFrame.h&quot;
</ins><span class="cx"> #include &quot;MemoryCache.h&quot;
</span><span class="cx"> #include &quot;Page.h&quot;
</span><span class="cx"> #include &quot;PageCache.h&quot;
</span><span class="lines">@@ -45,6 +48,10 @@
</span><span class="cx"> #include &quot;WorkerThread.h&quot;
</span><span class="cx"> #include &lt;wtf/FastMalloc.h&gt;
</span><span class="cx"> 
</span><ins>+#if PLATFORM(COCOA)
+#include &quot;ResourceUsageThread.h&quot;
+#endif
+
</ins><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><span class="cx"> static void releaseNoncriticalMemory()
</span><span class="lines">@@ -128,6 +135,88 @@
</span><span class="cx"> #endif
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+#if !RELEASE_LOG_DISABLED
+static unsigned pageCount()
+{
+    unsigned count = 0;
+    Page::forEachPage([&amp;] (Page&amp; page) {
+        if (!page.isUtilityPage())
+            ++count;
+    });
+    return count;
+}
+#endif
+
+void logMemoryStatisticsAtTimeOfDeath()
+{
+#if !RELEASE_LOG_DISABLED
+#if PLATFORM(COCOA)
+    auto pageSize = vmPageSize();
+    auto pages = pagesPerVMTag();
+
+    RELEASE_LOG(MemoryPressure, &quot;Dirty memory per VM tag at time of death:&quot;);
+    for (unsigned i = 0; i &lt; 256; ++i) {
+        size_t dirty = pages[i].dirty * pageSize;
+        if (!dirty)
+            continue;
+        String tagName = displayNameForVMTag(i);
+        if (!tagName)
+            tagName = String::format(&quot;Tag %u&quot;, i);
+        RELEASE_LOG(MemoryPressure, &quot;%16s: %lu MB&quot;, tagName.latin1().data(), dirty / MB);
+    }
+#endif
+
+    auto&amp; vm = commonVM();
+    RELEASE_LOG(MemoryPressure, &quot;Memory usage statistics at time of death:&quot;);
+    RELEASE_LOG(MemoryPressure, &quot;GC heap size: %zu&quot;, vm.heap.size());
+    RELEASE_LOG(MemoryPressure, &quot;GC heap extra memory size: %zu&quot;, vm.heap.extraMemorySize());
+#if ENABLE(RESOURCE_USAGE)
+    RELEASE_LOG(MemoryPressure, &quot;GC heap external memory: %zu&quot;, vm.heap.externalMemorySize());
+#endif
+    RELEASE_LOG(MemoryPressure, &quot;Global object count: %zu&quot;, vm.heap.globalObjectCount());
+
+    RELEASE_LOG(MemoryPressure, &quot;Page count: %u&quot;, pageCount());
+    RELEASE_LOG(MemoryPressure, &quot;Document count: %u&quot;, Document::allDocuments().size());
+    RELEASE_LOG(MemoryPressure, &quot;Live JavaScript objects:&quot;);
+    for (auto&amp; it : *vm.heap.objectTypeCounts())
+        RELEASE_LOG(MemoryPressure, &quot;  %s: %d&quot;, it.key, it.value);
+#endif
+}
+
+void didExceedMemoryLimitAndFailedToRecover()
+{
+    RELEASE_LOG(MemoryPressure, &quot;Crashing non-visible process due to excessive memory usage + inability to free up memory below panic threshold.&quot;);
+    logMemoryStatisticsAtTimeOfDeath();
+    CRASH();
+}
+
+bool processIsEligibleForMemoryKill()
+{
+    bool hasVisiblePages = false;
+    bool hasAudiblePages = false;
+    bool hasMainFrameNavigatedInTheLastHour = false;
+
+    auto now = MonotonicTime::now();
+    Page::forEachPage([&amp;] (Page&amp; page) {
+        if (page.isUtilityPage())
+            return;
+        if (page.isVisible())
+            hasVisiblePages = true;
+        if (page.activityState() &amp; ActivityState::IsAudible)
+            hasAudiblePages = true;
+        if (auto timeOfLastCompletedLoad = page.mainFrame().timeOfLastCompletedLoad()) {
+            if (now - timeOfLastCompletedLoad &lt;= Seconds::fromMinutes(60))
+                hasMainFrameNavigatedInTheLastHour = true;
+        }
+    });
+
+    bool eligible = !hasVisiblePages &amp;&amp; !hasAudiblePages &amp;&amp; !hasMainFrameNavigatedInTheLastHour;
+    if (!eligible)
+        RELEASE_LOG(MemoryPressure, &quot;Process not eligible for panic memory kill. Reasons: hasVisiblePages=%u, hasAudiblePages=%u, hasMainFrameNavigatedInTheLastHour=%u&quot;, hasVisiblePages, hasAudiblePages, hasMainFrameNavigatedInTheLastHour);
+
+    return eligible;
+}
+
</ins><span class="cx"> #if !PLATFORM(COCOA)
</span><span class="cx"> void platformReleaseMemory(Critical) { }
</span><span class="cx"> void jettisonExpensiveObjectsOnTopLevelNavigation() { }
</span></span></pre></div>
<a id="trunkSourceWebCorepageMemoryReleaseh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/MemoryRelease.h (211621 => 211622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/page/MemoryRelease.h        2017-02-03 06:30:30 UTC (rev 211621)
+++ trunk/Source/WebCore/page/MemoryRelease.h        2017-02-03 07:25:24 UTC (rev 211622)
</span><span class="lines">@@ -33,5 +33,8 @@
</span><span class="cx"> void platformReleaseMemory(Critical);
</span><span class="cx"> void jettisonExpensiveObjectsOnTopLevelNavigation();
</span><span class="cx"> WEBCORE_EXPORT void registerMemoryReleaseNotifyCallbacks();
</span><ins>+WEBCORE_EXPORT void logMemoryStatisticsAtTimeOfDeath();
+WEBCORE_EXPORT NO_RETURN_DUE_TO_CRASH void didExceedMemoryLimitAndFailedToRecover();
+WEBCORE_EXPORT bool processIsEligibleForMemoryKill();
</ins><span class="cx"> 
</span><span class="cx"> } // namespace WebCore
</span></span></pre></div>
<a id="trunkSourceWebCorepageResourceUsageThreadh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/ResourceUsageThread.h (211621 => 211622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/page/ResourceUsageThread.h        2017-02-03 06:30:30 UTC (rev 211621)
+++ trunk/Source/WebCore/page/ResourceUsageThread.h        2017-02-03 07:25:24 UTC (rev 211622)
</span><span class="lines">@@ -80,6 +80,7 @@
</span><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> const char* displayNameForVMTag(unsigned);
</span><ins>+size_t vmPageSize();
</ins><span class="cx"> std::array&lt;TagInfo, 256&gt; pagesPerVMTag();
</span><span class="cx"> void logFootprintComparison(const std::array&lt;TagInfo, 256&gt;&amp;, const std::array&lt;TagInfo, 256&gt;&amp;);
</span><span class="cx"> #endif
</span></span></pre></div>
<a id="trunkSourceWebCorepagecocoaResourceUsageThreadCocoamm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/page/cocoa/ResourceUsageThreadCocoa.mm (211621 => 211622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/page/cocoa/ResourceUsageThreadCocoa.mm        2017-02-03 06:30:30 UTC (rev 211621)
+++ trunk/Source/WebCore/page/cocoa/ResourceUsageThreadCocoa.mm        2017-02-03 07:25:24 UTC (rev 211622)
</span><span class="lines">@@ -37,7 +37,7 @@
</span><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><del>-static size_t vmPageSize()
</del><ins>+size_t vmPageSize()
</ins><span class="cx"> {
</span><span class="cx"> #if PLATFORM(IOS) &amp;&amp; __IPHONE_OS_VERSION_MIN_REQUIRED &gt;= 100000
</span><span class="cx">     return vm_kernel_page_size;
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformMemoryPressureHandlercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/MemoryPressureHandler.cpp (211621 => 211622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/MemoryPressureHandler.cpp        2017-02-03 06:30:30 UTC (rev 211621)
+++ trunk/Source/WebCore/platform/MemoryPressureHandler.cpp        2017-02-03 07:25:24 UTC (rev 211622)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2011, 2014 Apple Inc. All Rights Reserved.
</del><ins>+ * Copyright (C) 2011-2017 Apple Inc. All Rights Reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -27,6 +27,7 @@
</span><span class="cx"> #include &quot;MemoryPressureHandler.h&quot;
</span><span class="cx"> 
</span><span class="cx"> #include &quot;Logging.h&quot;
</span><ins>+#include &lt;wtf/MemoryFootprint.h&gt;
</ins><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><span class="lines">@@ -45,6 +46,117 @@
</span><span class="cx"> {
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void MemoryPressureHandler::setShouldUsePeriodicMemoryMonitor(bool use)
+{
+    if (use) {
+        m_measurementTimer = std::make_unique&lt;RunLoop::Timer&lt;MemoryPressureHandler&gt;&gt;(RunLoop::main(), this, &amp;MemoryPressureHandler::measurementTimerFired);
+        m_measurementTimer-&gt;startRepeating(30);
+    } else
+        m_measurementTimer = nullptr;
+}
+
+#if !RELEASE_LOG_DISABLED
+static const char* toString(MemoryUsagePolicy policy)
+{
+    switch (policy) {
+    case MemoryUsagePolicy::Unrestricted: return &quot;Unrestricted&quot;;
+    case MemoryUsagePolicy::Conservative: return &quot;Conservative&quot;;
+    case MemoryUsagePolicy::Strict: return &quot;Strict&quot;;
+    case MemoryUsagePolicy::Panic: return &quot;Panic&quot;;
+    }
+}
+#endif
+
+static size_t thresholdForPolicy(MemoryUsagePolicy policy)
+{
+    switch (policy) {
+    case MemoryUsagePolicy::Conservative:
+        return 1 * GB;
+    case MemoryUsagePolicy::Strict:
+        return 2 * GB;
+    case MemoryUsagePolicy::Panic:
+#if CPU(X86_64) || CPU(ARM64)
+        return 4 * GB;
+#else
+        return 3 * GB;
+#endif
+    case MemoryUsagePolicy::Unrestricted:
+    default:
+        ASSERT_NOT_REACHED();
+        return 0;
+    }
+}
+
+static MemoryUsagePolicy policyForFootprint(size_t footprint)
+{
+    if (footprint &gt;= thresholdForPolicy(MemoryUsagePolicy::Panic))
+        return MemoryUsagePolicy::Panic;
+    if (footprint &gt;= thresholdForPolicy(MemoryUsagePolicy::Strict))
+        return MemoryUsagePolicy::Strict;
+    if (footprint &gt;= thresholdForPolicy(MemoryUsagePolicy::Conservative))
+        return MemoryUsagePolicy::Conservative;
+    return MemoryUsagePolicy::Unrestricted;
+}
+
+void MemoryPressureHandler::measurementTimerFired()
+{
+    auto footprint = memoryFootprint();
+    if (!footprint)
+        return;
+
+    RELEASE_LOG(MemoryPressure, &quot;Current memory footprint: %lu MB&quot;, footprint.value() / MB);
+
+    auto newPolicy = policyForFootprint(footprint.value());
+    if (newPolicy == m_memoryUsagePolicy) {
+        if (m_memoryUsagePolicy != MemoryUsagePolicy::Panic)
+            return;
+        RELEASE_LOG(MemoryPressure, &quot;Memory usage still above panic threshold&quot;);
+    } else
+        RELEASE_LOG(MemoryPressure, &quot;Memory usage policy changed: %s -&gt; %s&quot;, toString(m_memoryUsagePolicy), toString(newPolicy));
+
+    m_memoryUsagePolicy = newPolicy;
+
+    if (newPolicy == MemoryUsagePolicy::Unrestricted)
+        return;
+
+    if (newPolicy == MemoryUsagePolicy::Conservative) {
+        // FIXME: Implement this policy by choosing which caches should respect it, and hooking them up.
+        return;
+    }
+
+    if (newPolicy == MemoryUsagePolicy::Strict) {
+        RELEASE_LOG(MemoryPressure, &quot;Attempting to reduce memory footprint by freeing less important objects.&quot;);
+        releaseMemory(Critical::No, Synchronous::No);
+        return;
+    }
+
+    RELEASE_ASSERT(newPolicy == MemoryUsagePolicy::Panic);
+
+    RELEASE_LOG(MemoryPressure, &quot;Attempting to reduce memory footprint by freeing more important objects.&quot;);
+    if (m_processIsEligibleForMemoryKillCallback) {
+        if (!m_processIsEligibleForMemoryKillCallback()) {
+            releaseMemory(Critical::Yes, Synchronous::No);
+            return;
+        }
+    }
+
+    releaseMemory(Critical::Yes, Synchronous::Yes);
+
+    // Remeasure footprint to see how well the pressure handler did.
+    footprint = memoryFootprint();
+    RELEASE_ASSERT(footprint);
+
+    RELEASE_LOG(MemoryPressure, &quot;New memory footprint: %lu MB&quot;, footprint.value() / MB);
+    if (footprint.value() &lt; thresholdForPolicy(MemoryUsagePolicy::Panic)) {
+        m_memoryUsagePolicy = policyForFootprint(footprint.value());
+        RELEASE_LOG(MemoryPressure, &quot;Pressure reduced below panic threshold. New memory usage policy: %s&quot;, toString(m_memoryUsagePolicy));
+        return;
+    }
+
+    if (m_memoryKillCallback)
+        m_memoryKillCallback();
+}
+
</ins><span class="cx"> void MemoryPressureHandler::beginSimulatedMemoryPressure()
</span><span class="cx"> {
</span><span class="cx">     m_isSimulatingMemoryPressure = true;
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformMemoryPressureHandlerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/MemoryPressureHandler.h (211621 => 211622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/MemoryPressureHandler.h        2017-02-03 06:30:30 UTC (rev 211621)
+++ trunk/Source/WebCore/platform/MemoryPressureHandler.h        2017-02-03 07:25:24 UTC (rev 211622)
</span><span class="lines">@@ -34,16 +34,16 @@
</span><span class="cx"> #include &lt;wtf/Forward.h&gt;
</span><span class="cx"> #include &lt;wtf/NeverDestroyed.h&gt;
</span><span class="cx"> #include &lt;wtf/Optional.h&gt;
</span><ins>+#include &lt;wtf/RunLoop.h&gt;
</ins><span class="cx"> 
</span><span class="cx"> #if PLATFORM(IOS)
</span><span class="cx"> #include &lt;wtf/Lock.h&gt;
</span><span class="cx"> #include &lt;wtf/ThreadingPrimitives.h&gt;
</span><del>-#elif OS(LINUX)
-#include &lt;wtf/RunLoop.h&gt;
</del><ins>+#endif
+
</ins><span class="cx"> #if USE(GLIB)
</span><span class="cx"> #include &lt;wtf/glib/GRefPtr.h&gt;
</span><span class="cx"> #endif
</span><del>-#endif
</del><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><span class="lines">@@ -55,6 +55,13 @@
</span><span class="cx"> };
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><ins>+enum class MemoryUsagePolicy {
+    Unrestricted, // Allocate as much as you want
+    Conservative, // Maybe you don't cache every single thing
+    Strict, // Time to start pinching pennies for real
+    Panic, // OH GOD WE'RE SINKING, THROW EVERYTHING OVERBOARD
+};
+
</ins><span class="cx"> enum class Critical { No, Yes };
</span><span class="cx"> enum class Synchronous { No, Yes };
</span><span class="cx"> 
</span><span class="lines">@@ -68,12 +75,24 @@
</span><span class="cx"> 
</span><span class="cx">     WEBCORE_EXPORT void install();
</span><span class="cx"> 
</span><ins>+    WEBCORE_EXPORT void setShouldUsePeriodicMemoryMonitor(bool);
+
+    void setMemoryKillCallback(WTF::Function&lt;void()&gt; function) { m_memoryKillCallback = WTFMove(function); }
+    void setProcessIsEligibleForMemoryKillCallback(WTF::Function&lt;bool()&gt; function) { m_processIsEligibleForMemoryKillCallback = WTFMove(function); }
+
</ins><span class="cx">     void setLowMemoryHandler(LowMemoryHandler&amp;&amp; handler)
</span><span class="cx">     {
</span><span class="cx">         m_lowMemoryHandler = WTFMove(handler);
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    bool isUnderMemoryPressure() const { return m_underMemoryPressure || m_isSimulatingMemoryPressure; }
</del><ins>+    bool isUnderMemoryPressure() const
+    {
+        return m_underMemoryPressure
+#if PLATFORM(MAC)
+            || m_memoryUsagePolicy &gt;= MemoryUsagePolicy::Strict
+#endif
+            || m_isSimulatingMemoryPressure;
+    }
</ins><span class="cx">     void setUnderMemoryPressure(bool b) { m_underMemoryPressure = b; }
</span><span class="cx"> 
</span><span class="cx"> #if PLATFORM(IOS)
</span><span class="lines">@@ -149,6 +168,9 @@
</span><span class="cx">     void respondToMemoryPressure(Critical, Synchronous = Synchronous::No);
</span><span class="cx">     void platformReleaseMemory(Critical);
</span><span class="cx"> 
</span><ins>+    NO_RETURN_DUE_TO_CRASH void didExceedMemoryLimitAndFailedToRecover();
+    void measurementTimerFired();
+
</ins><span class="cx"> #if OS(LINUX)
</span><span class="cx">     class EventFDPoller {
</span><span class="cx">         WTF_MAKE_NONCOPYABLE(EventFDPoller); WTF_MAKE_FAST_ALLOCATED;
</span><span class="lines">@@ -170,12 +192,16 @@
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx">     bool m_installed { false };
</span><del>-    time_t m_lastRespondTime { 0 };
</del><span class="cx">     LowMemoryHandler m_lowMemoryHandler;
</span><span class="cx"> 
</span><span class="cx">     std::atomic&lt;bool&gt; m_underMemoryPressure;
</span><span class="cx">     bool m_isSimulatingMemoryPressure { false };
</span><span class="cx"> 
</span><ins>+    std::unique_ptr&lt;RunLoop::Timer&lt;MemoryPressureHandler&gt;&gt; m_measurementTimer;
+    MemoryUsagePolicy m_memoryUsagePolicy { MemoryUsagePolicy::Unrestricted };
+    WTF::Function&lt;void()&gt; m_memoryKillCallback;
+    WTF::Function&lt;bool()&gt; m_processIsEligibleForMemoryKillCallback;
+
</ins><span class="cx"> #if PLATFORM(IOS)
</span><span class="cx">     // FIXME: Can we share more of this with OpenSource?
</span><span class="cx">     uint32_t m_memoryPressureReason { MemoryPressureReasonNone };
</span></span></pre></div>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (211621 => 211622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/ChangeLog        2017-02-03 06:30:30 UTC (rev 211621)
+++ trunk/Source/WebKit2/ChangeLog        2017-02-03 07:25:24 UTC (rev 211622)
</span><span class="lines">@@ -1,3 +1,16 @@
</span><ins>+2017-02-02  Andreas Kling  &lt;akling@apple.com&gt;
+
+        [Mac] In-process memory pressure monitor for WebContent processes AKA websam
+        &lt;https://webkit.org/b/167491&gt;
+        &lt;rdar://problem/30116072&gt;
+
+        Reviewed by Antti Koivisto.
+
+        Enable the in-process memory monitor for WebContent processes on macOS 10.12+
+
+        * WebProcess/WebProcess.cpp:
+        (WebKit::WebProcess::initializeWebProcess):
+
</ins><span class="cx"> 2017-02-02  Commit Queue  &lt;commit-queue@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Unreviewed, rolling out r211535, r211566, and r211568.
</span></span></pre></div>
<a id="trunkSourceWebKit2WebProcessWebProcesscpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/WebProcess/WebProcess.cpp (211621 => 211622)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/WebProcess/WebProcess.cpp        2017-02-03 06:30:30 UTC (rev 211621)
+++ trunk/Source/WebKit2/WebProcess/WebProcess.cpp        2017-02-03 07:25:24 UTC (rev 211622)
</span><span class="lines">@@ -261,6 +261,15 @@
</span><span class="cx">         memoryPressureHandler.setLowMemoryHandler([] (Critical critical, Synchronous synchronous) {
</span><span class="cx">             WebCore::releaseMemory(critical, synchronous);
</span><span class="cx">         });
</span><ins>+#if PLATFORM(MAC) &amp;&amp; __MAC_OS_X_VERSION_MAX_ALLOWED &gt;= 101200
+        memoryPressureHandler.setShouldUsePeriodicMemoryMonitor(true);
+        memoryPressureHandler.setMemoryKillCallback([] () {
+            WebCore::didExceedMemoryLimitAndFailedToRecover();
+        });
+        memoryPressureHandler.setProcessIsEligibleForMemoryKillCallback([] () {
+            return WebCore::processIsEligibleForMemoryKill();
+        });
+#endif
</ins><span class="cx">         memoryPressureHandler.install();
</span><span class="cx">     }
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>