Bumping manifests a=b2g-bump
[gecko.git] / xpcom / base / nsMemoryReporterManager.h
blob6955defd0a94fa8d17ad49e6799a81223e938952
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 #ifndef nsMemoryReporterManager_h__
8 #define nsMemoryReporterManager_h__
10 #include "nsIMemoryReporter.h"
11 #include "nsITimer.h"
12 #include "nsServiceManagerUtils.h"
13 #include "mozilla/Mutex.h"
14 #include "nsTHashtable.h"
15 #include "nsHashKeys.h"
17 class nsITimer;
19 namespace mozilla {
20 namespace dom {
21 class MemoryReport;
25 class nsMemoryReporterManager MOZ_FINAL : public nsIMemoryReporterManager
27 virtual ~nsMemoryReporterManager();
29 public:
30 NS_DECL_THREADSAFE_ISUPPORTS
31 NS_DECL_NSIMEMORYREPORTERMANAGER
33 nsMemoryReporterManager();
35 // Gets the memory reporter manager service.
36 static nsMemoryReporterManager* GetOrCreate()
38 nsCOMPtr<nsIMemoryReporterManager> imgr =
39 do_GetService("@mozilla.org/memory-reporter-manager;1");
40 return static_cast<nsMemoryReporterManager*>(imgr.get());
43 typedef nsTHashtable<nsRefPtrHashKey<nsIMemoryReporter>> StrongReportersTable;
44 typedef nsTHashtable<nsPtrHashKey<nsIMemoryReporter>> WeakReportersTable;
46 void IncrementNumChildProcesses();
47 void DecrementNumChildProcesses();
49 // Inter-process memory reporting proceeds as follows.
51 // - GetReports() (declared within NS_DECL_NSIMEMORYREPORTERMANAGER)
52 // synchronously gets memory reports for the current process, tells all
53 // child processes to get memory reports, and sets up some state
54 // (mGetReportsState) for when the child processes report back, including a
55 // timer. Control then returns to the main event loop.
57 // - HandleChildReports() is called (asynchronously) once per child process
58 // that reports back. If all child processes report back before time-out,
59 // the timer is cancelled. (The number of child processes is part of the
60 // saved request state.)
62 // - TimeoutCallback() is called (asynchronously) if all the child processes
63 // don't respond within the time threshold.
65 // - FinishReporting() finishes things off. It is *always* called -- either
66 // from HandleChildReports() (if all child processes have reported back) or
67 // from TimeoutCallback() (if time-out occurs).
69 // All operations occur on the main thread.
71 // The above sequence of steps is a "request". A partially-completed request
72 // is described as "in flight".
74 // Each request has a "generation", a unique number that identifies it. This
75 // is used to ensure that each reports from a child process corresponds to
76 // the appropriate request from the parent process. (It's easier to
77 // implement a generation system than to implement a child report request
78 // cancellation mechanism.)
80 // Failures are mostly ignored, because it's (a) typically the most sensible
81 // thing to do, and (b) often hard to do anything else. The following are
82 // the failure cases of note.
84 // - If a request is made while the previous request is in flight, the new
85 // request is ignored, as per getReports()'s specification. No error is
86 // reported, because the previous request will complete soon enough.
88 // - If one or more child processes fail to respond within the time limit,
89 // things will proceed as if they don't exist. No error is reported,
90 // because partial information is better than nothing.
92 // - If a child process reports after the time-out occurs, it is ignored.
93 // (Generation checking will ensure it is ignored even if a subsequent
94 // request is in flight; this is the main use of generations.) No error
95 // is reported, because there's nothing sensible to be done about it at
96 // this late stage.
98 // Now, what what happens if a child process is created/destroyed in the
99 // middle of a request? Well, GetReportsState contains a copy of
100 // mNumChildProcesses which it uses to determine finished-ness. So...
102 // - If a process is created, it won't have received the request for reports,
103 // and the GetReportsState's mNumChildProcesses won't account for it. So
104 // the reported data will reflect how things were when the request began.
106 // - If a process is destroyed before reporting back, we'll just hit the
107 // time-out, because we'll have received reports (barring other errors)
108 // from N-1 child process. So the reported data will reflect how things
109 // are when the request ends.
111 // - If a process is destroyed after reporting back, but before all other
112 // child processes have reported back, it will be included in the reported
113 // data. So the reported data will reflect how things were when the
114 // request began.
116 // The inconsistencies between these three cases are unfortunate but
117 // difficult to avoid. It's enough of an edge case to not be worth doing
118 // more.
120 void HandleChildReports(
121 const uint32_t& aGeneration,
122 const InfallibleTArray<mozilla::dom::MemoryReport>& aChildReports);
123 nsresult FinishReporting();
125 // Functions that (a) implement distinguished amounts, and (b) are outside of
126 // this module.
127 struct AmountFns
129 mozilla::InfallibleAmountFn mJSMainRuntimeGCHeap;
130 mozilla::InfallibleAmountFn mJSMainRuntimeTemporaryPeak;
131 mozilla::InfallibleAmountFn mJSMainRuntimeCompartmentsSystem;
132 mozilla::InfallibleAmountFn mJSMainRuntimeCompartmentsUser;
134 mozilla::InfallibleAmountFn mImagesContentUsedUncompressed;
136 mozilla::InfallibleAmountFn mStorageSQLite;
138 mozilla::InfallibleAmountFn mLowMemoryEventsVirtual;
139 mozilla::InfallibleAmountFn mLowMemoryEventsPhysical;
141 mozilla::InfallibleAmountFn mGhostWindows;
143 AmountFns()
145 mozilla::PodZero(this);
148 AmountFns mAmountFns;
150 // Convenience function to get RSS easily from other code. This is useful
151 // when debugging transient memory spikes with printf instrumentation.
152 static int64_t ResidentFast();
154 // Convenience function to get USS easily from other code. This is useful
155 // when debugging unshared memory pages for forked processes.
156 static int64_t ResidentUnique();
158 // Functions that measure per-tab memory consumption.
159 struct SizeOfTabFns
161 mozilla::JSSizeOfTabFn mJS;
162 mozilla::NonJSSizeOfTabFn mNonJS;
164 SizeOfTabFns()
166 mozilla::PodZero(this);
169 SizeOfTabFns mSizeOfTabFns;
171 private:
172 nsresult RegisterReporterHelper(nsIMemoryReporter* aReporter,
173 bool aForce, bool aStrongRef);
174 nsresult StartGettingReports();
176 static void TimeoutCallback(nsITimer* aTimer, void* aData);
177 // Note: this timeout needs to be long enough to allow for the
178 // possibility of DMD reports and/or running on a low-end phone.
179 static const uint32_t kTimeoutLengthMS = 50000;
181 mozilla::Mutex mMutex;
182 bool mIsRegistrationBlocked;
184 StrongReportersTable* mStrongReporters;
185 WeakReportersTable* mWeakReporters;
187 // These two are only used for testing purposes.
188 StrongReportersTable* mSavedStrongReporters;
189 WeakReportersTable* mSavedWeakReporters;
191 uint32_t mNumChildProcesses;
192 uint32_t mNextGeneration;
194 struct GetReportsState
196 uint32_t mGeneration;
197 bool mAnonymize;
198 nsCOMPtr<nsITimer> mTimer;
199 uint32_t mNumChildProcesses;
200 uint32_t mNumChildProcessesCompleted;
201 bool mParentDone;
202 nsCOMPtr<nsIHandleReportCallback> mHandleReport;
203 nsCOMPtr<nsISupports> mHandleReportData;
204 nsCOMPtr<nsIFinishReportingCallback> mFinishReporting;
205 nsCOMPtr<nsISupports> mFinishReportingData;
206 nsString mDMDDumpIdent;
208 GetReportsState(uint32_t aGeneration, bool aAnonymize, nsITimer* aTimer,
209 uint32_t aNumChildProcesses,
210 nsIHandleReportCallback* aHandleReport,
211 nsISupports* aHandleReportData,
212 nsIFinishReportingCallback* aFinishReporting,
213 nsISupports* aFinishReportingData,
214 const nsAString& aDMDDumpIdent)
215 : mGeneration(aGeneration)
216 , mAnonymize(aAnonymize)
217 , mTimer(aTimer)
218 , mNumChildProcesses(aNumChildProcesses)
219 , mNumChildProcessesCompleted(0)
220 , mParentDone(false)
221 , mHandleReport(aHandleReport)
222 , mHandleReportData(aHandleReportData)
223 , mFinishReporting(aFinishReporting)
224 , mFinishReportingData(aFinishReportingData)
225 , mDMDDumpIdent(aDMDDumpIdent)
230 // When this is non-null, a request is in flight. Note: We use manual
231 // new/delete for this because its lifetime doesn't match block scope or
232 // anything like that.
233 GetReportsState* mGetReportsState;
236 #define NS_MEMORY_REPORTER_MANAGER_CID \
237 { 0xfb97e4f5, 0x32dd, 0x497a, \
238 { 0xba, 0xa2, 0x7d, 0x1e, 0x55, 0x7, 0x99, 0x10 } }
240 #endif // nsMemoryReporterManager_h__