haikuwebkit/Source/WTF/wtf/MemoryFootprint.h

36 lines
1.4 KiB
C
Raw Permalink Normal View History

[Mac] In-process memory pressure monitor for WebContent processes AKA websam <https://webkit.org/b/167491> <rdar://problem/30116072> Reviewed by Antti Koivisto. Source/JavaScriptCore: 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: 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: 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: Canonical link: https://commits.webkit.org/184825@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@211622 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2017-02-03 07:25:24 +00:00
/*
* 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
namespace WTF {
memoryFootprint should return size_t not optional<size_t> https://bugs.webkit.org/show_bug.cgi?id=188444 Reviewed by Simon Fraser. Source/WebCore: * page/cocoa/ResourceUsageOverlayCocoa.mm: (WebCore::ResourceUsageOverlay::platformDraw): Source/WTF: We're now going to return zero instead of returning nullopt on failure. There was a lot of code dancing around memoryFootprint failing for no good reason. Users of this API were previously doing this on failure: - Treating it as zero (this was the most common user). - Crashing. - Bailing out early and not changing our memory pressure state. This change has the effect that instead of not changing our memory pressure state on failure, we will go back to thinking we're not under memory pressure. Since we relied on this API not failing to do anything useful (like kill the process or release memory), this won't change our behavior here in a meaningful way. * wtf/MemoryFootprint.h: * wtf/MemoryPressureHandler.cpp: (WTF::MemoryPressureHandler::currentMemoryUsagePolicy): (WTF::MemoryPressureHandler::shrinkOrDie): (WTF::MemoryPressureHandler::measurementTimerFired): * wtf/cocoa/MemoryFootprintCocoa.cpp: (WTF::memoryFootprint): * wtf/linux/MemoryFootprintLinux.cpp: (WTF::memoryFootprint): * wtf/linux/MemoryPressureHandlerLinux.cpp: (WTF::MemoryPressureHandler::ReliefLogger::platformMemoryUsage): * wtf/win/MemoryFootprintWin.cpp: (WTF::memoryFootprint): Canonical link: https://commits.webkit.org/203546@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@234733 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2018-08-09 20:13:27 +00:00
WTF_EXPORT_PRIVATE size_t memoryFootprint();
[Mac] In-process memory pressure monitor for WebContent processes AKA websam <https://webkit.org/b/167491> <rdar://problem/30116072> Reviewed by Antti Koivisto. Source/JavaScriptCore: 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: 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: 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: Canonical link: https://commits.webkit.org/184825@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@211622 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2017-02-03 07:25:24 +00:00
}
using WTF::memoryFootprint;