1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "AvailableMemoryWatcher.h"
#include "mozilla/Atomics.h"
#include "mozilla/Services.h"
#include "mozilla/StaticPrefs_browser.h"
#include "nsAppRunner.h"
#include "nsExceptionHandler.h"
#include "nsICrashReporter.h"
#include "nsIObserver.h"
#include "nsISupports.h"
#include "nsITimer.h"
#include "nsMemoryPressure.h"
#include "nsServiceManagerUtils.h"
#include "nsWindowsHelpers.h"
#include <memoryapi.h>
extern mozilla::Atomic<uint32_t, mozilla::MemoryOrdering::Relaxed>
sNumLowPhysicalMemEvents;
namespace mozilla {
// This class is used to monitor low memory events delivered by Windows via
// memory resource notification objects. When we enter a low memory scenario
// the LowMemoryCallback() is invoked by Windows. This initial call triggers
// an nsITimer that polls to see when the low memory condition has been lifted.
// When it has, we'll stop polling and start waiting for the next
// LowMemoryCallback(). Meanwhile, the polling may be stopped and restarted by
// user-interaction events from the observer service.
class nsAvailableMemoryWatcher final : public nsITimerCallback,
public nsINamed,
public nsAvailableMemoryWatcherBase {
public:
NS_DECL_ISUPPORTS_INHERITED
NS_DECL_NSIOBSERVER
NS_DECL_NSITIMERCALLBACK
NS_DECL_NSINAMED
nsAvailableMemoryWatcher();
nsresult Init() override;
private:
static VOID CALLBACK LowMemoryCallback(PVOID aContext, BOOLEAN aIsTimer);
static void RecordLowMemoryEvent();
static bool IsCommitSpaceLow();
~nsAvailableMemoryWatcher();
bool RegisterMemoryResourceHandler(const MutexAutoLock& aLock)
MOZ_REQUIRES(mMutex);
void UnregisterMemoryResourceHandler(const MutexAutoLock&)
MOZ_REQUIRES(mMutex);
void MaybeSaveMemoryReport(const MutexAutoLock&) MOZ_REQUIRES(mMutex);
void Shutdown(const MutexAutoLock& aLock) MOZ_REQUIRES(mMutex);
bool ListenForLowMemory(const MutexAutoLock&) MOZ_REQUIRES(mMutex);
void OnLowMemory(const MutexAutoLock&) MOZ_REQUIRES(mMutex);
void OnHighMemory(const MutexAutoLock&) MOZ_REQUIRES(mMutex);
void StartPollingIfUserInteracting(const MutexAutoLock& aLock)
MOZ_REQUIRES(mMutex);
void StopPolling(const MutexAutoLock&) MOZ_REQUIRES(mMutex);
void StopPollingIfUserIdle(const MutexAutoLock&) MOZ_REQUIRES(mMutex);
void OnUserInteracting(const MutexAutoLock&) MOZ_REQUIRES(mMutex);
nsCOMPtr<nsITimer> mTimer MOZ_GUARDED_BY(mMutex);
nsAutoHandle mLowMemoryHandle MOZ_GUARDED_BY(mMutex);
HANDLE mWaitHandle MOZ_GUARDED_BY(mMutex);
bool mPolling MOZ_GUARDED_BY(mMutex);
// Indicates whether to start a timer when user interaction is notified.
// This flag is needed because the low-memory callback may be triggered when
// the user is inactive and we want to delay-start the timer.
bool mNeedToRestartTimerOnUserInteracting MOZ_GUARDED_BY(mMutex);
// Indicate that the available commit space is low. The timer handler needs
// this flag because it is triggered by the low physical memory regardless
// of the available commit space.
bool mUnderMemoryPressure MOZ_GUARDED_BY(mMutex);
bool mSavedReport MOZ_GUARDED_BY(mMutex);
bool mIsShutdown MOZ_GUARDED_BY(mMutex);
// Members below this line are used only in the main thread.
// No lock is needed.
// Don't fire a low-memory notification more often than this interval.
uint32_t mPollingInterval;
};
NS_IMPL_ISUPPORTS_INHERITED(nsAvailableMemoryWatcher,
nsAvailableMemoryWatcherBase, nsIObserver,
nsITimerCallback, nsINamed)
nsAvailableMemoryWatcher::nsAvailableMemoryWatcher()
: mWaitHandle(nullptr),
mPolling(false),
mNeedToRestartTimerOnUserInteracting(false),
mUnderMemoryPressure(false),
mSavedReport(false),
mIsShutdown(false),
mPollingInterval(0) {}
nsresult nsAvailableMemoryWatcher::Init() {
nsresult rv = nsAvailableMemoryWatcherBase::Init();
if (NS_FAILED(rv)) {
return rv;
}
MutexAutoLock lock(mMutex);
mTimer = NS_NewTimer();
if (!mTimer) {
return NS_ERROR_OUT_OF_MEMORY;
}
// Use a very short interval for GTest to verify the timer's behavior.
mPollingInterval = gIsGtest ? 10 : 10000;
if (!RegisterMemoryResourceHandler(lock)) {
return NS_ERROR_FAILURE;
}
static_assert(sizeof(sNumLowPhysicalMemEvents) == sizeof(uint32_t));
CrashReporter::RegisterAnnotationU32(
CrashReporter::Annotation::LowPhysicalMemoryEvents,
reinterpret_cast<uint32_t*>(&sNumLowPhysicalMemEvents));
return NS_OK;
}
nsAvailableMemoryWatcher::~nsAvailableMemoryWatcher() {
// These handles should have been released during the shutdown phase.
MOZ_ASSERT(!mLowMemoryHandle);
MOZ_ASSERT(!mWaitHandle);
}
// static
VOID CALLBACK nsAvailableMemoryWatcher::LowMemoryCallback(PVOID aContext,
BOOLEAN aIsTimer) {
if (aIsTimer) {
return;
}
// The |watcher| was addref'ed when we registered the wait handle in
// ListenForLowMemory(). It is decremented when exiting the function,
// so please make sure we unregister the wait handle after this line.
RefPtr<nsAvailableMemoryWatcher> watcher =
already_AddRefed<nsAvailableMemoryWatcher>(
static_cast<nsAvailableMemoryWatcher*>(aContext));
MutexAutoLock lock(watcher->mMutex);
if (watcher->mIsShutdown) {
// mWaitHandle should have been unregistered during shutdown
MOZ_ASSERT(!watcher->mWaitHandle);
return;
}
::UnregisterWait(watcher->mWaitHandle);
watcher->mWaitHandle = nullptr;
// On Windows, memory allocations fails when the available commit space is
// not sufficient. It's possible that this callback function is invoked
// but there is still commit space enough for the application to continue
// to run. In such a case, there is no strong need to trigger the memory
// pressure event. So we trigger the event only when the available commit
// space is low.
if (IsCommitSpaceLow()) {
watcher->OnLowMemory(lock);
} else {
// Since we have unregistered the callback, we need to start a timer to
// continue watching memory.
watcher->StartPollingIfUserInteracting(lock);
}
}
// static
void nsAvailableMemoryWatcher::RecordLowMemoryEvent() {
sNumLowPhysicalMemEvents++;
}
bool nsAvailableMemoryWatcher::RegisterMemoryResourceHandler(
const MutexAutoLock& aLock) {
mLowMemoryHandle.own(
::CreateMemoryResourceNotification(LowMemoryResourceNotification));
if (!mLowMemoryHandle) {
return false;
}
return ListenForLowMemory(aLock);
}
void nsAvailableMemoryWatcher::UnregisterMemoryResourceHandler(
const MutexAutoLock&) {
if (mWaitHandle) {
bool res = ::UnregisterWait(mWaitHandle);
if (res || ::GetLastError() != ERROR_IO_PENDING) {
// We decrement the refcount only when we're sure the LowMemoryCallback()
// callback won't be invoked, otherwise the callback will do it
this->Release();
}
mWaitHandle = nullptr;
}
mLowMemoryHandle.reset();
}
void nsAvailableMemoryWatcher::Shutdown(const MutexAutoLock& aLock) {
mIsShutdown = true;
mNeedToRestartTimerOnUserInteracting = false;
if (mTimer) {
mTimer->Cancel();
mTimer = nullptr;
}
UnregisterMemoryResourceHandler(aLock);
}
bool nsAvailableMemoryWatcher::ListenForLowMemory(const MutexAutoLock&) {
if (mLowMemoryHandle && !mWaitHandle) {
// We're giving ownership of this object to the LowMemoryCallback(). We
// increment the count here so that the object is kept alive until the
// callback decrements it.
this->AddRef();
bool res = ::RegisterWaitForSingleObject(
&mWaitHandle, mLowMemoryHandle, LowMemoryCallback, this, INFINITE,
WT_EXECUTEDEFAULT | WT_EXECUTEONLYONCE);
if (!res) {
// We couldn't register the callback, decrement the count
this->Release();
}
// Once we register the wait handle, we no longer need to start
// the timer because we can continue watching memory via callback.
mNeedToRestartTimerOnUserInteracting = false;
return res;
}
return false;
}
void nsAvailableMemoryWatcher::MaybeSaveMemoryReport(const MutexAutoLock&) {
if (mSavedReport) {
return;
}
if (nsCOMPtr<nsICrashReporter> cr =
do_GetService("@mozilla.org/toolkit/crash-reporter;1")) {
mSavedReport = NS_SUCCEEDED(cr->SaveMemoryReport());
}
}
void nsAvailableMemoryWatcher::OnLowMemory(const MutexAutoLock& aLock) {
mUnderMemoryPressure = true;
RecordLowMemoryEvent();
if (NS_IsMainThread()) {
MaybeSaveMemoryReport(aLock);
UpdateLowMemoryTimeStamp();
{
// Don't invoke UnloadTabAsync() with the lock to avoid deadlock
// because nsAvailableMemoryWatcher::Notify may be invoked while
// running the method.
MutexAutoUnlock unlock(mMutex);
mTabUnloader->UnloadTabAsync();
}
} else {
// SaveMemoryReport and mTabUnloader needs to be run in the main thread
// (See nsMemoryReporterManager::GetReportsForThisProcessExtended)
NS_DispatchToMainThread(NS_NewRunnableFunction(
"nsAvailableMemoryWatcher::OnLowMemory", [self = RefPtr{this}]() {
{
MutexAutoLock lock(self->mMutex);
self->MaybeSaveMemoryReport(lock);
self->UpdateLowMemoryTimeStamp();
}
self->mTabUnloader->UnloadTabAsync();
}));
}
StartPollingIfUserInteracting(aLock);
}
void nsAvailableMemoryWatcher::OnHighMemory(const MutexAutoLock& aLock) {
MOZ_ASSERT(NS_IsMainThread());
if (mUnderMemoryPressure) {
RecordTelemetryEventOnHighMemory(aLock);
NS_NotifyOfEventualMemoryPressure(MemoryPressureState::NoPressure);
}
mUnderMemoryPressure = false;
mSavedReport = false; // Will save a new report if memory gets low again
StopPolling(aLock);
ListenForLowMemory(aLock);
}
// static
bool nsAvailableMemoryWatcher::IsCommitSpaceLow() {
// Other options to get the available page file size:
// - GetPerformanceInfo
// Too slow, don't use it.
// - PdhCollectQueryData and PdhGetRawCounterValue
// Faster than GetPerformanceInfo, but slower than GlobalMemoryStatusEx.
// - NtQuerySystemInformation(SystemMemoryUsageInformation)
// Faster than GlobalMemoryStatusEx, but undocumented.
MEMORYSTATUSEX memStatus = {sizeof(memStatus)};
if (!::GlobalMemoryStatusEx(&memStatus)) {
return false;
}
constexpr size_t kBytesPerMB = 1024 * 1024;
return (memStatus.ullAvailPageFile / kBytesPerMB) <
StaticPrefs::browser_low_commit_space_threshold_mb();
}
void nsAvailableMemoryWatcher::StartPollingIfUserInteracting(
const MutexAutoLock&) {
// When the user is inactive, we mark the flag to delay-start
// the timer when the user becomes active later.
mNeedToRestartTimerOnUserInteracting = true;
if (mInteracting && !mPolling) {
if (NS_SUCCEEDED(mTimer->InitWithCallback(
this, mPollingInterval, nsITimer::TYPE_REPEATING_SLACK))) {
mPolling = true;
}
}
}
void nsAvailableMemoryWatcher::StopPolling(const MutexAutoLock&) {
mTimer->Cancel();
mPolling = false;
}
void nsAvailableMemoryWatcher::StopPollingIfUserIdle(
const MutexAutoLock& aLock) {
if (!mInteracting) {
StopPolling(aLock);
}
}
void nsAvailableMemoryWatcher::OnUserInteracting(const MutexAutoLock& aLock) {
if (mNeedToRestartTimerOnUserInteracting) {
StartPollingIfUserInteracting(aLock);
}
}
// Timer callback, polls the low memory resource notification to detect when
// we've freed enough memory or if we have to send more memory pressure events.
// Polling stops automatically when the user is not interacting with the UI.
NS_IMETHODIMP
nsAvailableMemoryWatcher::Notify(nsITimer* aTimer) {
MutexAutoLock lock(mMutex);
StopPollingIfUserIdle(lock);
if (IsCommitSpaceLow()) {
OnLowMemory(lock);
} else {
OnHighMemory(lock);
}
return NS_OK;
}
NS_IMETHODIMP
nsAvailableMemoryWatcher::GetName(nsACString& aName) {
aName.AssignLiteral("nsAvailableMemoryWatcher");
return NS_OK;
}
// Observer service callback, used to stop the polling timer when the user
// stops interacting with Firefox and resuming it when they interact again.
// Also used to shut down the service if the application is quitting.
NS_IMETHODIMP
nsAvailableMemoryWatcher::Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData) {
nsresult rv = nsAvailableMemoryWatcherBase::Observe(aSubject, aTopic, aData);
if (NS_FAILED(rv)) {
return rv;
}
MutexAutoLock lock(mMutex);
if (strcmp(aTopic, "xpcom-shutdown") == 0) {
Shutdown(lock);
} else if (strcmp(aTopic, "user-interaction-active") == 0) {
OnUserInteracting(lock);
}
return NS_OK;
}
already_AddRefed<nsAvailableMemoryWatcherBase> CreateAvailableMemoryWatcher() {
RefPtr watcher(new nsAvailableMemoryWatcher);
if (NS_FAILED(watcher->Init())) {
return do_AddRef(new nsAvailableMemoryWatcherBase); // fallback
}
return watcher.forget();
}
} // namespace mozilla
|