Skip to content

Commit

Permalink
Bug 1690905 - Report the DOM portion of memory usage for data documen…
Browse files Browse the repository at this point in the history
…ts in about:memory r=smaug

Depends on D111317

Differential Revision: https://phabricator.services.mozilla.com/D111318
  • Loading branch information
sefeng211 committed May 27, 2021
1 parent 2f4edcb commit 65c295f
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 5 deletions.
38 changes: 37 additions & 1 deletion dom/base/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1307,6 +1307,7 @@ Document::Document(const char* aContentType)
mIsInitialDocumentInWindow(false),
mIgnoreDocGroupMismatches(false),
mLoadedAsData(false),
mAddedToMemoryReportingAsDataDocument(false),
mMayStartLayout(true),
mHaveFiredTitleChange(false),
mIsShowing(false),
Expand Down Expand Up @@ -2239,6 +2240,8 @@ Document::~Document() {
}

UnlinkOriginalDocumentIfStatic();

UnregisterFromMemoryReportingForDataDocument();
}

NS_INTERFACE_TABLE_HEAD(Document)
Expand Down Expand Up @@ -2585,6 +2588,9 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Document)
tmp->mResizeObserverController->Unlink();
}
tmp->mMetaViewports.Clear();

tmp->UnregisterFromMemoryReportingForDataDocument();

NS_IMPL_CYCLE_COLLECTION_UNLINK(mL10nProtoElements)
NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_PTR
NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_REFERENCE
Expand Down Expand Up @@ -3267,6 +3273,7 @@ nsresult Document::StartDocumentLoad(const char* aCommand, nsIChannel* aChannel,

if (nsCRT::strcmp(kLoadAsData, aCommand) == 0) {
mLoadedAsData = true;
SetLoadedAsData(true, /* aConsiderForMemoryReporting */ true);
// We need to disable script & style loading in this case.
// We leave them disabled even in EndLoad(), and let anyone
// who puts the document on display to worry about enabling.
Expand Down Expand Up @@ -3436,6 +3443,20 @@ nsresult Document::StartDocumentLoad(const char* aCommand, nsIChannel* aChannel,
return NS_OK;
}

void Document::SetLoadedAsData(bool aLoadedAsData,
bool aConsiderForMemoryReporting) {
mLoadedAsData = aLoadedAsData;
if (aConsiderForMemoryReporting) {
nsIGlobalObject* global = GetScopeObject();
if (global) {
if (nsPIDOMWindowInner* window = global->AsInnerWindow()) {
nsGlobalWindowInner::Cast(window)
->RegisterDataDocumentForMemoryReporting(this);
}
}
}
}

nsIContentSecurityPolicy* Document::GetCsp() const { return mCSP; }

void Document::SetCsp(nsIContentSecurityPolicy* aCSP) { mCSP = aCSP; }
Expand Down Expand Up @@ -11722,7 +11743,9 @@ nsresult Document::CloneDocHelper(Document* clone) const {
clone->SetScopeObject(GetScopeObject());
}
// Make the clone a data document
clone->SetLoadedAsData(true);
clone->SetLoadedAsData(
true,
/* aConsiderForMemoryReporting */ !mCreatingStaticClone);

// Misc state

Expand Down Expand Up @@ -17279,4 +17302,17 @@ void Document::RemoveMediaElementWithMSE() {
}
}

void Document::UnregisterFromMemoryReportingForDataDocument() {
if (!mAddedToMemoryReportingAsDataDocument) {
return;
}
mAddedToMemoryReportingAsDataDocument = false;
nsIGlobalObject* global = GetScopeObject();
if (global) {
if (nsPIDOMWindowInner* win = global->AsInnerWindow()) {
nsGlobalWindowInner::Cast(win)->UnregisterDataDocumentForMemoryReporting(
this);
}
}
}
} // namespace mozilla::dom
12 changes: 11 additions & 1 deletion dom/base/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,7 @@ class Document : public nsINode,
*/
void SetIsInitialDocument(bool aIsInitialDocument);

void SetLoadedAsData(bool aLoadedAsData) { mLoadedAsData = aLoadedAsData; }
void SetLoadedAsData(bool aLoadedAsData, bool aConsiderForMemoryReporting);

