Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Source/JavaScriptCore:
[Mac] In-process memory pressure monitor for WebContent processes.
<https://webkit.org/b/167491>
<rdar://problem/30116072>

Reviewed by Antti Koivisto.

Remove the sloppy "max live heap size" 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:
[Mac] In-process memory pressure monitor for WebContent processes AKA websam
<https://webkit.org/b/167491>
<rdar://problem/30116072>

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 "isUnderMemoryPressure()" API will return true.

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

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

At Panic, we run the "critical" 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:
[Mac] In-process memory pressure monitor for WebContent processes.
<https://webkit.org/b/167491>
<rdar://problem/30116072>

Reviewed by Antti Koivisto.

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

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

Source/WTF:
[Mac] In-process memory pressure monitor for WebContent processes.
<https://webkit.org/b/167491>
<rdar://problem/30116072>

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:


Canonical link: https://commits.webkit.org/184785@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@211571 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
Andreas Kling committed Feb 2, 2017
1 parent 58e85ee commit 7532df3
Show file tree
Hide file tree
Showing 21 changed files with 468 additions and 27 deletions.
17 changes: 17 additions & 0 deletions Source/JavaScriptCore/ChangeLog
@@ -1,3 +1,20 @@
2017-02-02 Andreas Kling <akling@apple.com>

[Mac] In-process memory pressure monitor for WebContent processes.
<https://webkit.org/b/167491>
<rdar://problem/30116072>

Reviewed by Antti Koivisto.

Remove the sloppy "max live heap size" 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.

2017-02-02 Joseph Pecoraro <pecoraro@apple.com>

Removed unused m_errorHandlingModeReentry from Interpreter
Expand Down
8 changes: 0 additions & 8 deletions Source/JavaScriptCore/heap/Heap.cpp
Expand Up @@ -1726,11 +1726,6 @@ void Heap::notifyIncrementalSweeper()
m_sweeper->startSweeping();
}

NEVER_INLINE void Heap::didExceedMaxLiveSize()
{
CRASH();
}

