dom/base/nsWindowMemoryReporter.h

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:689fab4f5734
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/. */
6
7 #ifndef nsWindowMemoryReporter_h__
8 #define nsWindowMemoryReporter_h__
9
10 #include "nsIMemoryReporter.h"
11 #include "nsIObserver.h"
12 #include "nsITimer.h"
13 #include "nsDataHashtable.h"
14 #include "nsWeakReference.h"
15 #include "nsAutoPtr.h"
16 #include "mozilla/Attributes.h"
17 #include "mozilla/Assertions.h"
18 #include "mozilla/MemoryReporting.h"
19 #include "mozilla/PodOperations.h"
20 #include "mozilla/TimeStamp.h"
21 #include "nsArenaMemoryStats.h"
22
23 class nsWindowSizes {
24 #define FOR_EACH_SIZE(macro) \
25 macro(DOM, mDOMElementNodesSize) \
26 macro(DOM, mDOMTextNodesSize) \
27 macro(DOM, mDOMCDATANodesSize) \
28 macro(DOM, mDOMCommentNodesSize) \
29 macro(DOM, mDOMEventTargetsSize) \
30 macro(DOM, mDOMOtherSize) \
31 macro(Style, mStyleSheetsSize) \
32 macro(Other, mLayoutPresShellSize) \
33 macro(Style, mLayoutStyleSetsSize) \
34 macro(Other, mLayoutTextRunsSize) \
35 macro(Other, mLayoutPresContextSize) \
36 macro(Other, mPropertyTablesSize) \
37
38 public:
39 nsWindowSizes(mozilla::MallocSizeOf aMallocSizeOf)
40 :
41 #define ZERO_SIZE(kind, mSize) mSize(0),
42 FOR_EACH_SIZE(ZERO_SIZE)
43 #undef ZERO_SIZE
44 mDOMEventTargetsCount(0),
45 mDOMEventListenersCount(0),
46 mArenaStats(),
47 mMallocSizeOf(aMallocSizeOf)
48 {}
49
50 void addToTabSizes(nsTabSizes *sizes) const {
51 #define ADD_TO_TAB_SIZES(kind, mSize) sizes->add(nsTabSizes::kind, mSize);
52 FOR_EACH_SIZE(ADD_TO_TAB_SIZES)
53 #undef ADD_TO_TAB_SIZES
54 mArenaStats.addToTabSizes(sizes);
55 }
56
57 #define DECL_SIZE(kind, mSize) size_t mSize;
58 FOR_EACH_SIZE(DECL_SIZE);
59 #undef DECL_SIZE
60
61 uint32_t mDOMEventTargetsCount;
62 uint32_t mDOMEventListenersCount;
63
64 nsArenaMemoryStats mArenaStats;
65 mozilla::MallocSizeOf mMallocSizeOf;
66
67 #undef FOR_EACH_SIZE
68 };
69
70 /**
71 * nsWindowMemoryReporter is responsible for the 'explicit/window-objects'
72 * memory reporter.
73 *
74 * We classify DOM window objects into one of three categories:
75 *
76 * - "active" windows, which are displayed in a tab (as the top-level window
77 * or an iframe),
78 *
79 * - "cached" windows, which are in the fastback cache (aka the bfcache), and
80 *
81 * - "detached" windows, which have a null docshell. A window becomes
82 * detached when its <iframe> or tab containing the window is destroyed --
83 * i.e., when the window is no longer active or cached.
84 *
85 * Additionally, we classify a subset of detached windows as "ghost" windows.
86 * Although ghost windows can happen legitimately (a page can hold a reference
87 * to a cross-domain window and then close its container), the presence of
88 * ghost windows is often indicative of a memory leak.
89 *
90 * A window is a ghost if it meets the following three criteria:
91 *
92 * 1) The window is detached.
93 *
94 * 2) There exist no non-detached windows with the same base domain as
95 * the window's principal. (For example, the base domain of
96 * "wiki.mozilla.co.uk" is "mozilla.co.uk".) This criterion makes us less
97 * likely to flag a legitimately held-alive detached window as a ghost.
98 *
99 * 3) The window has met criteria (1) and (2) above for at least
100 * memory.ghost_window_timeout_seconds. This criterion is in place so we
101 * don't immediately declare a window a ghost before the GC/CC has had a
102 * chance to run.
103 *
104 * nsWindowMemoryReporter observes window detachment and uses mDetachedWindows
105 * to remember when a window first met criteria (1) and (2). When we generate
106 * a memory report, we use this accounting to determine which windows are
107 * ghosts.
108 *
109 *
110 * We use the following memory reporter path for active and cached windows:
111 *
112 * explicit/window-objects/top(<top-outer-uri>, id=<top-outer-id>)/<category>/window(<window-uri>)/...
113 *
114 * For detached and ghost windows, we use
115 *
116 * explicit/window-objects/top(none)/<category>/window(<window-uri>)/...
117 *
118 * Where
119 *
120 * - <category> is "active", "cached", "detached", or "ghost", as described
121 * above.
122 *
123 * - <top-outer-id> is the window id of the top outer window (i.e. the tab, or
124 * the top level chrome window). Exposing this ensures that each tab gets
125 * its own sub-tree, even if multiple tabs are showing the same URI.
126 *
127 * - <top-uri> is the URI of the top window. Excepting special windows (such
128 * as browser.xul or hiddenWindow.html) it's what the address bar shows for
129 * the tab.
130 *
131 */
132 class nsWindowMemoryReporter MOZ_FINAL : public nsIMemoryReporter,
133 public nsIObserver,
134 public nsSupportsWeakReference
135 {
136 public:
137 NS_DECL_ISUPPORTS
138 NS_DECL_NSIMEMORYREPORTER
139 NS_DECL_NSIOBSERVER
140
141 static void Init();
142
143 ~nsWindowMemoryReporter();
144
145 #ifdef DEBUG
146 /**
147 * Unlink all known ghost windows, to enable investigating what caused them
148 * to become ghost windows in the first place.
149 */
150 static void UnlinkGhostWindows();
151 #endif
152
153 private:
154 /**
155 * nsGhostWindowReporter generates the "ghost-windows" report, which counts
156 * the number of ghost windows present.
157 */
158 class GhostWindowsReporter MOZ_FINAL : public nsIMemoryReporter
159 {
160 public:
161 NS_DECL_ISUPPORTS
162
163 static int64_t DistinguishedAmount();
164
165 NS_IMETHOD
166 CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData)
167 {
168 return MOZ_COLLECT_REPORT(
169 "ghost-windows", KIND_OTHER, UNITS_COUNT, DistinguishedAmount(),
170 "The number of ghost windows present (the number of nodes underneath "
171 "explicit/window-objects/top(none)/ghost, modulo race conditions). A ghost "
172 "window is not shown in any tab, does not share a domain with any non-detached "
173 "windows, and has met these criteria for at least "
174 "memory.ghost_window_timeout_seconds, or has survived a round of "
175 "about:memory's minimize memory usage button.\n\n"
176 "Ghost windows can happen legitimately, but they are often indicative of "
177 "leaks in the browser or add-ons.");
178 }
179 };
180
181 // Protect ctor, use Init() instead.
182 nsWindowMemoryReporter();
183
184 /**
185 * Get the number of seconds for which a window must satisfy ghost criteria
186 * (1) and (2) before we deem that it satisfies criterion (3).
187 */
188 uint32_t GetGhostTimeout();
189
190 void ObserveDOMWindowDetached(nsISupports* aWindow);
191 void ObserveAfterMinimizeMemoryUsage();
192
193 /**
194 * Iterate over all weak window pointers in mDetachedWindows and update our
195 * accounting of which windows meet ghost criterion (2).
196 *
197 * This method also cleans up mDetachedWindows, removing entries for windows
198 * which have been destroyed or are no longer detached.
199 *
200 * If aOutGhostIDs is non-null, we populate it with the Window IDs of the
201 * ghost windows.
202 *
203 * This is called asynchronously after we observe a DOM window being detached
204 * from its docshell, and also right before we generate a memory report.
205 */
206 void CheckForGhostWindows(nsTHashtable<nsUint64HashKey> *aOutGhostIDs = nullptr);
207
208 /**
209 * Eventually do a check for ghost windows, if we haven't done one recently
210 * and we aren't already planning to do one soon.
211 */
212 void AsyncCheckForGhostWindows();
213
214 /**
215 * Kill the check timer, if it exists.
216 */
217 void KillCheckTimer();
218
219 static void CheckTimerFired(nsITimer* aTimer, void* aClosure);
220
221 /**
222 * Maps a weak reference to a detached window (nsIWeakReference) to the time
223 * when we observed that the window met ghost criterion (2) above.
224 *
225 * If the window has not yet met criterion (2) it maps to the null timestamp.
226 *
227 * (Although windows are not added to this table until they're detached, it's
228 * possible for a detached window to become non-detached, and we won't
229 * remove it from the table until CheckForGhostWindows runs.)
230 */
231 nsDataHashtable<nsISupportsHashKey, mozilla::TimeStamp> mDetachedWindows;
232
233 /**
234 * Track the last time we ran CheckForGhostWindows(), to avoid running it
235 * too often after a DOM window is detached.
236 */
237 mozilla::TimeStamp mLastCheckForGhostWindows;
238
239 nsCOMPtr<nsITimer> mCheckTimer;
240
241 bool mCycleCollectorIsRunning;
242
243 bool mCheckTimerWaitingForCCEnd;
244 };
245
246 #endif // nsWindowMemoryReporter_h__
247

mercurial