Bug 1728955: part 3) Add logging to `nsBaseClipboard`. r=masayuki
[gecko.git] / dom / base / nsWindowMemoryReporter.h
blobed2b296f266a8f6b3b50c5f83b7b2b0f3e254961
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 "nsGlobalWindow.h"
11 #include "nsIMemoryReporter.h"
12 #include "nsIObserver.h"
13 #include "nsITimer.h"
14 #include "nsTHashMap.h"
15 #include "nsTHashSet.h"
16 #include "nsWeakReference.h"
17 #include "mozilla/Attributes.h"
18 #include "mozilla/Assertions.h"
19 #include "mozilla/MemoryReporting.h"
20 #include "mozilla/PodOperations.h"
21 #include "mozilla/TimeStamp.h"
23 /**
24 * nsWindowMemoryReporter is responsible for the 'explicit/window-objects'
25 * memory reporter.
27 * We classify DOM window objects into one of three categories:
29 * - "active" windows, which are displayed in a tab (as the top-level window
30 * or an iframe),
32 * - "cached" windows, which are in the fastback cache (aka the bfcache), and
34 * - "detached" windows, which have a null docshell. A window becomes
35 * detached when its <iframe> or tab containing the window is destroyed --
36 * i.e., when the window is no longer active or cached.
38 * Additionally, we classify a subset of detached windows as "ghost" windows.
39 * Although ghost windows can happen legitimately (a page can hold a reference
40 * to a cross-domain window and then close its container), the presence of
41 * ghost windows is often indicative of a memory leak.
43 * A window is a ghost if it meets the following three criteria:
45 * 1) The window is detached.
47 * 2) There exist no non-detached windows with the same base domain as
48 * the window's principal. (For example, the base domain of
49 * "wiki.mozilla.co.uk" is "mozilla.co.uk".) This criterion makes us less
50 * likely to flag a legitimately held-alive detached window as a ghost.
52 * 3) The window has met criteria (1) and (2) above for at least
53 * memory.ghost_window_timeout_seconds. This criterion is in place so we
54 * don't immediately declare a window a ghost before the GC/CC has had a
55 * chance to run.
57 * nsWindowMemoryReporter observes window detachment and uses mDetachedWindows
58 * to remember when a window first met criteria (1) and (2). When we generate
59 * a memory report, we use this accounting to determine which windows are
60 * ghosts.
63 * We use the following memory reporter path for active and cached windows:
65 * explicit/window-objects/top(<top-outer-uri>, id=<top-outer-id>)/
66 * <category>/window(<window-uri>)/...
68 * For detached and ghost windows, we use
70 * explicit/window-objects/top(none)/<category>/window(<window-uri>)/...
72 * Where
74 * - <category> is "active", "cached", "detached", or "ghost", as described
75 * above.
77 * - <top-outer-id> is the window id of the top outer window (i.e. the tab, or
78 * the top level chrome window). Exposing this ensures that each tab gets
79 * its own sub-tree, even if multiple tabs are showing the same URI.
81 * - <top-uri> is the URI of the top window. Excepting special windows (such
82 * as browser.xhtml or hiddenWindow.html) it's what the address bar shows for
83 * the tab.
86 class nsWindowMemoryReporter final : public nsIMemoryReporter,
87 public nsIObserver,
88 public nsSupportsWeakReference {
89 public:
90 NS_DECL_ISUPPORTS
91 NS_DECL_NSIMEMORYREPORTER
92 NS_DECL_NSIOBSERVER
94 static void Init();
96 #ifdef DEBUG
97 /**
98 * Unlink all known ghost windows, to enable investigating what caused them
99 * to become ghost windows in the first place.
101 static void UnlinkGhostWindows();
102 #endif
104 static nsWindowMemoryReporter* Get();
105 void ObserveDOMWindowDetached(nsGlobalWindowInner* aWindow);
107 static int64_t GhostWindowsDistinguishedAmount();
109 private:
110 ~nsWindowMemoryReporter();
112 // Protect ctor, use Init() instead.
113 nsWindowMemoryReporter();
116 * Get the number of seconds for which a window must satisfy ghost criteria
117 * (1) and (2) before we deem that it satisfies criterion (3).
119 uint32_t GetGhostTimeout();
121 void ObserveAfterMinimizeMemoryUsage();
124 * Iterate over all weak window pointers in mDetachedWindows and update our
125 * accounting of which windows meet ghost criterion (2).
127 * This method also cleans up mDetachedWindows, removing entries for windows
128 * which have been destroyed or are no longer detached.
130 * If aOutGhostIDs is non-null, we populate it with the Window IDs of the
131 * ghost windows.
133 * This is called asynchronously after we observe a DOM window being detached
134 * from its docshell, and also right before we generate a memory report.
136 void CheckForGhostWindows(nsTHashSet<uint64_t>* aOutGhostIDs = nullptr);
139 * Eventually do a check for ghost windows, if we haven't done one recently
140 * and we aren't already planning to do one soon.
142 void AsyncCheckForGhostWindows();
145 * Kill the check timer, if it exists.
147 void KillCheckTimer();
149 static void CheckTimerFired(nsITimer* aTimer, void* aClosure);
152 * Maps a weak reference to a detached window (nsIWeakReference) to the time
153 * when we observed that the window met ghost criterion (2) above.
155 * If the window has not yet met criterion (2) it maps to the null timestamp.
157 * (Although windows are not added to this table until they're detached, it's
158 * possible for a detached window to become non-detached, and we won't
159 * remove it from the table until CheckForGhostWindows runs.)
161 nsTHashMap<nsISupportsHashKey, mozilla::TimeStamp> mDetachedWindows;
164 * Track the last time we ran CheckForGhostWindows(), to avoid running it
165 * too often after a DOM window is detached.
167 mozilla::TimeStamp mLastCheckForGhostWindows;
169 nsCOMPtr<nsITimer> mCheckTimer;
171 bool mCycleCollectorIsRunning;
173 bool mCheckTimerWaitingForCCEnd;
175 int64_t mGhostWindowCount;
178 #endif // nsWindowMemoryReporter_h__