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