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 typedef struct _HEAP_OPTIMIZE_RESOURCES_INFORMATION
{
46 } HEAP_OPTIMIZE_RESOURCES_INFORMATION
, *PHEAP_OPTIMIZE_RESOURCES_INFORMATION
;
49 static int64_t LowMemoryEventsPhysicalDistinguishedAmount() {
50 return sNumLowPhysicalMemEvents
;
53 class LowEventsReporter final
: public nsIMemoryReporter
{
54 ~LowEventsReporter() {}
59 NS_IMETHOD
CollectReports(nsIHandleReportCallback
* aHandleReport
,
60 nsISupports
* aData
, bool aAnonymize
) override
{
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.");
75 NS_IMPL_ISUPPORTS(LowEventsReporter
, nsIMemoryReporter
)
77 #endif // defined(XP_WIN)
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
85 class nsJemallocFreeDirtyPagesRunnable final
: public Runnable
{
86 ~nsJemallocFreeDirtyPagesRunnable() = default;
89 void OptimizeSystemHeap();
95 nsJemallocFreeDirtyPagesRunnable()
96 : Runnable("nsJemallocFreeDirtyPagesRunnable") {}
100 nsJemallocFreeDirtyPagesRunnable::Run() {
101 MOZ_ASSERT(NS_IsMainThread());
103 #if defined(MOZ_MEMORY)
104 jemalloc_free_dirty_pages();
108 OptimizeSystemHeap();
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;
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();
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.
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
);
173 namespace AvailableMemoryTracker
{
176 // The watchers are held alive by the observer service.
177 RefPtr
<nsMemoryPressureWatcher
> watcher
= new nsMemoryPressureWatcher();
181 RegisterLowMemoryEventsPhysicalDistinguishedAmount(
182 LowMemoryEventsPhysicalDistinguishedAmount
);
183 #endif // defined(XP_WIN)
186 } // namespace AvailableMemoryTracker
187 } // namespace mozilla