void Heap::updateAllocationLimits()
{
static const bool verbose = false;
Expand Down Expand Up @@ -1762,9 +1757,6 @@ void Heap::updateAllocationLimits()

if (verbose)
dataLog("extraMemorySize() = ", extraMemorySize(), ", currentHeapSize = ", currentHeapSize, "\n");

if (m_maxLiveSize && currentHeapSize > m_maxLiveSize)
didExceedMaxLiveSize();

if (Options::gcMaxHeapSize() && currentHeapSize > Options::gcMaxHeapSize())
HeapStatistics::exitWithFailure();
Expand Down
6 changes: 0 additions & 6 deletions Source/JavaScriptCore/heap/Heap.h
Expand Up @@ -132,9 +132,6 @@ class Heap {

VM* vm() const;

// Set a hard limit where JSC will crash if live heap size exceeds it.
void setMaxLiveSize(size_t size) { m_maxLiveSize = size; }

MarkedSpace& objectSpace() { return m_objectSpace; }
MachineThreads& machineThreads() { return m_machineThreads; }

Expand Down Expand Up @@ -603,9 +600,6 @@ class Heap {
size_t m_blockBytesAllocated { 0 };
size_t m_externalMemorySize { 0 };
#endif

NO_RETURN_DUE_TO_CRASH void didExceedMaxLiveSize();
size_t m_maxLiveSize { 0 };

std::unique_ptr<MutatorScheduler> m_scheduler;

Expand Down
16 changes: 16 additions & 0 deletions Source/WTF/ChangeLog
@@ -1,3 +1,19 @@
2017-02-02 Andreas Kling <akling@apple.com>

[Mac] In-process memory pressure monitor for WebContent processes.
<https://webkit.org/b/167491>
<rdar://problem/30116072>

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:

2017-02-01 Wenson Hsieh <wenson_hsieh@apple.com>

Unreviewed, fix the WebKit nightly open source build
Expand Down
8 changes: 8 additions & 0 deletions Source/WTF/WTF.xcodeproj/project.pbxproj
Expand Up @@ -337,6 +337,8 @@
A8A47487151A825B004123FF /* WTFThreadData.h in Headers */ = {isa = PBXBuildFile; fileRef = A8A4737B151A825B004123FF /* WTFThreadData.h */; };
A8A4748C151A8264004123FF /* config.h in Headers */ = {isa = PBXBuildFile; fileRef = A8A4748B151A8264004123FF /* config.h */; };
AD7C434B1DD2A4A70026888B /* Expected.h in Headers */ = {isa = PBXBuildFile; fileRef = AD7C434A1DD2A4A70026888B /* Expected.h */; };
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 */; };
B38FD7BD168953E80065C969 /* FeatureDefines.h in Headers */ = {isa = PBXBuildFile; fileRef = B38FD7BC168953E80065C969 /* FeatureDefines.h */; };
C4F8A93719C65EB400B2B15D /* Stopwatch.h in Headers */ = {isa = PBXBuildFile; fileRef = C4F8A93619C65EB400B2B15D /* Stopwatch.h */; };
C8B0E1A1E01A486EB95E0D11 /* IndexSet.h in Headers */ = {isa = PBXBuildFile; fileRef = 3137E1D7DBD84AC38FAE4D34 /* IndexSet.h */; };
Expand Down Expand Up @@ -719,6 +721,8 @@
A8A4737B151A825B004123FF /* WTFThreadData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WTFThreadData.h; sourceTree = "<group>"; };
A8A4748B151A8264004123FF /* config.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = config.h; sourceTree = "<group>"; };
AD7C434A1DD2A4A70026888B /* Expected.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Expected.h; sourceTree = "<group>"; };
ADF2CE641E39F106006889DB /* MemoryFootprint.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MemoryFootprint.h; sourceTree = "<group>"; };
ADF2CE651E39F106006889DB /* MemoryFootprint.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MemoryFootprint.cpp; sourceTree = "<group>"; };
B38FD7BC168953E80065C969 /* FeatureDefines.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = FeatureDefines.h; sourceTree = "<group>"; };
C4F8A93619C65EB400B2B15D /* Stopwatch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Stopwatch.h; sourceTree = "<group>"; };
CD5497AA15857D0300B5BC30 /* MediaTime.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MediaTime.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1005,6 +1009,8 @@
A8A472CB151A825B004123FF /* MD5.h */,
CD5497AA15857D0300B5BC30 /* MediaTime.cpp */,
CD5497AB15857D0300B5BC30 /* MediaTime.h */,
ADF2CE641E39F106006889DB /* MemoryFootprint.h */,
ADF2CE651E39F106006889DB /* MemoryFootprint.cpp */,
A8A472CC151A825B004123FF /* MessageQueue.h */,
A8A472CD151A825B004123FF /* MetaAllocator.cpp */,
A8A472CE151A825B004123FF /* MetaAllocator.h */,
Expand Down Expand Up @@ -1494,6 +1500,7 @@
A8A4741C151A825B004123FF /* RefPtr.h in Headers */,
A8A4741E151A825B004123FF /* RetainPtr.h in Headers */,
2CDED0F418115C85004DBA70 /* RunLoop.h in Headers */,
ADF2CE661E39F106006889DB /* MemoryFootprint.h in Headers */,
1469419216EAAF6D0024E146 /* RunLoopTimer.h in Headers */,
A5098B001C169E0700087797 /* SandboxSPI.h in Headers */,
14F3B0F715E45E4600210069 /* SaturatedArithmetic.h in Headers */,
Expand Down Expand Up @@ -1701,6 +1708,7 @@
1ACADD841884480100D8B71D /* DeprecatedSymbolsUsedBySafari.mm in Sources */,
A8A473AE151A825B004123FF /* diy-fp.cc in Sources */,
A8A473B0151A825B004123FF /* double-conversion.cc in Sources */,
ADF2CE671E39F106006889DB /* MemoryFootprint.cpp in Sources */,
A8A473BA151A825B004123FF /* dtoa.cpp in Sources */,
A8A473B3151A825B004123FF /* fast-dtoa.cc in Sources */,
0F7C5FB61D885CF20044F5E2 /* FastBitVector.cpp in Sources */,
Expand Down
2 changes: 2 additions & 0 deletions Source/WTF/wtf/CMakeLists.txt
Expand Up @@ -64,6 +64,7 @@ set(WTF_HEADERS
MallocPtr.h
MathExtras.h
MediaTime.h
MemoryFootprint.h
MessageQueue.h
MetaAllocator.h
MetaAllocatorHandle.h
Expand Down Expand Up @@ -204,6 +205,7 @@ set(WTF_SOURCES
MD5.cpp
MainThread.cpp
MediaTime.cpp
MemoryFootprint.cpp
MetaAllocator.cpp
MonotonicTime.cpp
NumberOfCores.cpp
Expand Down
50 changes: 50 additions & 0 deletions Source/WTF/wtf/MemoryFootprint.cpp
@@ -0,0 +1,50 @@
/*
* 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 "config.h"
#include "MemoryFootprint.h"

#if PLATFORM(COCOA)
#include <mach/mach.h>
#include <mach/task_info.h>
#endif

namespace WTF {

std::optional<size_t> 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) &vmInfo, &count);
if (result != KERN_SUCCESS)
return std::nullopt;
return static_cast<size_t>(vmInfo.phys_footprint);
#else
return std::nullopt;
#endif
}

}
37 changes: 37 additions & 0 deletions Source/WTF/wtf/MemoryFootprint.h
@@ -0,0 +1,37 @@
/*
* 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 <wtf/Optional.h>

namespace WTF {

WTF_EXPORT_PRIVATE std::optional<size_t> memoryFootprint();

}

using WTF::memoryFootprint;

65 changes: 65 additions & 0 deletions Source/WebCore/ChangeLog
@@ -1,3 +1,68 @@
2017-02-02 Andreas Kling <akling@apple.com>

[Mac] In-process memory pressure monitor for WebContent processes AKA websam
<https://webkit.org/b/167491>
<rdar://problem/30116072>

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 "isUnderMemoryPressure()" API will return true.

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

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

At Panic, we run the "critical" 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):

2017-02-02 Chris Dumez <cdumez@apple.com>

[Crash] com.apple.WebKit.WebContent at WebKit: WebKit::WebPage::fromCorePage()
Expand Down
4 changes: 0 additions & 4 deletions Source/WebCore/bindings/js/CommonVM.cpp
Expand Up @@ -46,10 +46,6 @@ VM& commonVMSlow()

ScriptController::initializeThreading();
g_commonVMOrNull = &VM::createLeaked(LargeHeap).leakRef();
#if CPU(X86_64) || CPU(ARM64)
static const size_t maxGCHeapSize = 4 * GB;
g_commonVMOrNull->heap.setMaxLiveSize(maxGCHeapSize);
#endif
g_commonVMOrNull->heap.acquireAccess(); // At any time, we may do things that affect the GC.
#if !PLATFORM(IOS)
g_commonVMOrNull->setExclusiveThread(std::this_thread::get_id());
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/loader/FrameLoader.cpp
Expand Up @@ -1742,7 +1742,7 @@ void FrameLoader::setState(FrameState newState)
if (m_documentLoader)
m_documentLoader->stopRecordingResponses();
if (m_frame.isMainFrame() && oldState != newState)
static_cast<MainFrame&>(m_frame).performanceLogging().didReachPointOfInterest(PerformanceLogging::MainFrameLoadCompleted);
static_cast<MainFrame&>(m_frame).didCompleteLoad();
}
}

Expand Down
6 changes: 6 additions & 0 deletions Source/WebCore/page/MainFrame.cpp
Expand Up @@ -95,6 +95,12 @@ void MainFrame::dropChildren()
tree().removeChild(*child);
}

void MainFrame::didCompleteLoad()
{
m_timeOfLastCompletedLoad = MonotonicTime::now();
performanceLogging().didReachPointOfInterest(PerformanceLogging::MainFrameLoadCompleted);
}

#if PLATFORM(MAC)
ScrollLatchingState* MainFrame::latchingState()
{
Expand Down
5 changes: 5 additions & 0 deletions Source/WebCore/page/MainFrame.h
Expand Up @@ -69,6 +69,9 @@ class MainFrame final : public Frame {

PerformanceLogging& performanceLogging() const { return *m_performanceLogging; }

void didCompleteLoad();
MonotonicTime timeOfLastCompletedLoad() const { return m_timeOfLastCompletedLoad; }

private:
MainFrame(Page&, PageConfiguration&);

Expand All @@ -91,6 +94,8 @@ class MainFrame final : public Frame {
#endif

std::unique_ptr<PerformanceLogging> m_performanceLogging;

MonotonicTime m_timeOfLastCompletedLoad;
};

} // namespace WebCore

0 comments on commit 7532df3

Please sign in to comment.