Skip to content

Commit

Permalink
Change compositing policy in response to memory pressure status
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=259580
rdar://109875865

Reviewed by Simon Fraser.

We switch to a more conservative compositing policy when the
MemoryPressureHandler has a change in memory usage policy at most every
2 seconds. In some cases we can enter memory warning and critical memory
pressure situations and get a foreground jetsam before this 2s has
expired. This change will respond to recent memory warning and memory
pressure notifications in order to change the compositing policy. While we
can't guarantee to not jetsam, this does improve our chances of
reducing layer counts before the system gets critically low on
memory.

* Source/WTF/wtf/MemoryPressureHandler.h:
(WTF::MemoryPressureHandler::isUnderMemoryWarning const):
* Source/WebCore/rendering/RenderLayerCompositor.cpp:
(WebCore::RenderLayerCompositor::updateCompositingPolicy):

Canonical link: https://commits.webkit.org/266497@main
  • Loading branch information
rreno committed Aug 1, 2023
1 parent 0d88eeb commit 8299d76
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
10 changes: 10 additions & 0 deletions Source/WTF/wtf/MemoryPressureHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ class MemoryPressureHandler {
m_lowMemoryHandler = WTFMove(handler);
}

bool isUnderMemoryWarning() const
{
auto memoryPressureStatus = m_memoryPressureStatus.load();
return memoryPressureStatus == MemoryPressureStatus::SystemWarning
#if PLATFORM(MAC)
|| m_memoryUsagePolicy == MemoryUsagePolicy::Conservative
#endif
|| memoryPressureStatus == MemoryPressureStatus::ProcessLimitWarning;
}

bool isUnderMemoryPressure() const
{
auto memoryPressureStatus = m_memoryPressureStatus.load();
Expand Down
16 changes: 10 additions & 6 deletions Source/WebCore/rendering/RenderLayerCompositor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -593,14 +593,18 @@ bool RenderLayerCompositor::updateCompositingPolicy()
m_compositingPolicy = page().compositingPolicyOverride().value();
return m_compositingPolicy != currentPolicy;
}


const auto isCurrentlyUnderMemoryPressureOrWarning = [] {
return MemoryPressureHandler::singleton().isUnderMemoryPressure() || MemoryPressureHandler::singleton().isUnderMemoryWarning();
};

static auto cachedMemoryPolicy = WTF::MemoryUsagePolicy::Unrestricted;
static MonotonicTime cachedMemoryPolicyTime;
static constexpr auto memoryPolicyCachingDuration = 2_s;
auto now = MonotonicTime::now();
if (now - cachedMemoryPolicyTime > memoryPolicyCachingDuration) {
bool nowUnderMemoryPressure = isCurrentlyUnderMemoryPressureOrWarning();
static bool cachedIsUnderMemoryPressureOrWarning = nowUnderMemoryPressure;

if (cachedIsUnderMemoryPressureOrWarning != nowUnderMemoryPressure) {
cachedMemoryPolicy = MemoryPressureHandler::singleton().currentMemoryUsagePolicy();
cachedMemoryPolicyTime = now;
cachedIsUnderMemoryPressureOrWarning = nowUnderMemoryPressure;
}

m_compositingPolicy = cachedMemoryPolicy == WTF::MemoryUsagePolicy::Unrestricted ? CompositingPolicy::Normal : CompositingPolicy::Conservative;
Expand Down

0 comments on commit 8299d76

Please sign in to comment.