<!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>[194204] 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/194204">194204</a></dd>
<dt>Author</dt> <dd>akling@apple.com</dd>
<dt>Date</dt> <dd>2015-12-16 18:55:02 -0800 (Wed, 16 Dec 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Give kernel VM some hints about non-live memory-cached resources.
&lt;https://webkit.org/b/152362&gt;

Reviewed by Geoffrey Garen.

Source/WebCore:

When a file-backed CachedResource has no live clients left, and is only being kept alive
because it's cached in the MemoryCache, give the OS a hint that we probably won't need
that memory very soon.

Normally this doesn't do anything, but in case the system comes under memory pressure,
the kernel can prioritize eviction of such clean-but-unneeded pages, which prevents us
from throwing other processes under the bus too soon.

* loader/cache/CachedResource.cpp:
(WebCore::CachedResource::deleteIfPossible):
* platform/SharedBuffer.cpp:
(WebCore::SharedBuffer::hintMemoryNotNeededSoon):
* platform/SharedBuffer.h:
* platform/cf/SharedBufferCF.cpp:
(WebCore::SharedBuffer::hintMemoryNotNeededSoon):

Source/WTF:

Add an API to OSAllocator for hinting to the OS that a range of memory
is not expected to be used anytime soon.

* wtf/OSAllocator.h:
* wtf/OSAllocatorPosix.cpp:
(WTF::OSAllocator::hintMemoryNotNeededSoon):
* wtf/OSAllocatorWin.cpp:
(WTF::OSAllocator::hintMemoryNotNeededSoon):
* wtf/Platform.h:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWTFChangeLog">trunk/Source/WTF/ChangeLog</a></li>
<li><a href="#trunkSourceWTFwtfOSAllocatorh">trunk/Source/WTF/wtf/OSAllocator.h</a></li>
<li><a href="#trunkSourceWTFwtfOSAllocatorPosixcpp">trunk/Source/WTF/wtf/OSAllocatorPosix.cpp</a></li>
<li><a href="#trunkSourceWTFwtfOSAllocatorWincpp">trunk/Source/WTF/wtf/OSAllocatorWin.cpp</a></li>
<li><a href="#trunkSourceWTFwtfPlatformh">trunk/Source/WTF/wtf/Platform.h</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreloadercacheCachedResourcecpp">trunk/Source/WebCore/loader/cache/CachedResource.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformSharedBuffercpp">trunk/Source/WebCore/platform/SharedBuffer.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformSharedBufferh">trunk/Source/WebCore/platform/SharedBuffer.h</a></li>
<li><a href="#trunkSourceWebCoreplatformcfSharedBufferCFcpp">trunk/Source/WebCore/platform/cf/SharedBufferCF.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWTFChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/ChangeLog (194203 => 194204)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/ChangeLog        2015-12-17 02:26:06 UTC (rev 194203)
+++ trunk/Source/WTF/ChangeLog        2015-12-17 02:55:02 UTC (rev 194204)
</span><span class="lines">@@ -1,3 +1,20 @@
</span><ins>+2015-12-16  Andreas Kling  &lt;akling@apple.com&gt;
+
+        Give kernel VM some hints about non-live memory-cached resources.
+        &lt;https://webkit.org/b/152362&gt;
+
+        Reviewed by Geoffrey Garen.
+
+        Add an API to OSAllocator for hinting to the OS that a range of memory
+        is not expected to be used anytime soon.
+
+        * wtf/OSAllocator.h:
+        * wtf/OSAllocatorPosix.cpp:
+        (WTF::OSAllocator::hintMemoryNotNeededSoon):
+        * wtf/OSAllocatorWin.cpp:
+        (WTF::OSAllocator::hintMemoryNotNeededSoon):
+        * wtf/Platform.h:
+
</ins><span class="cx"> 2015-12-16  Alex Christensen  &lt;achristensen@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Fix internal Windows build
</span></span></pre></div>
<a id="trunkSourceWTFwtfOSAllocatorh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/OSAllocator.h (194203 => 194204)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/OSAllocator.h        2015-12-17 02:26:06 UTC (rev 194203)
+++ trunk/Source/WTF/wtf/OSAllocator.h        2015-12-17 02:55:02 UTC (rev 194204)
</span><span class="lines">@@ -69,6 +69,9 @@
</span><span class="cx">     // This interface is provided since it may be possible to optimize this operation on some platforms.
</span><span class="cx">     template&lt;typename T&gt;
</span><span class="cx">     static T* reallocateCommitted(T*, size_t oldSize, size_t newSize, Usage = UnknownUsage, bool writable = true, bool executable = false);
</span><ins>+
+    // Hint to the OS that an address range is not expected to be accessed anytime soon.
+    WTF_EXPORT_PRIVATE static void hintMemoryNotNeededSoon(void*, size_t);
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> inline void* OSAllocator::reserveAndCommit(size_t reserveSize, size_t commitSize, Usage usage, bool writable, bool executable)
</span></span></pre></div>
<a id="trunkSourceWTFwtfOSAllocatorPosixcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/OSAllocatorPosix.cpp (194203 => 194204)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/OSAllocatorPosix.cpp        2015-12-17 02:26:06 UTC (rev 194203)
+++ trunk/Source/WTF/wtf/OSAllocatorPosix.cpp        2015-12-17 02:55:02 UTC (rev 194204)
</span><span class="lines">@@ -164,6 +164,16 @@
</span><span class="cx"> #endif
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void OSAllocator::hintMemoryNotNeededSoon(void* address, size_t bytes)
+{
+#if HAVE(MADV_DONTNEED)
+    while (madvise(address, bytes, MADV_DONTNEED) == -1 &amp;&amp; errno == EAGAIN) { }
+#else
+    UNUSED_PARAM(address);
+    UNUSED_PARAM(bytes);
+#endif
+}
+
</ins><span class="cx"> void OSAllocator::releaseDecommitted(void* address, size_t bytes)
</span><span class="cx"> {
</span><span class="cx">     int result = munmap(address, bytes);
</span></span></pre></div>
<a id="trunkSourceWTFwtfOSAllocatorWincpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/OSAllocatorWin.cpp (194203 => 194204)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/OSAllocatorWin.cpp        2015-12-17 02:26:06 UTC (rev 194203)
+++ trunk/Source/WTF/wtf/OSAllocatorWin.cpp        2015-12-17 02:55:02 UTC (rev 194204)
</span><span class="lines">@@ -93,6 +93,10 @@
</span><span class="cx">         CRASH();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void OSAllocator::hintMemoryNotNeededSoon(void*, size_t)
+{
+}
+
</ins><span class="cx"> } // namespace WTF
</span><span class="cx"> 
</span><span class="cx"> #endif // OS(WINDOWS)
</span></span></pre></div>
<a id="trunkSourceWTFwtfPlatformh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WTF/wtf/Platform.h (194203 => 194204)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WTF/wtf/Platform.h        2015-12-17 02:26:06 UTC (rev 194203)
+++ trunk/Source/WTF/wtf/Platform.h        2015-12-17 02:55:02 UTC (rev 194204)
</span><span class="lines">@@ -630,6 +630,7 @@
</span><span class="cx"> #define HAVE_DISPATCH_H 1
</span><span class="cx"> #define HAVE_MADV_FREE 1
</span><span class="cx"> #define HAVE_MADV_FREE_REUSE 1
</span><ins>+#define HAVE_MADV_DONTNEED 1
</ins><span class="cx"> #define HAVE_MERGESORT 1
</span><span class="cx"> #define HAVE_PTHREAD_SETNAME_NP 1
</span><span class="cx"> #define HAVE_READLINE 1
</span></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (194203 => 194204)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2015-12-17 02:26:06 UTC (rev 194203)
+++ trunk/Source/WebCore/ChangeLog        2015-12-17 02:55:02 UTC (rev 194204)
</span><span class="lines">@@ -1,3 +1,26 @@
</span><ins>+2015-12-16  Andreas Kling  &lt;akling@apple.com&gt;
+
+        Give kernel VM some hints about non-live memory-cached resources.
+        &lt;https://webkit.org/b/152362&gt;
+
+        Reviewed by Geoffrey Garen.
+
+        When a file-backed CachedResource has no live clients left, and is only being kept alive
+        because it's cached in the MemoryCache, give the OS a hint that we probably won't need
+        that memory very soon.
+
+        Normally this doesn't do anything, but in case the system comes under memory pressure,
+        the kernel can prioritize eviction of such clean-but-unneeded pages, which prevents us
+        from throwing other processes under the bus too soon.
+
+        * loader/cache/CachedResource.cpp:
+        (WebCore::CachedResource::deleteIfPossible):
+        * platform/SharedBuffer.cpp:
+        (WebCore::SharedBuffer::hintMemoryNotNeededSoon):
+        * platform/SharedBuffer.h:
+        * platform/cf/SharedBufferCF.cpp:
+        (WebCore::SharedBuffer::hintMemoryNotNeededSoon):
+
</ins><span class="cx"> 2015-12-16  Gyuyoung Kim  &lt;gyuyoung.kim@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Reduce PassRefPtr uses in dom - 2
</span></span></pre></div>
<a id="trunkSourceWebCoreloadercacheCachedResourcecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/loader/cache/CachedResource.cpp (194203 => 194204)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/loader/cache/CachedResource.cpp        2015-12-17 02:26:06 UTC (rev 194203)
+++ trunk/Source/WebCore/loader/cache/CachedResource.cpp        2015-12-17 02:55:02 UTC (rev 194204)
</span><span class="lines">@@ -504,10 +504,14 @@
</span><span class="cx"> 
</span><span class="cx"> bool CachedResource::deleteIfPossible()
</span><span class="cx"> {
</span><del>-    if (canDelete() &amp;&amp; !inCache()) {
-        InspectorInstrumentation::willDestroyCachedResource(*this);
-        delete this;
-        return true;
</del><ins>+    if (canDelete()) {
+        if (!inCache()) {
+            InspectorInstrumentation::willDestroyCachedResource(*this);
+            delete this;
+            return true;
+        }
+        if (m_data)
+            m_data-&gt;hintMemoryNotNeededSoon();
</ins><span class="cx">     }
</span><span class="cx">     return false;
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformSharedBuffercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/SharedBuffer.cpp (194203 => 194204)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/SharedBuffer.cpp        2015-12-17 02:26:06 UTC (rev 194203)
+++ trunk/Source/WebCore/platform/SharedBuffer.cpp        2015-12-17 02:55:02 UTC (rev 194204)
</span><span class="lines">@@ -302,6 +302,12 @@
</span><span class="cx">         m_buffer-&gt;data.clear();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+#if !USE(CF)
+void SharedBuffer::hintMemoryNotNeededSoon()
+{
+}
+#endif
+
</ins><span class="cx"> #if !USE(NETWORK_CFDATA_ARRAY_CALLBACK)
</span><span class="cx"> 
</span><span class="cx"> void SharedBuffer::copyBufferAndClear(char* destination, unsigned bytesToCopy) const
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformSharedBufferh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/SharedBuffer.h (194203 => 194204)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/SharedBuffer.h        2015-12-17 02:26:06 UTC (rev 194203)
+++ trunk/Source/WebCore/platform/SharedBuffer.h        2015-12-17 02:55:02 UTC (rev 194204)
</span><span class="lines">@@ -126,6 +126,8 @@
</span><span class="cx">         Vector&lt;char&gt; data;
</span><span class="cx">     };
</span><span class="cx"> 
</span><ins>+    void hintMemoryNotNeededSoon();
+
</ins><span class="cx"> private:
</span><span class="cx">     WEBCORE_EXPORT SharedBuffer();
</span><span class="cx">     explicit SharedBuffer(unsigned);
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformcfSharedBufferCFcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/cf/SharedBufferCF.cpp (194203 => 194204)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/cf/SharedBufferCF.cpp        2015-12-17 02:26:06 UTC (rev 194203)
+++ trunk/Source/WebCore/platform/cf/SharedBufferCF.cpp        2015-12-17 02:55:02 UTC (rev 194204)
</span><span class="lines">@@ -28,6 +28,7 @@
</span><span class="cx"> #include &quot;config.h&quot;
</span><span class="cx"> #include &quot;SharedBuffer.h&quot;
</span><span class="cx"> 
</span><ins>+#include &lt;wtf/OSAllocator.h&gt;
</ins><span class="cx"> #include &lt;wtf/cf/TypeCastsCF.h&gt;
</span><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="lines">@@ -73,6 +74,13 @@
</span><span class="cx">     return CFDataGetLength(m_cfData.get());
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void SharedBuffer::hintMemoryNotNeededSoon()
+{
+    if (!hasPlatformData())
+        return;
+    OSAllocator::hintMemoryNotNeededSoon(const_cast&lt;char*&gt;(platformData()), platformDataSize());
+}
+
</ins><span class="cx"> void SharedBuffer::maybeTransferPlatformData()
</span><span class="cx"> {
</span><span class="cx">     if (!m_cfData)
</span></span></pre>
</div>
</div>

</body>
</html>