summaryrefslogtreecommitdiff
path: root/test/common/heap.js
diff options
context:
space:
mode:
authorJoyee Cheung <joyeec9h3@gmail.com>2018-09-23 17:52:09 -0400
committerJoyee Cheung <joyeec9h3@gmail.com>2018-10-04 15:32:30 +0200
commit92fa0fcdb76e2b6cb0040eede97fe3c167c31897 (patch)
tree5016ac6cbf7e5873421788387a5b2e011c31ac56 /test/common/heap.js
parentc0c58d5660aeea93c492877894f66dd55771be2e (diff)
downloadandroid-node-v8-92fa0fcdb76e2b6cb0040eede97fe3c167c31897.tar.gz
android-node-v8-92fa0fcdb76e2b6cb0040eede97fe3c167c31897.tar.bz2
android-node-v8-92fa0fcdb76e2b6cb0040eede97fe3c167c31897.zip
src: name EmbededderGraph edges and use class names for nodes
This patch: - Refactors the `MemoryRetainer` API so that the impementer no longer calls `TrackThis()` that sets the size of node on the top of the stack, which may be hard to understand. Instead now they implements `SelfSize()` to provide their self sizes. Also documents the API in the header. - Refactors `MemoryTracker` so it calls `MemoryInfoName()` and `SelfSize()` of `MemoryRetainer` to retrieve info about them, and separate `node_names` and `edge_names` so the edges can be properly named with reference names and the nodes can be named with class names. (Previously the nodes are named with reference names while the edges are all indexed and appear as array elements). - Adds `SET_MEMORY_INFO_NAME()`, `SET_SELF_SIZE()` and `SET_NO_MEMORY_INFO()` convenience macros - Fixes a few `MemoryInfo` calls in some `MemoryRetainers` to track their references properly. - Refactors the heapdump tests to check both node names and edge names, distinguishing between wrapped JS nodes (without prefixes) and embedder wrappers (prefixed with `Node / `). PR-URL: https://github.com/nodejs/node/pull/23072 Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'test/common/heap.js')
-rw-r--r--test/common/heap.js112
1 files changed, 72 insertions, 40 deletions
diff --git a/test/common/heap.js b/test/common/heap.js
index 382d1d3642..e23670b64c 100644
--- a/test/common/heap.js
+++ b/test/common/heap.js
@@ -12,75 +12,107 @@ try {
}
const { createJSHeapDump, buildEmbedderGraph } = internalTestHeap;
+function inspectNode(snapshot) {
+ return util.inspect(snapshot, { depth: 4 });
+}
+
+function isEdge(edge, { node_name, edge_name }) {
+ if (edge.name !== edge_name) {
+ return false;
+ }
+ // From our internal embedded graph
+ if (edge.to.value) {
+ if (edge.to.value.constructor.name !== node_name) {
+ return false;
+ }
+ } else if (edge.to.name !== node_name) {
+ return false;
+ }
+ return true;
+}
+
class State {
constructor() {
this.snapshot = createJSHeapDump();
this.embedderGraph = buildEmbedderGraph();
}
- validateSnapshotNodes(name, expected, { loose = false } = {}) {
- const snapshot = this.snapshot.filter(
- (node) => node.name === 'Node / ' + name && node.type !== 'string');
- if (loose)
- assert(snapshot.length >= expected.length);
- else
- assert.strictEqual(snapshot.length, expected.length);
- for (const expectedNode of expected) {
- if (expectedNode.children) {
- for (const expectedChild of expectedNode.children) {
- const check = typeof expectedChild === 'function' ?
- expectedChild :
- (node) => [expectedChild.name, 'Node / ' + expectedChild.name]
- .includes(node.name);
+ // Validate the v8 heap snapshot
+ validateSnapshot(rootName, expected, { loose = false } = {}) {
+ const rootNodes = this.snapshot.filter(
+ (node) => node.name === rootName && node.type !== 'string');
+ if (loose) {
+ assert(rootNodes.length >= expected.length,
+ `Expect to find at least ${expected.length} '${rootName}', ` +
+ `found ${rootNodes.length}`);
+ } else {
+ assert.strictEqual(
+ rootNodes.length, expected.length,
+ `Expect to find ${expected.length} '${rootName}', ` +
+ `found ${rootNodes.length}`);
+ }
- const hasChild = snapshot.some((node) => {
- return node.outgoingEdges.map((edge) => edge.toNode).some(check);
- });
+ for (const expectation of expected) {
+ if (expectation.children) {
+ for (const expectedEdge of expectation.children) {
+ const check = typeof expectedEdge === 'function' ? expectedEdge :
+ (edge) => (isEdge(edge, expectedEdge));
+ const hasChild = rootNodes.some(
+ (node) => node.outgoingEdges.some(check)
+ );
// Don't use assert with a custom message here. Otherwise the
// inspection in the message is done eagerly and wastes a lot of CPU
// time.
if (!hasChild) {
throw new Error(
'expected to find child ' +
- `${util.inspect(expectedChild)} in ${util.inspect(snapshot)}`);
+ `${util.inspect(expectedEdge)} in ${inspectNode(rootNodes)}`);
}
}
}
}
+ }
- const graph = this.embedderGraph.filter((node) => node.name === name);
- if (loose)
- assert(graph.length >= expected.length);
- else
- assert.strictEqual(graph.length, expected.length);
- for (const expectedNode of expected) {
- if (expectedNode.children) {
- for (const expectedChild of expectedNode.children) {
- const check = (edge) => {
- // TODO(joyeecheung): check the edge names
- const node = edge.to;
- if (typeof expectedChild === 'function') {
- return expectedChild(node);
- }
- return node.name === expectedChild.name ||
- (node.value &&
- node.value.constructor &&
- node.value.constructor.name === expectedChild.name);
- };
-
+ // Validate our internal embedded graph representation
+ validateGraph(rootName, expected, { loose = false } = {}) {
+ const rootNodes = this.embedderGraph.filter(
+ (node) => node.name === rootName
+ );
+ if (loose) {
+ assert(rootNodes.length >= expected.length,
+ `Expect to find at least ${expected.length} '${rootName}', ` +
+ `found ${rootNodes.length}`);
+ } else {
+ assert.strictEqual(
+ rootNodes.length, expected.length,
+ `Expect to find ${expected.length} '${rootName}', ` +
+ `found ${rootNodes.length}`);
+ }
+ for (const expectation of expected) {
+ if (expectation.children) {
+ for (const expectedEdge of expectation.children) {
+ const check = typeof expectedEdge === 'function' ? expectedEdge :
+ (edge) => (isEdge(edge, expectedEdge));
// Don't use assert with a custom message here. Otherwise the
// inspection in the message is done eagerly and wastes a lot of CPU
// time.
- const hasChild = graph.some((node) => node.edges.some(check));
+ const hasChild = rootNodes.some(
+ (node) => node.edges.some(check)
+ );
if (!hasChild) {
throw new Error(
'expected to find child ' +
- `${util.inspect(expectedChild)} in ${util.inspect(snapshot)}`);
+ `${util.inspect(expectedEdge)} in ${inspectNode(rootNodes)}`);
}
}
}
}
}
+
+ validateSnapshotNodes(rootName, expected, { loose = false } = {}) {
+ this.validateSnapshot(rootName, expected, { loose });
+ this.validateGraph(rootName, expected, { loose });
+ }
}
function recordState() {