Bug 1910362 - Create new Nimbus helper r=aaronmt,ohorvath
[gecko.git] / xpcom / base / AvailableMemoryTracker.cpp
blob2b711a50b3f1f9b83edfb4f5ea5fc67a8b7b277c
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/AvailableMemoryTracker.h"
9 #if defined(XP_WIN)
10 # include <windows.h>
11 # include "nsIMemoryReporter.h"
12 #endif
14 #include "nsIObserver.h"
15 #include "nsIObserverService.h"
16 #include "nsIRunnable.h"
17 #include "nsISupports.h"
18 #include "nsThreadUtils.h"
19 #include "nsXULAppAPI.h"
21 #include "mozilla/Mutex.h"
22 #include "mozilla/ResultExtensions.h"
23 #include "mozilla/Services.h"
25 #if defined(MOZ_MEMORY)
26 # include "mozmemory.h"
27 #endif // MOZ_MEMORY
29 using namespace mozilla;
31 Atomic<uint32_t, MemoryOrdering::Relaxed> sNumLowPhysicalMemEvents;
33 namespace {
35 #if defined(XP_WIN)
37 # if defined(__MINGW32__)
38 // Definitions for heap optimization that require the Windows SDK to target the
39 // Windows 8.1 Update
40 static const HEAP_INFORMATION_CLASS HeapOptimizeResources =
41 static_cast<HEAP_INFORMATION_CLASS>(3);
43 typedef struct _HEAP_OPTIMIZE_RESOURCES_INFORMATION {
44 DWORD Version;
45 DWORD Flags;
46 } HEAP_OPTIMIZE_RESOURCES_INFORMATION, *PHEAP_OPTIMIZE_RESOURCES_INFORMATION;
47 # endif
49 static int64_t LowMemoryEventsPhysicalDistinguishedAmount() {
50 return sNumLowPhysicalMemEvents;
53 class LowEventsReporter final : public nsIMemoryReporter {
54 ~LowEventsReporter() {}
56 public:
57 NS_DECL_ISUPPORTS
59 NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
60 nsISupports* aData, bool aAnonymize) override {
61 // clang-format off
62 MOZ_COLLECT_REPORT(
63 "low-memory-events/physical", KIND_OTHER, UNITS_COUNT_CUMULATIVE,
64 LowMemoryEventsPhysicalDistinguishedAmount(),
65 "Number of low-physical-memory events fired since startup. We fire such an "
66 "event when a windows low memory resource notification is signaled. The "
67 "machine will start to page if it runs out of physical memory. This may "
68 "cause it to run slowly, but it shouldn't cause it to crash.");
69 // clang-format on
71 return NS_OK;
75 NS_IMPL_ISUPPORTS(LowEventsReporter, nsIMemoryReporter)
77 #endif // defined(XP_WIN)
79 /**
80 * This runnable is executed in response to a memory-pressure event; we spin
81 * the event-loop when receiving the memory-pressure event in the hope that
82 * other observers will synchronously free some memory that we'll be able to
83 * purge here.
85 class nsJemallocFreeDirtyPagesRunnable final : public Runnable {
86 ~nsJemallocFreeDirtyPagesRunnable() = default;
88 #if defined(XP_WIN)
89 void OptimizeSystemHeap();
90 #endif
92 public:
93 NS_DECL_NSIRUNNABLE
95 nsJemallocFreeDirtyPagesRunnable()
96 : Runnable("nsJemallocFreeDirtyPagesRunnable") {}
99 NS_IMETHODIMP
100 nsJemallocFreeDirtyPagesRunnable::Run() {
101 MOZ_ASSERT(NS_IsMainThread());
103 #if defined(MOZ_MEMORY)
104 jemalloc_free_dirty_pages();
105 #endif
107 #if defined(XP_WIN)
108 OptimizeSystemHeap();
109 #endif
111 return NS_OK;
114 #if defined(XP_WIN)
115 void nsJemallocFreeDirtyPagesRunnable::OptimizeSystemHeap() {
116 HEAP_OPTIMIZE_RESOURCES_INFORMATION heapOptInfo = {
117 HEAP_OPTIMIZE_RESOURCES_CURRENT_VERSION};
119 ::HeapSetInformation(nullptr, HeapOptimizeResources, &heapOptInfo,
120 sizeof(heapOptInfo));
122 #endif // defined(XP_WIN)
125 * The memory pressure watcher is used for listening to memory-pressure events
126 * and reacting upon them. We use one instance per process currently only for
127 * cleaning up dirty unused pages held by jemalloc.
129 class nsMemoryPressureWatcher final : public nsIObserver {
130 ~nsMemoryPressureWatcher() = default;
132 public:
133 NS_DECL_ISUPPORTS
134 NS_DECL_NSIOBSERVER
136 void Init();
139 NS_IMPL_ISUPPORTS(nsMemoryPressureWatcher, nsIObserver)
142 * Initialize and subscribe to the memory-pressure events. We subscribe to the
143 * observer service in this method and not in the constructor because we need
144 * to hold a strong reference to 'this' before calling the observer service.
146 void nsMemoryPressureWatcher::Init() {
147 nsCOMPtr<nsIObserverService> os = services::GetObserverService();
149 if (os) {
150 os->AddObserver(this, "memory-pressure", /* ownsWeak */ false);
155 * Reacts to all types of memory-pressure events, launches a runnable to
156 * free dirty pages held by jemalloc.
158 NS_IMETHODIMP
159 nsMemoryPressureWatcher::Observe(nsISupports* aSubject, const char* aTopic,
160 const char16_t* aData) {
161 MOZ_ASSERT(!strcmp(aTopic, "memory-pressure"), "Unknown topic");
163 nsCOMPtr<nsIRunnable> runnable = new nsJemallocFreeDirtyPagesRunnable();
165 NS_DispatchToMainThread(runnable);
167 return NS_OK;
170 } // namespace
172 namespace mozilla {
173 namespace AvailableMemoryTracker {
175 void Init() {
176 // The watchers are held alive by the observer service.
177 RefPtr<nsMemoryPressureWatcher> watcher = new nsMemoryPressureWatcher();
178 watcher->Init();
180 #if defined(XP_WIN)
181 RegisterLowMemoryEventsPhysicalDistinguishedAmount(
182 LowMemoryEventsPhysicalDistinguishedAmount);
183 #endif // defined(XP_WIN)
186 } // namespace AvailableMemoryTracker
187 } // namespace mozilla