Bumping manifests a=b2g-bump
[gecko.git] / dom / base / nsWindowMemoryReporter.h
blob83bcc44a58afdf2d7b17245be2ffe275732abcfc
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 nsWindowMemoryReporter_h__
8 #define nsWindowMemoryReporter_h__
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"
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) \
38 public:
39 explicit nsWindowSizes(mozilla::MallocSizeOf aMallocSizeOf)
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)
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);
57 size_t getTotalSize() const
59 size_t total = 0;
60 #define ADD_TO_TOTAL_SIZE(kind, mSize) total += mSize;
61 FOR_EACH_SIZE(ADD_TO_TOTAL_SIZE)
62 #undef ADD_TO_TOTAL_SIZE
63 total += mArenaStats.getTotalSize();
64 return total;
67 #define DECL_SIZE(kind, mSize) size_t mSize;
68 FOR_EACH_SIZE(DECL_SIZE);
69 #undef DECL_SIZE
71 uint32_t mDOMEventTargetsCount;
72 uint32_t mDOMEventListenersCount;
74 nsArenaMemoryStats mArenaStats;
75 mozilla::MallocSizeOf mMallocSizeOf;
77 #undef FOR_EACH_SIZE
80 /**
81 * nsWindowMemoryReporter is responsible for the 'explicit/window-objects'
82 * memory reporter.
84 * We classify DOM window objects into one of three categories:
86 * - "active" windows, which are displayed in a tab (as the top-level window
87 * or an iframe),
89 * - "cached" windows, which are in the fastback cache (aka the bfcache), and
91 * - "detached" windows, which have a null docshell. A window becomes
92 * detached when its <iframe> or tab containing the window is destroyed --
93 * i.e., when the window is no longer active or cached.
95 * Additionally, we classify a subset of detached windows as "ghost" windows.
96 * Although ghost windows can happen legitimately (a page can hold a reference
97 * to a cross-domain window and then close its container), the presence of
98 * ghost windows is often indicative of a memory leak.
100 * A window is a ghost if it meets the following three criteria:
102 * 1) The window is detached.
104 * 2) There exist no non-detached windows with the same base domain as
105 * the window's principal. (For example, the base domain of
106 * "wiki.mozilla.co.uk" is "mozilla.co.uk".) This criterion makes us less
107 * likely to flag a legitimately held-alive detached window as a ghost.
109 * 3) The window has met criteria (1) and (2) above for at least
110 * memory.ghost_window_timeout_seconds. This criterion is in place so we
111 * don't immediately declare a window a ghost before the GC/CC has had a
112 * chance to run.
114 * nsWindowMemoryReporter observes window detachment and uses mDetachedWindows
115 * to remember when a window first met criteria (1) and (2). When we generate
116 * a memory report, we use this accounting to determine which windows are
117 * ghosts.
120 * We use the following memory reporter path for active and cached windows:
122 * explicit/window-objects/top(<top-outer-uri>, id=<top-outer-id>)/<category>/window(<window-uri>)/...
124 * For detached and ghost windows, we use
126 * explicit/window-objects/top(none)/<category>/window(<window-uri>)/...
128 * Where
130 * - <category> is "active", "cached", "detached", or "ghost", as described
131 * above.
133 * - <top-outer-id> is the window id of the top outer window (i.e. the tab, or
134 * the top level chrome window). Exposing this ensures that each tab gets
135 * its own sub-tree, even if multiple tabs are showing the same URI.
137 * - <top-uri> is the URI of the top window. Excepting special windows (such
138 * as browser.xul or hiddenWindow.html) it's what the address bar shows for
139 * the tab.
142 class nsWindowMemoryReporter MOZ_FINAL : public nsIMemoryReporter,
143 public nsIObserver,
144 public nsSupportsWeakReference
146 public:
147 NS_DECL_ISUPPORTS
148 NS_DECL_NSIMEMORYREPORTER
149 NS_DECL_NSIOBSERVER
151 static void Init();
153 #ifdef DEBUG
155 * Unlink all known ghost windows, to enable investigating what caused them
156 * to become ghost windows in the first place.
158 static void UnlinkGhostWindows();
159 #endif
161 private:
162 ~nsWindowMemoryReporter();
165 * nsGhostWindowReporter generates the "ghost-windows" report, which counts
166 * the number of ghost windows present.
168 class GhostWindowsReporter MOZ_FINAL : public nsIMemoryReporter
170 ~GhostWindowsReporter() {}
171 public:
172 NS_DECL_ISUPPORTS
174 static int64_t DistinguishedAmount();
176 NS_IMETHOD
177 CollectReports(nsIHandleReportCallback* aHandleReport, nsISupports* aData,
178 bool aAnonymize)
180 return MOZ_COLLECT_REPORT(
181 "ghost-windows", KIND_OTHER, UNITS_COUNT, DistinguishedAmount(),
182 "The number of ghost windows present (the number of nodes underneath "
183 "explicit/window-objects/top(none)/ghost, modulo race conditions). A ghost "
184 "window is not shown in any tab, does not share a domain with any non-detached "
185 "windows, and has met these criteria for at least "
186 "memory.ghost_window_timeout_seconds, or has survived a round of "
187 "about:memory's minimize memory usage button.\n\n"
188 "Ghost windows can happen legitimately, but they are often indicative of "
189 "leaks in the browser or add-ons.");
193 // Protect ctor, use Init() instead.
194 nsWindowMemoryReporter();
197 * Get the number of seconds for which a window must satisfy ghost criteria
198 * (1) and (2) before we deem that it satisfies criterion (3).
200 uint32_t GetGhostTimeout();
202 void ObserveDOMWindowDetached(nsISupports* aWindow);
203 void ObserveAfterMinimizeMemoryUsage();
206 * Iterate over all weak window pointers in mDetachedWindows and update our
207 * accounting of which windows meet ghost criterion (2).
209 * This method also cleans up mDetachedWindows, removing entries for windows
210 * which have been destroyed or are no longer detached.
212 * If aOutGhostIDs is non-null, we populate it with the Window IDs of the
213 * ghost windows.
215 * This is called asynchronously after we observe a DOM window being detached
216 * from its docshell, and also right before we generate a memory report.
218 void CheckForGhostWindows(nsTHashtable<nsUint64HashKey> *aOutGhostIDs = nullptr);
221 * Eventually do a check for ghost windows, if we haven't done one recently
222 * and we aren't already planning to do one soon.
224 void AsyncCheckForGhostWindows();
227 * Kill the check timer, if it exists.
229 void KillCheckTimer();
231 static void CheckTimerFired(nsITimer* aTimer, void* aClosure);
234 * Maps a weak reference to a detached window (nsIWeakReference) to the time
235 * when we observed that the window met ghost criterion (2) above.
237 * If the window has not yet met criterion (2) it maps to the null timestamp.
239 * (Although windows are not added to this table until they're detached, it's
240 * possible for a detached window to become non-detached, and we won't
241 * remove it from the table until CheckForGhostWindows runs.)
243 nsDataHashtable<nsISupportsHashKey, mozilla::TimeStamp> mDetachedWindows;
246 * Track the last time we ran CheckForGhostWindows(), to avoid running it
247 * too often after a DOM window is detached.
249 mozilla::TimeStamp mLastCheckForGhostWindows;
251 nsCOMPtr<nsITimer> mCheckTimer;
253 bool mCycleCollectorIsRunning;
255 bool mCheckTimerWaitingForCCEnd;
258 #endif // nsWindowMemoryReporter_h__