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 | #include "amIAddonManager.h" |
michael@0 | 8 | #include "nsWindowMemoryReporter.h" |
michael@0 | 9 | #include "nsGlobalWindow.h" |
michael@0 | 10 | #include "nsIDocument.h" |
michael@0 | 11 | #include "nsIDOMWindowCollection.h" |
michael@0 | 12 | #include "nsIEffectiveTLDService.h" |
michael@0 | 13 | #include "mozilla/ClearOnShutdown.h" |
michael@0 | 14 | #include "mozilla/Preferences.h" |
michael@0 | 15 | #include "mozilla/Services.h" |
michael@0 | 16 | #include "mozilla/StaticPtr.h" |
michael@0 | 17 | #include "nsNetCID.h" |
michael@0 | 18 | #include "nsPrintfCString.h" |
michael@0 | 19 | #include "XPCJSMemoryReporter.h" |
michael@0 | 20 | #include "js/MemoryMetrics.h" |
michael@0 | 21 | #include "nsServiceManagerUtils.h" |
michael@0 | 22 | |
michael@0 | 23 | using namespace mozilla; |
michael@0 | 24 | |
michael@0 | 25 | StaticRefPtr<nsWindowMemoryReporter> sWindowReporter; |
michael@0 | 26 | |
michael@0 | 27 | /** |
michael@0 | 28 | * Don't trigger a ghost window check when a DOM window is detached if we've |
michael@0 | 29 | * run it this recently. |
michael@0 | 30 | */ |
michael@0 | 31 | const int32_t kTimeBetweenChecks = 45; /* seconds */ |
michael@0 | 32 | |
michael@0 | 33 | nsWindowMemoryReporter::nsWindowMemoryReporter() |
michael@0 | 34 | : mLastCheckForGhostWindows(TimeStamp::NowLoRes()), |
michael@0 | 35 | mCycleCollectorIsRunning(false), |
michael@0 | 36 | mCheckTimerWaitingForCCEnd(false) |
michael@0 | 37 | { |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | nsWindowMemoryReporter::~nsWindowMemoryReporter() |
michael@0 | 41 | { |
michael@0 | 42 | KillCheckTimer(); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | NS_IMPL_ISUPPORTS(nsWindowMemoryReporter, nsIMemoryReporter, nsIObserver, |
michael@0 | 46 | nsISupportsWeakReference) |
michael@0 | 47 | |
michael@0 | 48 | static nsresult |
michael@0 | 49 | AddNonJSSizeOfWindowAndItsDescendents(nsGlobalWindow* aWindow, |
michael@0 | 50 | nsTabSizes* aSizes) |
michael@0 | 51 | { |
michael@0 | 52 | // Measure the window. |
michael@0 | 53 | nsWindowSizes windowSizes(moz_malloc_size_of); |
michael@0 | 54 | aWindow->AddSizeOfIncludingThis(&windowSizes); |
michael@0 | 55 | windowSizes.addToTabSizes(aSizes); |
michael@0 | 56 | |
michael@0 | 57 | // Measure the inner window, if there is one. |
michael@0 | 58 | nsWindowSizes innerWindowSizes(moz_malloc_size_of); |
michael@0 | 59 | nsGlobalWindow* inner = aWindow->GetCurrentInnerWindowInternal(); |
michael@0 | 60 | if (inner) { |
michael@0 | 61 | inner->AddSizeOfIncludingThis(&innerWindowSizes); |
michael@0 | 62 | innerWindowSizes.addToTabSizes(aSizes); |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | nsCOMPtr<nsIDOMWindowCollection> frames; |
michael@0 | 66 | nsresult rv = aWindow->GetFrames(getter_AddRefs(frames)); |
michael@0 | 67 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 68 | |
michael@0 | 69 | uint32_t length; |
michael@0 | 70 | rv = frames->GetLength(&length); |
michael@0 | 71 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 72 | |
michael@0 | 73 | // Measure this window's descendents. |
michael@0 | 74 | for (uint32_t i = 0; i < length; i++) { |
michael@0 | 75 | nsCOMPtr<nsIDOMWindow> child; |
michael@0 | 76 | rv = frames->Item(i, getter_AddRefs(child)); |
michael@0 | 77 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 78 | NS_ENSURE_STATE(child); |
michael@0 | 79 | |
michael@0 | 80 | nsGlobalWindow* childWin = |
michael@0 | 81 | static_cast<nsGlobalWindow*>(static_cast<nsIDOMWindow *>(child.get())); |
michael@0 | 82 | |
michael@0 | 83 | rv = AddNonJSSizeOfWindowAndItsDescendents(childWin, aSizes); |
michael@0 | 84 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 85 | } |
michael@0 | 86 | return NS_OK; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | static nsresult |
michael@0 | 90 | NonJSSizeOfTab(nsPIDOMWindow* aWindow, size_t* aDomSize, size_t* aStyleSize, size_t* aOtherSize) |
michael@0 | 91 | { |
michael@0 | 92 | nsGlobalWindow* window = static_cast<nsGlobalWindow*>(aWindow); |
michael@0 | 93 | |
michael@0 | 94 | nsTabSizes sizes; |
michael@0 | 95 | nsresult rv = AddNonJSSizeOfWindowAndItsDescendents(window, &sizes); |
michael@0 | 96 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 97 | |
michael@0 | 98 | *aDomSize = sizes.mDom; |
michael@0 | 99 | *aStyleSize = sizes.mStyle; |
michael@0 | 100 | *aOtherSize = sizes.mOther; |
michael@0 | 101 | return NS_OK; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | /* static */ void |
michael@0 | 105 | nsWindowMemoryReporter::Init() |
michael@0 | 106 | { |
michael@0 | 107 | MOZ_ASSERT(!sWindowReporter); |
michael@0 | 108 | sWindowReporter = new nsWindowMemoryReporter(); |
michael@0 | 109 | ClearOnShutdown(&sWindowReporter); |
michael@0 | 110 | RegisterStrongMemoryReporter(sWindowReporter); |
michael@0 | 111 | RegisterNonJSSizeOfTab(NonJSSizeOfTab); |
michael@0 | 112 | |
michael@0 | 113 | nsCOMPtr<nsIObserverService> os = services::GetObserverService(); |
michael@0 | 114 | if (os) { |
michael@0 | 115 | // DOM_WINDOW_DESTROYED_TOPIC announces what we call window "detachment", |
michael@0 | 116 | // when a window's docshell is set to nullptr. |
michael@0 | 117 | os->AddObserver(sWindowReporter, DOM_WINDOW_DESTROYED_TOPIC, |
michael@0 | 118 | /* weakRef = */ true); |
michael@0 | 119 | os->AddObserver(sWindowReporter, "after-minimize-memory-usage", |
michael@0 | 120 | /* weakRef = */ true); |
michael@0 | 121 | os->AddObserver(sWindowReporter, "cycle-collector-begin", |
michael@0 | 122 | /* weakRef = */ true); |
michael@0 | 123 | os->AddObserver(sWindowReporter, "cycle-collector-end", |
michael@0 | 124 | /* weakRef = */ true); |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | RegisterStrongMemoryReporter(new GhostWindowsReporter()); |
michael@0 | 128 | RegisterGhostWindowsDistinguishedAmount(GhostWindowsReporter::DistinguishedAmount); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | static already_AddRefed<nsIURI> |
michael@0 | 132 | GetWindowURI(nsIDOMWindow *aWindow) |
michael@0 | 133 | { |
michael@0 | 134 | nsCOMPtr<nsPIDOMWindow> pWindow = do_QueryInterface(aWindow); |
michael@0 | 135 | NS_ENSURE_TRUE(pWindow, nullptr); |
michael@0 | 136 | |
michael@0 | 137 | nsCOMPtr<nsIDocument> doc = pWindow->GetExtantDoc(); |
michael@0 | 138 | nsCOMPtr<nsIURI> uri; |
michael@0 | 139 | |
michael@0 | 140 | if (doc) { |
michael@0 | 141 | uri = doc->GetDocumentURI(); |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | if (!uri) { |
michael@0 | 145 | nsCOMPtr<nsIScriptObjectPrincipal> scriptObjPrincipal = |
michael@0 | 146 | do_QueryInterface(aWindow); |
michael@0 | 147 | NS_ENSURE_TRUE(scriptObjPrincipal, nullptr); |
michael@0 | 148 | |
michael@0 | 149 | // GetPrincipal() will print a warning if the window does not have an outer |
michael@0 | 150 | // window, so check here for an outer window first. This code is |
michael@0 | 151 | // functionally correct if we leave out the GetOuterWindow() check, but we |
michael@0 | 152 | // end up printing a lot of warnings during debug mochitests. |
michael@0 | 153 | if (pWindow->GetOuterWindow()) { |
michael@0 | 154 | nsIPrincipal* principal = scriptObjPrincipal->GetPrincipal(); |
michael@0 | 155 | if (principal) { |
michael@0 | 156 | principal->GetURI(getter_AddRefs(uri)); |
michael@0 | 157 | } |
michael@0 | 158 | } |
michael@0 | 159 | } |
michael@0 | 160 | |
michael@0 | 161 | return uri.forget(); |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | static void |
michael@0 | 165 | AppendWindowURI(nsGlobalWindow *aWindow, nsACString& aStr) |
michael@0 | 166 | { |
michael@0 | 167 | nsCOMPtr<nsIURI> uri = GetWindowURI(aWindow); |
michael@0 | 168 | |
michael@0 | 169 | if (uri) { |
michael@0 | 170 | nsCString spec; |
michael@0 | 171 | uri->GetSpec(spec); |
michael@0 | 172 | |
michael@0 | 173 | // A hack: replace forward slashes with '\\' so they aren't |
michael@0 | 174 | // treated as path separators. Users of the reporters |
michael@0 | 175 | // (such as about:memory) have to undo this change. |
michael@0 | 176 | spec.ReplaceChar('/', '\\'); |
michael@0 | 177 | |
michael@0 | 178 | aStr += spec; |
michael@0 | 179 | } else { |
michael@0 | 180 | // If we're unable to find a URI, we're dealing with a chrome window with |
michael@0 | 181 | // no document in it (or somesuch), so we call this a "system window". |
michael@0 | 182 | aStr += NS_LITERAL_CSTRING("[system]"); |
michael@0 | 183 | } |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | MOZ_DEFINE_MALLOC_SIZE_OF(WindowsMallocSizeOf) |
michael@0 | 187 | |
michael@0 | 188 | // The key is the window ID. |
michael@0 | 189 | typedef nsDataHashtable<nsUint64HashKey, nsCString> WindowPaths; |
michael@0 | 190 | |
michael@0 | 191 | static nsresult |
michael@0 | 192 | ReportAmount(const nsCString& aBasePath, const char* aPathTail, |
michael@0 | 193 | size_t aAmount, const nsCString& aDescription, |
michael@0 | 194 | uint32_t aKind, uint32_t aUnits, |
michael@0 | 195 | nsIMemoryReporterCallback* aCb, |
michael@0 | 196 | nsISupports* aClosure) |
michael@0 | 197 | { |
michael@0 | 198 | if (aAmount == 0) { |
michael@0 | 199 | return NS_OK; |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | nsAutoCString path(aBasePath); |
michael@0 | 203 | path += aPathTail; |
michael@0 | 204 | |
michael@0 | 205 | return aCb->Callback(EmptyCString(), path, aKind, aUnits, |
michael@0 | 206 | aAmount, aDescription, aClosure); |
michael@0 | 207 | } |
michael@0 | 208 | |
michael@0 | 209 | static nsresult |
michael@0 | 210 | ReportSize(const nsCString& aBasePath, const char* aPathTail, |
michael@0 | 211 | size_t aAmount, const nsCString& aDescription, |
michael@0 | 212 | nsIMemoryReporterCallback* aCb, |
michael@0 | 213 | nsISupports* aClosure) |
michael@0 | 214 | { |
michael@0 | 215 | return ReportAmount(aBasePath, aPathTail, aAmount, aDescription, |
michael@0 | 216 | nsIMemoryReporter::KIND_HEAP, |
michael@0 | 217 | nsIMemoryReporter::UNITS_BYTES, aCb, aClosure); |
michael@0 | 218 | } |
michael@0 | 219 | |
michael@0 | 220 | static nsresult |
michael@0 | 221 | ReportCount(const nsCString& aBasePath, const char* aPathTail, |
michael@0 | 222 | size_t aAmount, const nsCString& aDescription, |
michael@0 | 223 | nsIMemoryReporterCallback* aCb, |
michael@0 | 224 | nsISupports* aClosure) |
michael@0 | 225 | { |
michael@0 | 226 | return ReportAmount(aBasePath, aPathTail, aAmount, aDescription, |
michael@0 | 227 | nsIMemoryReporter::KIND_OTHER, |
michael@0 | 228 | nsIMemoryReporter::UNITS_COUNT, aCb, aClosure); |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | static nsresult |
michael@0 | 232 | CollectWindowReports(nsGlobalWindow *aWindow, |
michael@0 | 233 | amIAddonManager *addonManager, |
michael@0 | 234 | nsWindowSizes *aWindowTotalSizes, |
michael@0 | 235 | nsTHashtable<nsUint64HashKey> *aGhostWindowIDs, |
michael@0 | 236 | WindowPaths *aWindowPaths, |
michael@0 | 237 | WindowPaths *aTopWindowPaths, |
michael@0 | 238 | nsIMemoryReporterCallback *aCb, |
michael@0 | 239 | nsISupports *aClosure) |
michael@0 | 240 | { |
michael@0 | 241 | nsAutoCString windowPath("explicit/"); |
michael@0 | 242 | |
michael@0 | 243 | // Avoid calling aWindow->GetTop() if there's no outer window. It will work |
michael@0 | 244 | // just fine, but will spew a lot of warnings. |
michael@0 | 245 | nsGlobalWindow *top = nullptr; |
michael@0 | 246 | nsCOMPtr<nsIURI> location; |
michael@0 | 247 | if (aWindow->GetOuterWindow()) { |
michael@0 | 248 | // Our window should have a null top iff it has a null docshell. |
michael@0 | 249 | MOZ_ASSERT(!!aWindow->GetTop() == !!aWindow->GetDocShell()); |
michael@0 | 250 | top = aWindow->GetTop(); |
michael@0 | 251 | if (top) { |
michael@0 | 252 | location = GetWindowURI(top); |
michael@0 | 253 | } |
michael@0 | 254 | } |
michael@0 | 255 | if (!location) { |
michael@0 | 256 | location = GetWindowURI(aWindow); |
michael@0 | 257 | } |
michael@0 | 258 | |
michael@0 | 259 | if (addonManager && location) { |
michael@0 | 260 | bool ok; |
michael@0 | 261 | nsAutoCString id; |
michael@0 | 262 | if (NS_SUCCEEDED(addonManager->MapURIToAddonID(location, id, &ok)) && ok) { |
michael@0 | 263 | windowPath += NS_LITERAL_CSTRING("add-ons/") + id + |
michael@0 | 264 | NS_LITERAL_CSTRING("/"); |
michael@0 | 265 | } |
michael@0 | 266 | } |
michael@0 | 267 | |
michael@0 | 268 | windowPath += NS_LITERAL_CSTRING("window-objects/"); |
michael@0 | 269 | |
michael@0 | 270 | if (top) { |
michael@0 | 271 | windowPath += NS_LITERAL_CSTRING("top("); |
michael@0 | 272 | AppendWindowURI(top, windowPath); |
michael@0 | 273 | windowPath += NS_LITERAL_CSTRING(", id="); |
michael@0 | 274 | windowPath.AppendInt(top->WindowID()); |
michael@0 | 275 | windowPath += NS_LITERAL_CSTRING(")"); |
michael@0 | 276 | |
michael@0 | 277 | aTopWindowPaths->Put(aWindow->WindowID(), windowPath); |
michael@0 | 278 | |
michael@0 | 279 | windowPath += aWindow->IsFrozen() ? NS_LITERAL_CSTRING("/cached/") |
michael@0 | 280 | : NS_LITERAL_CSTRING("/active/"); |
michael@0 | 281 | } else { |
michael@0 | 282 | if (aGhostWindowIDs->Contains(aWindow->WindowID())) { |
michael@0 | 283 | windowPath += NS_LITERAL_CSTRING("top(none)/ghost/"); |
michael@0 | 284 | } else { |
michael@0 | 285 | windowPath += NS_LITERAL_CSTRING("top(none)/detached/"); |
michael@0 | 286 | } |
michael@0 | 287 | } |
michael@0 | 288 | |
michael@0 | 289 | windowPath += NS_LITERAL_CSTRING("window("); |
michael@0 | 290 | AppendWindowURI(aWindow, windowPath); |
michael@0 | 291 | windowPath += NS_LITERAL_CSTRING(")"); |
michael@0 | 292 | |
michael@0 | 293 | // Use |windowPath|, but replace "explicit/" with "event-counts/". |
michael@0 | 294 | nsCString censusWindowPath(windowPath); |
michael@0 | 295 | censusWindowPath.Replace(0, strlen("explicit"), "event-counts"); |
michael@0 | 296 | |
michael@0 | 297 | // Remember the path for later. |
michael@0 | 298 | aWindowPaths->Put(aWindow->WindowID(), windowPath); |
michael@0 | 299 | |
michael@0 | 300 | #define REPORT_SIZE(_pathTail, _amount, _desc) \ |
michael@0 | 301 | do { \ |
michael@0 | 302 | nsresult rv = ReportSize(windowPath, _pathTail, _amount, \ |
michael@0 | 303 | NS_LITERAL_CSTRING(_desc), aCb, aClosure); \ |
michael@0 | 304 | NS_ENSURE_SUCCESS(rv, rv); \ |
michael@0 | 305 | } while (0) |
michael@0 | 306 | |
michael@0 | 307 | #define REPORT_COUNT(_pathTail, _amount, _desc) \ |
michael@0 | 308 | do { \ |
michael@0 | 309 | nsresult rv = ReportCount(censusWindowPath, _pathTail, _amount, \ |
michael@0 | 310 | NS_LITERAL_CSTRING(_desc), aCb, aClosure); \ |
michael@0 | 311 | NS_ENSURE_SUCCESS(rv, rv); \ |
michael@0 | 312 | } while (0) |
michael@0 | 313 | |
michael@0 | 314 | nsWindowSizes windowSizes(WindowsMallocSizeOf); |
michael@0 | 315 | aWindow->AddSizeOfIncludingThis(&windowSizes); |
michael@0 | 316 | |
michael@0 | 317 | REPORT_SIZE("/dom/element-nodes", windowSizes.mDOMElementNodesSize, |
michael@0 | 318 | "Memory used by the element nodes in a window's DOM."); |
michael@0 | 319 | aWindowTotalSizes->mDOMElementNodesSize += windowSizes.mDOMElementNodesSize; |
michael@0 | 320 | |
michael@0 | 321 | REPORT_SIZE("/dom/text-nodes", windowSizes.mDOMTextNodesSize, |
michael@0 | 322 | "Memory used by the text nodes in a window's DOM."); |
michael@0 | 323 | aWindowTotalSizes->mDOMTextNodesSize += windowSizes.mDOMTextNodesSize; |
michael@0 | 324 | |
michael@0 | 325 | REPORT_SIZE("/dom/cdata-nodes", windowSizes.mDOMCDATANodesSize, |
michael@0 | 326 | "Memory used by the CDATA nodes in a window's DOM."); |
michael@0 | 327 | aWindowTotalSizes->mDOMCDATANodesSize += windowSizes.mDOMCDATANodesSize; |
michael@0 | 328 | |
michael@0 | 329 | REPORT_SIZE("/dom/comment-nodes", windowSizes.mDOMCommentNodesSize, |
michael@0 | 330 | "Memory used by the comment nodes in a window's DOM."); |
michael@0 | 331 | aWindowTotalSizes->mDOMCommentNodesSize += windowSizes.mDOMCommentNodesSize; |
michael@0 | 332 | |
michael@0 | 333 | REPORT_SIZE("/dom/event-targets", windowSizes.mDOMEventTargetsSize, |
michael@0 | 334 | "Memory used by the event targets table in a window's DOM, and " |
michael@0 | 335 | "the objects it points to, which include XHRs."); |
michael@0 | 336 | aWindowTotalSizes->mDOMEventTargetsSize += windowSizes.mDOMEventTargetsSize; |
michael@0 | 337 | |
michael@0 | 338 | REPORT_COUNT("/dom/event-targets", windowSizes.mDOMEventTargetsCount, |
michael@0 | 339 | "Number of non-node event targets in the event targets table " |
michael@0 | 340 | "in a window's DOM, such as XHRs."); |
michael@0 | 341 | aWindowTotalSizes->mDOMEventTargetsCount += |
michael@0 | 342 | windowSizes.mDOMEventTargetsCount; |
michael@0 | 343 | |
michael@0 | 344 | REPORT_COUNT("/dom/event-listeners", windowSizes.mDOMEventListenersCount, |
michael@0 | 345 | "Number of event listeners in a window, including event " |
michael@0 | 346 | "listeners on nodes and other event targets."); |
michael@0 | 347 | aWindowTotalSizes->mDOMEventListenersCount += |
michael@0 | 348 | windowSizes.mDOMEventListenersCount; |
michael@0 | 349 | |
michael@0 | 350 | REPORT_SIZE("/dom/other", windowSizes.mDOMOtherSize, |
michael@0 | 351 | "Memory used by a window's DOM that isn't measured by the " |
michael@0 | 352 | "other 'dom/' numbers."); |
michael@0 | 353 | aWindowTotalSizes->mDOMOtherSize += windowSizes.mDOMOtherSize; |
michael@0 | 354 | |
michael@0 | 355 | REPORT_SIZE("/property-tables", |
michael@0 | 356 | windowSizes.mPropertyTablesSize, |
michael@0 | 357 | "Memory used for the property tables within a window."); |
michael@0 | 358 | aWindowTotalSizes->mPropertyTablesSize += windowSizes.mPropertyTablesSize; |
michael@0 | 359 | |
michael@0 | 360 | REPORT_SIZE("/style-sheets", windowSizes.mStyleSheetsSize, |
michael@0 | 361 | "Memory used by style sheets within a window."); |
michael@0 | 362 | aWindowTotalSizes->mStyleSheetsSize += windowSizes.mStyleSheetsSize; |
michael@0 | 363 | |
michael@0 | 364 | REPORT_SIZE("/layout/pres-shell", windowSizes.mLayoutPresShellSize, |
michael@0 | 365 | "Memory used by layout's PresShell, along with any structures " |
michael@0 | 366 | "allocated in its arena and not measured elsewhere, " |
michael@0 | 367 | "within a window."); |
michael@0 | 368 | aWindowTotalSizes->mLayoutPresShellSize += windowSizes.mLayoutPresShellSize; |
michael@0 | 369 | |
michael@0 | 370 | REPORT_SIZE("/layout/line-boxes", windowSizes.mArenaStats.mLineBoxes, |
michael@0 | 371 | "Memory used by line boxes within a window."); |
michael@0 | 372 | aWindowTotalSizes->mArenaStats.mLineBoxes |
michael@0 | 373 | += windowSizes.mArenaStats.mLineBoxes; |
michael@0 | 374 | |
michael@0 | 375 | REPORT_SIZE("/layout/rule-nodes", windowSizes.mArenaStats.mRuleNodes, |
michael@0 | 376 | "Memory used by CSS rule nodes within a window."); |
michael@0 | 377 | aWindowTotalSizes->mArenaStats.mRuleNodes |
michael@0 | 378 | += windowSizes.mArenaStats.mRuleNodes; |
michael@0 | 379 | |
michael@0 | 380 | REPORT_SIZE("/layout/style-contexts", windowSizes.mArenaStats.mStyleContexts, |
michael@0 | 381 | "Memory used by style contexts within a window."); |
michael@0 | 382 | aWindowTotalSizes->mArenaStats.mStyleContexts |
michael@0 | 383 | += windowSizes.mArenaStats.mStyleContexts; |
michael@0 | 384 | |
michael@0 | 385 | REPORT_SIZE("/layout/style-sets", windowSizes.mLayoutStyleSetsSize, |
michael@0 | 386 | "Memory used by style sets within a window."); |
michael@0 | 387 | aWindowTotalSizes->mLayoutStyleSetsSize += windowSizes.mLayoutStyleSetsSize; |
michael@0 | 388 | |
michael@0 | 389 | REPORT_SIZE("/layout/text-runs", windowSizes.mLayoutTextRunsSize, |
michael@0 | 390 | "Memory used for text-runs (glyph layout) in the PresShell's " |
michael@0 | 391 | "frame tree, within a window."); |
michael@0 | 392 | aWindowTotalSizes->mLayoutTextRunsSize += windowSizes.mLayoutTextRunsSize; |
michael@0 | 393 | |
michael@0 | 394 | REPORT_SIZE("/layout/pres-contexts", windowSizes.mLayoutPresContextSize, |
michael@0 | 395 | "Memory used for the PresContext in the PresShell's frame " |
michael@0 | 396 | "within a window."); |
michael@0 | 397 | aWindowTotalSizes->mLayoutPresContextSize += |
michael@0 | 398 | windowSizes.mLayoutPresContextSize; |
michael@0 | 399 | |
michael@0 | 400 | // There are many different kinds of frames, but it is very likely |
michael@0 | 401 | // that only a few matter. Implement a cutoff so we don't bloat |
michael@0 | 402 | // about:memory with many uninteresting entries. |
michael@0 | 403 | const size_t FRAME_SUNDRIES_THRESHOLD = |
michael@0 | 404 | js::MemoryReportingSundriesThreshold(); |
michael@0 | 405 | |
michael@0 | 406 | size_t frameSundriesSize = 0; |
michael@0 | 407 | #define FRAME_ID(classname) \ |
michael@0 | 408 | { \ |
michael@0 | 409 | size_t frameSize \ |
michael@0 | 410 | = windowSizes.mArenaStats.FRAME_ID_STAT_FIELD(classname); \ |
michael@0 | 411 | if (frameSize < FRAME_SUNDRIES_THRESHOLD) { \ |
michael@0 | 412 | frameSundriesSize += frameSize; \ |
michael@0 | 413 | } else { \ |
michael@0 | 414 | REPORT_SIZE("/layout/frames/" # classname, frameSize, \ |
michael@0 | 415 | "Memory used by frames of " \ |
michael@0 | 416 | "type " #classname " within a window."); \ |
michael@0 | 417 | } \ |
michael@0 | 418 | aWindowTotalSizes->mArenaStats.FRAME_ID_STAT_FIELD(classname) \ |
michael@0 | 419 | += frameSize; \ |
michael@0 | 420 | } |
michael@0 | 421 | #include "nsFrameIdList.h" |
michael@0 | 422 | #undef FRAME_ID |
michael@0 | 423 | |
michael@0 | 424 | if (frameSundriesSize > 0) { |
michael@0 | 425 | REPORT_SIZE("/layout/frames/sundries", frameSundriesSize, |
michael@0 | 426 | "The sum of all memory used by frames which were too small " |
michael@0 | 427 | "to be shown individually."); |
michael@0 | 428 | } |
michael@0 | 429 | |
michael@0 | 430 | #undef REPORT_SIZE |
michael@0 | 431 | #undef REPORT_COUNT |
michael@0 | 432 | |
michael@0 | 433 | return NS_OK; |
michael@0 | 434 | } |
michael@0 | 435 | |
michael@0 | 436 | typedef nsTArray< nsRefPtr<nsGlobalWindow> > WindowArray; |
michael@0 | 437 | |
michael@0 | 438 | static |
michael@0 | 439 | PLDHashOperator |
michael@0 | 440 | GetWindows(const uint64_t& aId, nsGlobalWindow*& aWindow, void* aClosure) |
michael@0 | 441 | { |
michael@0 | 442 | ((WindowArray *)aClosure)->AppendElement(aWindow); |
michael@0 | 443 | |
michael@0 | 444 | return PL_DHASH_NEXT; |
michael@0 | 445 | } |
michael@0 | 446 | |
michael@0 | 447 | struct ReportGhostWindowsEnumeratorData |
michael@0 | 448 | { |
michael@0 | 449 | nsIMemoryReporterCallback* callback; |
michael@0 | 450 | nsISupports* closure; |
michael@0 | 451 | nsresult rv; |
michael@0 | 452 | }; |
michael@0 | 453 | |
michael@0 | 454 | static PLDHashOperator |
michael@0 | 455 | ReportGhostWindowsEnumerator(nsUint64HashKey* aIDHashKey, void* aClosure) |
michael@0 | 456 | { |
michael@0 | 457 | ReportGhostWindowsEnumeratorData *data = |
michael@0 | 458 | static_cast<ReportGhostWindowsEnumeratorData*>(aClosure); |
michael@0 | 459 | |
michael@0 | 460 | nsGlobalWindow::WindowByIdTable* windowsById = |
michael@0 | 461 | nsGlobalWindow::GetWindowsTable(); |
michael@0 | 462 | if (!windowsById) { |
michael@0 | 463 | NS_WARNING("Couldn't get window-by-id hashtable?"); |
michael@0 | 464 | return PL_DHASH_NEXT; |
michael@0 | 465 | } |
michael@0 | 466 | |
michael@0 | 467 | nsGlobalWindow* window = windowsById->Get(aIDHashKey->GetKey()); |
michael@0 | 468 | if (!window) { |
michael@0 | 469 | NS_WARNING("Could not look up window?"); |
michael@0 | 470 | return PL_DHASH_NEXT; |
michael@0 | 471 | } |
michael@0 | 472 | |
michael@0 | 473 | nsAutoCString path; |
michael@0 | 474 | path.AppendLiteral("ghost-windows/"); |
michael@0 | 475 | AppendWindowURI(window, path); |
michael@0 | 476 | |
michael@0 | 477 | nsresult rv = data->callback->Callback( |
michael@0 | 478 | /* process = */ EmptyCString(), |
michael@0 | 479 | path, |
michael@0 | 480 | nsIMemoryReporter::KIND_OTHER, |
michael@0 | 481 | nsIMemoryReporter::UNITS_COUNT, |
michael@0 | 482 | /* amount = */ 1, |
michael@0 | 483 | /* description = */ NS_LITERAL_CSTRING("A ghost window."), |
michael@0 | 484 | data->closure); |
michael@0 | 485 | |
michael@0 | 486 | if (NS_FAILED(rv) && NS_SUCCEEDED(data->rv)) { |
michael@0 | 487 | data->rv = rv; |
michael@0 | 488 | } |
michael@0 | 489 | |
michael@0 | 490 | return PL_DHASH_NEXT; |
michael@0 | 491 | } |
michael@0 | 492 | |
michael@0 | 493 | NS_IMETHODIMP |
michael@0 | 494 | nsWindowMemoryReporter::CollectReports(nsIMemoryReporterCallback* aCb, |
michael@0 | 495 | nsISupports* aClosure) |
michael@0 | 496 | { |
michael@0 | 497 | nsGlobalWindow::WindowByIdTable* windowsById = |
michael@0 | 498 | nsGlobalWindow::GetWindowsTable(); |
michael@0 | 499 | NS_ENSURE_TRUE(windowsById, NS_OK); |
michael@0 | 500 | |
michael@0 | 501 | // Hold on to every window in memory so that window objects can't be |
michael@0 | 502 | // destroyed while we're calling the memory reporter callback. |
michael@0 | 503 | WindowArray windows; |
michael@0 | 504 | windowsById->Enumerate(GetWindows, &windows); |
michael@0 | 505 | |
michael@0 | 506 | // Get the IDs of all the "ghost" windows, and call aCb->Callback() for each |
michael@0 | 507 | // one. |
michael@0 | 508 | nsTHashtable<nsUint64HashKey> ghostWindows; |
michael@0 | 509 | CheckForGhostWindows(&ghostWindows); |
michael@0 | 510 | ReportGhostWindowsEnumeratorData reportGhostWindowsEnumData = |
michael@0 | 511 | { aCb, aClosure, NS_OK }; |
michael@0 | 512 | ghostWindows.EnumerateEntries(ReportGhostWindowsEnumerator, |
michael@0 | 513 | &reportGhostWindowsEnumData); |
michael@0 | 514 | nsresult rv = reportGhostWindowsEnumData.rv; |
michael@0 | 515 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 516 | |
michael@0 | 517 | WindowPaths windowPaths; |
michael@0 | 518 | WindowPaths topWindowPaths; |
michael@0 | 519 | |
michael@0 | 520 | // Collect window memory usage. |
michael@0 | 521 | nsWindowSizes windowTotalSizes(nullptr); |
michael@0 | 522 | nsCOMPtr<amIAddonManager> addonManager; |
michael@0 | 523 | if (XRE_GetProcessType() == GeckoProcessType_Default) { |
michael@0 | 524 | // Only try to access the service from the main process. |
michael@0 | 525 | addonManager = do_GetService("@mozilla.org/addons/integration;1"); |
michael@0 | 526 | } |
michael@0 | 527 | for (uint32_t i = 0; i < windows.Length(); i++) { |
michael@0 | 528 | rv = CollectWindowReports(windows[i], addonManager, |
michael@0 | 529 | &windowTotalSizes, &ghostWindows, |
michael@0 | 530 | &windowPaths, &topWindowPaths, aCb, |
michael@0 | 531 | aClosure); |
michael@0 | 532 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 533 | } |
michael@0 | 534 | |
michael@0 | 535 | // Report JS memory usage. We do this from here because the JS memory |
michael@0 | 536 | // reporter needs to be passed |windowPaths|. |
michael@0 | 537 | rv = xpc::JSReporter::CollectReports(&windowPaths, &topWindowPaths, |
michael@0 | 538 | aCb, aClosure); |
michael@0 | 539 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 540 | |
michael@0 | 541 | #define REPORT(_path, _amount, _desc) \ |
michael@0 | 542 | do { \ |
michael@0 | 543 | nsresult rv; \ |
michael@0 | 544 | rv = aCb->Callback(EmptyCString(), NS_LITERAL_CSTRING(_path), \ |
michael@0 | 545 | KIND_OTHER, UNITS_BYTES, _amount, \ |
michael@0 | 546 | NS_LITERAL_CSTRING(_desc), aClosure); \ |
michael@0 | 547 | NS_ENSURE_SUCCESS(rv, rv); \ |
michael@0 | 548 | } while (0) |
michael@0 | 549 | |
michael@0 | 550 | REPORT("window-objects/dom/element-nodes", windowTotalSizes.mDOMElementNodesSize, |
michael@0 | 551 | "This is the sum of all windows' 'dom/element-nodes' numbers."); |
michael@0 | 552 | |
michael@0 | 553 | REPORT("window-objects/dom/text-nodes", windowTotalSizes.mDOMTextNodesSize, |
michael@0 | 554 | "This is the sum of all windows' 'dom/text-nodes' numbers."); |
michael@0 | 555 | |
michael@0 | 556 | REPORT("window-objects/dom/cdata-nodes", windowTotalSizes.mDOMCDATANodesSize, |
michael@0 | 557 | "This is the sum of all windows' 'dom/cdata-nodes' numbers."); |
michael@0 | 558 | |
michael@0 | 559 | REPORT("window-objects/dom/comment-nodes", windowTotalSizes.mDOMCommentNodesSize, |
michael@0 | 560 | "This is the sum of all windows' 'dom/comment-nodes' numbers."); |
michael@0 | 561 | |
michael@0 | 562 | REPORT("window-objects/dom/event-targets", windowTotalSizes.mDOMEventTargetsSize, |
michael@0 | 563 | "This is the sum of all windows' 'dom/event-targets' numbers."); |
michael@0 | 564 | |
michael@0 | 565 | REPORT("window-objects/dom/other", windowTotalSizes.mDOMOtherSize, |
michael@0 | 566 | "This is the sum of all windows' 'dom/other' numbers."); |
michael@0 | 567 | |
michael@0 | 568 | REPORT("window-objects/property-tables", |
michael@0 | 569 | windowTotalSizes.mPropertyTablesSize, |
michael@0 | 570 | "This is the sum of all windows' 'property-tables' numbers."); |
michael@0 | 571 | |
michael@0 | 572 | REPORT("window-objects/style-sheets", windowTotalSizes.mStyleSheetsSize, |
michael@0 | 573 | "This is the sum of all windows' 'style-sheets' numbers."); |
michael@0 | 574 | |
michael@0 | 575 | REPORT("window-objects/layout/pres-shell", windowTotalSizes.mLayoutPresShellSize, |
michael@0 | 576 | "This is the sum of all windows' 'layout/arenas' numbers."); |
michael@0 | 577 | |
michael@0 | 578 | REPORT("window-objects/layout/line-boxes", |
michael@0 | 579 | windowTotalSizes.mArenaStats.mLineBoxes, |
michael@0 | 580 | "This is the sum of all windows' 'layout/line-boxes' numbers."); |
michael@0 | 581 | |
michael@0 | 582 | REPORT("window-objects/layout/rule-nodes", |
michael@0 | 583 | windowTotalSizes.mArenaStats.mRuleNodes, |
michael@0 | 584 | "This is the sum of all windows' 'layout/rule-nodes' numbers."); |
michael@0 | 585 | |
michael@0 | 586 | REPORT("window-objects/layout/style-contexts", |
michael@0 | 587 | windowTotalSizes.mArenaStats.mStyleContexts, |
michael@0 | 588 | "This is the sum of all windows' 'layout/style-contexts' numbers."); |
michael@0 | 589 | |
michael@0 | 590 | REPORT("window-objects/layout/style-sets", windowTotalSizes.mLayoutStyleSetsSize, |
michael@0 | 591 | "This is the sum of all windows' 'layout/style-sets' numbers."); |
michael@0 | 592 | |
michael@0 | 593 | REPORT("window-objects/layout/text-runs", windowTotalSizes.mLayoutTextRunsSize, |
michael@0 | 594 | "This is the sum of all windows' 'layout/text-runs' numbers."); |
michael@0 | 595 | |
michael@0 | 596 | REPORT("window-objects/layout/pres-contexts", windowTotalSizes.mLayoutPresContextSize, |
michael@0 | 597 | "This is the sum of all windows' 'layout/pres-contexts' numbers."); |
michael@0 | 598 | |
michael@0 | 599 | size_t frameTotal = 0; |
michael@0 | 600 | #define FRAME_ID(classname) \ |
michael@0 | 601 | frameTotal += windowTotalSizes.mArenaStats.FRAME_ID_STAT_FIELD(classname); |
michael@0 | 602 | #include "nsFrameIdList.h" |
michael@0 | 603 | #undef FRAME_ID |
michael@0 | 604 | |
michael@0 | 605 | REPORT("window-objects/layout/frames", frameTotal, |
michael@0 | 606 | "Memory used for layout frames within windows. " |
michael@0 | 607 | "This is the sum of all windows' 'layout/frames/' numbers."); |
michael@0 | 608 | |
michael@0 | 609 | #undef REPORT |
michael@0 | 610 | |
michael@0 | 611 | return NS_OK; |
michael@0 | 612 | } |
michael@0 | 613 | |
michael@0 | 614 | uint32_t |
michael@0 | 615 | nsWindowMemoryReporter::GetGhostTimeout() |
michael@0 | 616 | { |
michael@0 | 617 | return Preferences::GetUint("memory.ghost_window_timeout_seconds", 60); |
michael@0 | 618 | } |
michael@0 | 619 | |
michael@0 | 620 | NS_IMETHODIMP |
michael@0 | 621 | nsWindowMemoryReporter::Observe(nsISupports *aSubject, const char *aTopic, |
michael@0 | 622 | const char16_t *aData) |
michael@0 | 623 | { |
michael@0 | 624 | if (!strcmp(aTopic, DOM_WINDOW_DESTROYED_TOPIC)) { |
michael@0 | 625 | ObserveDOMWindowDetached(aSubject); |
michael@0 | 626 | } else if (!strcmp(aTopic, "after-minimize-memory-usage")) { |
michael@0 | 627 | ObserveAfterMinimizeMemoryUsage(); |
michael@0 | 628 | } else if (!strcmp(aTopic, "cycle-collector-begin")) { |
michael@0 | 629 | if (mCheckTimer) { |
michael@0 | 630 | mCheckTimerWaitingForCCEnd = true; |
michael@0 | 631 | KillCheckTimer(); |
michael@0 | 632 | } |
michael@0 | 633 | mCycleCollectorIsRunning = true; |
michael@0 | 634 | } else if (!strcmp(aTopic, "cycle-collector-end")) { |
michael@0 | 635 | mCycleCollectorIsRunning = false; |
michael@0 | 636 | if (mCheckTimerWaitingForCCEnd) { |
michael@0 | 637 | mCheckTimerWaitingForCCEnd = false; |
michael@0 | 638 | AsyncCheckForGhostWindows(); |
michael@0 | 639 | } |
michael@0 | 640 | } else { |
michael@0 | 641 | MOZ_ASSERT(false); |
michael@0 | 642 | } |
michael@0 | 643 | |
michael@0 | 644 | return NS_OK; |
michael@0 | 645 | } |
michael@0 | 646 | |
michael@0 | 647 | void |
michael@0 | 648 | nsWindowMemoryReporter::ObserveDOMWindowDetached(nsISupports* aWindow) |
michael@0 | 649 | { |
michael@0 | 650 | nsWeakPtr weakWindow = do_GetWeakReference(aWindow); |
michael@0 | 651 | if (!weakWindow) { |
michael@0 | 652 | NS_WARNING("Couldn't take weak reference to a window?"); |
michael@0 | 653 | return; |
michael@0 | 654 | } |
michael@0 | 655 | |
michael@0 | 656 | mDetachedWindows.Put(weakWindow, TimeStamp()); |
michael@0 | 657 | |
michael@0 | 658 | AsyncCheckForGhostWindows(); |
michael@0 | 659 | } |
michael@0 | 660 | |
michael@0 | 661 | // static |
michael@0 | 662 | void |
michael@0 | 663 | nsWindowMemoryReporter::CheckTimerFired(nsITimer* aTimer, void* aClosure) |
michael@0 | 664 | { |
michael@0 | 665 | if (sWindowReporter) { |
michael@0 | 666 | MOZ_ASSERT(!sWindowReporter->mCycleCollectorIsRunning); |
michael@0 | 667 | sWindowReporter->CheckForGhostWindows(); |
michael@0 | 668 | } |
michael@0 | 669 | } |
michael@0 | 670 | |
michael@0 | 671 | void |
michael@0 | 672 | nsWindowMemoryReporter::AsyncCheckForGhostWindows() |
michael@0 | 673 | { |
michael@0 | 674 | if (mCheckTimer) { |
michael@0 | 675 | return; |
michael@0 | 676 | } |
michael@0 | 677 | |
michael@0 | 678 | if (mCycleCollectorIsRunning) { |
michael@0 | 679 | mCheckTimerWaitingForCCEnd = true; |
michael@0 | 680 | return; |
michael@0 | 681 | } |
michael@0 | 682 | |
michael@0 | 683 | // If more than kTimeBetweenChecks seconds have elapsed since the last check, |
michael@0 | 684 | // timerDelay is 0. Otherwise, it is kTimeBetweenChecks, reduced by the time |
michael@0 | 685 | // since the last check. Reducing the delay by the time since the last check |
michael@0 | 686 | // prevents the timer from being completely starved if it is repeatedly killed |
michael@0 | 687 | // and restarted. |
michael@0 | 688 | int32_t timeSinceLastCheck = (TimeStamp::NowLoRes() - mLastCheckForGhostWindows).ToSeconds(); |
michael@0 | 689 | int32_t timerDelay = (kTimeBetweenChecks - std::min(timeSinceLastCheck, kTimeBetweenChecks)) * PR_MSEC_PER_SEC; |
michael@0 | 690 | |
michael@0 | 691 | CallCreateInstance<nsITimer>("@mozilla.org/timer;1", getter_AddRefs(mCheckTimer)); |
michael@0 | 692 | |
michael@0 | 693 | if (mCheckTimer) { |
michael@0 | 694 | mCheckTimer->InitWithFuncCallback(CheckTimerFired, nullptr, |
michael@0 | 695 | timerDelay, nsITimer::TYPE_ONE_SHOT); |
michael@0 | 696 | } |
michael@0 | 697 | } |
michael@0 | 698 | |
michael@0 | 699 | static PLDHashOperator |
michael@0 | 700 | BackdateTimeStampsEnumerator(nsISupports *aKey, TimeStamp &aTimeStamp, |
michael@0 | 701 | void* aClosure) |
michael@0 | 702 | { |
michael@0 | 703 | TimeStamp *minTimeStamp = static_cast<TimeStamp*>(aClosure); |
michael@0 | 704 | |
michael@0 | 705 | if (!aTimeStamp.IsNull() && aTimeStamp > *minTimeStamp) { |
michael@0 | 706 | aTimeStamp = *minTimeStamp; |
michael@0 | 707 | } |
michael@0 | 708 | |
michael@0 | 709 | return PL_DHASH_NEXT; |
michael@0 | 710 | } |
michael@0 | 711 | |
michael@0 | 712 | void |
michael@0 | 713 | nsWindowMemoryReporter::ObserveAfterMinimizeMemoryUsage() |
michael@0 | 714 | { |
michael@0 | 715 | // Someone claims they've done enough GC/CCs so that all eligible windows |
michael@0 | 716 | // have been free'd. So we deem that any windows which satisfy ghost |
michael@0 | 717 | // criteria (1) and (2) now satisfy criterion (3) as well. |
michael@0 | 718 | // |
michael@0 | 719 | // To effect this change, we'll backdate some of our timestamps. |
michael@0 | 720 | |
michael@0 | 721 | TimeStamp minTimeStamp = TimeStamp::Now() - |
michael@0 | 722 | TimeDuration::FromSeconds(GetGhostTimeout()); |
michael@0 | 723 | |
michael@0 | 724 | mDetachedWindows.Enumerate(BackdateTimeStampsEnumerator, |
michael@0 | 725 | &minTimeStamp); |
michael@0 | 726 | } |
michael@0 | 727 | |
michael@0 | 728 | struct CheckForGhostWindowsEnumeratorData |
michael@0 | 729 | { |
michael@0 | 730 | nsTHashtable<nsCStringHashKey> *nonDetachedDomains; |
michael@0 | 731 | nsTHashtable<nsUint64HashKey> *ghostWindowIDs; |
michael@0 | 732 | nsIEffectiveTLDService *tldService; |
michael@0 | 733 | uint32_t ghostTimeout; |
michael@0 | 734 | TimeStamp now; |
michael@0 | 735 | }; |
michael@0 | 736 | |
michael@0 | 737 | static PLDHashOperator |
michael@0 | 738 | CheckForGhostWindowsEnumerator(nsISupports *aKey, TimeStamp& aTimeStamp, |
michael@0 | 739 | void* aClosure) |
michael@0 | 740 | { |
michael@0 | 741 | CheckForGhostWindowsEnumeratorData *data = |
michael@0 | 742 | static_cast<CheckForGhostWindowsEnumeratorData*>(aClosure); |
michael@0 | 743 | |
michael@0 | 744 | nsWeakPtr weakKey = do_QueryInterface(aKey); |
michael@0 | 745 | nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(weakKey); |
michael@0 | 746 | if (!window) { |
michael@0 | 747 | // The window object has been destroyed. Stop tracking its weak ref in our |
michael@0 | 748 | // hashtable. |
michael@0 | 749 | return PL_DHASH_REMOVE; |
michael@0 | 750 | } |
michael@0 | 751 | |
michael@0 | 752 | // Avoid calling GetTop() if we have no outer window. Nothing will break if |
michael@0 | 753 | // we do, but it will spew debug output, which can cause our test logs to |
michael@0 | 754 | // overflow. |
michael@0 | 755 | nsCOMPtr<nsIDOMWindow> top; |
michael@0 | 756 | if (window->GetOuterWindow()) { |
michael@0 | 757 | window->GetTop(getter_AddRefs(top)); |
michael@0 | 758 | } |
michael@0 | 759 | |
michael@0 | 760 | if (top) { |
michael@0 | 761 | // The window is no longer detached, so we no longer want to track it. |
michael@0 | 762 | return PL_DHASH_REMOVE; |
michael@0 | 763 | } |
michael@0 | 764 | |
michael@0 | 765 | nsCOMPtr<nsIURI> uri = GetWindowURI(window); |
michael@0 | 766 | |
michael@0 | 767 | nsAutoCString domain; |
michael@0 | 768 | if (uri) { |
michael@0 | 769 | // GetBaseDomain works fine if |uri| is null, but it outputs a warning |
michael@0 | 770 | // which ends up overrunning the mochitest logs. |
michael@0 | 771 | data->tldService->GetBaseDomain(uri, 0, domain); |
michael@0 | 772 | } |
michael@0 | 773 | |
michael@0 | 774 | if (data->nonDetachedDomains->Contains(domain)) { |
michael@0 | 775 | // This window shares a domain with a non-detached window, so reset its |
michael@0 | 776 | // clock. |
michael@0 | 777 | aTimeStamp = TimeStamp(); |
michael@0 | 778 | } else { |
michael@0 | 779 | // This window does not share a domain with a non-detached window, so it |
michael@0 | 780 | // meets ghost criterion (2). |
michael@0 | 781 | if (aTimeStamp.IsNull()) { |
michael@0 | 782 | // This may become a ghost window later; start its clock. |
michael@0 | 783 | aTimeStamp = data->now; |
michael@0 | 784 | } else if ((data->now - aTimeStamp).ToSeconds() > data->ghostTimeout) { |
michael@0 | 785 | // This definitely is a ghost window, so add it to ghostWindowIDs, if |
michael@0 | 786 | // that is not null. |
michael@0 | 787 | if (data->ghostWindowIDs) { |
michael@0 | 788 | nsCOMPtr<nsPIDOMWindow> pWindow = do_QueryInterface(window); |
michael@0 | 789 | if (pWindow) { |
michael@0 | 790 | data->ghostWindowIDs->PutEntry(pWindow->WindowID()); |
michael@0 | 791 | } |
michael@0 | 792 | } |
michael@0 | 793 | } |
michael@0 | 794 | } |
michael@0 | 795 | |
michael@0 | 796 | return PL_DHASH_NEXT; |
michael@0 | 797 | } |
michael@0 | 798 | |
michael@0 | 799 | struct GetNonDetachedWindowDomainsEnumeratorData |
michael@0 | 800 | { |
michael@0 | 801 | nsTHashtable<nsCStringHashKey> *nonDetachedDomains; |
michael@0 | 802 | nsIEffectiveTLDService *tldService; |
michael@0 | 803 | }; |
michael@0 | 804 | |
michael@0 | 805 | static PLDHashOperator |
michael@0 | 806 | GetNonDetachedWindowDomainsEnumerator(const uint64_t& aId, nsGlobalWindow* aWindow, |
michael@0 | 807 | void* aClosure) |
michael@0 | 808 | { |
michael@0 | 809 | GetNonDetachedWindowDomainsEnumeratorData *data = |
michael@0 | 810 | static_cast<GetNonDetachedWindowDomainsEnumeratorData*>(aClosure); |
michael@0 | 811 | |
michael@0 | 812 | // Null outer window implies null top, but calling GetTop() when there's no |
michael@0 | 813 | // outer window causes us to spew debug warnings. |
michael@0 | 814 | if (!aWindow->GetOuterWindow() || !aWindow->GetTop()) { |
michael@0 | 815 | // This window is detached, so we don't care about its domain. |
michael@0 | 816 | return PL_DHASH_NEXT; |
michael@0 | 817 | } |
michael@0 | 818 | |
michael@0 | 819 | nsCOMPtr<nsIURI> uri = GetWindowURI(aWindow); |
michael@0 | 820 | |
michael@0 | 821 | nsAutoCString domain; |
michael@0 | 822 | if (uri) { |
michael@0 | 823 | data->tldService->GetBaseDomain(uri, 0, domain); |
michael@0 | 824 | } |
michael@0 | 825 | |
michael@0 | 826 | data->nonDetachedDomains->PutEntry(domain); |
michael@0 | 827 | return PL_DHASH_NEXT; |
michael@0 | 828 | } |
michael@0 | 829 | |
michael@0 | 830 | /** |
michael@0 | 831 | * Iterate over mDetachedWindows and update it to reflect the current state of |
michael@0 | 832 | * the world. In particular: |
michael@0 | 833 | * |
michael@0 | 834 | * - Remove weak refs to windows which no longer exist. |
michael@0 | 835 | * |
michael@0 | 836 | * - Remove references to windows which are no longer detached. |
michael@0 | 837 | * |
michael@0 | 838 | * - Reset the timestamp on detached windows which share a domain with a |
michael@0 | 839 | * non-detached window (they no longer meet ghost criterion (2)). |
michael@0 | 840 | * |
michael@0 | 841 | * - If a window now meets ghost criterion (2) but didn't before, set its |
michael@0 | 842 | * timestamp to now. |
michael@0 | 843 | * |
michael@0 | 844 | * Additionally, if aOutGhostIDs is not null, fill it with the window IDs of |
michael@0 | 845 | * all ghost windows we found. |
michael@0 | 846 | */ |
michael@0 | 847 | void |
michael@0 | 848 | nsWindowMemoryReporter::CheckForGhostWindows( |
michael@0 | 849 | nsTHashtable<nsUint64HashKey> *aOutGhostIDs /* = nullptr */) |
michael@0 | 850 | { |
michael@0 | 851 | nsCOMPtr<nsIEffectiveTLDService> tldService = do_GetService( |
michael@0 | 852 | NS_EFFECTIVETLDSERVICE_CONTRACTID); |
michael@0 | 853 | if (!tldService) { |
michael@0 | 854 | NS_WARNING("Couldn't get TLDService."); |
michael@0 | 855 | return; |
michael@0 | 856 | } |
michael@0 | 857 | |
michael@0 | 858 | nsGlobalWindow::WindowByIdTable *windowsById = |
michael@0 | 859 | nsGlobalWindow::GetWindowsTable(); |
michael@0 | 860 | if (!windowsById) { |
michael@0 | 861 | NS_WARNING("GetWindowsTable returned null"); |
michael@0 | 862 | return; |
michael@0 | 863 | } |
michael@0 | 864 | |
michael@0 | 865 | mLastCheckForGhostWindows = TimeStamp::NowLoRes(); |
michael@0 | 866 | KillCheckTimer(); |
michael@0 | 867 | |
michael@0 | 868 | nsTHashtable<nsCStringHashKey> nonDetachedWindowDomains; |
michael@0 | 869 | |
michael@0 | 870 | // Populate nonDetachedWindowDomains. |
michael@0 | 871 | GetNonDetachedWindowDomainsEnumeratorData nonDetachedEnumData = |
michael@0 | 872 | { &nonDetachedWindowDomains, tldService }; |
michael@0 | 873 | windowsById->EnumerateRead(GetNonDetachedWindowDomainsEnumerator, |
michael@0 | 874 | &nonDetachedEnumData); |
michael@0 | 875 | |
michael@0 | 876 | // Update mDetachedWindows and write the ghost window IDs into aOutGhostIDs, |
michael@0 | 877 | // if it's not null. |
michael@0 | 878 | CheckForGhostWindowsEnumeratorData ghostEnumData = |
michael@0 | 879 | { &nonDetachedWindowDomains, aOutGhostIDs, tldService, |
michael@0 | 880 | GetGhostTimeout(), mLastCheckForGhostWindows }; |
michael@0 | 881 | mDetachedWindows.Enumerate(CheckForGhostWindowsEnumerator, |
michael@0 | 882 | &ghostEnumData); |
michael@0 | 883 | } |
michael@0 | 884 | |
michael@0 | 885 | NS_IMPL_ISUPPORTS(nsWindowMemoryReporter::GhostWindowsReporter, |
michael@0 | 886 | nsIMemoryReporter) |
michael@0 | 887 | |
michael@0 | 888 | /* static */ int64_t |
michael@0 | 889 | nsWindowMemoryReporter::GhostWindowsReporter::DistinguishedAmount() |
michael@0 | 890 | { |
michael@0 | 891 | nsTHashtable<nsUint64HashKey> ghostWindows; |
michael@0 | 892 | sWindowReporter->CheckForGhostWindows(&ghostWindows); |
michael@0 | 893 | return ghostWindows.Count(); |
michael@0 | 894 | } |
michael@0 | 895 | |
michael@0 | 896 | void |
michael@0 | 897 | nsWindowMemoryReporter::KillCheckTimer() |
michael@0 | 898 | { |
michael@0 | 899 | if (mCheckTimer) { |
michael@0 | 900 | mCheckTimer->Cancel(); |
michael@0 | 901 | mCheckTimer = nullptr; |
michael@0 | 902 | } |
michael@0 | 903 | } |
michael@0 | 904 | |
michael@0 | 905 | #ifdef DEBUG |
michael@0 | 906 | static PLDHashOperator |
michael@0 | 907 | UnlinkGhostWindowsEnumerator(nsUint64HashKey* aIDHashKey, void *) |
michael@0 | 908 | { |
michael@0 | 909 | nsGlobalWindow::WindowByIdTable* windowsById = |
michael@0 | 910 | nsGlobalWindow::GetWindowsTable(); |
michael@0 | 911 | if (!windowsById) { |
michael@0 | 912 | return PL_DHASH_NEXT; |
michael@0 | 913 | } |
michael@0 | 914 | |
michael@0 | 915 | nsRefPtr<nsGlobalWindow> window = windowsById->Get(aIDHashKey->GetKey()); |
michael@0 | 916 | if (window) { |
michael@0 | 917 | window->RiskyUnlink(); |
michael@0 | 918 | } |
michael@0 | 919 | |
michael@0 | 920 | return PL_DHASH_NEXT; |
michael@0 | 921 | } |
michael@0 | 922 | |
michael@0 | 923 | /* static */ void |
michael@0 | 924 | nsWindowMemoryReporter::UnlinkGhostWindows() |
michael@0 | 925 | { |
michael@0 | 926 | if (!sWindowReporter) { |
michael@0 | 927 | return; |
michael@0 | 928 | } |
michael@0 | 929 | |
michael@0 | 930 | nsGlobalWindow::WindowByIdTable* windowsById = |
michael@0 | 931 | nsGlobalWindow::GetWindowsTable(); |
michael@0 | 932 | if (!windowsById) { |
michael@0 | 933 | return; |
michael@0 | 934 | } |
michael@0 | 935 | |
michael@0 | 936 | // Hold on to every window in memory so that window objects can't be |
michael@0 | 937 | // destroyed while we're calling the UnlinkGhostWindows callback. |
michael@0 | 938 | WindowArray windows; |
michael@0 | 939 | windowsById->Enumerate(GetWindows, &windows); |
michael@0 | 940 | |
michael@0 | 941 | // Get the IDs of all the "ghost" windows, and unlink them all. |
michael@0 | 942 | nsTHashtable<nsUint64HashKey> ghostWindows; |
michael@0 | 943 | sWindowReporter->CheckForGhostWindows(&ghostWindows); |
michael@0 | 944 | ghostWindows.EnumerateEntries(UnlinkGhostWindowsEnumerator, nullptr); |
michael@0 | 945 | } |
michael@0 | 946 | #endif |