haikuwebkit/Source/bmalloc/bmalloc/AvailableMemory.h

72 lines
2.3 KiB
C
Raw Permalink Normal View History

[iOS] Use memory footprint to dynamically adjust behavior of allocators https://bugs.webkit.org/show_bug.cgi?id=171944 Reviewed by Filip Pizlo. Source/bmalloc: This change is iOS only. After the scavenger thread completes scavenging, it asks the OS for how much total memory the process is using. This information is used to update the sleep delay for the scanvenger thread, as well as to provide memory in use data for other parts of the system. The scavenger sleep time is calculated using the following quadradic equation. scavengerSleep = 1.2*percentFreeMemory^2 - percentFreeMemory + 2 Where percentFreeMemory is between 0 and 100. The result is constrained to the values 2 and 250. This equation empirically works out to providing a 2ms sleep time when we have less than 10% memory available, 30ms when 20% is available and 250ms when 50% or more is available. In testing, this exponentially agressive scavenging delay by itself reduced memory usage and made it much more deterministic when used without the corresponding change in the JSC Heap. Changed the scavenger thread to use the User Initiated QOS priority to ensure it doesn't get starved. Moved the non-Windows functionality of WTF::RAMSize() to new files AvailableMemory.{cpp,h} and implemented in the function availableMemory(). That functions limits the value returned on iOS to a maximum of 840MB as that is the jetsam soft memory limit. Added a new API availableMemory() so that WTF::RAMSize() will use this value. * CMakeLists.txt: * bmalloc.xcodeproj/project.pbxproj: * bmalloc/BPlatform.h: * bmalloc/Heap.cpp: (bmalloc::Heap::Heap): (bmalloc::Heap::updateMemoryInUseParameters): (bmalloc::Heap::concurrentScavenge): (bmalloc::Heap::scavenge): * bmalloc/Heap.h: (bmalloc::Heap::memoryFootprint): (bmalloc::Heap::percentAvailableMemoryInUse): * bmalloc/Sizes.h: * bmalloc/bmalloc.h: (bmalloc::api::availableMemory): (bmalloc::api::memoryFootprint): (bmalloc::api::percentAvailableMemoryInUse): * bmalloc/AvailableMemory.cpp: Added. (bmalloc::computeAvailableMemory): (bmalloc::availableMemory): * bmalloc/AvailableMemory.h: Added. Source/JavaScriptCore: This change is iOS only. Added the ability to react to when memory usage is critical. This is defined as memory usage being above the newly added option criticalGCMemoryThreshold. When we are in this critical state, all collections are Full and we limit the amount of memory we allocate between collections to 1/4th the memory above the critical threshold. Changed the calculation of proportionalHeapSize to be based on process memory footprint and not how big the heap is. Also, the values of Options::smallHeapRAMFraction and Options::mediumHeapRAMFraction are overriden so that most of the heap growth is happens using the more agressive Options::smallHeapGrowthFactor. * heap/Heap.cpp: (JSC::Heap::Heap): (JSC::Heap::overCriticalMemoryThreshold): (JSC::Heap::shouldDoFullCollection): (JSC::Heap::collectIfNecessaryOrDefer): * heap/Heap.h: * runtime/Options.cpp: (JSC::overrideDefaults): (JSC::Options::initialize): * runtime/Options.h: Source/WTF: Moved the non-Windows implementation of RAMSize() to bmalloc/AvailableMemory.cpp and called the function availableMemory(). * wtf/RAMSize.cpp: (WTF::computeRAMSize): Canonical link: https://commits.webkit.org/188967@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@216763 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2017-05-12 14:15:08 +00:00
/*
* Copyright (C) 2012-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 "BPlatform.h"
#include "Sizes.h"
namespace bmalloc {
[bmalloc] Add declspec support for export macros https://bugs.webkit.org/show_bug.cgi?id=207158 Reviewed by Yusuke Suzuki. Add a check for __has_declspec_attribute which is a Clang extension for Microsoft style __declspec attributes in BCompiler.h. Then use that check within BPlatform.h to determine if __declspec or visibility("default") should be used for the export macros. The BExport.h file was then expanded to properly implement __declspec definitions for export and import. Libraries that could be compiled statically in WebKit have a STATICALLY_LINKED_WITH_${library} definition for when they're compiled statically and later exposed through a different shared library. In practice though this is really only needed when __declspec is used since this is the only case where the import and export declarations differ. The BEXPORT on StaticPerProcess resulted in undefined symbol errors when WebCore was linked. Specifically IsoSharedHeap::getFastCase and IsoSharedHeap::getSlowCase which is a subclass. It appears that Storage was the only one required to be exported. This only appeared due to how __declspec requires exports and imports to be defined differently. Also added support for export macros on PlayStation in the BPlatform.h file and fixed any exports for that platform when building. * CMakeLists.txt: * bmalloc/Allocator.h: * bmalloc/AvailableMemory.h: * bmalloc/BCompiler.h: * bmalloc/BExport.h: * bmalloc/BPlatform.h: * bmalloc/Cache.h: * bmalloc/IsoTLS.h: * bmalloc/StaticPerProcess.h: Canonical link: https://commits.webkit.org/220257@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@255829 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2020-02-05 18:37:42 +00:00
BEXPORT size_t availableMemory();
[iOS] Use memory footprint to dynamically adjust behavior of allocators https://bugs.webkit.org/show_bug.cgi?id=171944 Reviewed by Filip Pizlo. Source/bmalloc: This change is iOS only. After the scavenger thread completes scavenging, it asks the OS for how much total memory the process is using. This information is used to update the sleep delay for the scanvenger thread, as well as to provide memory in use data for other parts of the system. The scavenger sleep time is calculated using the following quadradic equation. scavengerSleep = 1.2*percentFreeMemory^2 - percentFreeMemory + 2 Where percentFreeMemory is between 0 and 100. The result is constrained to the values 2 and 250. This equation empirically works out to providing a 2ms sleep time when we have less than 10% memory available, 30ms when 20% is available and 250ms when 50% or more is available. In testing, this exponentially agressive scavenging delay by itself reduced memory usage and made it much more deterministic when used without the corresponding change in the JSC Heap. Changed the scavenger thread to use the User Initiated QOS priority to ensure it doesn't get starved. Moved the non-Windows functionality of WTF::RAMSize() to new files AvailableMemory.{cpp,h} and implemented in the function availableMemory(). That functions limits the value returned on iOS to a maximum of 840MB as that is the jetsam soft memory limit. Added a new API availableMemory() so that WTF::RAMSize() will use this value. * CMakeLists.txt: * bmalloc.xcodeproj/project.pbxproj: * bmalloc/BPlatform.h: * bmalloc/Heap.cpp: (bmalloc::Heap::Heap): (bmalloc::Heap::updateMemoryInUseParameters): (bmalloc::Heap::concurrentScavenge): (bmalloc::Heap::scavenge): * bmalloc/Heap.h: (bmalloc::Heap::memoryFootprint): (bmalloc::Heap::percentAvailableMemoryInUse): * bmalloc/Sizes.h: * bmalloc/bmalloc.h: (bmalloc::api::availableMemory): (bmalloc::api::memoryFootprint): (bmalloc::api::percentAvailableMemoryInUse): * bmalloc/AvailableMemory.cpp: Added. (bmalloc::computeAvailableMemory): (bmalloc::availableMemory): * bmalloc/AvailableMemory.h: Added. Source/JavaScriptCore: This change is iOS only. Added the ability to react to when memory usage is critical. This is defined as memory usage being above the newly added option criticalGCMemoryThreshold. When we are in this critical state, all collections are Full and we limit the amount of memory we allocate between collections to 1/4th the memory above the critical threshold. Changed the calculation of proportionalHeapSize to be based on process memory footprint and not how big the heap is. Also, the values of Options::smallHeapRAMFraction and Options::mediumHeapRAMFraction are overriden so that most of the heap growth is happens using the more agressive Options::smallHeapGrowthFactor. * heap/Heap.cpp: (JSC::Heap::Heap): (JSC::Heap::overCriticalMemoryThreshold): (JSC::Heap::shouldDoFullCollection): (JSC::Heap::collectIfNecessaryOrDefer): * heap/Heap.h: * runtime/Options.cpp: (JSC::overrideDefaults): (JSC::Options::initialize): * runtime/Options.h: Source/WTF: Moved the non-Windows implementation of RAMSize() to bmalloc/AvailableMemory.cpp and called the function availableMemory(). * wtf/RAMSize.cpp: (WTF::computeRAMSize): Canonical link: https://commits.webkit.org/188967@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@216763 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2017-05-12 14:15:08 +00:00
#if BPLATFORM(IOS_FAMILY) || BOS(LINUX) || BOS(FREEBSD)
struct MemoryStatus {
MemoryStatus(size_t memoryFootprint, double percentAvailableMemoryInUse)
: memoryFootprint(memoryFootprint)
, percentAvailableMemoryInUse(percentAvailableMemoryInUse)
{
}
size_t memoryFootprint;
double percentAvailableMemoryInUse;
};
MemoryStatus memoryStatus();
inline size_t memoryFootprint()
{
auto memoryUse = memoryStatus();
return memoryUse.memoryFootprint;
[iOS] Use memory footprint to dynamically adjust behavior of allocators https://bugs.webkit.org/show_bug.cgi?id=171944 Reviewed by Filip Pizlo. Source/bmalloc: This change is iOS only. After the scavenger thread completes scavenging, it asks the OS for how much total memory the process is using. This information is used to update the sleep delay for the scanvenger thread, as well as to provide memory in use data for other parts of the system. The scavenger sleep time is calculated using the following quadradic equation. scavengerSleep = 1.2*percentFreeMemory^2 - percentFreeMemory + 2 Where percentFreeMemory is between 0 and 100. The result is constrained to the values 2 and 250. This equation empirically works out to providing a 2ms sleep time when we have less than 10% memory available, 30ms when 20% is available and 250ms when 50% or more is available. In testing, this exponentially agressive scavenging delay by itself reduced memory usage and made it much more deterministic when used without the corresponding change in the JSC Heap. Changed the scavenger thread to use the User Initiated QOS priority to ensure it doesn't get starved. Moved the non-Windows functionality of WTF::RAMSize() to new files AvailableMemory.{cpp,h} and implemented in the function availableMemory(). That functions limits the value returned on iOS to a maximum of 840MB as that is the jetsam soft memory limit. Added a new API availableMemory() so that WTF::RAMSize() will use this value. * CMakeLists.txt: * bmalloc.xcodeproj/project.pbxproj: * bmalloc/BPlatform.h: * bmalloc/Heap.cpp: (bmalloc::Heap::Heap): (bmalloc::Heap::updateMemoryInUseParameters): (bmalloc::Heap::concurrentScavenge): (bmalloc::Heap::scavenge): * bmalloc/Heap.h: (bmalloc::Heap::memoryFootprint): (bmalloc::Heap::percentAvailableMemoryInUse): * bmalloc/Sizes.h: * bmalloc/bmalloc.h: (bmalloc::api::availableMemory): (bmalloc::api::memoryFootprint): (bmalloc::api::percentAvailableMemoryInUse): * bmalloc/AvailableMemory.cpp: Added. (bmalloc::computeAvailableMemory): (bmalloc::availableMemory): * bmalloc/AvailableMemory.h: Added. Source/JavaScriptCore: This change is iOS only. Added the ability to react to when memory usage is critical. This is defined as memory usage being above the newly added option criticalGCMemoryThreshold. When we are in this critical state, all collections are Full and we limit the amount of memory we allocate between collections to 1/4th the memory above the critical threshold. Changed the calculation of proportionalHeapSize to be based on process memory footprint and not how big the heap is. Also, the values of Options::smallHeapRAMFraction and Options::mediumHeapRAMFraction are overriden so that most of the heap growth is happens using the more agressive Options::smallHeapGrowthFactor. * heap/Heap.cpp: (JSC::Heap::Heap): (JSC::Heap::overCriticalMemoryThreshold): (JSC::Heap::shouldDoFullCollection): (JSC::Heap::collectIfNecessaryOrDefer): * heap/Heap.h: * runtime/Options.cpp: (JSC::overrideDefaults): (JSC::Options::initialize): * runtime/Options.h: Source/WTF: Moved the non-Windows implementation of RAMSize() to bmalloc/AvailableMemory.cpp and called the function availableMemory(). * wtf/RAMSize.cpp: (WTF::computeRAMSize): Canonical link: https://commits.webkit.org/188967@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@216763 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2017-05-12 14:15:08 +00:00
}
inline double percentAvailableMemoryInUse()
{
auto memoryUse = memoryStatus();
return memoryUse.percentAvailableMemoryInUse;
}
#endif
inline bool isUnderMemoryPressure()
{
#if BPLATFORM(IOS_FAMILY) || BOS(LINUX) || BOS(FREEBSD)
return percentAvailableMemoryInUse() > memoryPressureThreshold;
#else
return false;
#endif
}
} // namespace bmalloc