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/. */
6 
7 #include "mozilla/AvailableMemoryTracker.h"
8 
9 #if defined(XP_WIN)
10 #  include "mozilla/WindowsVersion.h"
11 #endif
12 
13 #include "nsIObserver.h"
14 #include "nsIObserverService.h"
15 #include "nsIRunnable.h"
16 #include "nsISupports.h"
17 #include "nsThreadUtils.h"
18 #include "nsXULAppAPI.h"
19 
20 #include "mozilla/Mutex.h"
21 #include "mozilla/ResultExtensions.h"
22 #include "mozilla/Services.h"
23 
24 #if defined(MOZ_MEMORY)
25 #  include "mozmemory.h"
26 #endif  // MOZ_MEMORY
27 
28 using namespace mozilla;
29 
30 namespace {
31 
32 #if defined(XP_WIN)
33 
34 #  if (NTDDI_VERSION < NTDDI_WINBLUE) || \
35       (NTDDI_VERSION == NTDDI_WINBLUE && !defined(WINBLUE_KBSPRING14))
36 // Definitions for heap optimization that require the Windows SDK to target the
37 // Windows 8.1 Update
38 static const HEAP_INFORMATION_CLASS HeapOptimizeResources =
39     static_cast<HEAP_INFORMATION_CLASS>(3);
40 
41 static const DWORD HEAP_OPTIMIZE_RESOURCES_CURRENT_VERSION = 1;
42 
43 typedef struct _HEAP_OPTIMIZE_RESOURCES_INFORMATION {
44   DWORD Version;
45   DWORD Flags;
46 } HEAP_OPTIMIZE_RESOURCES_INFORMATION, *PHEAP_OPTIMIZE_RESOURCES_INFORMATION;
47 #  endif
48 
49 Atomic<uint32_t, MemoryOrdering::Relaxed> sNumLowPhysicalMemEvents;
50 
LowMemoryEventsPhysicalDistinguishedAmount()51 static int64_t LowMemoryEventsPhysicalDistinguishedAmount() {
52   return sNumLowPhysicalMemEvents;
53 }
54 
55 class LowEventsReporter final : public nsIMemoryReporter {
~LowEventsReporter()56   ~LowEventsReporter() {}
57 
58  public:
59   NS_DECL_ISUPPORTS
60 
CollectReports(nsIHandleReportCallback * aHandleReport,nsISupports * aData,bool aAnonymize)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
72 
73     return NS_OK;
74   }
75 };
76 
77 NS_IMPL_ISUPPORTS(LowEventsReporter, nsIMemoryReporter)
78 
79 #endif  // defined(XP_WIN)
80 
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.
86  */
87 class nsJemallocFreeDirtyPagesRunnable final : public Runnable {
88   ~nsJemallocFreeDirtyPagesRunnable() = default;
89 
90 #if defined(XP_WIN)
91   void OptimizeSystemHeap();
92 #endif
93 
94  public:
95   NS_DECL_NSIRUNNABLE
96 
nsJemallocFreeDirtyPagesRunnable()97   nsJemallocFreeDirtyPagesRunnable()
98       : Runnable("nsJemallocFreeDirtyPagesRunnable") {}
99 };
100 
101 NS_IMETHODIMP
Run()102 nsJemallocFreeDirtyPagesRunnable::Run() {
103   MOZ_ASSERT(NS_IsMainThread());
104 
105 #if defined(MOZ_MEMORY)
106   jemalloc_free_dirty_pages();
107 #endif
108 
109 #if defined(XP_WIN)
110   OptimizeSystemHeap();
111 #endif
112 
113   return NS_OK;
114 }
115 
116 #if defined(XP_WIN)
OptimizeSystemHeap()117 void nsJemallocFreeDirtyPagesRunnable::OptimizeSystemHeap() {
118   // HeapSetInformation exists prior to Windows 8.1, but the
119   // HeapOptimizeResources information class does not.
120   if (IsWin8Point1OrLater()) {
121     HEAP_OPTIMIZE_RESOURCES_INFORMATION heapOptInfo = {
122         HEAP_OPTIMIZE_RESOURCES_CURRENT_VERSION};
123 
124     ::HeapSetInformation(nullptr, HeapOptimizeResources, &heapOptInfo,
125                          sizeof(heapOptInfo));
126   }
127 }
128 #endif  // defined(XP_WIN)
129 
130 /**
131  * The memory pressure watcher is used for listening to memory-pressure events
132  * and reacting upon them. We use one instance per process currently only for
133  * cleaning up dirty unused pages held by jemalloc.
134  */
135 class nsMemoryPressureWatcher final : public nsIObserver {
136   ~nsMemoryPressureWatcher() = default;
137 
138  public:
139   NS_DECL_ISUPPORTS
140   NS_DECL_NSIOBSERVER
141 
142   void Init();
143 };
144 
NS_IMPL_ISUPPORTS(nsMemoryPressureWatcher,nsIObserver)145 NS_IMPL_ISUPPORTS(nsMemoryPressureWatcher, nsIObserver)
146 
147 /**
148  * Initialize and subscribe to the memory-pressure events. We subscribe to the
149  * observer service in this method and not in the constructor because we need
150  * to hold a strong reference to 'this' before calling the observer service.
151  */
152 void nsMemoryPressureWatcher::Init() {
153   nsCOMPtr<nsIObserverService> os = services::GetObserverService();
154 
155   if (os) {
156     os->AddObserver(this, "memory-pressure", /* ownsWeak */ false);
157   }
158 }
159 
160 /**
161  * Reacts to all types of memory-pressure events, launches a runnable to
162  * free dirty pages held by jemalloc.
163  */
164 NS_IMETHODIMP
Observe(nsISupports * aSubject,const char * aTopic,const char16_t * aData)165 nsMemoryPressureWatcher::Observe(nsISupports* aSubject, const char* aTopic,
166                                  const char16_t* aData) {
167   MOZ_ASSERT(!strcmp(aTopic, "memory-pressure"), "Unknown topic");
168 
169   nsCOMPtr<nsIRunnable> runnable = new nsJemallocFreeDirtyPagesRunnable();
170 
171   NS_DispatchToMainThread(runnable);
172 
173   return NS_OK;
174 }
175 
176 }  // namespace
177 
178 namespace mozilla {
179 namespace AvailableMemoryTracker {
180 
Init()181 void Init() {
182   // The watchers are held alive by the observer service.
183   RefPtr<nsMemoryPressureWatcher> watcher = new nsMemoryPressureWatcher();
184   watcher->Init();
185 
186 #if defined(XP_WIN)
187   RegisterLowMemoryEventsPhysicalDistinguishedAmount(
188       LowMemoryEventsPhysicalDistinguishedAmount);
189 #endif  // defined(XP_WIN)
190 }
191 
192 }  // namespace AvailableMemoryTracker
193 }  // namespace mozilla
194