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>
129 lines
3.9 KiB
JavaScript
129 lines
3.9 KiB
JavaScript
/* eslint-disable node-core/required-modules */
|
|
'use strict';
|
|
const assert = require('assert');
|
|
const util = require('util');
|
|
|
|
let internalTestHeap;
|
|
try {
|
|
internalTestHeap = require('internal/test/heap');
|
|
} catch (e) {
|
|
console.log('using `test/common/heap.js` requires `--expose-internals`');
|
|
throw e;
|
|
}
|
|
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();
|
|
}
|
|
|
|
// 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}`);
|
|
}
|
|
|
|
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(expectedEdge)} in ${inspectNode(rootNodes)}`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 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 = rootNodes.some(
|
|
(node) => node.edges.some(check)
|
|
);
|
|
if (!hasChild) {
|
|
throw new Error(
|
|
'expected to find child ' +
|
|
`${util.inspect(expectedEdge)} in ${inspectNode(rootNodes)}`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
validateSnapshotNodes(rootName, expected, { loose = false } = {}) {
|
|
this.validateSnapshot(rootName, expected, { loose });
|
|
this.validateGraph(rootName, expected, { loose });
|
|
}
|
|
}
|
|
|
|
function recordState() {
|
|
return new State();
|
|
}
|
|
|
|
function validateSnapshotNodes(...args) {
|
|
return recordState().validateSnapshotNodes(...args);
|
|
}
|
|
|
|
module.exports = {
|
|
recordState,
|
|
validateSnapshotNodes
|
|
};
|