1/*
2 * Copyright (C) 2011, 2012 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2014 Raspberry Pi Foundation. All Rights Reserved.
4 * Copyright (C) 2018 Igalia S.L.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "config.h"
29#include <wtf/MemoryPressureHandler.h>
30
31#include <malloc.h>
32#include <unistd.h>
33#include <wtf/MainThread.h>
34#include <wtf/MemoryFootprint.h>
35#include <wtf/linux/CurrentProcessMemoryStatus.h>
36#include <wtf/text/WTFString.h>
37
38#define LOG_CHANNEL_PREFIX Log
39
40namespace WTF {
41
42// Disable memory event reception for a minimum of s_minimumHoldOffTime
43// seconds after receiving an event. Don't let events fire any sooner than
44// s_holdOffMultiplier times the last cleanup processing time. Effectively
45// this is 1 / s_holdOffMultiplier percent of the time.
46// If after releasing the memory we don't free at least s_minimumBytesFreedToUseMinimumHoldOffTime,
47// we wait longer to try again (s_maximumHoldOffTime).
48// These value seems reasonable and testing verifies that it throttles frequent
49// low memory events, greatly reducing CPU usage.
50static const Seconds s_minimumHoldOffTime { 5_s };
51static const Seconds s_maximumHoldOffTime { 30_s };
52static const size_t s_minimumBytesFreedToUseMinimumHoldOffTime = 1 * MB;
53static const unsigned s_holdOffMultiplier = 20;
54
55void MemoryPressureHandler::triggerMemoryPressureEvent(bool isCritical)
56{
57 if (!m_installed)
58 return;
59
60 if (ReliefLogger::loggingEnabled())
61 LOG(MemoryPressure, "Got memory pressure notification (%s)", isCritical ? "critical" : "non-critical");
62
63 setUnderMemoryPressure(true);
64
65 if (isMainThread())
66 respondToMemoryPressure(isCritical ? Critical::Yes : Critical::No);
67 else
68 RunLoop::main().dispatch([this, isCritical] {
69 respondToMemoryPressure(isCritical ? Critical::Yes : Critical::No);
70 });
71
72 if (ReliefLogger::loggingEnabled() && isUnderMemoryPressure())
73 LOG(MemoryPressure, "System is no longer under memory pressure.");
74
75 setUnderMemoryPressure(false);
76}
77
78void MemoryPressureHandler::install()
79{
80 if (m_installed || m_holdOffTimer.isActive())
81 return;
82
83 m_installed = true;
84}
85
86void MemoryPressureHandler::uninstall()
87{
88 if (!m_installed)
89 return;
90
91 m_holdOffTimer.stop();
92
93 m_installed = false;
94}
95
96void MemoryPressureHandler::holdOffTimerFired()
97{
98 install();
99}
100
101void MemoryPressureHandler::holdOff(Seconds seconds)
102{
103 m_holdOffTimer.startOneShot(seconds);
104}
105
106static size_t processMemoryUsage()
107{
108 ProcessMemoryStatus memoryStatus;
109 currentProcessMemoryStatus(memoryStatus);
110 return (memoryStatus.resident - memoryStatus.shared);
111}
112
113void MemoryPressureHandler::respondToMemoryPressure(Critical critical, Synchronous synchronous)
114{
115 uninstall();
116
117 MonotonicTime startTime = MonotonicTime::now();
118 int64_t processMemory = processMemoryUsage();
119 releaseMemory(critical, synchronous);
120 int64_t bytesFreed = processMemory - processMemoryUsage();
121 Seconds holdOffTime = s_maximumHoldOffTime;
122 if (bytesFreed > 0 && static_cast<size_t>(bytesFreed) >= s_minimumBytesFreedToUseMinimumHoldOffTime)
123 holdOffTime = (MonotonicTime::now() - startTime) * s_holdOffMultiplier;
124 holdOff(std::max(holdOffTime, s_minimumHoldOffTime));
125}
126
127void MemoryPressureHandler::platformReleaseMemory(Critical)
128{
129#if HAVE(MALLOC_TRIM)
130 malloc_trim(0);
131#endif
132}
133
134Optional<MemoryPressureHandler::ReliefLogger::MemoryUsage> MemoryPressureHandler::ReliefLogger::platformMemoryUsage()
135{
136 return MemoryUsage {processMemoryUsage(), memoryFootprint()};
137}
138
139} // namespace WTF
140