Bug 1597999 - Implements ChromeUtils.getObjectNodeId. r=jimb

This helps retrieving the NodeId for any JS Object you pass in.
This allows identifying a particular object when using HeapSnapshot API.
`HeapSnapshot.computeDominatorTree()` returns a tree of object,
but there is no way to identify a given JS object in it.
Getting the node id of a given JS object helps you browse this tree
from a given object you know is leaking.

Differential Revision: https://phabricator.services.mozilla.com/D53971

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Alexandre Poirot 2019-11-21 22:12:20 +00:00
parent ee1e094b81
commit 77af789c1f
5 changed files with 76 additions and 0 deletions

View File

@ -1504,6 +1504,15 @@ void ChromeUtils::SaveHeapSnapshotShared(
edgeCount);
}
/* static */
uint64_t ChromeUtils::GetObjectNodeId(GlobalObject& global,
JS::HandleObject val) {
JS::RootedObject obj(global.Context(), val);
JS::ubi::Node node(obj);
return node.identifier();
}
/* static */
void ChromeUtils::SaveHeapSnapshot(GlobalObject& global,
const HeapSnapshotBoundaries& boundaries,

View File

@ -0,0 +1,57 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
// Test ChromeUtils.getObjectNodeId()
function run_test() {
// Create a test object, which we want to analyse
const testObject = {
foo: {
bar: {},
},
};
const path = ChromeUtils.saveHeapSnapshot({ runtime: true });
const snapshot = ChromeUtils.readHeapSnapshot(path);
// Get the NodeId for our test object
const objectNodeIdRoot = ChromeUtils.getObjectNodeId(testObject);
const objectNodeIdFoo = ChromeUtils.getObjectNodeId(testObject.foo);
const objectNodeIdBar = ChromeUtils.getObjectNodeId(testObject.foo.bar);
// Also try to ensure that this is the right object via its retained path
const shortestPaths = snapshot.computeShortestPaths(
objectNodeIdRoot,
[objectNodeIdBar],
50
);
ok(shortestPaths);
ok(shortestPaths instanceof Map);
ok(
shortestPaths.size == 1,
"We get only one path between the root object and bar object"
);
const paths = shortestPaths.get(objectNodeIdBar);
ok(paths.length == 1, "There is only one path between root and bar");
ok(
paths[0].length == 2,
"The shortest path is made of two edges: foo and bar"
);
const [path1, path2] = paths[0];
ok(
path1.predecessor == objectNodeIdRoot,
"The first edge goes from the root object"
);
ok(path1.edge == "foo", "The first edge is the foo attribute");
ok(
path2.predecessor == objectNodeIdFoo,
"The second edge goes from the foo object"
);
ok(path2.edge == "bar", "The first edge is the bar attribute");
do_test_finished();
}

View File

@ -76,6 +76,7 @@ support-files =
[test_HeapSnapshot_describeNode_01.js]
[test_HeapSnapshot_computeShortestPaths_01.js]
[test_HeapSnapshot_computeShortestPaths_02.js]
[test_HeapSnapshot_getObjectNodeId_01.js]
[test_HeapSnapshot_takeCensus_01.js]
[test_HeapSnapshot_takeCensus_02.js]
[test_HeapSnapshot_takeCensus_03.js]

View File

@ -38,6 +38,9 @@ class ChromeUtils {
ErrorResult& rv);
public:
// Implemented in devtools/shared/heapsnapshot/HeapSnapshot.cpp
static uint64_t GetObjectNodeId(GlobalObject& global, JS::HandleObject aVal);
// Implemented in devtools/shared/heapsnapshot/HeapSnapshot.cpp
static void SaveHeapSnapshot(GlobalObject& global,
const HeapSnapshotBoundaries& boundaries,

View File

@ -27,6 +27,12 @@ interface MozQueryInterface {
*/
[ChromeOnly, Exposed=(Window,Worker)]
namespace ChromeUtils {
/**
* Get the |NodeId| for the given JS Object.
* |NodeId| is the identifier of |JS::ubi::Node|.
*/
NodeId getObjectNodeId(object obj);
/**
* Serialize a snapshot of the heap graph, as seen by |JS::ubi::Node| and
* restricted by |boundaries|, and write it to the provided file path.