Skip to content

Commit d075814

Browse files
joyeecheungtargos
authored andcommitted
src: replace heap_utils.createHeapSnapshot with v8.getHeapSnapshot
Remove the internal testing utility and use the public API instead. PR-URL: #26671 Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Minwoo Jung <minwoo@nodesource.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 31995e4 commit d075814

File tree

4 files changed

+77
-141
lines changed

4 files changed

+77
-141
lines changed

lib/internal/test/heap.js

-89
This file was deleted.

node.gyp

-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@
179179
'lib/internal/repl/utils.js',
180180
'lib/internal/socket_list.js',
181181
'lib/internal/test/binding.js',
182-
'lib/internal/test/heap.js',
183182
'lib/internal/timers.js',
184183
'lib/internal/tls.js',
185184
'lib/internal/trace_events_async_hooks.js',

src/heap_utils.cc

-48
Original file line numberDiff line numberDiff line change
@@ -200,40 +200,6 @@ void BuildEmbedderGraph(const FunctionCallbackInfo<Value>& args) {
200200
args.GetReturnValue().Set(ret);
201201
}
202202

203-
204-
class BufferOutputStream : public v8::OutputStream {
205-
public:
206-
BufferOutputStream() : buffer_(new JSString()) {}
207-
208-
void EndOfStream() override {}
209-
int GetChunkSize() override { return 1024 * 1024; }
210-
WriteResult WriteAsciiChunk(char* data, int size) override {
211-
buffer_->Append(data, size);
212-
return kContinue;
213-
}
214-
215-
Local<String> ToString(Isolate* isolate) {
216-
return String::NewExternalOneByte(isolate,
217-
buffer_.release()).ToLocalChecked();
218-
}
219-
220-
private:
221-
class JSString : public String::ExternalOneByteStringResource {
222-
public:
223-
void Append(char* data, size_t count) {
224-
store_.append(data, count);
225-
}
226-
227-
const char* data() const override { return store_.data(); }
228-
size_t length() const override { return store_.size(); }
229-
230-
private:
231-
std::string store_;
232-
};
233-
234-
std::unique_ptr<JSString> buffer_;
235-
};
236-
237203
namespace {
238204
class FileOutputStream : public v8::OutputStream {
239205
public:
@@ -370,17 +336,6 @@ inline bool WriteSnapshot(Isolate* isolate, const char* filename) {
370336

371337
} // namespace
372338

373-
void CreateHeapSnapshot(const FunctionCallbackInfo<Value>& args) {
374-
Isolate* isolate = args.GetIsolate();
375-
BufferOutputStream out;
376-
TakeSnapshot(isolate, &out);
377-
Local<Value> ret;
378-
if (JSON::Parse(isolate->GetCurrentContext(),
379-
out.ToString(isolate)).ToLocal(&ret)) {
380-
args.GetReturnValue().Set(ret);
381-
}
382-
}
383-
384339
void CreateHeapSnapshotStream(const FunctionCallbackInfo<Value>& args) {
385340
Environment* env = Environment::GetCurrent(args);
386341
HandleScope scope(env->isolate());
@@ -430,9 +385,6 @@ void Initialize(Local<Object> target,
430385
env->SetMethodNoSideEffect(target,
431386
"buildEmbedderGraph",
432387
BuildEmbedderGraph);
433-
env->SetMethodNoSideEffect(target,
434-
"createHeapSnapshot",
435-
CreateHeapSnapshot);
436388
env->SetMethodNoSideEffect(target,
437389
"triggerHeapSnapshot",
438390
TriggerHeapSnapshot);

test/common/heap.js

+77-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,88 @@
33
const assert = require('assert');
44
const util = require('util');
55

6-
let internalTestHeap;
6+
let internalBinding;
77
try {
8-
internalTestHeap = require('internal/test/heap');
8+
internalBinding = require('internal/test/binding').internalBinding;
99
} catch (e) {
1010
console.log('using `test/common/heap.js` requires `--expose-internals`');
1111
throw e;
1212
}
13-
const { createJSHeapSnapshot, buildEmbedderGraph } = internalTestHeap;
13+
14+
const { buildEmbedderGraph } = internalBinding('heap_utils');
15+
const { getHeapSnapshot } = require('v8');
16+
17+
function createJSHeapSnapshot() {
18+
const stream = getHeapSnapshot();
19+
stream.pause();
20+
const dump = JSON.parse(stream.read());
21+
const meta = dump.snapshot.meta;
22+
23+
const nodes =
24+
readHeapInfo(dump.nodes, meta.node_fields, meta.node_types, dump.strings);
25+
const edges =
26+
readHeapInfo(dump.edges, meta.edge_fields, meta.edge_types, dump.strings);
27+
28+
for (const node of nodes) {
29+
node.incomingEdges = [];
30+
node.outgoingEdges = [];
31+
}
32+
33+
let fromNodeIndex = 0;
34+
let edgeIndex = 0;
35+
for (const { type, name_or_index, to_node } of edges) {
36+
while (edgeIndex === nodes[fromNodeIndex].edge_count) {
37+
edgeIndex = 0;
38+
fromNodeIndex++;
39+
}
40+
const toNode = nodes[to_node / meta.node_fields.length];
41+
const fromNode = nodes[fromNodeIndex];
42+
const edge = {
43+
type,
44+
to: toNode,
45+
from: fromNode,
46+
name: typeof name_or_index === 'string' ? name_or_index : null
47+
};
48+
toNode.incomingEdges.push(edge);
49+
fromNode.outgoingEdges.push(edge);
50+
edgeIndex++;
51+
}
52+
53+
for (const node of nodes) {
54+
assert.strictEqual(node.edge_count, node.outgoingEdges.length,
55+
`${node.edge_count} !== ${node.outgoingEdges.length}`);
56+
}
57+
return nodes;
58+
}
59+
60+
function readHeapInfo(raw, fields, types, strings) {
61+
const items = [];
62+
63+
for (let i = 0; i < raw.length; i += fields.length) {
64+
const item = {};
65+
for (let j = 0; j < fields.length; j++) {
66+
const name = fields[j];
67+
let type = types[j];
68+
if (Array.isArray(type)) {
69+
item[name] = type[raw[i + j]];
70+
} else if (name === 'name_or_index') { // type === 'string_or_number'
71+
if (item.type === 'element' || item.type === 'hidden')
72+
type = 'number';
73+
else
74+
type = 'string';
75+
}
76+
77+
if (type === 'string') {
78+
item[name] = strings[raw[i + j]];
79+
} else if (type === 'number' || type === 'node') {
80+
item[name] = raw[i + j];
81+
}
82+
}
83+
items.push(item);
84+
}
85+
86+
return items;
87+
}
1488

1589
function inspectNode(snapshot) {
1690
return util.inspect(snapshot, { depth: 4 });

0 commit comments

Comments
 (0)