2013-01-14 16:28:36 -08: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 04:12:37 -07: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 11:54:37 -08:00
|
|
|
#ifndef nsMemoryReporterManager_h__
|
|
|
|
#define nsMemoryReporterManager_h__
|
|
|
|
|
2008-03-05 17:28:25 -08:00
|
|
|
#include "nsIMemoryReporter.h"
|
2014-04-07 07:46:20 -07:00
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
2010-12-15 09:47:16 -08:00
|
|
|
#include "mozilla/Mutex.h"
|
2013-02-01 07:12:52 -08:00
|
|
|
#include "nsTHashtable.h"
|
|
|
|
#include "nsHashKeys.h"
|
2010-12-15 09:47:16 -08:00
|
|
|
|
2013-10-22 22:26:24 -07:00
|
|
|
class nsITimer;
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
class MemoryReport;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-05 17:28:25 -08:00
|
|
|
class nsMemoryReporterManager : public nsIMemoryReporterManager
|
|
|
|
{
|
|
|
|
public:
|
2013-07-18 19:31:26 -07:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2013-01-14 16:28:36 -08:00
|
|
|
NS_DECL_NSIMEMORYREPORTERMANAGER
|
2008-03-05 17:28:25 -08:00
|
|
|
|
2013-01-14 16:28:36 -08:00
|
|
|
nsMemoryReporterManager();
|
|
|
|
virtual ~nsMemoryReporterManager();
|
2010-12-15 09:47:16 -08:00
|
|
|
|
2013-10-22 22:26:24 -07: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());
|
|
|
|
}
|
|
|
|
|
2014-05-13 10:41:38 -07:00
|
|
|
typedef nsTHashtable<nsRefPtrHashKey<nsIMemoryReporter>> StrongReportersTable;
|
|
|
|
typedef nsTHashtable<nsPtrHashKey<nsIMemoryReporter>> WeakReportersTable;
|
2013-11-06 21:35:30 -08:00
|
|
|
|
2013-10-22 22:26:24 -07:00
|
|
|
void IncrementNumChildProcesses();
|
|
|
|
void DecrementNumChildProcesses();
|
|
|
|
|
|
|
|
// Inter-process memory reporting proceeds as follows.
|
|
|
|
//
|
|
|
|
// - GetReports() (declared within NS_DECL_NSIMEMORYREPORTERMANAGER)
|
|
|
|
// synchronously gets memory reports for the current process, tells all
|
|
|
|
// child processes to get memory reports, and sets up some state
|
|
|
|
// (mGetReportsState) for when the child processes report back, including a
|
|
|
|
// timer. Control then returns to the main event loop.
|
|
|
|
//
|
|
|
|
// - HandleChildReports() is called (asynchronously) once per child process
|
|
|
|
// that reports back. If all child processes report back before time-out,
|
|
|
|
// the timer is cancelled. (The number of child processes is part of the
|
|
|
|
// saved request state.)
|
|
|
|
//
|
|
|
|
// - 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
|
|
|
|
// from HandleChildReports() (if all child processes have reported back) or
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// Now, what what happens if a child process is created/destroyed in the
|
|
|
|
// middle of a request? Well, GetReportsState contains a copy of
|
|
|
|
// mNumChildProcesses which it uses to determine finished-ness. So...
|
|
|
|
//
|
|
|
|
// - If a process is created, it won't have received the request for reports,
|
|
|
|
// and the GetReportsState's mNumChildProcesses won't account for it. So
|
|
|
|
// the reported data will reflect how things were when the request began.
|
|
|
|
//
|
|
|
|
// - If a process is destroyed before reporting back, we'll just hit the
|
|
|
|
// time-out, because we'll have received reports (barring other errors)
|
|
|
|
// from N-1 child process. So the reported data will reflect how things
|
|
|
|
// are when the request ends.
|
|
|
|
//
|
|
|
|
// - 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.
|
|
|
|
//
|
|
|
|
// The inconsistencies between these three cases are unfortunate but
|
|
|
|
// difficult to avoid. It's enough of an edge case to not be worth doing
|
|
|
|
// more.
|
|
|
|
//
|
|
|
|
void HandleChildReports(
|
2014-05-13 10:41:38 -07:00
|
|
|
const uint32_t& aGeneration,
|
2013-10-22 22:26:24 -07:00
|
|
|
const InfallibleTArray<mozilla::dom::MemoryReport>& aChildReports);
|
2014-03-04 18:27:13 -08:00
|
|
|
nsresult FinishReporting();
|
2013-10-22 22:26:24 -07:00
|
|
|
|
2013-09-19 15:52:28 -07:00
|
|
|
// Functions that (a) implement distinguished amounts, and (b) are outside of
|
|
|
|
// this module.
|
2014-01-02 19:19:32 -08:00
|
|
|
struct AmountFns
|
|
|
|
{
|
2013-09-19 15:52:30 -07:00
|
|
|
mozilla::InfallibleAmountFn mJSMainRuntimeGCHeap;
|
|
|
|
mozilla::InfallibleAmountFn mJSMainRuntimeTemporaryPeak;
|
2013-09-19 15:52:28 -07:00
|
|
|
mozilla::InfallibleAmountFn mJSMainRuntimeCompartmentsSystem;
|
|
|
|
mozilla::InfallibleAmountFn mJSMainRuntimeCompartmentsUser;
|
2013-09-19 15:52:30 -07:00
|
|
|
|
|
|
|
mozilla::InfallibleAmountFn mImagesContentUsedUncompressed;
|
|
|
|
|
|
|
|
mozilla::InfallibleAmountFn mStorageSQLite;
|
|
|
|
|
|
|
|
mozilla::InfallibleAmountFn mLowMemoryEventsVirtual;
|
|
|
|
mozilla::InfallibleAmountFn mLowMemoryEventsPhysical;
|
|
|
|
|
|
|
|
mozilla::InfallibleAmountFn mGhostWindows;
|
2013-10-22 22:26:24 -07:00
|
|
|
|
2014-05-13 10:41:38 -07:00
|
|
|
AmountFns()
|
|
|
|
{
|
|
|
|
mozilla::PodZero(this);
|
|
|
|
}
|
2013-09-19 15:52:28 -07:00
|
|
|
};
|
|
|
|
AmountFns mAmountFns;
|
|
|
|
|
2014-04-07 07:46:20 -07: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();
|
|
|
|
|
2014-06-05 08:31:09 -07: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 14:53:26 -07:00
|
|
|
// Functions that measure per-tab memory consumption.
|
2014-01-02 19:19:32 -08:00
|
|
|
struct SizeOfTabFns
|
|
|
|
{
|
2013-10-22 14:53:26 -07:00
|
|
|
mozilla::JSSizeOfTabFn mJS;
|
|
|
|
mozilla::NonJSSizeOfTabFn mNonJS;
|
2013-10-22 22:26:24 -07:00
|
|
|
|
2014-01-02 19:19:32 -08:00
|
|
|
SizeOfTabFns()
|
|
|
|
{
|
|
|
|
mozilla::PodZero(this);
|
|
|
|
}
|
2013-10-22 14:53:26 -07:00
|
|
|
};
|
|
|
|
SizeOfTabFns mSizeOfTabFns;
|
|
|
|
|
2008-03-05 17:28:25 -08:00
|
|
|
private:
|
2013-11-06 21:35:30 -08:00
|
|
|
nsresult RegisterReporterHelper(nsIMemoryReporter* aReporter,
|
|
|
|
bool aForce, bool aStrongRef);
|
2014-03-04 18:27:13 -08:00
|
|
|
nsresult StartGettingReports();
|
2013-10-22 22:26:24 -07:00
|
|
|
|
|
|
|
static void TimeoutCallback(nsITimer* aTimer, void* aData);
|
2014-03-04 18:27:13 -08: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-01 21:39:44 -07:00
|
|
|
|
2014-01-02 19:19:32 -08:00
|
|
|
mozilla::Mutex mMutex;
|
2013-04-01 21:39:44 -07:00
|
|
|
bool mIsRegistrationBlocked;
|
2013-10-22 22:26:24 -07:00
|
|
|
|
2013-11-06 21:35:30 -08:00
|
|
|
StrongReportersTable* mStrongReporters;
|
|
|
|
WeakReportersTable* mWeakReporters;
|
|
|
|
|
|
|
|
// These two are only used for testing purposes.
|
|
|
|
StrongReportersTable* mSavedStrongReporters;
|
|
|
|
WeakReportersTable* mSavedWeakReporters;
|
|
|
|
|
2013-10-22 22:26:24 -07:00
|
|
|
uint32_t mNumChildProcesses;
|
|
|
|
uint32_t mNextGeneration;
|
|
|
|
|
2014-01-02 19:19:32 -08:00
|
|
|
struct GetReportsState
|
|
|
|
{
|
2013-10-22 22:26:24 -07:00
|
|
|
uint32_t mGeneration;
|
2014-05-20 23:06:54 -07:00
|
|
|
bool mAnonymize;
|
2013-10-22 22:26:24 -07:00
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
uint32_t mNumChildProcesses;
|
|
|
|
uint32_t mNumChildProcessesCompleted;
|
2014-04-07 04:33:00 -07:00
|
|
|
bool mParentDone;
|
2013-10-22 22:26:24 -07:00
|
|
|
nsCOMPtr<nsIHandleReportCallback> mHandleReport;
|
|
|
|
nsCOMPtr<nsISupports> mHandleReportData;
|
|
|
|
nsCOMPtr<nsIFinishReportingCallback> mFinishReporting;
|
|
|
|
nsCOMPtr<nsISupports> mFinishReportingData;
|
2014-03-04 18:27:13 -08:00
|
|
|
nsString mDMDDumpIdent;
|
2013-10-22 22:26:24 -07:00
|
|
|
|
2014-05-20 23:06:54 -07:00
|
|
|
GetReportsState(uint32_t aGeneration, bool aAnonymize, nsITimer* aTimer,
|
2013-10-22 22:26:24 -07:00
|
|
|
uint32_t aNumChildProcesses,
|
|
|
|
nsIHandleReportCallback* aHandleReport,
|
|
|
|
nsISupports* aHandleReportData,
|
|
|
|
nsIFinishReportingCallback* aFinishReporting,
|
2014-03-04 18:27:13 -08:00
|
|
|
nsISupports* aFinishReportingData,
|
2014-05-13 10:41:38 -07:00
|
|
|
const nsAString& aDMDDumpIdent)
|
2014-01-02 19:19:32 -08:00
|
|
|
: mGeneration(aGeneration)
|
2014-05-20 23:06:54 -07:00
|
|
|
, mAnonymize(aAnonymize)
|
2014-01-02 19:19:32 -08:00
|
|
|
, mTimer(aTimer)
|
|
|
|
, mNumChildProcesses(aNumChildProcesses)
|
|
|
|
, mNumChildProcessesCompleted(0)
|
2014-04-07 04:33:00 -07:00
|
|
|
, mParentDone(false)
|
2014-01-02 19:19:32 -08:00
|
|
|
, mHandleReport(aHandleReport)
|
|
|
|
, mHandleReportData(aHandleReportData)
|
|
|
|
, mFinishReporting(aFinishReporting)
|
|
|
|
, mFinishReportingData(aFinishReportingData)
|
2014-03-04 18:27:13 -08:00
|
|
|
, mDMDDumpIdent(aDMDDumpIdent)
|
2014-01-02 19:19:32 -08:00
|
|
|
{
|
|
|
|
}
|
2013-10-22 22:26:24 -07: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.
|
|
|
|
GetReportsState* mGetReportsState;
|
2008-03-05 17:28:25 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define NS_MEMORY_REPORTER_MANAGER_CID \
|
|
|
|
{ 0xfb97e4f5, 0x32dd, 0x497a, \
|
|
|
|
{ 0xba, 0xa2, 0x7d, 0x1e, 0x55, 0x7, 0x99, 0x10 } }
|
2014-01-27 11:54:37 -08:00
|
|
|
|
|
|
|
#endif // nsMemoryReporterManager_h__
|