2013-01-15 00:28:36 +00: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 11:12:37 +00: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/. */
|
|
|
|
|
2014-01-27 19:54:37 +00:00
|
|
|
#ifndef nsMemoryReporterManager_h__
|
|
|
|
#define nsMemoryReporterManager_h__
|
|
|
|
|
2015-05-06 18:51:00 +00:00
|
|
|
#include "mozilla/Mutex.h"
|
|
|
|
#include "nsHashKeys.h"
|
2008-03-06 01:28:25 +00:00
|
|
|
#include "nsIMemoryReporter.h"
|
2014-04-07 14:46:20 +00:00
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2013-02-01 15:12:52 +00:00
|
|
|
#include "nsTHashtable.h"
|
2013-10-23 05:26:24 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
2015-05-06 18:51:00 +00:00
|
|
|
class ContentParent;
|
2013-10-23 05:26:24 +00:00
|
|
|
class MemoryReport;
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
2013-10-23 05:26:24 +00:00
|
|
|
|
2015-05-06 18:51:00 +00:00
|
|
|
class nsITimer;
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class nsMemoryReporterManager final : public nsIMemoryReporterManager
|
2008-03-06 01:28:25 +00:00
|
|
|
{
|
2014-06-30 22:11:53 +00:00
|
|
|
virtual ~nsMemoryReporterManager();
|
|
|
|
|
2008-03-06 01:28:25 +00:00
|
|
|
public:
|
2013-07-19 02:31:26 +00:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2013-01-15 00:28:36 +00:00
|
|
|
NS_DECL_NSIMEMORYREPORTERMANAGER
|
2008-03-06 01:28:25 +00:00
|
|
|
|
2013-01-15 00:28:36 +00:00
|
|
|
nsMemoryReporterManager();
|
2010-12-15 17:47:16 +00:00
|
|
|
|
2013-10-23 05:26:24 +00:00
|
|
|
// Gets the memory reporter manager service.
|
|
|
|
static nsMemoryReporterManager* GetOrCreate()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIMemoryReporterManager> imgr =
|
|
|
|
do_GetService("@mozilla.org/memory-reporter-manager;1");
|
|
|
|
return static_cast<nsMemoryReporterManager*>(imgr.get());
|
|
|
|
}
|
|
|
|
|
2015-10-14 23:52:58 +00:00
|
|
|
typedef nsDataHashtable<nsRefPtrHashKey<nsIMemoryReporter>, bool> StrongReportersTable;
|
|
|
|
typedef nsDataHashtable<nsPtrHashKey<nsIMemoryReporter>, bool> WeakReportersTable;
|
2013-11-07 05:35:30 +00:00
|
|
|
|
2013-10-23 05:26:24 +00:00
|
|
|
// Inter-process memory reporting proceeds as follows.
|
|
|
|
//
|
|
|
|
// - GetReports() (declared within NS_DECL_NSIMEMORYREPORTERMANAGER)
|
2015-05-06 18:51:00 +00:00
|
|
|
// synchronously gets memory reports for the current process, sets up some
|
2015-10-14 23:52:57 +00:00
|
|
|
// state (mPendingProcessesState) for when child processes report back --
|
2015-05-06 18:51:00 +00:00
|
|
|
// including a timer -- and starts telling child processes to get memory
|
|
|
|
// reports. Control then returns to the main event loop.
|
|
|
|
//
|
|
|
|
// The number of concurrent child process reports is limited by the pref
|
|
|
|
// "memory.report_concurrency" in order to prevent the memory overhead of
|
|
|
|
// memory reporting from causing problems, especially on B2G when swapping
|
|
|
|
// to compressed RAM; see bug 1154053.
|
2013-10-23 05:26:24 +00:00
|
|
|
//
|
2015-04-27 19:45:00 +00:00
|
|
|
// - HandleChildReport() is called (asynchronously) once per child process
|
|
|
|
// reporter callback.
|
|
|
|
//
|
2015-05-06 18:51:00 +00:00
|
|
|
// - EndProcessReport() is called (asynchronously) once per process that
|
|
|
|
// finishes reporting back, including the parent. If all processes do so
|
|
|
|
// before time-out, the timer is cancelled. If there are child processes
|
|
|
|
// whose requests have not yet been sent, they will be started until the
|
|
|
|
// concurrency limit is (again) reached.
|
2013-10-23 05:26:24 +00:00
|
|
|
//
|
|
|
|
// - TimeoutCallback() is called (asynchronously) if all the child processes
|
|
|
|
// don't respond within the time threshold.
|
|
|
|
//
|
|
|
|
// - FinishReporting() finishes things off. It is *always* called -- either
|
2015-04-27 19:45:00 +00:00
|
|
|
// from EndChildReport() (if all child processes have reported back) or
|
2013-10-23 05:26:24 +00:00
|
|
|
// from TimeoutCallback() (if time-out occurs).
|
|
|
|
//
|
|
|
|
// All operations occur on the main thread.
|
|
|
|
//
|
|
|
|
// The above sequence of steps is a "request". A partially-completed request
|
|
|
|
// is described as "in flight".
|
|
|
|
//
|
|
|
|
// Each request has a "generation", a unique number that identifies it. This
|
|
|
|
// is used to ensure that each reports from a child process corresponds to
|
|
|
|
// the appropriate request from the parent process. (It's easier to
|
|
|
|
// implement a generation system than to implement a child report request
|
|
|
|
// cancellation mechanism.)
|
|
|
|
//
|
|
|
|
// Failures are mostly ignored, because it's (a) typically the most sensible
|
|
|
|
// thing to do, and (b) often hard to do anything else. The following are
|
|
|
|
// the failure cases of note.
|
|
|
|
//
|
|
|
|
// - If a request is made while the previous request is in flight, the new
|
|
|
|
// request is ignored, as per getReports()'s specification. No error is
|
|
|
|
// reported, because the previous request will complete soon enough.
|
|
|
|
//
|
|
|
|
// - If one or more child processes fail to respond within the time limit,
|
|
|
|
// things will proceed as if they don't exist. No error is reported,
|
|
|
|
// because partial information is better than nothing.
|
|
|
|
//
|
|
|
|
// - If a child process reports after the time-out occurs, it is ignored.
|
|
|
|
// (Generation checking will ensure it is ignored even if a subsequent
|
|
|
|
// request is in flight; this is the main use of generations.) No error
|
|
|
|
// is reported, because there's nothing sensible to be done about it at
|
|
|
|
// this late stage.
|
|
|
|
//
|
2015-04-27 19:45:00 +00:00
|
|
|
// - If the time-out occurs after a child process has sent some reports but
|
|
|
|
// before it has signaled completion (see bug 1151597), then what it
|
|
|
|
// successfully sent will be included, with no explicit indication that it
|
|
|
|
// is incomplete.
|
|
|
|
//
|
2013-10-23 05:26:24 +00:00
|
|
|
// Now, what what happens if a child process is created/destroyed in the
|
2015-10-14 23:52:57 +00:00
|
|
|
// middle of a request? Well, PendingProcessesState is initialized with an array
|
2015-05-06 18:51:00 +00:00
|
|
|
// of child process actors as of when the report started. So...
|
2013-10-23 05:26:24 +00:00
|
|
|
//
|
2015-05-06 18:51:00 +00:00
|
|
|
// - If a process is created after reporting starts, it won't be sent a
|
|
|
|
// request for reports. So the reported data will reflect how things were
|
|
|
|
// when the request began.
|
2013-10-23 05:26:24 +00:00
|
|
|
//
|
2015-04-27 19:45:00 +00:00
|
|
|
// - If a process is destroyed before it starts reporting back, the reported
|
|
|
|
// data will reflect how things are when the request ends.
|
|
|
|
//
|
|
|
|
// - If a process is destroyed after it starts reporting back but before it
|
|
|
|
// finishes, the reported data will contain a partial report for it.
|
2013-10-23 05:26:24 +00:00
|
|
|
//
|
|
|
|
// - If a process is destroyed after reporting back, but before all other
|
|
|
|
// child processes have reported back, it will be included in the reported
|
|
|
|
// data. So the reported data will reflect how things were when the
|
|
|
|
// request began.
|
|
|
|
//
|
2015-04-27 19:45:00 +00:00
|
|
|
// The inconsistencies between these cases are unfortunate but difficult to
|
|
|
|
// avoid. It's enough of an edge case to not be worth doing more.
|
2013-10-23 05:26:24 +00:00
|
|
|
//
|
2015-04-27 19:45:00 +00:00
|
|
|
void HandleChildReport(uint32_t aGeneration,
|
|
|
|
const mozilla::dom::MemoryReport& aChildReport);
|
2015-05-06 18:51:00 +00:00
|
|
|
void EndProcessReport(uint32_t aGeneration, bool aSuccess);
|
2013-10-23 05:26:24 +00:00
|
|
|
|
2013-09-19 22:52:28 +00:00
|
|
|
// Functions that (a) implement distinguished amounts, and (b) are outside of
|
|
|
|
// this module.
|
2014-01-03 03:19:32 +00:00
|
|
|
struct AmountFns
|
|
|
|
{
|
2013-09-19 22:52:30 +00:00
|
|
|
mozilla::InfallibleAmountFn mJSMainRuntimeGCHeap;
|
|
|
|
mozilla::InfallibleAmountFn mJSMainRuntimeTemporaryPeak;
|
2013-09-19 22:52:28 +00:00
|
|
|
mozilla::InfallibleAmountFn mJSMainRuntimeCompartmentsSystem;
|
|
|
|
mozilla::InfallibleAmountFn mJSMainRuntimeCompartmentsUser;
|
2013-09-19 22:52:30 +00:00
|
|
|
|
|
|
|
mozilla::InfallibleAmountFn mImagesContentUsedUncompressed;
|
|
|
|
|
|
|
|
mozilla::InfallibleAmountFn mStorageSQLite;
|
|
|
|
|
|
|
|
mozilla::InfallibleAmountFn mLowMemoryEventsVirtual;
|
|
|
|
mozilla::InfallibleAmountFn mLowMemoryEventsPhysical;
|
|
|
|
|
|
|
|
mozilla::InfallibleAmountFn mGhostWindows;
|
2013-10-23 05:26:24 +00:00
|
|
|
|
2014-05-13 17:41:38 +00:00
|
|
|
AmountFns()
|
|
|
|
{
|
|
|
|
mozilla::PodZero(this);
|
|
|
|
}
|
2013-09-19 22:52:28 +00:00
|
|
|
};
|
|
|
|
AmountFns mAmountFns;
|
|
|
|
|
2014-04-07 14:46:20 +00:00
|
|
|
// Convenience function to get RSS easily from other code. This is useful
|
|
|
|
// when debugging transient memory spikes with printf instrumentation.
|
|
|
|
static int64_t ResidentFast();
|
|
|
|
|
2015-03-19 22:16:37 +00:00
|
|
|
// Convenience function to get peak RSS easily from other code.
|
|
|
|
static int64_t ResidentPeak();
|
|
|
|
|
2014-06-05 15:31:09 +00:00
|
|
|
// Convenience function to get USS easily from other code. This is useful
|
|
|
|
// when debugging unshared memory pages for forked processes.
|
|
|
|
static int64_t ResidentUnique();
|
|
|
|
|
2013-10-22 21:53:26 +00:00
|
|
|
// Functions that measure per-tab memory consumption.
|
2014-01-03 03:19:32 +00:00
|
|
|
struct SizeOfTabFns
|
|
|
|
{
|
2013-10-22 21:53:26 +00:00
|
|
|
mozilla::JSSizeOfTabFn mJS;
|
|
|
|
mozilla::NonJSSizeOfTabFn mNonJS;
|
2013-10-23 05:26:24 +00:00
|
|
|
|
2014-01-03 03:19:32 +00:00
|
|
|
SizeOfTabFns()
|
|
|
|
{
|
|
|
|
mozilla::PodZero(this);
|
|
|
|
}
|
2013-10-22 21:53:26 +00:00
|
|
|
};
|
|
|
|
SizeOfTabFns mSizeOfTabFns;
|
|
|
|
|
2008-03-06 01:28:25 +00:00
|
|
|
private:
|
2013-11-07 05:35:30 +00:00
|
|
|
nsresult RegisterReporterHelper(nsIMemoryReporter* aReporter,
|
2015-10-14 23:52:58 +00:00
|
|
|
bool aForce, bool aStrongRef, bool aIsAsync);
|
2014-03-05 02:27:13 +00:00
|
|
|
nsresult StartGettingReports();
|
2015-04-27 19:45:00 +00:00
|
|
|
nsresult FinishReporting();
|
2013-10-23 05:26:24 +00:00
|
|
|
|
2015-10-14 23:52:59 +00:00
|
|
|
void DispatchReporter(nsIMemoryReporter* aReporter, bool aIsAsync,
|
|
|
|
nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aHandleReportData,
|
|
|
|
bool aAnonymize);
|
|
|
|
|
2013-10-23 05:26:24 +00:00
|
|
|
static void TimeoutCallback(nsITimer* aTimer, void* aData);
|
2014-03-05 02:27:13 +00:00
|
|
|
// Note: this timeout needs to be long enough to allow for the
|
|
|
|
// possibility of DMD reports and/or running on a low-end phone.
|
|
|
|
static const uint32_t kTimeoutLengthMS = 50000;
|
2013-04-02 04:39:44 +00:00
|
|
|
|
2014-01-03 03:19:32 +00:00
|
|
|
mozilla::Mutex mMutex;
|
2013-04-02 04:39:44 +00:00
|
|
|
bool mIsRegistrationBlocked;
|
2013-10-23 05:26:24 +00:00
|
|
|
|
2013-11-07 05:35:30 +00:00
|
|
|
StrongReportersTable* mStrongReporters;
|
|
|
|
WeakReportersTable* mWeakReporters;
|
|
|
|
|
|
|
|
// These two are only used for testing purposes.
|
|
|
|
StrongReportersTable* mSavedStrongReporters;
|
|
|
|
WeakReportersTable* mSavedWeakReporters;
|
|
|
|
|
2013-10-23 05:26:24 +00:00
|
|
|
uint32_t mNextGeneration;
|
|
|
|
|
2015-10-14 23:52:59 +00:00
|
|
|
// Used to keep track of state of which processes are currently running and
|
|
|
|
// waiting to run memory reports. Holds references to parameters needed when
|
|
|
|
// requesting a memory report and finishing reporting.
|
2015-10-14 23:52:57 +00:00
|
|
|
struct PendingProcessesState
|
2014-01-03 03:19:32 +00:00
|
|
|
{
|
2013-10-23 05:26:24 +00:00
|
|
|
uint32_t mGeneration;
|
2014-05-21 06:06:54 +00:00
|
|
|
bool mAnonymize;
|
2015-04-27 19:46:00 +00:00
|
|
|
bool mMinimize;
|
2013-10-23 05:26:24 +00:00
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
2015-10-18 05:24:48 +00:00
|
|
|
nsTArray<RefPtr<mozilla::dom::ContentParent>> mChildrenPending;
|
2015-05-06 18:51:00 +00:00
|
|
|
uint32_t mNumProcessesRunning;
|
|
|
|
uint32_t mNumProcessesCompleted;
|
|
|
|
uint32_t mConcurrencyLimit;
|
2013-10-23 05:26:24 +00:00
|
|
|
nsCOMPtr<nsIHandleReportCallback> mHandleReport;
|
|
|
|
nsCOMPtr<nsISupports> mHandleReportData;
|
|
|
|
nsCOMPtr<nsIFinishReportingCallback> mFinishReporting;
|
|
|
|
nsCOMPtr<nsISupports> mFinishReportingData;
|
2014-03-05 02:27:13 +00:00
|
|
|
nsString mDMDDumpIdent;
|
2013-10-23 05:26:24 +00:00
|
|
|
|
2015-10-14 23:52:57 +00:00
|
|
|
PendingProcessesState(uint32_t aGeneration, bool aAnonymize, bool aMinimize,
|
|
|
|
uint32_t aConcurrencyLimit,
|
|
|
|
nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aHandleReportData,
|
|
|
|
nsIFinishReportingCallback* aFinishReporting,
|
|
|
|
nsISupports* aFinishReportingData,
|
|
|
|
const nsAString& aDMDDumpIdent);
|
2015-10-06 18:23:36 +00:00
|
|
|
};
|
|
|
|
|
2015-10-14 23:52:59 +00:00
|
|
|
// Used to keep track of the state of the asynchronously run memory
|
|
|
|
// reporters. The callback and file handle used when all memory reporters
|
|
|
|
// have finished are also stored here.
|
|
|
|
struct PendingReportersState
|
|
|
|
{
|
|
|
|
// Number of memory reporters currently running.
|
|
|
|
uint32_t mReportsPending;
|
|
|
|
|
|
|
|
// Callback for when all memory reporters have completed.
|
|
|
|
nsCOMPtr<nsIFinishReportingCallback> mFinishReporting;
|
|
|
|
nsCOMPtr<nsISupports> mFinishReportingData;
|
|
|
|
|
|
|
|
// File handle to write a DMD report to if requested.
|
|
|
|
FILE* mDMDFile;
|
|
|
|
|
|
|
|
PendingReportersState(nsIFinishReportingCallback* aFinishReporting,
|
|
|
|
nsISupports* aFinishReportingData,
|
|
|
|
FILE* aDMDFile)
|
|
|
|
: mReportsPending(0)
|
|
|
|
, mFinishReporting(aFinishReporting)
|
|
|
|
, mFinishReportingData(aFinishReportingData)
|
|
|
|
, mDMDFile(aDMDFile)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-10-23 05:26:24 +00:00
|
|
|
// When this is non-null, a request is in flight. Note: We use manual
|
|
|
|
// new/delete for this because its lifetime doesn't match block scope or
|
|
|
|
// anything like that.
|
2015-10-14 23:52:57 +00:00
|
|
|
PendingProcessesState* mPendingProcessesState;
|
2015-10-06 18:23:36 +00:00
|
|
|
|
2015-10-14 23:52:59 +00:00
|
|
|
// This is reinitialized each time a call to GetReports is initiated.
|
|
|
|
PendingReportersState* mPendingReportersState;
|
|
|
|
|
2015-10-14 23:52:57 +00:00
|
|
|
PendingProcessesState* GetStateForGeneration(uint32_t aGeneration);
|
2015-05-06 18:51:00 +00:00
|
|
|
static bool StartChildReport(mozilla::dom::ContentParent* aChild,
|
2015-10-14 23:52:57 +00:00
|
|
|
const PendingProcessesState* aState);
|
2008-03-06 01:28:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define NS_MEMORY_REPORTER_MANAGER_CID \
|
|
|
|
{ 0xfb97e4f5, 0x32dd, 0x497a, \
|
|
|
|
{ 0xba, 0xa2, 0x7d, 0x1e, 0x55, 0x7, 0x99, 0x10 } }
|
2014-01-27 19:54:37 +00:00
|
|
|
|
|
|
|
#endif // nsMemoryReporterManager_h__
|