2011-06-29 15:01:07 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Mozilla Foundation
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2011
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Mounir Lamouri <mounir.lamouri@mozilla.com> (original author)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
2012-02-24 06:43:33 +04:00
|
|
|
#include "nsWindowMemoryReporter.h"
|
2011-06-29 15:06:43 +04:00
|
|
|
#include "nsGlobalWindow.h"
|
2011-06-29 15:01:07 +04:00
|
|
|
|
|
|
|
|
2012-02-24 06:43:33 +04:00
|
|
|
nsWindowMemoryReporter::nsWindowMemoryReporter()
|
2011-06-29 15:01:07 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-02-24 06:43:33 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(nsWindowMemoryReporter, nsIMemoryMultiReporter)
|
2011-06-29 15:01:07 +04:00
|
|
|
|
|
|
|
/* static */
|
|
|
|
void
|
2012-02-24 06:43:33 +04:00
|
|
|
nsWindowMemoryReporter::Init()
|
2011-06-29 15:01:07 +04:00
|
|
|
{
|
|
|
|
// The memory reporter manager is going to own this object.
|
2012-02-24 06:43:33 +04:00
|
|
|
NS_RegisterMemoryMultiReporter(new nsWindowMemoryReporter());
|
2011-06-29 15:01:07 +04:00
|
|
|
}
|
|
|
|
|
2012-03-13 10:00:18 +04:00
|
|
|
static void
|
2011-12-09 09:42:20 +04:00
|
|
|
AppendWindowURI(nsGlobalWindow *aWindow, nsACString& aStr)
|
2011-06-29 15:01:07 +04:00
|
|
|
{
|
2011-12-09 09:42:20 +04:00
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aWindow->GetExtantDocument());
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2011-06-29 15:01:07 +04:00
|
|
|
|
2011-12-09 09:42:20 +04:00
|
|
|
if (doc) {
|
|
|
|
uri = doc->GetDocumentURI();
|
|
|
|
}
|
2011-06-29 15:01:07 +04:00
|
|
|
|
2011-12-09 09:42:20 +04:00
|
|
|
if (!uri) {
|
|
|
|
nsIPrincipal *principal = aWindow->GetPrincipal();
|
2011-06-29 15:01:07 +04:00
|
|
|
|
2011-12-09 09:42:20 +04:00
|
|
|
if (principal) {
|
|
|
|
principal->GetURI(getter_AddRefs(uri));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-13 10:00:18 +04:00
|
|
|
if (uri) {
|
|
|
|
nsCString spec;
|
|
|
|
uri->GetSpec(spec);
|
2011-12-09 09:42:20 +04:00
|
|
|
|
2012-03-13 10:00:18 +04: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 09:42:20 +04:00
|
|
|
|
2012-03-13 10:00:18 +04:00
|
|
|
aStr += spec;
|
|
|
|
} else {
|
|
|
|
aStr += NS_LITERAL_CSTRING("[system]");
|
|
|
|
}
|
2011-06-29 15:01:07 +04:00
|
|
|
}
|
2011-06-29 15:06:43 +04:00
|
|
|
|
2012-02-21 19:10:11 +04:00
|
|
|
NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(DOMStyleMallocSizeOf, "windows")
|
2011-12-09 09:42:20 +04:00
|
|
|
|
2012-03-05 03:26:30 +04:00
|
|
|
static nsresult
|
2012-01-03 06:19:14 +04:00
|
|
|
CollectWindowReports(nsGlobalWindow *aWindow,
|
2012-02-21 19:10:11 +04:00
|
|
|
nsWindowSizes *aWindowTotalSizes,
|
2012-01-03 06:19:14 +04:00
|
|
|
nsIMemoryMultiReporterCallback *aCb,
|
|
|
|
nsISupports *aClosure)
|
|
|
|
{
|
2011-12-09 09:42:20 +04:00
|
|
|
// DOM window objects fall into one of three categories:
|
|
|
|
// - "active" windows are currently either displayed in an active
|
|
|
|
// tab, or a child of such a window.
|
|
|
|
// - "cached" windows are in the fastback cache.
|
|
|
|
// - "other" windows are closed (or navigated away from w/o being
|
|
|
|
// cached) yet held alive by either a website or our code. The
|
|
|
|
// latter case may be a memory leak, but not necessarily.
|
|
|
|
//
|
2012-03-13 10:00:18 +04:00
|
|
|
// For each window we show how much memory the window and its
|
|
|
|
// document, etc, use, and we report those per URI, where the URI is
|
|
|
|
// the document URI, if available, or the codebase of the principal in
|
|
|
|
// the window. In the case where we're unable to find a URI we're
|
|
|
|
// dealing with a chrome window with no document in it (or somesuch),
|
|
|
|
// and for that we make the URI be the string "[system]".
|
|
|
|
//
|
|
|
|
// Outer windows are lumped in with inner windows, because the amount
|
|
|
|
// of memory used by outer windows is small.
|
2011-12-09 09:42:20 +04:00
|
|
|
//
|
2012-03-13 10:00:18 +04:00
|
|
|
// The path we give to the reporter callback for "active" and "cached"
|
|
|
|
// windows (both inner and outer) is as follows:
|
2011-12-09 09:42:20 +04:00
|
|
|
//
|
2012-03-13 10:00:18 +04:00
|
|
|
// explicit/window-objects/top(<top-outer-uri>, id=<top-outer-id>)/<category>/window(<window-uri>)/...
|
2011-12-09 09:42:20 +04:00
|
|
|
//
|
2012-03-13 10:00:18 +04:00
|
|
|
// The path we give for "other" windows is as follows:
|
|
|
|
//
|
|
|
|
// explicit/window-objects/top(none)/window(<window-uri>)/...
|
2011-12-09 09:42:20 +04:00
|
|
|
//
|
|
|
|
// Where:
|
2012-03-13 10:00:18 +04:00
|
|
|
// - <category> is "active" or "cached", as described above.
|
2011-12-09 09:42:20 +04:00
|
|
|
// - <top-outer-id> is the window id (nsPIDOMWindow::WindowID()) of
|
|
|
|
// the top outer window (i.e. tab, or top level chrome window).
|
2012-03-13 10:00:18 +04:00
|
|
|
// - <top-inner-uri> is the URI of the top outer window. Excepting
|
|
|
|
// special windows (such as browser.xul or hiddenWindow.html) it's
|
|
|
|
// what the address bar shows for the tab.
|
|
|
|
// - <window-uri> is the URI of aWindow.
|
2011-12-09 09:42:20 +04:00
|
|
|
//
|
2012-03-13 10:00:18 +04:00
|
|
|
// Exposing the top-outer-id ensures that each tab gets its own
|
|
|
|
// sub-tree, even if multiple tabs are showing the same URI.
|
2011-12-09 09:42:20 +04:00
|
|
|
|
2012-02-21 19:10:11 +04:00
|
|
|
nsCAutoString windowPath("explicit/window-objects/");
|
2011-12-09 09:42:20 +04:00
|
|
|
|
|
|
|
nsGlobalWindow *top = aWindow->GetTop();
|
2012-03-13 10:00:18 +04:00
|
|
|
windowPath += NS_LITERAL_CSTRING("top(");
|
|
|
|
if (top) {
|
|
|
|
AppendWindowURI(top, windowPath);
|
|
|
|
windowPath += NS_LITERAL_CSTRING(", id=");
|
|
|
|
windowPath.AppendInt(top->WindowID());
|
2011-12-09 09:42:20 +04:00
|
|
|
} else {
|
2012-03-13 10:00:18 +04:00
|
|
|
windowPath += NS_LITERAL_CSTRING("none");
|
2011-12-09 09:42:20 +04:00
|
|
|
}
|
2012-03-13 10:00:18 +04:00
|
|
|
windowPath += NS_LITERAL_CSTRING(")/");
|
2011-12-09 09:42:20 +04:00
|
|
|
|
2012-03-13 10:00:18 +04:00
|
|
|
nsIDocShell *docShell = aWindow->GetDocShell();
|
|
|
|
if (docShell) {
|
|
|
|
MOZ_ASSERT(top, "'cached' or 'active' window lacks a top window");
|
|
|
|
windowPath += aWindow->IsFrozen() ? NS_LITERAL_CSTRING("cached/")
|
|
|
|
: NS_LITERAL_CSTRING("active/");
|
2011-12-09 09:42:20 +04:00
|
|
|
} else {
|
2012-03-13 10:00:18 +04:00
|
|
|
MOZ_ASSERT(!top, "'other' window has a top window");
|
2011-12-09 09:42:20 +04:00
|
|
|
}
|
|
|
|
|
2012-03-13 10:00:18 +04:00
|
|
|
windowPath += NS_LITERAL_CSTRING("window(");
|
|
|
|
AppendWindowURI(aWindow, windowPath);
|
|
|
|
windowPath += NS_LITERAL_CSTRING(")");
|
|
|
|
|
|
|
|
#define REPORT(_pathTail, _amount, _desc) \
|
2012-02-21 19:10:11 +04:00
|
|
|
do { \
|
|
|
|
if (_amount > 0) { \
|
2012-03-13 10:00:18 +04:00
|
|
|
nsCAutoString path(windowPath); \
|
|
|
|
path += _pathTail; \
|
2012-03-05 03:26:30 +04:00
|
|
|
nsresult rv; \
|
|
|
|
rv = aCb->Callback(EmptyCString(), path, nsIMemoryReporter::KIND_HEAP,\
|
2012-02-21 19:10:11 +04:00
|
|
|
nsIMemoryReporter::UNITS_BYTES, _amount, \
|
|
|
|
NS_LITERAL_CSTRING(_desc), aClosure); \
|
2012-03-05 03:26:30 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv); \
|
2012-02-21 19:10:11 +04:00
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2012-03-13 10:00:18 +04:00
|
|
|
nsWindowSizes windowSizes(DOMStyleMallocSizeOf);
|
|
|
|
aWindow->SizeOfIncludingThis(&windowSizes);
|
|
|
|
|
|
|
|
REPORT("/dom", windowSizes.mDOM,
|
2012-02-21 19:10:11 +04:00
|
|
|
"Memory used by a window and the DOM within it.");
|
|
|
|
aWindowTotalSizes->mDOM += windowSizes.mDOM;
|
|
|
|
|
2012-03-13 10:00:18 +04:00
|
|
|
REPORT("/style-sheets", windowSizes.mStyleSheets,
|
2012-02-21 19:10:11 +04:00
|
|
|
"Memory used by style sheets within a window.");
|
|
|
|
aWindowTotalSizes->mStyleSheets += windowSizes.mStyleSheets;
|
|
|
|
|
2012-03-13 10:00:18 +04:00
|
|
|
REPORT("/layout/arenas", windowSizes.mLayoutArenas,
|
2012-02-21 19:10:11 +04:00
|
|
|
"Memory used by layout PresShell, PresContext, and other related "
|
|
|
|
"areas within a window.");
|
|
|
|
aWindowTotalSizes->mLayoutArenas += windowSizes.mLayoutArenas;
|
|
|
|
|
2012-03-13 10:00:18 +04:00
|
|
|
REPORT("/layout/style-sets", windowSizes.mLayoutStyleSets,
|
2012-02-21 19:10:11 +04:00
|
|
|
"Memory used by style sets within a window.");
|
|
|
|
aWindowTotalSizes->mLayoutStyleSets += windowSizes.mLayoutStyleSets;
|
|
|
|
|
2012-03-13 10:00:18 +04:00
|
|
|
REPORT("/layout/text-runs", windowSizes.mLayoutTextRuns,
|
2012-02-21 19:10:11 +04:00
|
|
|
"Memory used for text-runs (glyph layout) in the PresShell's frame "
|
|
|
|
"tree, within a window.");
|
|
|
|
aWindowTotalSizes->mLayoutTextRuns += windowSizes.mLayoutTextRuns;
|
|
|
|
|
|
|
|
#undef REPORT
|
2012-03-05 03:26:30 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2011-06-29 16:29:29 +04:00
|
|
|
}
|
|
|
|
|
2011-12-09 09:42:20 +04:00
|
|
|
typedef nsTArray< nsRefPtr<nsGlobalWindow> > WindowArray;
|
|
|
|
|
2011-06-29 15:06:43 +04:00
|
|
|
static
|
|
|
|
PLDHashOperator
|
2011-12-09 09:42:20 +04:00
|
|
|
GetWindows(const PRUint64& aId, nsGlobalWindow*& aWindow, void* aClosure)
|
2011-06-29 15:06:43 +04:00
|
|
|
{
|
2011-12-09 09:42:20 +04:00
|
|
|
((WindowArray *)aClosure)->AppendElement(aWindow);
|
|
|
|
|
2011-06-29 15:06:43 +04:00
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2012-02-07 05:02:59 +04:00
|
|
|
NS_IMETHODIMP
|
2012-02-24 06:43:33 +04:00
|
|
|
nsWindowMemoryReporter::GetName(nsACString &aName)
|
2012-02-07 05:02:59 +04:00
|
|
|
{
|
2012-02-21 19:10:11 +04:00
|
|
|
aName.AssignLiteral("window-objects");
|
2012-02-07 05:02:59 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-06-29 15:01:07 +04:00
|
|
|
NS_IMETHODIMP
|
2012-02-24 06:43:33 +04:00
|
|
|
nsWindowMemoryReporter::CollectReports(nsIMemoryMultiReporterCallback* aCb,
|
|
|
|
nsISupports* aClosure)
|
2011-12-09 09:42:20 +04: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);
|
|
|
|
|
|
|
|
// Collect window memory usage.
|
|
|
|
nsRefPtr<nsGlobalWindow> *w = windows.Elements();
|
|
|
|
nsRefPtr<nsGlobalWindow> *end = w + windows.Length();
|
2012-02-21 19:10:11 +04:00
|
|
|
nsWindowSizes windowTotalSizes(NULL);
|
2011-12-09 09:42:20 +04:00
|
|
|
for (; w != end; ++w) {
|
2012-03-05 03:26:30 +04:00
|
|
|
nsresult rv = CollectWindowReports(*w, &windowTotalSizes, aCb, aClosure);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-12-09 09:42:20 +04:00
|
|
|
}
|
2011-06-29 15:06:43 +04:00
|
|
|
|
2012-02-21 19:10:11 +04:00
|
|
|
#define REPORT(_path, _amount, _desc) \
|
|
|
|
do { \
|
2012-03-05 03:26:30 +04:00
|
|
|
nsresult rv; \
|
|
|
|
rv = aCb->Callback(EmptyCString(), NS_LITERAL_CSTRING(_path), \
|
|
|
|
nsIMemoryReporter::KIND_OTHER, \
|
|
|
|
nsIMemoryReporter::UNITS_BYTES, _amount, \
|
|
|
|
NS_LITERAL_CSTRING(_desc), aClosure); \
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv); \
|
2012-02-21 19:10:11 +04:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
REPORT("window-objects-dom", windowTotalSizes.mDOM,
|
|
|
|
"Memory used for the DOM within windows. "
|
|
|
|
"This is the sum of all windows' 'dom' numbers.");
|
|
|
|
|
|
|
|
REPORT("window-objects-style-sheets", windowTotalSizes.mStyleSheets,
|
|
|
|
"Memory used for style sheets within windows. "
|
|
|
|
"This is the sum of all windows' 'style-sheets' numbers.");
|
|
|
|
|
|
|
|
REPORT("window-objects-layout-arenas", windowTotalSizes.mLayoutArenas,
|
|
|
|
"Memory used by layout PresShell, PresContext, and other related "
|
|
|
|
"areas within windows. This is the sum of all windows' "
|
|
|
|
"'layout/arenas' numbers.");
|
|
|
|
|
|
|
|
REPORT("window-objects-layout-style-sets", windowTotalSizes.mLayoutStyleSets,
|
|
|
|
"Memory used for style sets within windows. "
|
|
|
|
"This is the sum of all windows' 'layout/style-sets' numbers.");
|
|
|
|
|
|
|
|
REPORT("window-objects-layout-text-runs", windowTotalSizes.mLayoutTextRuns,
|
|
|
|
"Memory used for text runs within windows. "
|
|
|
|
"This is the sum of all windows' 'layout/text-runs' numbers.");
|
|
|
|
|
|
|
|
#undef REPORT
|
|
|
|
|
2011-06-29 15:01:07 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-12-13 07:04:12 +04:00
|
|
|
NS_IMETHODIMP
|
2012-02-24 06:43:33 +04:00
|
|
|
nsWindowMemoryReporter::GetExplicitNonHeap(PRInt64* aAmount)
|
2011-12-13 07:04:12 +04:00
|
|
|
{
|
|
|
|
// This reporter only measures heap memory.
|
|
|
|
*aAmount = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|