Bug 1867190 - Add prefs for PHC probablities r=glandium
[gecko.git] / xpcom / base / AvailableMemoryTracker.cpp
bloba14c43bcbc4597038cba9090669acff2694befa1
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 static const DWORD HEAP_OPTIMIZE_RESOURCES_CURRENT_VERSION = 1;
45 typedef struct _HEAP_OPTIMIZE_RESOURCES_INFORMATION {
46 DWORD Version;
47 DWORD Flags;
48 } HEAP_OPTIMIZE_RESOURCES_INFORMATION, *PHEAP_OPTIMIZE_RESOURCES_INFORMATION;
49 # endif
51 static int64_t LowMemoryEventsPhysicalDistinguishedAmount() {
52 return sNumLowPhysicalMemEvents;
55 class LowEventsReporter final : public nsIMemoryReporter {
56 ~LowEventsReporter() {}
58 public:
59 NS_DECL_ISUPPORTS
61 NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
62 nsISupports* aData, bool aAnonymize) override {
63 // clang-format off
64 MOZ_COLLECT_REPORT(
65 "low-memory-events/physical", KIND_OTHER, UNITS_COUNT_CUMULATIVE,
66 LowMemoryEventsPhysicalDistinguishedAmount(),
67 "Number of low-physical-memory events fired since startup. We fire such an "
68 "event when a windows low memory resource notification is signaled. The "
69 "machine will start to page if it runs out of physical memory. This may "
70 "cause it to run slowly, but it shouldn't cause it to crash.");
71 // clang-format on
73 return NS_OK;
77 NS_IMPL_ISUPPORTS(LowEventsReporter, nsIMemoryReporter)
79 #endif // defined(XP_WIN)
81 /**
82 * This runnable is executed in response to a memory-pressure event; we spin
83 * the event-loop when receiving the memory-pressure event in the hope that
84 * other observers will synchronously free some memory that we'll be able to
85 * purge here.
87 class nsJemallocFreeDirtyPagesRunnable final : public Runnable {
88 ~nsJemallocFreeDirtyPagesRunnable() = default;
90 #if defined(XP_WIN)
91 void OptimizeSystemHeap();
92 #endif
94 public:
95 NS_DECL_NSIRUNNABLE
97 nsJemallocFreeDirtyPagesRunnable()
98 : Runnable("nsJemallocFreeDirtyPagesRunnable") {}
101 NS_IMETHODIMP
102 nsJemallocFreeDirtyPagesRunnable::Run() {
103 MOZ_ASSERT(NS_IsMainThread());
105 #if defined(MOZ_MEMORY)
106 jemalloc_free_dirty_pages();
107 #endif
109 #if defined(XP_WIN)
110 OptimizeSystemHeap();
111 #endif
113 return NS_OK;
116 #if defined(XP_WIN)
117 void nsJemallocFreeDirtyPagesRunnable::OptimizeSystemHeap() {
118 HEAP_OPTIMIZE_RESOURCES_INFORMATION heapOptInfo = {
119 HEAP_OPTIMIZE_RESOURCES_CURRENT_VERSION};
121 ::HeapSetInformation(nullptr, HeapOptimizeResources, &heapOptInfo,
122 sizeof(heapOptInfo));
124 #endif // defined(XP_WIN)
127 * The memory pressure watcher is used for listening to memory-pressure events
128 * and reacting upon them. We use one instance per process currently only for
129 * cleaning up dirty unused pages held by jemalloc.
131 class nsMemoryPressureWatcher final : public nsIObserver {
132 ~nsMemoryPressureWatcher() = default;
134 public:
135 NS_DECL_ISUPPORTS
136 NS_DECL_NSIOBSERVER
138 void Init();
141 NS_IMPL_ISUPPORTS(nsMemoryPressureWatcher, nsIObserver)
144 * Initialize and subscribe to the memory-pressure events. We subscribe to the
145 * observer service in this method and not in the constructor because we need
146 * to hold a strong reference to 'this' before calling the observer service.
148 void nsMemoryPressureWatcher::Init() {
149 nsCOMPtr<nsIObserverService> os = services::GetObserverService();
151 if (os) {
152 os->AddObserver(this, "memory-pressure", /* ownsWeak */ false);
157 * Reacts to all types of memory-pressure events, launches a runnable to
158 * free dirty pages held by jemalloc.
160 NS_IMETHODIMP
161 nsMemoryPressureWatcher::Observe(nsISupports* aSubject, const char* aTopic,
162 const char16_t* aData) {
163 MOZ_ASSERT(!strcmp(aTopic, "memory-pressure"), "Unknown topic");
165 nsCOMPtr<nsIRunnable> runnable = new nsJemallocFreeDirtyPagesRunnable();
167 NS_DispatchToMainThread(runnable);
169 return NS_OK;
172 } // namespace
174 namespace mozilla {
175 namespace AvailableMemoryTracker {
177 void Init() {
178 // The watchers are held alive by the observer service.
179 RefPtr<nsMemoryPressureWatcher> watcher = new nsMemoryPressureWatcher();
180 watcher->Init();
182 #if defined(XP_WIN)
183 RegisterLowMemoryEventsPhysicalDistinguishedAmount(
184 LowMemoryEventsPhysicalDistinguishedAmount);
185 #endif // defined(XP_WIN)
188 } // namespace AvailableMemoryTracker
189 } // namespace mozilla