2013-01-18 09:43:21 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-12-15 23:48:38 +04:00
|
|
|
|
|
|
|
#include "mozilla/AvailableMemoryTracker.h"
|
2012-11-12 20:41:23 +04:00
|
|
|
|
2013-09-19 22:29:31 +04:00
|
|
|
#if defined(XP_WIN)
|
2019-05-26 10:45:56 +03:00
|
|
|
# include "mozilla/WindowsVersion.h"
|
2018-05-28 11:09:20 +03:00
|
|
|
# include "nsExceptionHandler.h"
|
2018-08-21 00:53:27 +03:00
|
|
|
# include "nsICrashReporter.h"
|
2013-09-19 22:27:35 +04:00
|
|
|
# include "nsIMemoryReporter.h"
|
2013-09-19 22:29:31 +04:00
|
|
|
# include "nsMemoryPressure.h"
|
|
|
|
#endif
|
|
|
|
|
2012-11-12 20:41:23 +04:00
|
|
|
#include "nsIObserver.h"
|
|
|
|
#include "nsIObserverService.h"
|
|
|
|
#include "nsIRunnable.h"
|
|
|
|
#include "nsISupports.h"
|
2018-05-17 15:48:02 +03:00
|
|
|
#include "nsITimer.h"
|
2013-09-19 22:29:31 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2018-05-17 15:48:02 +03:00
|
|
|
#include "nsXULAppAPI.h"
|
2012-11-12 20:41:23 +04:00
|
|
|
|
2018-05-17 15:48:02 +03:00
|
|
|
#include "mozilla/ResultExtensions.h"
|
2012-11-12 20:41:23 +04:00
|
|
|
#include "mozilla/Services.h"
|
2018-05-17 15:48:02 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2012-11-12 20:41:23 +04:00
|
|
|
|
|
|
|
#if defined(MOZ_MEMORY)
|
2012-12-07 12:32:24 +04:00
|
|
|
# include "mozmemory.h"
|
2012-11-12 20:41:23 +04:00
|
|
|
#endif // MOZ_MEMORY
|
2011-12-15 23:48:38 +04:00
|
|
|
|
2011-12-19 20:57:57 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
namespace {
|
2011-12-15 23:48:38 +04:00
|
|
|
|
2018-05-17 15:48:02 +03:00
|
|
|
#if defined(XP_WIN)
|
2011-12-15 23:48:38 +04:00
|
|
|
|
2019-05-26 10:45:56 +03:00
|
|
|
# if (NTDDI_VERSION < NTDDI_WINBLUE) || \
|
|
|
|
(NTDDI_VERSION == NTDDI_WINBLUE && !defined(WINBLUE_KBSPRING14))
|
|
|
|
// Definitions for heap optimization that require the Windows SDK to target the
|
|
|
|
// Windows 8.1 Update
|
|
|
|
static const HEAP_INFORMATION_CLASS HeapOptimizeResources =
|
|
|
|
static_cast<HEAP_INFORMATION_CLASS>(3);
|
|
|
|
|
|
|
|
static const DWORD HEAP_OPTIMIZE_RESOURCES_CURRENT_VERSION = 1;
|
|
|
|
|
|
|
|
typedef struct _HEAP_OPTIMIZE_RESOURCES_INFORMATION {
|
|
|
|
DWORD Version;
|
|
|
|
DWORD Flags;
|
|
|
|
} HEAP_OPTIMIZE_RESOURCES_INFORMATION, *PHEAP_OPTIMIZE_RESOURCES_INFORMATION;
|
|
|
|
# endif
|
|
|
|
|
2018-04-18 18:07:39 +03:00
|
|
|
Atomic<uint32_t, MemoryOrdering::Relaxed> sNumLowVirtualMemEvents;
|
|
|
|
Atomic<uint32_t, MemoryOrdering::Relaxed> sNumLowCommitSpaceEvents;
|
|
|
|
Atomic<uint32_t, MemoryOrdering::Relaxed> sNumLowPhysicalMemEvents;
|
2011-12-19 20:57:57 +04:00
|
|
|
|
2018-05-17 15:48:02 +03:00
|
|
|
class nsAvailableMemoryWatcher final : public nsIObserver,
|
|
|
|
public nsITimerCallback {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
NS_DECL_NSITIMERCALLBACK
|
|
|
|
|
2018-08-21 00:53:27 +03:00
|
|
|
nsAvailableMemoryWatcher();
|
2018-05-17 15:48:02 +03:00
|
|
|
nsresult Init();
|
|
|
|
|
|
|
|
private:
|
2018-07-04 22:06:07 +03:00
|
|
|
// Fire a low-memory notification if we have less than this many bytes of
|
|
|
|
// virtual address space available.
|
|
|
|
# if defined(HAVE_64BIT_BUILD)
|
|
|
|
static const size_t kLowVirtualMemoryThreshold = 0;
|
|
|
|
# else
|
2019-03-19 02:57:17 +03:00
|
|
|
static const size_t kLowVirtualMemoryThreshold = 384 * 1024 * 1024;
|
2018-07-04 22:06:07 +03:00
|
|
|
# endif
|
|
|
|
|
2018-08-21 00:53:27 +03:00
|
|
|
// Fire a low-memory notification if we have less than this many bytes of
|
|
|
|
// commit space (physical memory plus page file) left.
|
2019-03-19 02:57:17 +03:00
|
|
|
static const size_t kLowCommitSpaceThreshold = 384 * 1024 * 1024;
|
2018-07-04 22:06:07 +03:00
|
|
|
|
|
|
|
// Fire a low-memory notification if we have less than this many bytes of
|
|
|
|
// physical memory available on the whole machine.
|
|
|
|
static const size_t kLowPhysicalMemoryThreshold = 0;
|
|
|
|
|
|
|
|
// Don't fire a low-memory notification because of low available physical
|
|
|
|
// memory or low commit space more often than this interval.
|
|
|
|
static const uint32_t kLowMemoryNotificationIntervalMS = 10000;
|
|
|
|
|
2018-05-17 15:48:02 +03:00
|
|
|
// Poll the amount of free memory at this rate.
|
|
|
|
static const uint32_t kPollingIntervalMS = 1000;
|
|
|
|
|
2018-07-04 22:06:07 +03:00
|
|
|
// Observer topics we subscribe to, see below.
|
2018-05-17 15:48:02 +03:00
|
|
|
static const char* const kObserverTopics[];
|
|
|
|
|
|
|
|
static bool IsVirtualMemoryLow(const MEMORYSTATUSEX& aStat);
|
|
|
|
static bool IsCommitSpaceLow(const MEMORYSTATUSEX& aStat);
|
|
|
|
static bool IsPhysicalMemoryLow(const MEMORYSTATUSEX& aStat);
|
|
|
|
|
|
|
|
~nsAvailableMemoryWatcher(){};
|
2018-08-21 00:53:27 +03:00
|
|
|
bool OngoingMemoryPressure() { return mUnderMemoryPressure; }
|
2018-05-17 15:48:02 +03:00
|
|
|
void AdjustPollingInterval(const bool aLowMemory);
|
|
|
|
void SendMemoryPressureEvent();
|
2018-08-21 00:53:27 +03:00
|
|
|
void MaybeSaveMemoryReport();
|
2018-05-17 15:48:02 +03:00
|
|
|
void Shutdown();
|
|
|
|
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
bool mUnderMemoryPressure;
|
2018-08-21 00:53:27 +03:00
|
|
|
bool mSavedReport;
|
2018-05-17 15:48:02 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const char* const nsAvailableMemoryWatcher::kObserverTopics[] = {
|
|
|
|
"quit-application",
|
|
|
|
"user-interaction-active",
|
|
|
|
"user-interaction-inactive",
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(nsAvailableMemoryWatcher, nsIObserver, nsITimerCallback)
|
|
|
|
|
2018-08-21 00:53:27 +03:00
|
|
|
nsAvailableMemoryWatcher::nsAvailableMemoryWatcher()
|
|
|
|
: mTimer(nullptr), mUnderMemoryPressure(false), mSavedReport(false) {}
|
|
|
|
|
2018-05-17 15:48:02 +03:00
|
|
|
nsresult nsAvailableMemoryWatcher::Init() {
|
|
|
|
mTimer = NS_NewTimer();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> observerService = services::GetObserverService();
|
|
|
|
MOZ_ASSERT(observerService);
|
|
|
|
|
|
|
|
for (auto topic : kObserverTopics) {
|
|
|
|
nsresult rv = observerService->AddObserver(this, topic,
|
|
|
|
/* ownsWeak */ false);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_TRY(mTimer->InitWithCallback(this, kPollingIntervalMS,
|
|
|
|
nsITimer::TYPE_REPEATING_SLACK));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsAvailableMemoryWatcher::Shutdown() {
|
|
|
|
nsCOMPtr<nsIObserverService> observerService = services::GetObserverService();
|
|
|
|
MOZ_ASSERT(observerService);
|
|
|
|
|
|
|
|
for (auto topic : kObserverTopics) {
|
|
|
|
Unused << observerService->RemoveObserver(this, topic);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
mTimer = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:14:01 +03:00
|
|
|
/* static */
|
|
|
|
bool nsAvailableMemoryWatcher::IsVirtualMemoryLow(const MEMORYSTATUSEX& aStat) {
|
2018-05-17 15:48:02 +03:00
|
|
|
if ((kLowVirtualMemoryThreshold != 0) &&
|
|
|
|
(aStat.ullAvailVirtual < kLowVirtualMemoryThreshold)) {
|
|
|
|
sNumLowVirtualMemEvents++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:14:01 +03:00
|
|
|
/* static */
|
|
|
|
bool nsAvailableMemoryWatcher::IsCommitSpaceLow(const MEMORYSTATUSEX& aStat) {
|
2018-05-17 15:48:02 +03:00
|
|
|
if ((kLowCommitSpaceThreshold != 0) &&
|
|
|
|
(aStat.ullAvailPageFile < kLowCommitSpaceThreshold)) {
|
|
|
|
sNumLowCommitSpaceEvents++;
|
2018-05-28 11:09:20 +03:00
|
|
|
CrashReporter::AnnotateCrashReport(
|
Bug 1348273 - Convert crash annotations into a machine-readable list of constants; r=ted.mielczarek,njn,dholbert,mak,cpearce,mcmanus,froydnj,Dexter,jrmuizel,jchen,jimm,bz,surkov
This introduces the machinery needed to generate crash annotations from a YAML
file. The relevant C++ functions are updated to take a typed enum. JavaScript
calls are unaffected but they will throw if the string argument does not
correspond to one of the known entries in the C++ enum. The existing whitelists
and blacklists of annotations are also generated from the YAML file and all
duplicate code related to them has been consolidated. Once written out to the
.extra file the annotations are converted in string form and are no different
than the existing ones.
All existing annotations have been included in the list (and some obsolete ones
have been removed) and all call sites have been updated including tests where
appropriate.
--HG--
extra : source : 4f6c43f2830701ec5552e08e3f1b06fe6d045860
2018-07-05 16:42:11 +03:00
|
|
|
CrashReporter::Annotation::LowCommitSpaceEvents,
|
|
|
|
uint32_t(sNumLowCommitSpaceEvents));
|
2018-05-17 15:48:02 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:14:01 +03:00
|
|
|
/* static */
|
|
|
|
bool nsAvailableMemoryWatcher::IsPhysicalMemoryLow(
|
2018-05-17 15:48:02 +03:00
|
|
|
const MEMORYSTATUSEX& aStat) {
|
|
|
|
if ((kLowPhysicalMemoryThreshold != 0) &&
|
|
|
|
(aStat.ullAvailPhys < kLowPhysicalMemoryThreshold)) {
|
|
|
|
sNumLowPhysicalMemEvents++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsAvailableMemoryWatcher::SendMemoryPressureEvent() {
|
2018-08-21 00:53:27 +03:00
|
|
|
MemoryPressureState state =
|
|
|
|
OngoingMemoryPressure() ? MemPressure_Ongoing : MemPressure_New;
|
2018-05-17 15:48:02 +03:00
|
|
|
NS_DispatchEventualMemoryPressure(state);
|
|
|
|
}
|
|
|
|
|
2018-08-21 00:53:27 +03:00
|
|
|
void nsAvailableMemoryWatcher::MaybeSaveMemoryReport() {
|
|
|
|
if (!mSavedReport && OngoingMemoryPressure()) {
|
|
|
|
nsCOMPtr<nsICrashReporter> cr =
|
|
|
|
do_GetService("@mozilla.org/toolkit/crash-reporter;1");
|
|
|
|
if (cr) {
|
|
|
|
if (NS_SUCCEEDED(cr->SaveMemoryReport())) {
|
|
|
|
mSavedReport = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 15:48:02 +03:00
|
|
|
void nsAvailableMemoryWatcher::AdjustPollingInterval(const bool aLowMemory) {
|
|
|
|
if (aLowMemory) {
|
|
|
|
// We entered a low-memory state, wait for a longer interval before polling
|
|
|
|
// again as there's no point in rapidly sending further notifications.
|
|
|
|
mTimer->SetDelay(kLowMemoryNotificationIntervalMS);
|
2018-08-21 00:53:27 +03:00
|
|
|
} else if (OngoingMemoryPressure()) {
|
2018-05-17 15:48:02 +03:00
|
|
|
// We were under memory pressure but we're not anymore, resume polling at
|
|
|
|
// a faster pace.
|
|
|
|
mTimer->SetDelay(kPollingIntervalMS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timer callback, polls memory stats to detect low-memory conditions. This
|
|
|
|
// will send memory-pressure events if memory is running low and adjust the
|
|
|
|
// polling interval accordingly.
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsAvailableMemoryWatcher::Notify(nsITimer* aTimer) {
|
|
|
|
MEMORYSTATUSEX stat;
|
|
|
|
stat.dwLength = sizeof(stat);
|
|
|
|
bool success = GlobalMemoryStatusEx(&stat);
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
bool lowMemory = IsVirtualMemoryLow(stat) || IsCommitSpaceLow(stat) ||
|
|
|
|
IsPhysicalMemoryLow(stat);
|
|
|
|
|
|
|
|
if (lowMemory) {
|
|
|
|
SendMemoryPressureEvent();
|
2018-08-21 00:53:27 +03:00
|
|
|
MaybeSaveMemoryReport();
|
|
|
|
} else {
|
|
|
|
mSavedReport = false; // Save a new report if memory gets low again
|
2018-05-17 15:48:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
AdjustPollingInterval(lowMemory);
|
|
|
|
mUnderMemoryPressure = lowMemory;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Observer service callback, used to stop the polling timer when the user
|
|
|
|
// stops interacting with Firefox and resuming it when they interact again.
|
|
|
|
// Also used to shut down the service if the application is quitting.
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsAvailableMemoryWatcher::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData) {
|
|
|
|
if (strcmp(aTopic, "quit-application") == 0) {
|
|
|
|
Shutdown();
|
|
|
|
} else if (strcmp(aTopic, "user-interaction-inactive") == 0) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
} else if (strcmp(aTopic, "user-interaction-active") == 0) {
|
|
|
|
mTimer->InitWithCallback(this, kPollingIntervalMS,
|
|
|
|
nsITimer::TYPE_REPEATING_SLACK);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown topic");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-09-20 02:52:30 +04:00
|
|
|
static int64_t LowMemoryEventsVirtualDistinguishedAmount() {
|
|
|
|
return sNumLowVirtualMemEvents;
|
|
|
|
}
|
|
|
|
|
2018-05-16 14:58:10 +03:00
|
|
|
static int64_t LowMemoryEventsCommitSpaceDistinguishedAmount() {
|
|
|
|
return sNumLowCommitSpaceEvents;
|
|
|
|
}
|
|
|
|
|
2013-12-08 10:09:10 +04:00
|
|
|
static int64_t LowMemoryEventsPhysicalDistinguishedAmount() {
|
|
|
|
return sNumLowPhysicalMemEvents;
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class LowEventsReporter final : public nsIMemoryReporter {
|
2014-07-15 05:47:01 +04:00
|
|
|
~LowEventsReporter() {}
|
|
|
|
|
2011-12-19 20:57:57 +04:00
|
|
|
public:
|
2013-12-08 10:09:10 +04:00
|
|
|
NS_DECL_ISUPPORTS
|
2011-12-19 20:57:57 +04:00
|
|
|
|
2013-12-08 10:09:10 +04:00
|
|
|
NS_IMETHOD CollectReports(nsIHandleReportCallback* aHandleReport,
|
2016-08-08 04:04:11 +03:00
|
|
|
nsISupports* aData, bool aAnonymize) override {
|
2018-11-29 00:06:13 +03:00
|
|
|
// clang-format off
|
2016-09-14 14:52:45 +03:00
|
|
|
MOZ_COLLECT_REPORT(
|
|
|
|
"low-memory-events/virtual", KIND_OTHER, UNITS_COUNT_CUMULATIVE,
|
|
|
|
LowMemoryEventsVirtualDistinguishedAmount(),
|
2013-12-08 10:09:10 +04:00
|
|
|
"Number of low-virtual-memory events fired since startup. We fire such an "
|
|
|
|
"event if we notice there is less than memory.low_virtual_mem_threshold_mb of "
|
|
|
|
"virtual address space available (if zero, this behavior is disabled). The "
|
|
|
|
"process will probably crash if it runs out of virtual address space, so "
|
|
|
|
"this event is dire.");
|
2013-01-18 09:43:21 +04:00
|
|
|
|
2016-08-24 08:23:45 +03:00
|
|
|
MOZ_COLLECT_REPORT(
|
2016-09-14 14:52:45 +03:00
|
|
|
"low-memory-events/commit-space", KIND_OTHER, UNITS_COUNT_CUMULATIVE,
|
2018-05-16 14:58:10 +03:00
|
|
|
LowMemoryEventsCommitSpaceDistinguishedAmount(),
|
2013-08-28 03:24:51 +04:00
|
|
|
"Number of low-commit-space events fired since startup. We fire such an "
|
|
|
|
"event if we notice there is less than memory.low_commit_space_threshold_mb of "
|
2013-12-08 10:09:10 +04:00
|
|
|
"commit space available (if zero, this behavior is disabled). Windows will "
|
2013-08-28 03:24:51 +04:00
|
|
|
"likely kill the process if it runs out of commit space, so this event is "
|
2013-12-08 10:09:10 +04:00
|
|
|
"dire.");
|
2013-09-20 02:52:30 +04:00
|
|
|
|
2016-08-24 08:23:45 +03:00
|
|
|
MOZ_COLLECT_REPORT(
|
2013-12-08 10:09:10 +04:00
|
|
|
"low-memory-events/physical", KIND_OTHER, UNITS_COUNT_CUMULATIVE,
|
|
|
|
LowMemoryEventsPhysicalDistinguishedAmount(),
|
2013-08-28 03:24:51 +04:00
|
|
|
"Number of low-physical-memory events fired since startup. We fire such an "
|
|
|
|
"event if we notice there is less than memory.low_physical_memory_threshold_mb "
|
|
|
|
"of physical memory available (if zero, this behavior is disabled). The "
|
|
|
|
"machine will start to page if it runs out of physical memory. This may "
|
2013-12-08 10:09:10 +04:00
|
|
|
"cause it to run slowly, but it shouldn't cause it to crash.");
|
2018-11-29 00:06:13 +03:00
|
|
|
// clang-format on
|
2011-12-19 20:57:57 +04:00
|
|
|
|
2013-12-08 10:09:10 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2013-01-18 09:43:21 +04:00
|
|
|
};
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(LowEventsReporter, nsIMemoryReporter)
|
2011-12-19 20:57:57 +04:00
|
|
|
|
2018-05-17 15:48:02 +03:00
|
|
|
#endif // defined(XP_WIN)
|
2012-11-12 20:41:23 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This runnable is executed in response to a memory-pressure event; we spin
|
|
|
|
* the event-loop when receiving the memory-pressure event in the hope that
|
|
|
|
* other observers will synchronously free some memory that we'll be able to
|
|
|
|
* purge here.
|
|
|
|
*/
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsJemallocFreeDirtyPagesRunnable final : public nsIRunnable {
|
2014-07-01 02:11:53 +04:00
|
|
|
~nsJemallocFreeDirtyPagesRunnable() {}
|
|
|
|
|
2019-05-26 10:45:56 +03:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
void OptimizeSystemHeap();
|
|
|
|
#endif
|
|
|
|
|
2012-11-12 20:41:23 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsJemallocFreeDirtyPagesRunnable, nsIRunnable)
|
2012-11-12 20:41:23 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJemallocFreeDirtyPagesRunnable::Run() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-12-07 12:32:24 +04:00
|
|
|
#if defined(MOZ_MEMORY)
|
2012-11-12 20:41:23 +04:00
|
|
|
jemalloc_free_dirty_pages();
|
|
|
|
#endif
|
|
|
|
|
2019-05-26 10:45:56 +03:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
OptimizeSystemHeap();
|
|
|
|
#endif
|
|
|
|
|
2012-11-12 20:41:23 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-26 10:45:56 +03:00
|
|
|
#if defined(XP_WIN)
|
|
|
|
void nsJemallocFreeDirtyPagesRunnable::OptimizeSystemHeap() {
|
|
|
|
// HeapSetInformation exists prior to Windows 8.1, but the
|
|
|
|
// HeapOptimizeResources information class does not.
|
|
|
|
if (IsWin8Point1OrLater()) {
|
|
|
|
HEAP_OPTIMIZE_RESOURCES_INFORMATION heapOptInfo = {
|
|
|
|
HEAP_OPTIMIZE_RESOURCES_CURRENT_VERSION};
|
|
|
|
|
|
|
|
::HeapSetInformation(nullptr, HeapOptimizeResources, &heapOptInfo,
|
|
|
|
sizeof(heapOptInfo));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // defined(XP_WIN)
|
|
|
|
|
2012-11-12 20:41:23 +04:00
|
|
|
/**
|
|
|
|
* The memory pressure watcher is used for listening to memory-pressure events
|
|
|
|
* and reacting upon them. We use one instance per process currently only for
|
|
|
|
* cleaning up dirty unused pages held by jemalloc.
|
|
|
|
*/
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsMemoryPressureWatcher final : public nsIObserver {
|
2014-07-01 02:11:53 +04:00
|
|
|
~nsMemoryPressureWatcher() {}
|
|
|
|
|
2012-11-12 20:41:23 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIOBSERVER
|
|
|
|
|
|
|
|
void Init();
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsMemoryPressureWatcher, nsIObserver)
|
2012-11-12 20:41:23 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize and subscribe to the memory-pressure events. We subscribe to the
|
|
|
|
* observer service in this method and not in the constructor because we need
|
|
|
|
* to hold a strong reference to 'this' before calling the observer service.
|
|
|
|
*/
|
|
|
|
void nsMemoryPressureWatcher::Init() {
|
|
|
|
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
|
|
|
|
|
|
|
if (os) {
|
|
|
|
os->AddObserver(this, "memory-pressure", /* ownsWeak */ false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reacts to all types of memory-pressure events, launches a runnable to
|
|
|
|
* free dirty pages held by jemalloc.
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
2014-05-13 21:41:38 +04:00
|
|
|
nsMemoryPressureWatcher::Observe(nsISupports* aSubject, const char* aTopic,
|
|
|
|
const char16_t* aData) {
|
|
|
|
MOZ_ASSERT(!strcmp(aTopic, "memory-pressure"), "Unknown topic");
|
2012-11-12 20:41:23 +04:00
|
|
|
|
2017-11-07 11:34:18 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable = new nsJemallocFreeDirtyPagesRunnable();
|
2012-11-12 20:41:23 +04:00
|
|
|
|
2017-11-07 11:34:18 +03:00
|
|
|
NS_DispatchToMainThread(runnable);
|
2012-11-12 20:41:23 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2011-12-19 20:57:57 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace AvailableMemoryTracker {
|
|
|
|
|
2018-07-04 22:06:07 +03:00
|
|
|
void Init() {
|
|
|
|
// The watchers are held alive by the observer service.
|
|
|
|
RefPtr<nsMemoryPressureWatcher> watcher = new nsMemoryPressureWatcher();
|
|
|
|
watcher->Init();
|
2012-04-09 21:53:08 +04:00
|
|
|
|
2018-07-04 22:06:07 +03:00
|
|
|
#if defined(XP_WIN)
|
2013-12-08 10:09:10 +04:00
|
|
|
RegisterStrongMemoryReporter(new LowEventsReporter());
|
2014-05-13 21:41:38 +04:00
|
|
|
RegisterLowMemoryEventsVirtualDistinguishedAmount(
|
|
|
|
LowMemoryEventsVirtualDistinguishedAmount);
|
2018-07-04 22:06:07 +03:00
|
|
|
RegisterLowMemoryEventsCommitSpaceDistinguishedAmount(
|
|
|
|
LowMemoryEventsCommitSpaceDistinguishedAmount);
|
2014-05-13 21:41:38 +04:00
|
|
|
RegisterLowMemoryEventsPhysicalDistinguishedAmount(
|
|
|
|
LowMemoryEventsPhysicalDistinguishedAmount);
|
2012-11-12 20:41:23 +04:00
|
|
|
|
2018-05-17 15:48:02 +03:00
|
|
|
if (XRE_IsParentProcess()) {
|
|
|
|
RefPtr<nsAvailableMemoryWatcher> poller = new nsAvailableMemoryWatcher();
|
|
|
|
|
|
|
|
if (NS_FAILED(poller->Init())) {
|
|
|
|
NS_WARNING("Could not start the available memory watcher");
|
|
|
|
}
|
|
|
|
}
|
2018-07-04 22:06:07 +03:00
|
|
|
#endif // defined(XP_WIN)
|
2011-12-15 23:48:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace AvailableMemoryTracker
|
|
|
|
} // namespace mozilla
|