2013-01-18 05:21:43 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-06-29 11:01:07 +00:00
|
|
|
|
2013-06-15 02:48:28 +00:00
|
|
|
#include "amIAddonManager.h"
|
2012-02-24 02:43:33 +00:00
|
|
|
#include "nsWindowMemoryReporter.h"
|
2011-06-29 11:06:43 +00:00
|
|
|
#include "nsGlobalWindow.h"
|
2013-03-22 00:05:20 +00:00
|
|
|
#include "nsIDocument.h"
|
2013-10-22 21:53:26 +00:00
|
|
|
#include "nsIDOMWindowCollection.h"
|
2012-04-03 02:28:04 +00:00
|
|
|
#include "nsIEffectiveTLDService.h"
|
2013-09-19 02:01:29 +00:00
|
|
|
#include "mozilla/ClearOnShutdown.h"
|
2013-09-25 03:31:00 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-09-19 02:01:29 +00:00
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "mozilla/StaticPtr.h"
|
2012-04-03 02:28:04 +00:00
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsPrintfCString.h"
|
2012-07-06 04:12:37 +00:00
|
|
|
#include "XPCJSMemoryReporter.h"
|
2013-08-13 17:51:50 +00:00
|
|
|
#include "js/MemoryMetrics.h"
|
2013-08-15 18:17:48 +00:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2011-06-29 11:01:07 +00:00
|
|
|
|
2012-04-03 02:28:04 +00:00
|
|
|
using namespace mozilla;
|
2011-06-29 11:01:07 +00:00
|
|
|
|
2013-09-19 02:01:29 +00:00
|
|
|
StaticRefPtr<nsWindowMemoryReporter> sWindowReporter;
|
|
|
|
|
2014-03-08 13:38:52 +00:00
|
|
|
/**
|
|
|
|
* Don't trigger a ghost window check when a DOM window is detached if we've
|
|
|
|
* run it this recently.
|
|
|
|
*/
|
|
|
|
const int32_t kTimeBetweenChecks = 45; /* seconds */
|
|
|
|
|
2012-02-24 02:43:33 +00:00
|
|
|
nsWindowMemoryReporter::nsWindowMemoryReporter()
|
2014-03-08 13:38:53 +00:00
|
|
|
: mLastCheckForGhostWindows(TimeStamp::NowLoRes()),
|
|
|
|
mCycleCollectorIsRunning(false),
|
|
|
|
mCheckTimerWaitingForCCEnd(false)
|
2014-03-08 13:38:52 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsWindowMemoryReporter::~nsWindowMemoryReporter()
|
2011-06-29 11:01:07 +00:00
|
|
|
{
|
2014-03-08 13:38:52 +00:00
|
|
|
KillCheckTimer();
|
2011-06-29 11:01:07 +00:00
|
|
|
}
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsWindowMemoryReporter, nsIMemoryReporter, nsIObserver,
|
|
|
|
nsISupportsWeakReference)
|
2011-06-29 11:01:07 +00:00
|
|
|
|
2013-10-22 21:53:26 +00:00
|
|
|
static nsresult
|
|
|
|
AddNonJSSizeOfWindowAndItsDescendents(nsGlobalWindow* aWindow,
|
|
|
|
nsTabSizes* aSizes)
|
|
|
|
{
|
|
|
|
// Measure the window.
|
|
|
|
nsWindowSizes windowSizes(moz_malloc_size_of);
|
|
|
|
aWindow->AddSizeOfIncludingThis(&windowSizes);
|
|
|
|
windowSizes.addToTabSizes(aSizes);
|
|
|
|
|
|
|
|
// Measure the inner window, if there is one.
|
|
|
|
nsWindowSizes innerWindowSizes(moz_malloc_size_of);
|
|
|
|
nsGlobalWindow* inner = aWindow->GetCurrentInnerWindowInternal();
|
|
|
|
if (inner) {
|
|
|
|
inner->AddSizeOfIncludingThis(&innerWindowSizes);
|
|
|
|
innerWindowSizes.addToTabSizes(aSizes);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMWindowCollection> frames;
|
|
|
|
nsresult rv = aWindow->GetFrames(getter_AddRefs(frames));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
uint32_t length;
|
|
|
|
rv = frames->GetLength(&length);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Measure this window's descendents.
|
|
|
|
for (uint32_t i = 0; i < length; i++) {
|
|
|
|
nsCOMPtr<nsIDOMWindow> child;
|
|
|
|
rv = frames->Item(i, getter_AddRefs(child));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_STATE(child);
|
|
|
|
|
|
|
|
nsGlobalWindow* childWin =
|
|
|
|
static_cast<nsGlobalWindow*>(static_cast<nsIDOMWindow *>(child.get()));
|
|
|
|
|
|
|
|
rv = AddNonJSSizeOfWindowAndItsDescendents(childWin, aSizes);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
NonJSSizeOfTab(nsPIDOMWindow* aWindow, size_t* aDomSize, size_t* aStyleSize, size_t* aOtherSize)
|
|
|
|
{
|
|
|
|
nsGlobalWindow* window = static_cast<nsGlobalWindow*>(aWindow);
|
|
|
|
|
|
|
|
nsTabSizes sizes;
|
|
|
|
nsresult rv = AddNonJSSizeOfWindowAndItsDescendents(window, &sizes);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
*aDomSize = sizes.mDom;
|
|
|
|
*aStyleSize = sizes.mStyle;
|
|
|
|
*aOtherSize = sizes.mOther;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
2012-02-24 02:43:33 +00:00
|
|
|
nsWindowMemoryReporter::Init()
|
2011-06-29 11:01:07 +00:00
|
|
|
{
|
2013-09-19 02:01:29 +00:00
|
|
|
MOZ_ASSERT(!sWindowReporter);
|
|
|
|
sWindowReporter = new nsWindowMemoryReporter();
|
|
|
|
ClearOnShutdown(&sWindowReporter);
|
2013-11-07 05:35:30 +00:00
|
|
|
RegisterStrongMemoryReporter(sWindowReporter);
|
2013-10-22 21:53:26 +00:00
|
|
|
RegisterNonJSSizeOfTab(NonJSSizeOfTab);
|
2012-04-03 02:28:04 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIObserverService> os = services::GetObserverService();
|
|
|
|
if (os) {
|
|
|
|
// DOM_WINDOW_DESTROYED_TOPIC announces what we call window "detachment",
|
2013-10-28 14:04:12 +00:00
|
|
|
// when a window's docshell is set to nullptr.
|
2013-09-19 02:01:29 +00:00
|
|
|
os->AddObserver(sWindowReporter, DOM_WINDOW_DESTROYED_TOPIC,
|
2012-04-03 02:28:05 +00:00
|
|
|
/* weakRef = */ true);
|
2013-09-19 02:01:29 +00:00
|
|
|
os->AddObserver(sWindowReporter, "after-minimize-memory-usage",
|
2012-04-03 02:28:05 +00:00
|
|
|
/* weakRef = */ true);
|
2014-03-08 13:38:53 +00:00
|
|
|
os->AddObserver(sWindowReporter, "cycle-collector-begin",
|
|
|
|
/* weakRef = */ true);
|
|
|
|
os->AddObserver(sWindowReporter, "cycle-collector-end",
|
|
|
|
/* weakRef = */ true);
|
2012-04-03 02:28:04 +00:00
|
|
|
}
|
2012-04-03 02:28:05 +00:00
|
|
|
|
2013-11-07 05:35:30 +00:00
|
|
|
RegisterStrongMemoryReporter(new GhostWindowsReporter());
|
2013-09-19 22:52:30 +00:00
|
|
|
RegisterGhostWindowsDistinguishedAmount(GhostWindowsReporter::DistinguishedAmount);
|
2011-06-29 11:01:07 +00:00
|
|
|
}
|
|
|
|
|
2012-04-03 02:28:04 +00:00
|
|
|
static already_AddRefed<nsIURI>
|
|
|
|
GetWindowURI(nsIDOMWindow *aWindow)
|
2011-06-29 11:01:07 +00:00
|
|
|
{
|
2012-04-03 02:28:04 +00:00
|
|
|
nsCOMPtr<nsPIDOMWindow> pWindow = do_QueryInterface(aWindow);
|
2013-04-28 11:52:10 +00:00
|
|
|
NS_ENSURE_TRUE(pWindow, nullptr);
|
2012-04-03 02:28:04 +00:00
|
|
|
|
2013-04-24 04:22:37 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc = pWindow->GetExtantDoc();
|
2011-12-09 05:42:20 +00:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
2011-06-29 11:01:07 +00:00
|
|
|
|
2011-12-09 05:42:20 +00:00
|
|
|
if (doc) {
|
|
|
|
uri = doc->GetDocumentURI();
|
|
|
|
}
|
2011-06-29 11:01:07 +00:00
|
|
|
|
2011-12-09 05:42:20 +00:00
|
|
|
if (!uri) {
|
2012-04-03 02:28:04 +00:00
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> scriptObjPrincipal =
|
|
|
|
do_QueryInterface(aWindow);
|
2013-04-28 11:52:10 +00:00
|
|
|
NS_ENSURE_TRUE(scriptObjPrincipal, nullptr);
|
2012-04-03 02:28:04 +00:00
|
|
|
|
2012-10-09 19:17:53 +00:00
|
|
|
// GetPrincipal() will print a warning if the window does not have an outer
|
|
|
|
// window, so check here for an outer window first. This code is
|
|
|
|
// functionally correct if we leave out the GetOuterWindow() check, but we
|
|
|
|
// end up printing a lot of warnings during debug mochitests.
|
|
|
|
if (pWindow->GetOuterWindow()) {
|
|
|
|
nsIPrincipal* principal = scriptObjPrincipal->GetPrincipal();
|
|
|
|
if (principal) {
|
|
|
|
principal->GetURI(getter_AddRefs(uri));
|
|
|
|
}
|
2011-12-09 05:42:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-03 02:28:04 +00:00
|
|
|
return uri.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2014-05-21 06:06:54 +00:00
|
|
|
AppendWindowURI(nsGlobalWindow *aWindow, nsACString& aStr, bool aAnonymize)
|
2012-04-03 02:28:04 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIURI> uri = GetWindowURI(aWindow);
|
|
|
|
|
2012-03-13 06:00:18 +00:00
|
|
|
if (uri) {
|
2014-05-21 06:06:54 +00:00
|
|
|
if (aAnonymize && !aWindow->IsChromeWindow()) {
|
|
|
|
aStr.AppendPrintf("<anonymized-%d>", aWindow->WindowID());
|
|
|
|
} else {
|
|
|
|
nsCString spec;
|
|
|
|
uri->GetSpec(spec);
|
2011-12-09 05:42:20 +00:00
|
|
|
|
2014-05-21 06:06:54 +00:00
|
|
|
// A hack: replace forward slashes with '\\' so they aren't
|
|
|
|
// treated as path separators. Users of the reporters
|
|
|
|
// (such as about:memory) have to undo this change.
|
|
|
|
spec.ReplaceChar('/', '\\');
|
2011-12-09 05:42:20 +00:00
|
|
|
|
2014-05-21 06:06:54 +00:00
|
|
|
aStr += spec;
|
|
|
|
}
|
2012-03-13 06:00:18 +00:00
|
|
|
} else {
|
2012-04-03 02:28:04 +00:00
|
|
|
// If we're unable to find a URI, we're dealing with a chrome window with
|
|
|
|
// no document in it (or somesuch), so we call this a "system window".
|
2012-03-13 06:00:18 +00:00
|
|
|
aStr += NS_LITERAL_CSTRING("[system]");
|
|
|
|
}
|
2011-06-29 11:01:07 +00:00
|
|
|
}
|
2011-06-29 11:06:43 +00:00
|
|
|
|
2013-12-08 05:38:32 +00:00
|
|
|
MOZ_DEFINE_MALLOC_SIZE_OF(WindowsMallocSizeOf)
|
2011-12-09 05:42:20 +00:00
|
|
|
|
2012-07-06 04:12:37 +00:00
|
|
|
// The key is the window ID.
|
|
|
|
typedef nsDataHashtable<nsUint64HashKey, nsCString> WindowPaths;
|
|
|
|
|
2014-01-10 15:15:53 +00:00
|
|
|
static nsresult
|
|
|
|
ReportAmount(const nsCString& aBasePath, const char* aPathTail,
|
|
|
|
size_t aAmount, const nsCString& aDescription,
|
|
|
|
uint32_t aKind, uint32_t aUnits,
|
|
|
|
nsIMemoryReporterCallback* aCb,
|
|
|
|
nsISupports* aClosure)
|
|
|
|
{
|
|
|
|
if (aAmount == 0) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString path(aBasePath);
|
|
|
|
path += aPathTail;
|
|
|
|
|
|
|
|
return aCb->Callback(EmptyCString(), path, aKind, aUnits,
|
|
|
|
aAmount, aDescription, aClosure);
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
ReportSize(const nsCString& aBasePath, const char* aPathTail,
|
|
|
|
size_t aAmount, const nsCString& aDescription,
|
|
|
|
nsIMemoryReporterCallback* aCb,
|
|
|
|
nsISupports* aClosure)
|
|
|
|
{
|
|
|
|
return ReportAmount(aBasePath, aPathTail, aAmount, aDescription,
|
|
|
|
nsIMemoryReporter::KIND_HEAP,
|
|
|
|
nsIMemoryReporter::UNITS_BYTES, aCb, aClosure);
|
|
|
|
}
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
ReportCount(const nsCString& aBasePath, const char* aPathTail,
|
|
|
|
size_t aAmount, const nsCString& aDescription,
|
|
|
|
nsIMemoryReporterCallback* aCb,
|
|
|
|
nsISupports* aClosure)
|
|
|
|
{
|
|
|
|
return ReportAmount(aBasePath, aPathTail, aAmount, aDescription,
|
|
|
|
nsIMemoryReporter::KIND_OTHER,
|
|
|
|
nsIMemoryReporter::UNITS_COUNT, aCb, aClosure);
|
|
|
|
}
|
|
|
|
|
2012-03-04 23:26:30 +00:00
|
|
|
static nsresult
|
2012-01-03 02:19:14 +00:00
|
|
|
CollectWindowReports(nsGlobalWindow *aWindow,
|
2013-06-15 02:48:28 +00:00
|
|
|
amIAddonManager *addonManager,
|
2012-02-21 15:10:11 +00:00
|
|
|
nsWindowSizes *aWindowTotalSizes,
|
2012-04-03 02:28:04 +00:00
|
|
|
nsTHashtable<nsUint64HashKey> *aGhostWindowIDs,
|
2012-07-06 04:12:37 +00:00
|
|
|
WindowPaths *aWindowPaths,
|
2013-03-17 03:36:37 +00:00
|
|
|
WindowPaths *aTopWindowPaths,
|
2013-08-27 23:24:51 +00:00
|
|
|
nsIMemoryReporterCallback *aCb,
|
2014-05-21 06:06:54 +00:00
|
|
|
nsISupports *aClosure,
|
|
|
|
bool aAnonymize)
|
2012-01-03 02:19:14 +00:00
|
|
|
{
|
2014-01-08 01:02:23 +00:00
|
|
|
nsAutoCString windowPath("explicit/");
|
2012-04-02 14:37:04 +00:00
|
|
|
|
2012-04-03 02:28:04 +00:00
|
|
|
// Avoid calling aWindow->GetTop() if there's no outer window. It will work
|
|
|
|
// just fine, but will spew a lot of warnings.
|
2013-10-28 14:04:12 +00:00
|
|
|
nsGlobalWindow *top = nullptr;
|
2013-06-15 02:48:28 +00:00
|
|
|
nsCOMPtr<nsIURI> location;
|
2012-04-03 02:28:04 +00:00
|
|
|
if (aWindow->GetOuterWindow()) {
|
|
|
|
// Our window should have a null top iff it has a null docshell.
|
|
|
|
MOZ_ASSERT(!!aWindow->GetTop() == !!aWindow->GetDocShell());
|
|
|
|
top = aWindow->GetTop();
|
2013-06-15 02:48:28 +00:00
|
|
|
if (top) {
|
|
|
|
location = GetWindowURI(top);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!location) {
|
|
|
|
location = GetWindowURI(aWindow);
|
|
|
|
}
|
|
|
|
|
2013-06-30 16:39:10 +00:00
|
|
|
if (addonManager && location) {
|
2013-06-15 02:48:28 +00:00
|
|
|
bool ok;
|
|
|
|
nsAutoCString id;
|
|
|
|
if (NS_SUCCEEDED(addonManager->MapURIToAddonID(location, id, &ok)) && ok) {
|
2014-05-21 06:06:54 +00:00
|
|
|
// Add-on names are not privacy-sensitive, so we can use them with
|
|
|
|
// impunity.
|
2013-06-15 02:48:28 +00:00
|
|
|
windowPath += NS_LITERAL_CSTRING("add-ons/") + id +
|
|
|
|
NS_LITERAL_CSTRING("/");
|
|
|
|
}
|
2012-04-03 02:28:04 +00:00
|
|
|
}
|
|
|
|
|
2013-06-15 02:48:28 +00:00
|
|
|
windowPath += NS_LITERAL_CSTRING("window-objects/");
|
|
|
|
|
2012-03-13 06:00:18 +00:00
|
|
|
if (top) {
|
2012-04-03 02:28:04 +00:00
|
|
|
windowPath += NS_LITERAL_CSTRING("top(");
|
2014-05-21 06:06:54 +00:00
|
|
|
AppendWindowURI(top, windowPath, aAnonymize);
|
2012-03-13 06:00:18 +00:00
|
|
|
windowPath += NS_LITERAL_CSTRING(", id=");
|
|
|
|
windowPath.AppendInt(top->WindowID());
|
2013-03-17 03:36:37 +00:00
|
|
|
windowPath += NS_LITERAL_CSTRING(")");
|
2011-12-09 05:42:20 +00:00
|
|
|
|
2013-03-17 03:36:37 +00:00
|
|
|
aTopWindowPaths->Put(aWindow->WindowID(), windowPath);
|
|
|
|
|
|
|
|
windowPath += aWindow->IsFrozen() ? NS_LITERAL_CSTRING("/cached/")
|
|
|
|
: NS_LITERAL_CSTRING("/active/");
|
2011-12-09 05:42:20 +00:00
|
|
|
} else {
|
2012-04-03 02:28:04 +00:00
|
|
|
if (aGhostWindowIDs->Contains(aWindow->WindowID())) {
|
|
|
|
windowPath += NS_LITERAL_CSTRING("top(none)/ghost/");
|
|
|
|
} else {
|
|
|
|
windowPath += NS_LITERAL_CSTRING("top(none)/detached/");
|
|
|
|
}
|
2011-12-09 05:42:20 +00:00
|
|
|
}
|
|
|
|
|
2012-03-13 06:00:18 +00:00
|
|
|
windowPath += NS_LITERAL_CSTRING("window(");
|
2014-05-21 06:06:54 +00:00
|
|
|
AppendWindowURI(aWindow, windowPath, aAnonymize);
|
2012-03-13 06:00:18 +00:00
|
|
|
windowPath += NS_LITERAL_CSTRING(")");
|
|
|
|
|
2014-01-08 01:02:23 +00:00
|
|
|
// Use |windowPath|, but replace "explicit/" with "event-counts/".
|
|
|
|
nsCString censusWindowPath(windowPath);
|
|
|
|
censusWindowPath.Replace(0, strlen("explicit"), "event-counts");
|
2013-10-28 09:47:18 +00:00
|
|
|
|
2012-07-06 04:12:37 +00:00
|
|
|
// Remember the path for later.
|
2014-01-08 01:02:23 +00:00
|
|
|
aWindowPaths->Put(aWindow->WindowID(), windowPath);
|
2012-07-06 04:12:37 +00:00
|
|
|
|
2013-10-28 09:47:18 +00:00
|
|
|
#define REPORT_SIZE(_pathTail, _amount, _desc) \
|
2012-02-21 15:10:11 +00:00
|
|
|
do { \
|
2014-01-10 15:15:53 +00:00
|
|
|
nsresult rv = ReportSize(windowPath, _pathTail, _amount, \
|
|
|
|
NS_LITERAL_CSTRING(_desc), aCb, aClosure); \
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv); \
|
2013-10-28 09:47:18 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define REPORT_COUNT(_pathTail, _amount, _desc) \
|
|
|
|
do { \
|
2014-01-10 15:15:53 +00:00
|
|
|
nsresult rv = ReportCount(censusWindowPath, _pathTail, _amount, \
|
|
|
|
NS_LITERAL_CSTRING(_desc), aCb, aClosure); \
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv); \
|
2012-02-21 15:10:11 +00:00
|
|
|
} while (0)
|
|
|
|
|
2012-12-24 00:59:51 +00:00
|
|
|
nsWindowSizes windowSizes(WindowsMallocSizeOf);
|
2013-09-30 23:20:23 +00:00
|
|
|
aWindow->AddSizeOfIncludingThis(&windowSizes);
|
2012-03-13 06:00:18 +00:00
|
|
|
|
2013-10-28 09:47:18 +00:00
|
|
|
REPORT_SIZE("/dom/element-nodes", windowSizes.mDOMElementNodesSize,
|
|
|
|
"Memory used by the element nodes in a window's DOM.");
|
|
|
|
aWindowTotalSizes->mDOMElementNodesSize += windowSizes.mDOMElementNodesSize;
|
|
|
|
|
|
|
|
REPORT_SIZE("/dom/text-nodes", windowSizes.mDOMTextNodesSize,
|
|
|
|
"Memory used by the text nodes in a window's DOM.");
|
|
|
|
aWindowTotalSizes->mDOMTextNodesSize += windowSizes.mDOMTextNodesSize;
|
|
|
|
|
|
|
|
REPORT_SIZE("/dom/cdata-nodes", windowSizes.mDOMCDATANodesSize,
|
|
|
|
"Memory used by the CDATA nodes in a window's DOM.");
|
|
|
|
aWindowTotalSizes->mDOMCDATANodesSize += windowSizes.mDOMCDATANodesSize;
|
|
|
|
|
|
|
|
REPORT_SIZE("/dom/comment-nodes", windowSizes.mDOMCommentNodesSize,
|
|
|
|
"Memory used by the comment nodes in a window's DOM.");
|
|
|
|
aWindowTotalSizes->mDOMCommentNodesSize += windowSizes.mDOMCommentNodesSize;
|
|
|
|
|
|
|
|
REPORT_SIZE("/dom/event-targets", windowSizes.mDOMEventTargetsSize,
|
|
|
|
"Memory used by the event targets table in a window's DOM, and "
|
|
|
|
"the objects it points to, which include XHRs.");
|
|
|
|
aWindowTotalSizes->mDOMEventTargetsSize += windowSizes.mDOMEventTargetsSize;
|
|
|
|
|
|
|
|
REPORT_COUNT("/dom/event-targets", windowSizes.mDOMEventTargetsCount,
|
|
|
|
"Number of non-node event targets in the event targets table "
|
|
|
|
"in a window's DOM, such as XHRs.");
|
|
|
|
aWindowTotalSizes->mDOMEventTargetsCount +=
|
|
|
|
windowSizes.mDOMEventTargetsCount;
|
|
|
|
|
|
|
|
REPORT_COUNT("/dom/event-listeners", windowSizes.mDOMEventListenersCount,
|
|
|
|
"Number of event listeners in a window, including event "
|
|
|
|
"listeners on nodes and other event targets.");
|
|
|
|
aWindowTotalSizes->mDOMEventListenersCount +=
|
|
|
|
windowSizes.mDOMEventListenersCount;
|
|
|
|
|
|
|
|
REPORT_SIZE("/dom/other", windowSizes.mDOMOtherSize,
|
|
|
|
"Memory used by a window's DOM that isn't measured by the "
|
|
|
|
"other 'dom/' numbers.");
|
|
|
|
aWindowTotalSizes->mDOMOtherSize += windowSizes.mDOMOtherSize;
|
|
|
|
|
|
|
|
REPORT_SIZE("/property-tables",
|
|
|
|
windowSizes.mPropertyTablesSize,
|
|
|
|
"Memory used for the property tables within a window.");
|
|
|
|
aWindowTotalSizes->mPropertyTablesSize += windowSizes.mPropertyTablesSize;
|
|
|
|
|
|
|
|
REPORT_SIZE("/style-sheets", windowSizes.mStyleSheetsSize,
|
|
|
|
"Memory used by style sheets within a window.");
|
|
|
|
aWindowTotalSizes->mStyleSheetsSize += windowSizes.mStyleSheetsSize;
|
|
|
|
|
|
|
|
REPORT_SIZE("/layout/pres-shell", windowSizes.mLayoutPresShellSize,
|
|
|
|
"Memory used by layout's PresShell, along with any structures "
|
|
|
|
"allocated in its arena and not measured elsewhere, "
|
|
|
|
"within a window.");
|
|
|
|
aWindowTotalSizes->mLayoutPresShellSize += windowSizes.mLayoutPresShellSize;
|
|
|
|
|
|
|
|
REPORT_SIZE("/layout/line-boxes", windowSizes.mArenaStats.mLineBoxes,
|
|
|
|
"Memory used by line boxes within a window.");
|
2012-06-06 17:35:40 +00:00
|
|
|
aWindowTotalSizes->mArenaStats.mLineBoxes
|
|
|
|
+= windowSizes.mArenaStats.mLineBoxes;
|
|
|
|
|
2013-10-28 09:47:18 +00:00
|
|
|
REPORT_SIZE("/layout/rule-nodes", windowSizes.mArenaStats.mRuleNodes,
|
|
|
|
"Memory used by CSS rule nodes within a window.");
|
2012-06-06 17:35:40 +00:00
|
|
|
aWindowTotalSizes->mArenaStats.mRuleNodes
|
|
|
|
+= windowSizes.mArenaStats.mRuleNodes;
|
|
|
|
|
2013-10-28 09:47:18 +00:00
|
|
|
REPORT_SIZE("/layout/style-contexts", windowSizes.mArenaStats.mStyleContexts,
|
|
|
|
"Memory used by style contexts within a window.");
|
2012-06-06 17:35:40 +00:00
|
|
|
aWindowTotalSizes->mArenaStats.mStyleContexts
|
|
|
|
+= windowSizes.mArenaStats.mStyleContexts;
|
|
|
|
|
2013-10-28 09:47:18 +00:00
|
|
|
REPORT_SIZE("/layout/style-sets", windowSizes.mLayoutStyleSetsSize,
|
|
|
|
"Memory used by style sets within a window.");
|
|
|
|
aWindowTotalSizes->mLayoutStyleSetsSize += windowSizes.mLayoutStyleSetsSize;
|
2012-02-21 15:10:11 +00:00
|
|
|
|
2013-10-28 09:47:18 +00:00
|
|
|
REPORT_SIZE("/layout/text-runs", windowSizes.mLayoutTextRunsSize,
|
|
|
|
"Memory used for text-runs (glyph layout) in the PresShell's "
|
|
|
|
"frame tree, within a window.");
|
|
|
|
aWindowTotalSizes->mLayoutTextRunsSize += windowSizes.mLayoutTextRunsSize;
|
2012-02-21 15:10:11 +00:00
|
|
|
|
2013-10-28 09:47:18 +00:00
|
|
|
REPORT_SIZE("/layout/pres-contexts", windowSizes.mLayoutPresContextSize,
|
2012-05-07 20:09:11 +00:00
|
|
|
"Memory used for the PresContext in the PresShell's frame "
|
|
|
|
"within a window.");
|
2013-10-28 09:47:18 +00:00
|
|
|
aWindowTotalSizes->mLayoutPresContextSize +=
|
|
|
|
windowSizes.mLayoutPresContextSize;
|
2012-05-07 20:09:11 +00:00
|
|
|
|
2012-06-06 17:29:16 +00:00
|
|
|
// There are many different kinds of frames, but it is very likely
|
|
|
|
// that only a few matter. Implement a cutoff so we don't bloat
|
|
|
|
// about:memory with many uninteresting entries.
|
2012-10-17 14:44:31 +00:00
|
|
|
const size_t FRAME_SUNDRIES_THRESHOLD =
|
|
|
|
js::MemoryReportingSundriesThreshold();
|
|
|
|
|
2012-06-06 17:29:16 +00:00
|
|
|
size_t frameSundriesSize = 0;
|
|
|
|
#define FRAME_ID(classname) \
|
|
|
|
{ \
|
|
|
|
size_t frameSize \
|
|
|
|
= windowSizes.mArenaStats.FRAME_ID_STAT_FIELD(classname); \
|
|
|
|
if (frameSize < FRAME_SUNDRIES_THRESHOLD) { \
|
|
|
|
frameSundriesSize += frameSize; \
|
|
|
|
} else { \
|
2013-10-28 09:47:18 +00:00
|
|
|
REPORT_SIZE("/layout/frames/" # classname, frameSize, \
|
|
|
|
"Memory used by frames of " \
|
|
|
|
"type " #classname " within a window."); \
|
2012-06-06 17:29:16 +00:00
|
|
|
} \
|
2012-06-26 00:06:50 +00:00
|
|
|
aWindowTotalSizes->mArenaStats.FRAME_ID_STAT_FIELD(classname) \
|
|
|
|
+= frameSize; \
|
2012-06-06 17:29:16 +00:00
|
|
|
}
|
|
|
|
#include "nsFrameIdList.h"
|
|
|
|
#undef FRAME_ID
|
|
|
|
|
|
|
|
if (frameSundriesSize > 0) {
|
2013-10-28 09:47:18 +00:00
|
|
|
REPORT_SIZE("/layout/frames/sundries", frameSundriesSize,
|
|
|
|
"The sum of all memory used by frames which were too small "
|
|
|
|
"to be shown individually.");
|
2012-06-06 17:29:16 +00:00
|
|
|
}
|
2013-01-18 05:21:43 +00:00
|
|
|
|
2013-10-28 09:47:18 +00:00
|
|
|
#undef REPORT_SIZE
|
|
|
|
#undef REPORT_COUNT
|
2012-03-04 23:26:30 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2011-06-29 12:29:29 +00:00
|
|
|
}
|
|
|
|
|
2011-12-09 05:42:20 +00:00
|
|
|
typedef nsTArray< nsRefPtr<nsGlobalWindow> > WindowArray;
|
|
|
|
|
2011-06-29 11:06:43 +00:00
|
|
|
static
|
|
|
|
PLDHashOperator
|
2012-08-22 15:56:38 +00:00
|
|
|
GetWindows(const uint64_t& aId, nsGlobalWindow*& aWindow, void* aClosure)
|
2011-06-29 11:06:43 +00:00
|
|
|
{
|
2011-12-09 05:42:20 +00:00
|
|
|
((WindowArray *)aClosure)->AppendElement(aWindow);
|
|
|
|
|
2011-06-29 11:06:43 +00:00
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2013-09-19 02:01:29 +00:00
|
|
|
struct ReportGhostWindowsEnumeratorData
|
|
|
|
{
|
2014-05-21 06:06:54 +00:00
|
|
|
nsIMemoryReporterCallback* mCallback;
|
|
|
|
nsISupports* mData;
|
|
|
|
bool mAnonymize;
|
|
|
|
nsresult mRv;
|
2013-09-19 02:01:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static PLDHashOperator
|
2014-05-21 06:06:54 +00:00
|
|
|
ReportGhostWindowsEnumerator(nsUint64HashKey* aIDHashKey, void* aData)
|
2013-09-19 02:01:29 +00:00
|
|
|
{
|
|
|
|
ReportGhostWindowsEnumeratorData *data =
|
2014-05-21 06:06:54 +00:00
|
|
|
static_cast<ReportGhostWindowsEnumeratorData*>(aData);
|
2013-09-19 02:01:29 +00:00
|
|
|
|
|
|
|
nsGlobalWindow::WindowByIdTable* windowsById =
|
|
|
|
nsGlobalWindow::GetWindowsTable();
|
|
|
|
if (!windowsById) {
|
|
|
|
NS_WARNING("Couldn't get window-by-id hashtable?");
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsGlobalWindow* window = windowsById->Get(aIDHashKey->GetKey());
|
|
|
|
if (!window) {
|
|
|
|
NS_WARNING("Could not look up window?");
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoCString path;
|
|
|
|
path.AppendLiteral("ghost-windows/");
|
2014-05-21 06:06:54 +00:00
|
|
|
AppendWindowURI(window, path, data->mAnonymize);
|
2013-09-19 02:01:29 +00:00
|
|
|
|
2014-05-21 06:06:54 +00:00
|
|
|
nsresult rv = data->mCallback->Callback(
|
2013-09-19 02:01:29 +00:00
|
|
|
/* process = */ EmptyCString(),
|
|
|
|
path,
|
|
|
|
nsIMemoryReporter::KIND_OTHER,
|
|
|
|
nsIMemoryReporter::UNITS_COUNT,
|
|
|
|
/* amount = */ 1,
|
|
|
|
/* description = */ NS_LITERAL_CSTRING("A ghost window."),
|
2014-05-21 06:06:54 +00:00
|
|
|
data->mData);
|
2013-09-19 02:01:29 +00:00
|
|
|
|
2014-05-21 06:06:54 +00:00
|
|
|
if (NS_FAILED(rv) && NS_SUCCEEDED(data->mRv)) {
|
|
|
|
data->mRv = rv;
|
2013-09-19 02:01:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2011-06-29 11:01:07 +00:00
|
|
|
NS_IMETHODIMP
|
2013-08-27 23:24:51 +00:00
|
|
|
nsWindowMemoryReporter::CollectReports(nsIMemoryReporterCallback* aCb,
|
2014-05-21 06:06:54 +00:00
|
|
|
nsISupports* aClosure, bool aAnonymize)
|
2011-12-09 05:42:20 +00:00
|
|
|
{
|
|
|
|
nsGlobalWindow::WindowByIdTable* windowsById =
|
|
|
|
nsGlobalWindow::GetWindowsTable();
|
|
|
|
NS_ENSURE_TRUE(windowsById, NS_OK);
|
|
|
|
|
|
|
|
// Hold on to every window in memory so that window objects can't be
|
|
|
|
// destroyed while we're calling the memory reporter callback.
|
|
|
|
WindowArray windows;
|
|
|
|
windowsById->Enumerate(GetWindows, &windows);
|
|
|
|
|
2013-09-19 02:01:29 +00:00
|
|
|
// Get the IDs of all the "ghost" windows, and call aCb->Callback() for each
|
|
|
|
// one.
|
2012-04-03 02:28:04 +00:00
|
|
|
nsTHashtable<nsUint64HashKey> ghostWindows;
|
|
|
|
CheckForGhostWindows(&ghostWindows);
|
2013-09-19 02:01:29 +00:00
|
|
|
ReportGhostWindowsEnumeratorData reportGhostWindowsEnumData =
|
2014-05-21 06:06:54 +00:00
|
|
|
{ aCb, aClosure, aAnonymize, NS_OK };
|
2013-09-19 02:01:29 +00:00
|
|
|
ghostWindows.EnumerateEntries(ReportGhostWindowsEnumerator,
|
|
|
|
&reportGhostWindowsEnumData);
|
2014-05-21 06:06:54 +00:00
|
|
|
nsresult rv = reportGhostWindowsEnumData.mRv;
|
2013-09-19 02:01:29 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-04-03 02:28:04 +00:00
|
|
|
|
2012-07-06 04:12:37 +00:00
|
|
|
WindowPaths windowPaths;
|
2013-03-17 03:36:37 +00:00
|
|
|
WindowPaths topWindowPaths;
|
|
|
|
|
2011-12-09 05:42:20 +00:00
|
|
|
// Collect window memory usage.
|
2013-10-28 14:04:12 +00:00
|
|
|
nsWindowSizes windowTotalSizes(nullptr);
|
2013-10-28 04:53:00 +00:00
|
|
|
nsCOMPtr<amIAddonManager> addonManager;
|
|
|
|
if (XRE_GetProcessType() == GeckoProcessType_Default) {
|
|
|
|
// Only try to access the service from the main process.
|
|
|
|
addonManager = do_GetService("@mozilla.org/addons/integration;1");
|
|
|
|
}
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < windows.Length(); i++) {
|
2013-09-19 02:01:29 +00:00
|
|
|
rv = CollectWindowReports(windows[i], addonManager,
|
|
|
|
&windowTotalSizes, &ghostWindows,
|
|
|
|
&windowPaths, &topWindowPaths, aCb,
|
2014-05-21 06:06:54 +00:00
|
|
|
aClosure, aAnonymize);
|
2012-03-04 23:26:30 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-12-09 05:42:20 +00:00
|
|
|
}
|
2011-06-29 11:06:43 +00:00
|
|
|
|
2012-07-06 04:12:37 +00:00
|
|
|
// Report JS memory usage. We do this from here because the JS memory
|
2013-08-27 23:24:51 +00:00
|
|
|
// reporter needs to be passed |windowPaths|.
|
2013-09-19 02:01:29 +00:00
|
|
|
rv = xpc::JSReporter::CollectReports(&windowPaths, &topWindowPaths,
|
2014-05-21 06:06:54 +00:00
|
|
|
aCb, aClosure, aAnonymize);
|
2012-07-06 04:12:37 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-02-21 15:10:11 +00:00
|
|
|
#define REPORT(_path, _amount, _desc) \
|
|
|
|
do { \
|
2012-03-04 23:26:30 +00:00
|
|
|
nsresult rv; \
|
|
|
|
rv = aCb->Callback(EmptyCString(), NS_LITERAL_CSTRING(_path), \
|
2013-12-11 19:59:39 +00:00
|
|
|
KIND_OTHER, UNITS_BYTES, _amount, \
|
2012-03-04 23:26:30 +00:00
|
|
|
NS_LITERAL_CSTRING(_desc), aClosure); \
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv); \
|
2012-02-21 15:10:11 +00:00
|
|
|
} while (0)
|
|
|
|
|
2013-10-28 09:47:18 +00:00
|
|
|
REPORT("window-objects/dom/element-nodes", windowTotalSizes.mDOMElementNodesSize,
|
2012-06-05 14:49:48 +00:00
|
|
|
"This is the sum of all windows' 'dom/element-nodes' numbers.");
|
|
|
|
|
2013-10-28 09:47:18 +00:00
|
|
|
REPORT("window-objects/dom/text-nodes", windowTotalSizes.mDOMTextNodesSize,
|
2012-06-05 14:49:48 +00:00
|
|
|
"This is the sum of all windows' 'dom/text-nodes' numbers.");
|
|
|
|
|
2013-10-28 09:47:18 +00:00
|
|
|
REPORT("window-objects/dom/cdata-nodes", windowTotalSizes.mDOMCDATANodesSize,
|
2012-06-05 14:49:48 +00:00
|
|
|
"This is the sum of all windows' 'dom/cdata-nodes' numbers.");
|
|
|
|
|
2013-10-28 09:47:18 +00:00
|
|
|
REPORT("window-objects/dom/comment-nodes", windowTotalSizes.mDOMCommentNodesSize,
|
2012-06-05 14:49:48 +00:00
|
|
|
"This is the sum of all windows' 'dom/comment-nodes' numbers.");
|
|
|
|
|
2013-10-28 09:47:18 +00:00
|
|
|
REPORT("window-objects/dom/event-targets", windowTotalSizes.mDOMEventTargetsSize,
|
2013-01-18 05:21:43 +00:00
|
|
|
"This is the sum of all windows' 'dom/event-targets' numbers.");
|
|
|
|
|
2013-10-28 09:47:18 +00:00
|
|
|
REPORT("window-objects/dom/other", windowTotalSizes.mDOMOtherSize,
|
2013-01-18 05:21:43 +00:00
|
|
|
"This is the sum of all windows' 'dom/other' numbers.");
|
|
|
|
|
2012-06-15 04:10:08 +00:00
|
|
|
REPORT("window-objects/property-tables",
|
2013-10-28 09:47:18 +00:00
|
|
|
windowTotalSizes.mPropertyTablesSize,
|
2012-06-15 04:10:08 +00:00
|
|
|
"This is the sum of all windows' 'property-tables' numbers.");
|
|
|
|
|
2013-10-28 09:47:18 +00:00
|
|
|
REPORT("window-objects/style-sheets", windowTotalSizes.mStyleSheetsSize,
|
2012-02-21 15:10:11 +00:00
|
|
|
"This is the sum of all windows' 'style-sheets' numbers.");
|
2013-01-18 05:21:43 +00:00
|
|
|
|
2013-10-28 09:47:18 +00:00
|
|
|
REPORT("window-objects/layout/pres-shell", windowTotalSizes.mLayoutPresShellSize,
|
2013-01-18 05:21:43 +00:00
|
|
|
"This is the sum of all windows' 'layout/arenas' numbers.");
|
|
|
|
|
2012-06-12 03:32:26 +00:00
|
|
|
REPORT("window-objects/layout/line-boxes",
|
2013-01-18 05:21:43 +00:00
|
|
|
windowTotalSizes.mArenaStats.mLineBoxes,
|
2012-06-06 17:35:40 +00:00
|
|
|
"This is the sum of all windows' 'layout/line-boxes' numbers.");
|
|
|
|
|
2012-06-12 03:32:26 +00:00
|
|
|
REPORT("window-objects/layout/rule-nodes",
|
2012-06-06 17:35:40 +00:00
|
|
|
windowTotalSizes.mArenaStats.mRuleNodes,
|
|
|
|
"This is the sum of all windows' 'layout/rule-nodes' numbers.");
|
|
|
|
|
2012-06-12 03:32:26 +00:00
|
|
|
REPORT("window-objects/layout/style-contexts",
|
2012-06-06 17:35:40 +00:00
|
|
|
windowTotalSizes.mArenaStats.mStyleContexts,
|
|
|
|
"This is the sum of all windows' 'layout/style-contexts' numbers.");
|
|
|
|
|
2013-10-28 09:47:18 +00:00
|
|
|
REPORT("window-objects/layout/style-sets", windowTotalSizes.mLayoutStyleSetsSize,
|
2012-02-21 15:10:11 +00:00
|
|
|
"This is the sum of all windows' 'layout/style-sets' numbers.");
|
2013-01-18 05:21:43 +00:00
|
|
|
|
2013-10-28 09:47:18 +00:00
|
|
|
REPORT("window-objects/layout/text-runs", windowTotalSizes.mLayoutTextRunsSize,
|
2012-02-21 15:10:11 +00:00
|
|
|
"This is the sum of all windows' 'layout/text-runs' numbers.");
|
|
|
|
|
2013-10-28 09:47:18 +00:00
|
|
|
REPORT("window-objects/layout/pres-contexts", windowTotalSizes.mLayoutPresContextSize,
|
2012-05-11 13:23:07 +00:00
|
|
|
"This is the sum of all windows' 'layout/pres-contexts' numbers.");
|
|
|
|
|
2012-06-06 17:29:16 +00:00
|
|
|
size_t frameTotal = 0;
|
|
|
|
#define FRAME_ID(classname) \
|
|
|
|
frameTotal += windowTotalSizes.mArenaStats.FRAME_ID_STAT_FIELD(classname);
|
|
|
|
#include "nsFrameIdList.h"
|
|
|
|
#undef FRAME_ID
|
|
|
|
|
2012-06-12 03:32:26 +00:00
|
|
|
REPORT("window-objects/layout/frames", frameTotal,
|
2012-06-06 17:29:16 +00:00
|
|
|
"Memory used for layout frames within windows. "
|
|
|
|
"This is the sum of all windows' 'layout/frames/' numbers.");
|
|
|
|
|
2012-02-21 15:10:11 +00:00
|
|
|
#undef REPORT
|
2013-01-18 05:21:43 +00:00
|
|
|
|
2011-06-29 11:01:07 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t
|
2012-04-03 02:28:05 +00:00
|
|
|
nsWindowMemoryReporter::GetGhostTimeout()
|
|
|
|
{
|
|
|
|
return Preferences::GetUint("memory.ghost_window_timeout_seconds", 60);
|
|
|
|
}
|
|
|
|
|
2012-04-03 02:28:04 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsWindowMemoryReporter::Observe(nsISupports *aSubject, const char *aTopic,
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t *aData)
|
2012-04-03 02:28:04 +00:00
|
|
|
{
|
2012-04-03 02:28:05 +00:00
|
|
|
if (!strcmp(aTopic, DOM_WINDOW_DESTROYED_TOPIC)) {
|
|
|
|
ObserveDOMWindowDetached(aSubject);
|
|
|
|
} else if (!strcmp(aTopic, "after-minimize-memory-usage")) {
|
|
|
|
ObserveAfterMinimizeMemoryUsage();
|
2014-03-08 13:38:53 +00:00
|
|
|
} else if (!strcmp(aTopic, "cycle-collector-begin")) {
|
|
|
|
if (mCheckTimer) {
|
|
|
|
mCheckTimerWaitingForCCEnd = true;
|
|
|
|
KillCheckTimer();
|
|
|
|
}
|
|
|
|
mCycleCollectorIsRunning = true;
|
|
|
|
} else if (!strcmp(aTopic, "cycle-collector-end")) {
|
|
|
|
mCycleCollectorIsRunning = false;
|
|
|
|
if (mCheckTimerWaitingForCCEnd) {
|
|
|
|
mCheckTimerWaitingForCCEnd = false;
|
|
|
|
AsyncCheckForGhostWindows();
|
|
|
|
}
|
2012-04-03 02:28:05 +00:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(false);
|
|
|
|
}
|
2012-04-02 14:37:04 +00:00
|
|
|
|
2012-04-03 02:28:05 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-04-03 02:28:04 +00:00
|
|
|
|
|
|
|
void
|
2012-04-03 02:28:05 +00:00
|
|
|
nsWindowMemoryReporter::ObserveDOMWindowDetached(nsISupports* aWindow)
|
2012-04-03 02:28:04 +00:00
|
|
|
{
|
|
|
|
nsWeakPtr weakWindow = do_GetWeakReference(aWindow);
|
|
|
|
if (!weakWindow) {
|
|
|
|
NS_WARNING("Couldn't take weak reference to a window?");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mDetachedWindows.Put(weakWindow, TimeStamp());
|
|
|
|
|
2014-03-08 13:38:52 +00:00
|
|
|
AsyncCheckForGhostWindows();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
|
|
|
nsWindowMemoryReporter::CheckTimerFired(nsITimer* aTimer, void* aClosure)
|
|
|
|
{
|
|
|
|
if (sWindowReporter) {
|
2014-03-08 13:38:53 +00:00
|
|
|
MOZ_ASSERT(!sWindowReporter->mCycleCollectorIsRunning);
|
2014-03-08 13:38:52 +00:00
|
|
|
sWindowReporter->CheckForGhostWindows();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWindowMemoryReporter::AsyncCheckForGhostWindows()
|
|
|
|
{
|
|
|
|
if (mCheckTimer) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-08 13:38:53 +00:00
|
|
|
if (mCycleCollectorIsRunning) {
|
|
|
|
mCheckTimerWaitingForCCEnd = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-08 13:38:52 +00:00
|
|
|
// If more than kTimeBetweenChecks seconds have elapsed since the last check,
|
|
|
|
// timerDelay is 0. Otherwise, it is kTimeBetweenChecks, reduced by the time
|
|
|
|
// since the last check. Reducing the delay by the time since the last check
|
|
|
|
// prevents the timer from being completely starved if it is repeatedly killed
|
|
|
|
// and restarted.
|
|
|
|
int32_t timeSinceLastCheck = (TimeStamp::NowLoRes() - mLastCheckForGhostWindows).ToSeconds();
|
|
|
|
int32_t timerDelay = (kTimeBetweenChecks - std::min(timeSinceLastCheck, kTimeBetweenChecks)) * PR_MSEC_PER_SEC;
|
|
|
|
|
2014-05-15 13:35:00 +00:00
|
|
|
mCheckTimer = do_CreateInstance("@mozilla.org/timer;1");
|
2014-03-08 13:38:52 +00:00
|
|
|
|
|
|
|
if (mCheckTimer) {
|
|
|
|
mCheckTimer->InitWithFuncCallback(CheckTimerFired, nullptr,
|
|
|
|
timerDelay, nsITimer::TYPE_ONE_SHOT);
|
2012-04-03 02:28:04 +00:00
|
|
|
}
|
2012-04-03 02:28:05 +00:00
|
|
|
}
|
2012-04-03 02:28:04 +00:00
|
|
|
|
2012-04-03 02:28:05 +00:00
|
|
|
static PLDHashOperator
|
|
|
|
BackdateTimeStampsEnumerator(nsISupports *aKey, TimeStamp &aTimeStamp,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
TimeStamp *minTimeStamp = static_cast<TimeStamp*>(aClosure);
|
2013-01-18 05:21:43 +00:00
|
|
|
|
2012-04-03 02:28:05 +00:00
|
|
|
if (!aTimeStamp.IsNull() && aTimeStamp > *minTimeStamp) {
|
|
|
|
aTimeStamp = *minTimeStamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsWindowMemoryReporter::ObserveAfterMinimizeMemoryUsage()
|
|
|
|
{
|
|
|
|
// Someone claims they've done enough GC/CCs so that all eligible windows
|
|
|
|
// have been free'd. So we deem that any windows which satisfy ghost
|
|
|
|
// criteria (1) and (2) now satisfy criterion (3) as well.
|
|
|
|
//
|
|
|
|
// To effect this change, we'll backdate some of our timestamps.
|
|
|
|
|
|
|
|
TimeStamp minTimeStamp = TimeStamp::Now() -
|
|
|
|
TimeDuration::FromSeconds(GetGhostTimeout());
|
|
|
|
|
|
|
|
mDetachedWindows.Enumerate(BackdateTimeStampsEnumerator,
|
|
|
|
&minTimeStamp);
|
2012-04-03 02:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct CheckForGhostWindowsEnumeratorData
|
|
|
|
{
|
|
|
|
nsTHashtable<nsCStringHashKey> *nonDetachedDomains;
|
|
|
|
nsTHashtable<nsUint64HashKey> *ghostWindowIDs;
|
|
|
|
nsIEffectiveTLDService *tldService;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t ghostTimeout;
|
2012-04-03 02:28:04 +00:00
|
|
|
TimeStamp now;
|
|
|
|
};
|
|
|
|
|
|
|
|
static PLDHashOperator
|
|
|
|
CheckForGhostWindowsEnumerator(nsISupports *aKey, TimeStamp& aTimeStamp,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
CheckForGhostWindowsEnumeratorData *data =
|
|
|
|
static_cast<CheckForGhostWindowsEnumeratorData*>(aClosure);
|
|
|
|
|
|
|
|
nsWeakPtr weakKey = do_QueryInterface(aKey);
|
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryReferent(weakKey);
|
|
|
|
if (!window) {
|
|
|
|
// The window object has been destroyed. Stop tracking its weak ref in our
|
|
|
|
// hashtable.
|
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid calling GetTop() if we have no outer window. Nothing will break if
|
|
|
|
// we do, but it will spew debug output, which can cause our test logs to
|
|
|
|
// overflow.
|
|
|
|
nsCOMPtr<nsIDOMWindow> top;
|
|
|
|
if (window->GetOuterWindow()) {
|
|
|
|
window->GetTop(getter_AddRefs(top));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (top) {
|
|
|
|
// The window is no longer detached, so we no longer want to track it.
|
|
|
|
return PL_DHASH_REMOVE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri = GetWindowURI(window);
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString domain;
|
2012-04-03 02:28:04 +00:00
|
|
|
if (uri) {
|
|
|
|
// GetBaseDomain works fine if |uri| is null, but it outputs a warning
|
|
|
|
// which ends up overrunning the mochitest logs.
|
|
|
|
data->tldService->GetBaseDomain(uri, 0, domain);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->nonDetachedDomains->Contains(domain)) {
|
|
|
|
// This window shares a domain with a non-detached window, so reset its
|
|
|
|
// clock.
|
|
|
|
aTimeStamp = TimeStamp();
|
|
|
|
} else {
|
|
|
|
// This window does not share a domain with a non-detached window, so it
|
|
|
|
// meets ghost criterion (2).
|
|
|
|
if (aTimeStamp.IsNull()) {
|
|
|
|
// This may become a ghost window later; start its clock.
|
|
|
|
aTimeStamp = data->now;
|
|
|
|
} else if ((data->now - aTimeStamp).ToSeconds() > data->ghostTimeout) {
|
|
|
|
// This definitely is a ghost window, so add it to ghostWindowIDs, if
|
|
|
|
// that is not null.
|
|
|
|
if (data->ghostWindowIDs) {
|
|
|
|
nsCOMPtr<nsPIDOMWindow> pWindow = do_QueryInterface(window);
|
|
|
|
if (pWindow) {
|
|
|
|
data->ghostWindowIDs->PutEntry(pWindow->WindowID());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct GetNonDetachedWindowDomainsEnumeratorData
|
|
|
|
{
|
|
|
|
nsTHashtable<nsCStringHashKey> *nonDetachedDomains;
|
|
|
|
nsIEffectiveTLDService *tldService;
|
|
|
|
};
|
|
|
|
|
|
|
|
static PLDHashOperator
|
2012-08-22 15:56:38 +00:00
|
|
|
GetNonDetachedWindowDomainsEnumerator(const uint64_t& aId, nsGlobalWindow* aWindow,
|
2012-04-03 02:28:04 +00:00
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
GetNonDetachedWindowDomainsEnumeratorData *data =
|
|
|
|
static_cast<GetNonDetachedWindowDomainsEnumeratorData*>(aClosure);
|
|
|
|
|
|
|
|
// Null outer window implies null top, but calling GetTop() when there's no
|
|
|
|
// outer window causes us to spew debug warnings.
|
|
|
|
if (!aWindow->GetOuterWindow() || !aWindow->GetTop()) {
|
|
|
|
// This window is detached, so we don't care about its domain.
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri = GetWindowURI(aWindow);
|
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString domain;
|
2012-04-03 02:28:04 +00:00
|
|
|
if (uri) {
|
|
|
|
data->tldService->GetBaseDomain(uri, 0, domain);
|
|
|
|
}
|
|
|
|
|
|
|
|
data->nonDetachedDomains->PutEntry(domain);
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterate over mDetachedWindows and update it to reflect the current state of
|
|
|
|
* the world. In particular:
|
|
|
|
*
|
|
|
|
* - Remove weak refs to windows which no longer exist.
|
|
|
|
*
|
|
|
|
* - Remove references to windows which are no longer detached.
|
|
|
|
*
|
|
|
|
* - Reset the timestamp on detached windows which share a domain with a
|
|
|
|
* non-detached window (they no longer meet ghost criterion (2)).
|
|
|
|
*
|
|
|
|
* - If a window now meets ghost criterion (2) but didn't before, set its
|
|
|
|
* timestamp to now.
|
|
|
|
*
|
|
|
|
* Additionally, if aOutGhostIDs is not null, fill it with the window IDs of
|
|
|
|
* all ghost windows we found.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
nsWindowMemoryReporter::CheckForGhostWindows(
|
2013-10-28 14:04:12 +00:00
|
|
|
nsTHashtable<nsUint64HashKey> *aOutGhostIDs /* = nullptr */)
|
2012-04-03 02:28:04 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIEffectiveTLDService> tldService = do_GetService(
|
|
|
|
NS_EFFECTIVETLDSERVICE_CONTRACTID);
|
|
|
|
if (!tldService) {
|
|
|
|
NS_WARNING("Couldn't get TLDService.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsGlobalWindow::WindowByIdTable *windowsById =
|
|
|
|
nsGlobalWindow::GetWindowsTable();
|
|
|
|
if (!windowsById) {
|
|
|
|
NS_WARNING("GetWindowsTable returned null");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-08 13:38:52 +00:00
|
|
|
mLastCheckForGhostWindows = TimeStamp::NowLoRes();
|
|
|
|
KillCheckTimer();
|
|
|
|
|
2012-04-03 02:28:04 +00:00
|
|
|
nsTHashtable<nsCStringHashKey> nonDetachedWindowDomains;
|
|
|
|
|
|
|
|
// Populate nonDetachedWindowDomains.
|
|
|
|
GetNonDetachedWindowDomainsEnumeratorData nonDetachedEnumData =
|
|
|
|
{ &nonDetachedWindowDomains, tldService };
|
|
|
|
windowsById->EnumerateRead(GetNonDetachedWindowDomainsEnumerator,
|
|
|
|
&nonDetachedEnumData);
|
|
|
|
|
|
|
|
// Update mDetachedWindows and write the ghost window IDs into aOutGhostIDs,
|
|
|
|
// if it's not null.
|
|
|
|
CheckForGhostWindowsEnumeratorData ghostEnumData =
|
|
|
|
{ &nonDetachedWindowDomains, aOutGhostIDs, tldService,
|
2014-03-08 13:38:52 +00:00
|
|
|
GetGhostTimeout(), mLastCheckForGhostWindows };
|
2012-04-03 02:28:04 +00:00
|
|
|
mDetachedWindows.Enumerate(CheckForGhostWindowsEnumerator,
|
|
|
|
&ghostEnumData);
|
|
|
|
}
|
2012-04-03 02:28:05 +00:00
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsWindowMemoryReporter::GhostWindowsReporter,
|
|
|
|
nsIMemoryReporter)
|
2013-12-08 06:09:10 +00:00
|
|
|
|
2013-09-19 22:52:30 +00:00
|
|
|
/* static */ int64_t
|
|
|
|
nsWindowMemoryReporter::GhostWindowsReporter::DistinguishedAmount()
|
2013-09-25 03:31:00 +00:00
|
|
|
{
|
|
|
|
nsTHashtable<nsUint64HashKey> ghostWindows;
|
2013-09-19 02:01:29 +00:00
|
|
|
sWindowReporter->CheckForGhostWindows(&ghostWindows);
|
2013-09-25 03:31:00 +00:00
|
|
|
return ghostWindows.Count();
|
|
|
|
}
|
2013-09-19 22:52:30 +00:00
|
|
|
|
2014-03-08 13:38:52 +00:00
|
|
|
void
|
|
|
|
nsWindowMemoryReporter::KillCheckTimer()
|
|
|
|
{
|
|
|
|
if (mCheckTimer) {
|
|
|
|
mCheckTimer->Cancel();
|
|
|
|
mCheckTimer = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-20 20:55:40 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
static PLDHashOperator
|
|
|
|
UnlinkGhostWindowsEnumerator(nsUint64HashKey* aIDHashKey, void *)
|
|
|
|
{
|
|
|
|
nsGlobalWindow::WindowByIdTable* windowsById =
|
|
|
|
nsGlobalWindow::GetWindowsTable();
|
|
|
|
if (!windowsById) {
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<nsGlobalWindow> window = windowsById->Get(aIDHashKey->GetKey());
|
|
|
|
if (window) {
|
|
|
|
window->RiskyUnlink();
|
|
|
|
}
|
|
|
|
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
nsWindowMemoryReporter::UnlinkGhostWindows()
|
|
|
|
{
|
|
|
|
if (!sWindowReporter) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsGlobalWindow::WindowByIdTable* windowsById =
|
|
|
|
nsGlobalWindow::GetWindowsTable();
|
|
|
|
if (!windowsById) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hold on to every window in memory so that window objects can't be
|
|
|
|
// destroyed while we're calling the UnlinkGhostWindows callback.
|
|
|
|
WindowArray windows;
|
|
|
|
windowsById->Enumerate(GetWindows, &windows);
|
|
|
|
|
|
|
|
// Get the IDs of all the "ghost" windows, and unlink them all.
|
|
|
|
nsTHashtable<nsUint64HashKey> ghostWindows;
|
|
|
|
sWindowReporter->CheckForGhostWindows(&ghostWindows);
|
|
|
|
ghostWindows.EnumerateEntries(UnlinkGhostWindowsEnumerator, nullptr);
|
|
|
|
}
|
|
|
|
#endif
|