gecko-dev/xpcom/base/AvailableMemoryTracker.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

190 строки
5.2 KiB
C++
Исходник Обычный вид История

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
2012-05-21 15:12:37 +04:00
/* 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 "mozilla/AvailableMemoryTracker.h"
#if defined(XP_WIN)
Bug 1733308 - Fix non-unified builds errors in xpcom/base. r=xpcom-reviewers,mccr8 objdir/dist/include/mozilla/DeferredFinalize.h:30:1: error: unknown type name 'MOZ_NEVER_INLINE' objdir/dist/include/mozilla/dom/BindingUtils.h:2776:5: error: no matching function for call to 'DeferredFinalize' xpcom/base/AvailableMemoryTracker.cpp:55,40:: error: expected class name xpcom/base/AvailableMemoryTracker.cpp:59,3:: error: 'AddRef' marked 'override' but does not override any member functions xpcom/base/AvailableMemoryTracker.cpp:59,3:: error: 'QueryInterface' marked 'override' but does not override any member functions xpcom/base/AvailableMemoryTracker.cpp:59,3:: error: 'Release' marked 'override' but does not override any member functions xpcom/base/AvailableMemoryTracker.cpp:61,29:: error: unknown type name 'nsIHandleReportCallback' xpcom/base/AvailableMemoryTracker.cpp:65,37:: error: use of undeclared identifier 'KIND_OTHER' xpcom/base/AvailableMemoryTracker.cpp:65,49:: error: use of undeclared identifier 'UNITS_COUNT_CUMULATIVE' xpcom/base/AvailableMemoryTracker.cpp:77,1:: error: invalid application of 'sizeof' to an incomplete type 'const QITableEntry []' xpcom/base/AvailableMemoryTracker.cpp:77,1:: error: static_cast from '(anonymous namespace)::LowEventsReporter *' to 'nsISupports *', which are not related by inheritance, is not allowed xpcom/base/AvailableMemoryTracker.cpp:77,38:: error: unknown type name 'nsIMemoryReporter' xpcom/base/AvailableMemoryTracker.cpp:77,38:: error: use of undeclared identifier 'nsIMemoryReporter' xpcom/base/AvailableMemoryTracker.cpp:187,3:: error: use of undeclared identifier 'RegisterLowMemoryEventsPhysicalDistinguishedAmount' xpcom/base/AvailableMemoryWatcherWin.cpp:31,47:: error: base class has incomplete type xpcom/base/AvailableMemoryWatcherWin.cpp:37,3:: error: unknown type name 'NS_DECL_NSIOBSERVER' xpcom/base/AvailableMemoryWatcherWin.cpp:38,3:: error: 'virtual' can only appear on non-static member functions xpcom/base/AvailableMemoryWatcherWin.cpp:38,3:: error: expected ';' at end of declaration list xpcom/base/AvailableMemoryWatcherWin.cpp:38,3:: error: static_assert failed due to requirement 'std::is_convertible<mozilla::nsAvailableMemoryWatcher *, nsINamed *>::value' "nsITimerCallback implementations must also implement nsINamed" xpcom/base/AvailableMemoryWatcherWin.cpp:39,3:: error: must use 'enum' tag to refer to type 'nsresult' in this scope xpcom/base/AvailableMemoryWatcherWin.cpp:42,3:: error: must use 'enum' tag to refer to type 'nsresult' in this scope xpcom/base/AvailableMemoryWatcherWin.cpp:102,1:: error: cannot initialize object parameter of type 'mozilla::nsAvailableMemoryWatcherBase' with an expression of type 'mozilla::nsAvailableMemoryWatcher' xpcom/base/AvailableMemoryWatcherWin.cpp:102,1:: error: invalid application of 'sizeof' to an incomplete type 'const QITableEntry []' xpcom/base/AvailableMemoryWatcherWin.cpp:102,1:: error: must use 'enum' tag to refer to type 'nsresult' in this scope xpcom/base/AvailableMemoryWatcherWin.cpp:102,1:: error: static_cast from 'mozilla::nsAvailableMemoryWatcher *' to 'nsINamed *', which are not related by inheritance, is not allowed xpcom/base/AvailableMemoryWatcherWin.cpp:102,1:: error: static_cast from 'mozilla::nsAvailableMemoryWatcher *' to 'nsIObserver *', which are not related by inheritance, is not allowed xpcom/base/AvailableMemoryWatcherWin.cpp:102,1:: error: static_cast from 'mozilla::nsAvailableMemoryWatcher *' to 'nsITimerCallback *', which are not related by inheritance, is not allowed xpcom/base/AvailableMemoryWatcherWin.cpp:103,59:: error: 'nsIObserver' does not refer to a value xpcom/base/AvailableMemoryWatcherWin.cpp:103,59:: error: incomplete type 'nsIObserver' named in nested name specifier xpcom/base/AvailableMemoryWatcherWin.cpp:140,45:: error: cannot initialize a parameter of type 'nsIObserver *' with an rvalue of type 'mozilla::nsAvailableMemoryWatcher *' xpcom/base/AvailableMemoryWatcherWin.cpp:140,5:: error: must use 'enum' tag to refer to type 'nsresult' in this scope xpcom/base/CycleCollectedJSRuntime.cpp:1605,7:: error: use of undeclared identifier 'PR_GetEnv' xpcom/base/CycleCollectedJSRuntime.cpp:1605:7: error: use of undeclared identifier 'PR_GetEnv' xpcom/base/DeferredFinalize.cpp:12,15:: error: out-of-line definition of 'DeferredFinalize' does not match any declaration in namespace 'mozilla' xpcom/base/DeferredFinalize.cpp:12:15: error: out-of-line definition of 'DeferredFinalize' does not match any declaration in namespace 'mozilla' xpcom/base/MemoryTelemetry.cpp:48,27:: error: unused variable 'kTotalMemoryCollectorTimeout' [-Werror,-Wunused-const-variable] xpcom/base/MemoryTelemetry.cpp:48:27: error: unused variable 'kTotalMemoryCollectorTimeout' [-Werror,-Wunused-const-variable] xpcom/base/nsErrorService.cpp:42,21:: error: use of function template name with no prior declaration in function call with explicit template arguments is a C++20 extension [-Werror,-Wc++20-extensions] xpcom/base/nsErrorService.cpp:42,21:: error: use of undeclared identifier 'MakeUnique'; did you mean 'mozilla::MakeUnique'? xpcom/base/nsErrorService.cpp:42:21: error: use of function template name with no prior declaration in function call with explicit template arguments is a C++20 extension [-Werror,-Wc++20-extensions] xpcom/base/nsErrorService.cpp:42:21: error: use of undeclared identifier 'MakeUnique'; did you mean 'mozilla::MakeUnique'? xpcom/base/nsMacPreferencesReader.mm:20:12: error: implicit instantiation of undefined template 'nsTSubstring<char16_t>' xpcom/base/nsMacPreferencesReader.mm:24:60: error: use of undeclared identifier 'NSString' xpcom/base/nsMacPreferencesReader.mm:24:69: error: expected expression xpcom/base/nsMacPreferencesReader.mm:26:48: error: unknown type name 'NSArray' xpcom/base/nsMacPreferencesReader.mm:28:30: error: use of undeclared identifier 'NSString' xpcom/base/nsMacPreferencesReader.mm:29:30: error: no matching function for call to 'MakeStringSpan' xpcom/base/nsMacPreferencesReader.mm:29:51: error: instance method '-UTF8String' not found (return type defaults to 'id') [-Werror,-Wobjc-method-access] xpcom/base/nsMacPreferencesReader.mm:30:22: error: instance method '-isKindOfClass:' not found (return type defaults to 'id') [-Werror,-Wobjc-method-access] xpcom/base/nsMacPreferencesReader.mm:30:37: error: receiver type 'int (*)(int)' is not 'id' or interface pointer, consider casting it to 'id' [-Werror,-Wreceiver-expr] xpcom/base/nsMacPreferencesReader.mm:30:37: error: use of undeclared identifier 'NSNumber'; did you mean 'isnumber'? xpcom/base/nsMacPreferencesReader.mm:30:46: error: instance method '-class' not found (return type defaults to 'id') [-Werror,-Wobjc-method-access] xpcom/base/nsMacPreferencesReader.mm:31:27: error: cannot initialize a parameter of type 'int64_t' (aka 'long long') with an rvalue of type 'id' xpcom/base/nsMacPreferencesReader.mm:31:33: error: instance method '-longLongValue' not found (return type defaults to 'id') [-Werror,-Wobjc-method-access] xpcom/base/nsMacPreferencesReader.mm:32:37: error: use of undeclared identifier 'NSArray' xpcom/base/nsMacPreferencesReader.mm:34:7: error: no matching function for call to 'EvaluateArray' xpcom/base/nsMacPreferencesReader.mm:36:37: error: use of undeclared identifier 'NSDictionary' xpcom/base/nsMacPreferencesReader.mm:38:7: error: no matching function for call to 'EvaluateDict' xpcom/base/nsMacPreferencesReader.mm:44:60: error: use of undeclared identifier 'NSString' xpcom/base/nsMacPreferencesReader.mm:44:69: error: expected expression xpcom/base/nsSystemInfo.cpp:415,35:: error: no template named 'NotNull'; did you mean 'NonNull'? xpcom/base/nsSystemInfo.cpp:419,25:: error: member reference type 'NonNull<IWSCProductList *>' is not a pointer; did you mean to use '.'? xpcom/base/nsSystemInfo.cpp:419,27:: error: no member named 'get_Count' in 'mozilla::dom::NonNull<IWSCProductList *>' xpcom/base/nsSystemInfo.cpp:426,19:: error: member reference type 'NonNull<IWSCProductList *>' is not a pointer; did you mean to use '.'? xpcom/base/nsSystemInfo.cpp:426,21:: error: no member named 'get_Item' in 'mozilla::dom::NonNull<IWSCProductList *>' xpcom/base/nsSystemInfo.cpp:484,17:: error: use of undeclared identifier 'ArrayLength'; did you mean 'mozilla::ArrayLength'? xpcom/base/nsSystemInfo.cpp:484,47:: error: use of undeclared identifier 'ArrayLength'; did you mean 'mozilla::ArrayLength'? xpcom/base/nsSystemInfo.cpp:487,36:: error: use of undeclared identifier 'ArrayLength'; did you mean 'mozilla::ArrayLength'? xpcom/base/nsSystemInfo.cpp:500,10:: error: no matching function for call to 'EnumWSCProductList' xpcom/base/nsSystemInfo.cpp:500,46:: error: use of undeclared identifier 'WrapNotNull'; did you mean 'mozilla::WrapNotNull'? xpcom/base/nsTraceRefcnt.cpp:313,32:: error: use of undeclared identifier 'MakeUnique' xpcom/base/nsTraceRefcnt.cpp:313:32: error: use of undeclared identifier 'MakeUnique' xpcom/base/nsTraceRefcnt.cpp:461,19:: error: no matching member function for call to 'WithEntryHandle' xpcom/base/nsTraceRefcnt.cpp:461:19: error: no matching member function for call to 'WithEntryHandle' xpcom/base/nsTraceRefcnt.cpp:468,33:: error: use of function template name with no prior declaration in function call with explicit template arguments is a C++20 extension [-Werror,-Wc++20-extensions] xpcom/base/nsTraceRefcnt.cpp:468,33:: error: use of undeclared identifier 'MakeUnique' xpcom/base/nsTraceRefcnt.cpp:468:33: error: use of undeclared identifier 'MakeUnique' xpcom/base/nsTraceRefcnt.cpp:736,20:: error: use of undeclared identifier '_write' Differential Revision: https://phabricator.services.mozilla.com/D127036
2021-10-01 04:49:16 +03:00
# include "nsIMemoryReporter.h"
#endif
#include "nsIObserver.h"
#include "nsIObserverService.h"
#include "nsIRunnable.h"
#include "nsISupports.h"
#include "nsThreadUtils.h"
#include "nsXULAppAPI.h"
#include "mozilla/Mutex.h"
#include "mozilla/ResultExtensions.h"
#include "mozilla/Services.h"
#if defined(MOZ_MEMORY)
# include "mozmemory.h"
#endif // MOZ_MEMORY
using namespace mozilla;
Bug 1733308 - Fix non-unified builds errors in xpcom/base. r=xpcom-reviewers,mccr8 objdir/dist/include/mozilla/DeferredFinalize.h:30:1: error: unknown type name 'MOZ_NEVER_INLINE' objdir/dist/include/mozilla/dom/BindingUtils.h:2776:5: error: no matching function for call to 'DeferredFinalize' xpcom/base/AvailableMemoryTracker.cpp:55,40:: error: expected class name xpcom/base/AvailableMemoryTracker.cpp:59,3:: error: 'AddRef' marked 'override' but does not override any member functions xpcom/base/AvailableMemoryTracker.cpp:59,3:: error: 'QueryInterface' marked 'override' but does not override any member functions xpcom/base/AvailableMemoryTracker.cpp:59,3:: error: 'Release' marked 'override' but does not override any member functions xpcom/base/AvailableMemoryTracker.cpp:61,29:: error: unknown type name 'nsIHandleReportCallback' xpcom/base/AvailableMemoryTracker.cpp:65,37:: error: use of undeclared identifier 'KIND_OTHER' xpcom/base/AvailableMemoryTracker.cpp:65,49:: error: use of undeclared identifier 'UNITS_COUNT_CUMULATIVE' xpcom/base/AvailableMemoryTracker.cpp:77,1:: error: invalid application of 'sizeof' to an incomplete type 'const QITableEntry []' xpcom/base/AvailableMemoryTracker.cpp:77,1:: error: static_cast from '(anonymous namespace)::LowEventsReporter *' to 'nsISupports *', which are not related by inheritance, is not allowed xpcom/base/AvailableMemoryTracker.cpp:77,38:: error: unknown type name 'nsIMemoryReporter' xpcom/base/AvailableMemoryTracker.cpp:77,38:: error: use of undeclared identifier 'nsIMemoryReporter' xpcom/base/AvailableMemoryTracker.cpp:187,3:: error: use of undeclared identifier 'RegisterLowMemoryEventsPhysicalDistinguishedAmount' xpcom/base/AvailableMemoryWatcherWin.cpp:31,47:: error: base class has incomplete type xpcom/base/AvailableMemoryWatcherWin.cpp:37,3:: error: unknown type name 'NS_DECL_NSIOBSERVER' xpcom/base/AvailableMemoryWatcherWin.cpp:38,3:: error: 'virtual' can only appear on non-static member functions xpcom/base/AvailableMemoryWatcherWin.cpp:38,3:: error: expected ';' at end of declaration list xpcom/base/AvailableMemoryWatcherWin.cpp:38,3:: error: static_assert failed due to requirement 'std::is_convertible<mozilla::nsAvailableMemoryWatcher *, nsINamed *>::value' "nsITimerCallback implementations must also implement nsINamed" xpcom/base/AvailableMemoryWatcherWin.cpp:39,3:: error: must use 'enum' tag to refer to type 'nsresult' in this scope xpcom/base/AvailableMemoryWatcherWin.cpp:42,3:: error: must use 'enum' tag to refer to type 'nsresult' in this scope xpcom/base/AvailableMemoryWatcherWin.cpp:102,1:: error: cannot initialize object parameter of type 'mozilla::nsAvailableMemoryWatcherBase' with an expression of type 'mozilla::nsAvailableMemoryWatcher' xpcom/base/AvailableMemoryWatcherWin.cpp:102,1:: error: invalid application of 'sizeof' to an incomplete type 'const QITableEntry []' xpcom/base/AvailableMemoryWatcherWin.cpp:102,1:: error: must use 'enum' tag to refer to type 'nsresult' in this scope xpcom/base/AvailableMemoryWatcherWin.cpp:102,1:: error: static_cast from 'mozilla::nsAvailableMemoryWatcher *' to 'nsINamed *', which are not related by inheritance, is not allowed xpcom/base/AvailableMemoryWatcherWin.cpp:102,1:: error: static_cast from 'mozilla::nsAvailableMemoryWatcher *' to 'nsIObserver *', which are not related by inheritance, is not allowed xpcom/base/AvailableMemoryWatcherWin.cpp:102,1:: error: static_cast from 'mozilla::nsAvailableMemoryWatcher *' to 'nsITimerCallback *', which are not related by inheritance, is not allowed xpcom/base/AvailableMemoryWatcherWin.cpp:103,59:: error: 'nsIObserver' does not refer to a value xpcom/base/AvailableMemoryWatcherWin.cpp:103,59:: error: incomplete type 'nsIObserver' named in nested name specifier xpcom/base/AvailableMemoryWatcherWin.cpp:140,45:: error: cannot initialize a parameter of type 'nsIObserver *' with an rvalue of type 'mozilla::nsAvailableMemoryWatcher *' xpcom/base/AvailableMemoryWatcherWin.cpp:140,5:: error: must use 'enum' tag to refer to type 'nsresult' in this scope xpcom/base/CycleCollectedJSRuntime.cpp:1605,7:: error: use of undeclared identifier 'PR_GetEnv' xpcom/base/CycleCollectedJSRuntime.cpp:1605:7: error: use of undeclared identifier 'PR_GetEnv' xpcom/base/DeferredFinalize.cpp:12,15:: error: out-of-line definition of 'DeferredFinalize' does not match any declaration in namespace 'mozilla' xpcom/base/DeferredFinalize.cpp:12:15: error: out-of-line definition of 'DeferredFinalize' does not match any declaration in namespace 'mozilla' xpcom/base/MemoryTelemetry.cpp:48,27:: error: unused variable 'kTotalMemoryCollectorTimeout' [-Werror,-Wunused-const-variable] xpcom/base/MemoryTelemetry.cpp:48:27: error: unused variable 'kTotalMemoryCollectorTimeout' [-Werror,-Wunused-const-variable] xpcom/base/nsErrorService.cpp:42,21:: error: use of function template name with no prior declaration in function call with explicit template arguments is a C++20 extension [-Werror,-Wc++20-extensions] xpcom/base/nsErrorService.cpp:42,21:: error: use of undeclared identifier 'MakeUnique'; did you mean 'mozilla::MakeUnique'? xpcom/base/nsErrorService.cpp:42:21: error: use of function template name with no prior declaration in function call with explicit template arguments is a C++20 extension [-Werror,-Wc++20-extensions] xpcom/base/nsErrorService.cpp:42:21: error: use of undeclared identifier 'MakeUnique'; did you mean 'mozilla::MakeUnique'? xpcom/base/nsMacPreferencesReader.mm:20:12: error: implicit instantiation of undefined template 'nsTSubstring<char16_t>' xpcom/base/nsMacPreferencesReader.mm:24:60: error: use of undeclared identifier 'NSString' xpcom/base/nsMacPreferencesReader.mm:24:69: error: expected expression xpcom/base/nsMacPreferencesReader.mm:26:48: error: unknown type name 'NSArray' xpcom/base/nsMacPreferencesReader.mm:28:30: error: use of undeclared identifier 'NSString' xpcom/base/nsMacPreferencesReader.mm:29:30: error: no matching function for call to 'MakeStringSpan' xpcom/base/nsMacPreferencesReader.mm:29:51: error: instance method '-UTF8String' not found (return type defaults to 'id') [-Werror,-Wobjc-method-access] xpcom/base/nsMacPreferencesReader.mm:30:22: error: instance method '-isKindOfClass:' not found (return type defaults to 'id') [-Werror,-Wobjc-method-access] xpcom/base/nsMacPreferencesReader.mm:30:37: error: receiver type 'int (*)(int)' is not 'id' or interface pointer, consider casting it to 'id' [-Werror,-Wreceiver-expr] xpcom/base/nsMacPreferencesReader.mm:30:37: error: use of undeclared identifier 'NSNumber'; did you mean 'isnumber'? xpcom/base/nsMacPreferencesReader.mm:30:46: error: instance method '-class' not found (return type defaults to 'id') [-Werror,-Wobjc-method-access] xpcom/base/nsMacPreferencesReader.mm:31:27: error: cannot initialize a parameter of type 'int64_t' (aka 'long long') with an rvalue of type 'id' xpcom/base/nsMacPreferencesReader.mm:31:33: error: instance method '-longLongValue' not found (return type defaults to 'id') [-Werror,-Wobjc-method-access] xpcom/base/nsMacPreferencesReader.mm:32:37: error: use of undeclared identifier 'NSArray' xpcom/base/nsMacPreferencesReader.mm:34:7: error: no matching function for call to 'EvaluateArray' xpcom/base/nsMacPreferencesReader.mm:36:37: error: use of undeclared identifier 'NSDictionary' xpcom/base/nsMacPreferencesReader.mm:38:7: error: no matching function for call to 'EvaluateDict' xpcom/base/nsMacPreferencesReader.mm:44:60: error: use of undeclared identifier 'NSString' xpcom/base/nsMacPreferencesReader.mm:44:69: error: expected expression xpcom/base/nsSystemInfo.cpp:415,35:: error: no template named 'NotNull'; did you mean 'NonNull'? xpcom/base/nsSystemInfo.cpp:419,25:: error: member reference type 'NonNull<IWSCProductList *>' is not a pointer; did you mean to use '.'? xpcom/base/nsSystemInfo.cpp:419,27:: error: no member named 'get_Count' in 'mozilla::dom::NonNull<IWSCProductList *>' xpcom/base/nsSystemInfo.cpp:426,19:: error: member reference type 'NonNull<IWSCProductList *>' is not a pointer; did you mean to use '.'? xpcom/base/nsSystemInfo.cpp:426,21:: error: no member named 'get_Item' in 'mozilla::dom::NonNull<IWSCProductList *>' xpcom/base/nsSystemInfo.cpp:484,17:: error: use of undeclared identifier 'ArrayLength'; did you mean 'mozilla::ArrayLength'? xpcom/base/nsSystemInfo.cpp:484,47:: error: use of undeclared identifier 'ArrayLength'; did you mean 'mozilla::ArrayLength'? xpcom/base/nsSystemInfo.cpp:487,36:: error: use of undeclared identifier 'ArrayLength'; did you mean 'mozilla::ArrayLength'? xpcom/base/nsSystemInfo.cpp:500,10:: error: no matching function for call to 'EnumWSCProductList' xpcom/base/nsSystemInfo.cpp:500,46:: error: use of undeclared identifier 'WrapNotNull'; did you mean 'mozilla::WrapNotNull'? xpcom/base/nsTraceRefcnt.cpp:313,32:: error: use of undeclared identifier 'MakeUnique' xpcom/base/nsTraceRefcnt.cpp:313:32: error: use of undeclared identifier 'MakeUnique' xpcom/base/nsTraceRefcnt.cpp:461,19:: error: no matching member function for call to 'WithEntryHandle' xpcom/base/nsTraceRefcnt.cpp:461:19: error: no matching member function for call to 'WithEntryHandle' xpcom/base/nsTraceRefcnt.cpp:468,33:: error: use of function template name with no prior declaration in function call with explicit template arguments is a C++20 extension [-Werror,-Wc++20-extensions] xpcom/base/nsTraceRefcnt.cpp:468,33:: error: use of undeclared identifier 'MakeUnique' xpcom/base/nsTraceRefcnt.cpp:468:33: error: use of undeclared identifier 'MakeUnique' xpcom/base/nsTraceRefcnt.cpp:736,20:: error: use of undeclared identifier '_write' Differential Revision: https://phabricator.services.mozilla.com/D127036
2021-10-01 04:49:16 +03:00
Atomic<uint32_t, MemoryOrdering::Relaxed> sNumLowPhysicalMemEvents;
namespace {
#if defined(XP_WIN)
# if (NTDDI_VERSION < NTDDI_WINBLUE) || \
(NTDDI_VERSION == NTDDI_WINBLUE && !defined(WINBLUE_KBSPRING14))
// Definitions for heap optimization that require the Windows SDK to target the
// Windows 8.1 Update
static const HEAP_INFORMATION_CLASS HeapOptimizeResources =
static_cast<HEAP_INFORMATION_CLASS>(3);
static const DWORD HEAP_OPTIMIZE_RESOURCES_CURRENT_VERSION = 1;
typedef struct _HEAP_OPTIMIZE_RESOURCES_INFORMATION {
DWORD Version;
DWORD Flags;
} HEAP_OPTIMIZE_RESOURCES_INFORMATION, *PHEAP_OPTIMIZE_RESOURCES_INFORMATION;
# endif
static int64_t LowMemoryEventsPhysicalDistinguishedAmount() {
return sNumLowPhysicalMemEvents;
}
class LowEventsReporter final : public nsIMemoryReporter {
~LowEventsReporter() {}
public:
NS_DECL_ISUPPORTS
NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
nsISupports* aData, bool aAnonymize) override {
// clang-format off
MOZ_COLLECT_REPORT(
"low-memory-events/physical", KIND_OTHER, UNITS_COUNT_CUMULATIVE,
LowMemoryEventsPhysicalDistinguishedAmount(),
"Number of low-physical-memory events fired since startup. We fire such an "
"event when a windows low memory resource notification is signaled. The "
"machine will start to page if it runs out of physical memory. This may "
"cause it to run slowly, but it shouldn't cause it to crash.");
// clang-format on
return NS_OK;
}
};
NS_IMPL_ISUPPORTS(LowEventsReporter, nsIMemoryReporter)
#endif // defined(XP_WIN)
/**
* This runnable is executed in response to a memory-pressure event; we spin
* the event-loop when receiving the memory-pressure event in the hope that
* other observers will synchronously free some memory that we'll be able to
* purge here.
*/
class nsJemallocFreeDirtyPagesRunnable final : public Runnable {
~nsJemallocFreeDirtyPagesRunnable() = default;
#if defined(XP_WIN)
void OptimizeSystemHeap();
#endif
public:
NS_DECL_NSIRUNNABLE
nsJemallocFreeDirtyPagesRunnable()
: Runnable("nsJemallocFreeDirtyPagesRunnable") {}
};
NS_IMETHODIMP
nsJemallocFreeDirtyPagesRunnable::Run() {
MOZ_ASSERT(NS_IsMainThread());
#if defined(MOZ_MEMORY)
jemalloc_free_dirty_pages();
#endif
#if defined(XP_WIN)
OptimizeSystemHeap();
#endif
return NS_OK;
}
#if defined(XP_WIN)
void nsJemallocFreeDirtyPagesRunnable::OptimizeSystemHeap() {
HEAP_OPTIMIZE_RESOURCES_INFORMATION heapOptInfo = {
HEAP_OPTIMIZE_RESOURCES_CURRENT_VERSION};
::HeapSetInformation(nullptr, HeapOptimizeResources, &heapOptInfo,
sizeof(heapOptInfo));
}
#endif // defined(XP_WIN)
/**
* The memory pressure watcher is used for listening to memory-pressure events
* and reacting upon them. We use one instance per process currently only for
* cleaning up dirty unused pages held by jemalloc.
*/
class nsMemoryPressureWatcher final : public nsIObserver {
~nsMemoryPressureWatcher() = default;
public:
NS_DECL_ISUPPORTS
NS_DECL_NSIOBSERVER
void Init();
};
NS_IMPL_ISUPPORTS(nsMemoryPressureWatcher, nsIObserver)
/**
* Initialize and subscribe to the memory-pressure events. We subscribe to the
* observer service in this method and not in the constructor because we need
* to hold a strong reference to 'this' before calling the observer service.
*/
void nsMemoryPressureWatcher::Init() {
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
if (os) {
os->AddObserver(this, "memory-pressure", /* ownsWeak */ false);
}
}
/**
* Reacts to all types of memory-pressure events, launches a runnable to
* free dirty pages held by jemalloc.
*/
NS_IMETHODIMP
nsMemoryPressureWatcher::Observe(nsISupports* aSubject, const char* aTopic,
const char16_t* aData) {
MOZ_ASSERT(!strcmp(aTopic, "memory-pressure"), "Unknown topic");
nsCOMPtr<nsIRunnable> runnable = new nsJemallocFreeDirtyPagesRunnable();
NS_DispatchToMainThread(runnable);
return NS_OK;
}
} // namespace
namespace mozilla {
namespace AvailableMemoryTracker {
void Init() {
// The watchers are held alive by the observer service.
RefPtr<nsMemoryPressureWatcher> watcher = new nsMemoryPressureWatcher();
watcher->Init();
#if defined(XP_WIN)
RegisterLowMemoryEventsPhysicalDistinguishedAmount(
LowMemoryEventsPhysicalDistinguishedAmount);
#endif // defined(XP_WIN)
}
} // namespace AvailableMemoryTracker
} // namespace mozilla