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"
11 # include "nsIMemoryReporter.h"
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"
29 using namespace mozilla
;
31 Atomic
<uint32_t, MemoryOrdering::Relaxed
> sNumLowPhysicalMemEvents
;
37 # if defined(__MINGW32__)
38 // Definitions for heap optimization that require the Windows SDK to target the
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
{
48 } HEAP_OPTIMIZE_RESOURCES_INFORMATION
, *PHEAP_OPTIMIZE_RESOURCES_INFORMATION
;
51 static int64_t LowMemoryEventsPhysicalDistinguishedAmount() {
52 return sNumLowPhysicalMemEvents
;
55 class LowEventsReporter final
: public nsIMemoryReporter
{
56 ~LowEventsReporter() {}
61 NS_IMETHOD
CollectReports(nsIHandleReportCallback
* aHandleReport
,
62 nsISupports
* aData
, bool aAnonymize
) override
{
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.");
77 NS_IMPL_ISUPPORTS(LowEventsReporter
, nsIMemoryReporter
)
79 #endif // defined(XP_WIN)
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
87 class nsJemallocFreeDirtyPagesRunnable final
: public Runnable
{
88 ~nsJemallocFreeDirtyPagesRunnable() = default;
91 void OptimizeSystemHeap();
97 nsJemallocFreeDirtyPagesRunnable()
98 : Runnable("nsJemallocFreeDirtyPagesRunnable") {}
102 nsJemallocFreeDirtyPagesRunnable::Run() {
103 MOZ_ASSERT(NS_IsMainThread());
105 #if defined(MOZ_MEMORY)
106 jemalloc_free_dirty_pages();
110 OptimizeSystemHeap();
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;
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();
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.
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
);
175 namespace AvailableMemoryTracker
{
178 // The watchers are held alive by the observer service.
179 RefPtr
<nsMemoryPressureWatcher
> watcher
= new nsMemoryPressureWatcher();
183 RegisterLowMemoryEventsPhysicalDistinguishedAmount(
184 LowMemoryEventsPhysicalDistinguishedAmount
);
185 #endif // defined(XP_WIN)
188 } // namespace AvailableMemoryTracker
189 } // namespace mozilla