/**
* Normally we assert if a runnable labeled with one DocGroup touches data
Expand Down Expand Up @@ -2519,6 +2519,12 @@ class Document : public nsINode,

bool IsLoadedAsData() { return mLoadedAsData; }

void SetAddedToMemoryReportAsDataDocument() {
mAddedToMemoryReportingAsDataDocument = true;
}

void UnregisterFromMemoryReportingForDataDocument();

bool MayStartLayout() { return mMayStartLayout; }

void SetMayStartLayout(bool aMayStartLayout);
Expand Down Expand Up @@ -4486,6 +4492,10 @@ class Document : public nsINode,
// as scripts and plugins, disabled.
bool mLoadedAsData : 1;

// True if the document is considered for memory reporting as a
// data document
bool mAddedToMemoryReportingAsDataDocument : 1;

// If true, whoever is creating the document has gotten it to the
// point where it's safe to start layout on it.
bool mMayStartLayout : 1;
Expand Down
23 changes: 23 additions & 0 deletions dom/base/nsGlobalWindowInner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6646,6 +6646,29 @@ void nsGlobalWindowInner::AddSizeOfIncludingThis(
}
}

void nsGlobalWindowInner::RegisterDataDocumentForMemoryReporting(
Document* aDocument) {
aDocument->SetAddedToMemoryReportAsDataDocument();
mDataDocumentsForMemoryReporting.AppendElement(
do_GetWeakReference(aDocument));
}

void nsGlobalWindowInner::UnregisterDataDocumentForMemoryReporting(
Document* aDocument) {
nsWeakPtr doc = do_GetWeakReference(aDocument);
MOZ_ASSERT(mDataDocumentsForMemoryReporting.Contains(doc));
mDataDocumentsForMemoryReporting.RemoveElement(doc);
}

void nsGlobalWindowInner::CollectDOMSizesForDataDocuments(
nsWindowSizes& aSize) const {
for (const nsWeakPtr& ptr : mDataDocumentsForMemoryReporting) {
if (nsCOMPtr<Document> doc = do_QueryReferent(ptr)) {
doc->DocAddSizeOfIncludingThis(aSize);
}
}
}

void nsGlobalWindowInner::AddGamepad(GamepadHandle aHandle, Gamepad* aGamepad) {
// Create the index we will present to content based on which indices are
// already taken, as required by the spec.
Expand Down
6 changes: 6 additions & 0 deletions dom/base/nsGlobalWindowInner.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,10 @@ class nsGlobalWindowInner final : public mozilla::dom::EventTarget,

void AddSizeOfIncludingThis(nsWindowSizes& aWindowSizes) const;

void CollectDOMSizesForDataDocuments(nsWindowSizes&) const;
void RegisterDataDocumentForMemoryReporting(Document*);
void UnregisterDataDocumentForMemoryReporting(Document*);

enum SlowScriptResponse {
ContinueSlowScript = 0,
ContinueSlowScriptAndKeepNotifying,
Expand Down Expand Up @@ -1500,6 +1504,8 @@ class nsGlobalWindowInner final : public mozilla::dom::EventTarget,

nsTArray<uint32_t> mScrollMarks;

nsTArray<nsWeakPtr> mDataDocumentsForMemoryReporting;

static InnerWindowByIdTable* sInnerWindowsById;

// Members in the mChromeFields member should only be used in chrome windows.
Expand Down
7 changes: 7 additions & 0 deletions dom/base/nsWindowMemoryReporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,13 @@ static void CollectWindowReports(nsGlobalWindowInner* aWindow,
ReportDOMSize(windowPath, aWindowTotalSizes->mDOMSizes, aHandleReport, aData,
windowSizes.mDOMSizes);

nsCString dataDocumentPath(windowPath);
dataDocumentPath += "/data-documents";
nsWindowSizes dataDocumentSizes(state);
aWindow->CollectDOMSizesForDataDocuments(dataDocumentSizes);
ReportDOMSize(dataDocumentPath, aWindowTotalSizes->mDOMSizes, aHandleReport,
aData, dataDocumentSizes.mDOMSizes);

REPORT_SIZE("/layout/style-sheets", mLayoutStyleSheetsSize,
"Memory used by document style sheets within a window.");

Expand Down
1 change: 1 addition & 0 deletions dom/base/test/browser.ini
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@ support-files =
[browser_bug1703472.js]
support-files =
file_bug1703472.html
[browser_data_documents_aboutmemory.js]
20 changes: 20 additions & 0 deletions dom/base/test/browser_data_documents_aboutmemory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
add_task(async function() {
const doc = new DOMParser().parseFromString("<p>dadada</p>", "text/html");

let mgr = Cc["@mozilla.org/memory-reporter-manager;1"].getService(
Ci.nsIMemoryReporterManager
);

let amount = 0;
const handleReport = (aProcess, aPath, aKind, aUnits, aAmount) => {
const regex = new RegExp(".*/window-objects/.*/data-documents/.*");
if (regex.test(aPath)) {
amount += aAmount;
}
};

await new Promise(r =>
mgr.getReports(handleReport, null, r, null, /* anonymized = */ false)
);
ok(amount > 0, "Got data documents amount");
});
2 changes: 1 addition & 1 deletion dom/html/nsHTMLDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ nsresult NS_NewHTMLDocument(Document** aInstancePtrResult, bool aLoadedAsData) {
return rv;
}

doc->SetLoadedAsData(aLoadedAsData);
doc->SetLoadedAsData(aLoadedAsData, /* aConsiderForMemoryReporting */ true);
doc.forget(aInstancePtrResult);

return NS_OK;
Expand Down
4 changes: 2 additions & 2 deletions dom/xml/XMLDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ nsresult NS_NewDOMDocument(Document** aInstancePtrResult,
d->SetCompatibilityMode(eCompatibility_FullStandards);
d->AsHTMLDocument()->SetIsXHTML(isXHTML);
}
d->SetLoadedAsData(aLoadedAsData);
d->SetLoadedAsData(aLoadedAsData, /* aConsiderForMemoryReporting */ true);
d->SetDocumentURI(aDocumentURI);
// Must set the principal first, since SetBaseURI checks it.
d->SetPrincipals(aPrincipal, aPrincipal);
Expand Down Expand Up @@ -185,7 +185,7 @@ nsresult NS_NewXMLDocument(Document** aInstancePtrResult, bool aLoadedAsData,
return rv;
}

doc->SetLoadedAsData(aLoadedAsData);
doc->SetLoadedAsData(aLoadedAsData, /* aConsiderForMemoryReporting */ true);
doc->mIsPlainDocument = aIsPlainDocument;
doc.forget(aInstancePtrResult);

Expand Down

0 comments on commit 65c295f

Please sign in to comment.