Bug 1852740: add tests for the `fetchpriority` attribute in Link headers. r=necko...
[gecko.git] / dom / base / nsWindowMemoryReporter.h
bloba91d60cddeacea628eada89a6ecc900f5a616aca
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 "nsTHashMap.h"
14 #include "nsTHashSet.h"
15 #include "nsWeakReference.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"
22 class nsGlobalWindowInner;
24 /**
25 * nsWindowMemoryReporter is responsible for the 'explicit/window-objects'
26 * memory reporter.
28 * We classify DOM window objects into one of three categories:
30 * - "active" windows, which are displayed in a tab (as the top-level window
31 * or an iframe),
33 * - "cached" windows, which are in the fastback cache (aka the bfcache), and
35 * - "detached" windows, which have a null docshell. A window becomes
36 * detached when its <iframe> or tab containing the window is destroyed --
37 * i.e., when the window is no longer active or cached.
39 * Additionally, we classify a subset of detached windows as "ghost" windows.
40 * Although ghost windows can happen legitimately (a page can hold a reference
41 * to a cross-domain window and then close its container), the presence of
42 * ghost windows is often indicative of a memory leak.
44 * A window is a ghost if it meets the following three criteria:
46 * 1) The window is detached.
48 * 2) There exist no non-detached windows with the same base domain as
49 * the window's principal. (For example, the base domain of
50 * "wiki.mozilla.co.uk" is "mozilla.co.uk".) This criterion makes us less
51 * likely to flag a legitimately held-alive detached window as a ghost.
53 * 3) The window has met criteria (1) and (2) above for at least
54 * memory.ghost_window_timeout_seconds. This criterion is in place so we
55 * don't immediately declare a window a ghost before the GC/CC has had a
56 * chance to run.
58 * nsWindowMemoryReporter observes window detachment and uses mDetachedWindows
59 * to remember when a window first met criteria (1) and (2). When we generate
60 * a memory report, we use this accounting to determine which windows are
61 * ghosts.
64 * We use the following memory reporter path for active and cached windows:
66 * explicit/window-objects/top(<top-outer-uri>, id=<top-outer-id>)/
67 * <category>/window(<window-uri>)/...
69 * For detached and ghost windows, we use
71 * explicit/window-objects/top(none)/<category>/window(<window-uri>)/...
73 * Where
75 * - <category> is "active", "cached", "detached", or "ghost", as described
76 * above.
78 * - <top-outer-id> is the window id of the top outer window (i.e. the tab, or
79 * the top level chrome window). Exposing this ensures that each tab gets
80 * its own sub-tree, even if multiple tabs are showing the same URI.
82 * - <top-uri> is the URI of the top window. Excepting special windows (such
83 * as browser.xhtml or hiddenWindow.html) it's what the address bar shows for
84 * the tab.
87 class nsWindowMemoryReporter final : public nsIMemoryReporter,
88 public nsIObserver,
89 public nsSupportsWeakReference {
90 public:
91 NS_DECL_ISUPPORTS
92 NS_DECL_NSIMEMORYREPORTER
93 NS_DECL_NSIOBSERVER
95 static void Init();
97 #ifdef DEBUG
98 /**
99 * Unlink all known ghost windows, to enable investigating what caused them
100 * to become ghost windows in the first place.
102 static void UnlinkGhostWindows();
103 #endif
105 static nsWindowMemoryReporter* Get();
106 void ObserveDOMWindowDetached(nsGlobalWindowInner* aWindow);
108 static int64_t GhostWindowsDistinguishedAmount();
110 private:
111 ~nsWindowMemoryReporter();
113 // Protect ctor, use Init() instead.
114 nsWindowMemoryReporter();
117 * Get the number of seconds for which a window must satisfy ghost criteria
118 * (1) and (2) before we deem that it satisfies criterion (3).
120 uint32_t GetGhostTimeout();
122 void ObserveAfterMinimizeMemoryUsage();
125 * Iterate over all weak window pointers in mDetachedWindows and update our
126 * accounting of which windows meet ghost criterion (2).
128 * This method also cleans up mDetachedWindows, removing entries for windows
129 * which have been destroyed or are no longer detached.
131 * If aOutGhostIDs is non-null, we populate it with the Window IDs of the
132 * ghost windows.
134 * This is called asynchronously after we observe a DOM window being detached
135 * from its docshell, and also right before we generate a memory report.
137 void CheckForGhostWindows(nsTHashSet<uint64_t>* aOutGhostIDs = nullptr);
140 * Eventually do a check for ghost windows, if we haven't done one recently
141 * and we aren't already planning to do one soon.
143 void AsyncCheckForGhostWindows();
146 * Kill the check timer, if it exists.
148 void KillCheckTimer();
150 static void CheckTimerFired(nsITimer* aTimer, void* aClosure);
153 * Maps a weak reference to a detached window (nsIWeakReference) to the time
154 * when we observed that the window met ghost criterion (2) above.
156 * If the window has not yet met criterion (2) it maps to the null timestamp.
158 * (Although windows are not added to this table until they're detached, it's
159 * possible for a detached window to become non-detached, and we won't
160 * remove it from the table until CheckForGhostWindows runs.)
162 nsTHashMap<nsISupportsHashKey, mozilla::TimeStamp> mDetachedWindows;
165 * Track the last time we ran CheckForGhostWindows(), to avoid running it
166 * too often after a DOM window is detached.
168 mozilla::TimeStamp mLastCheckForGhostWindows;
170 nsCOMPtr<nsITimer> mCheckTimer;
172 bool mCycleCollectorIsRunning;
174 bool mCheckTimerWaitingForCCEnd;
176 int64_t mGhostWindowCount;
179 #endif // nsWindowMemoryReporter_h__