src: support diagnostics channel in the snapshot

PR-URL: https://github.com/nodejs/node/pull/44193
Refs: https://github.com/nodejs/node/issues/44014
Refs: https://github.com/nodejs/node/issues/37476
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
This commit is contained in:
Joyee Cheung
2022-08-10 00:41:23 +08:00
parent d70aab663c
commit fddc701d3c
5 changed files with 132 additions and 18 deletions

View File

@@ -48,7 +48,7 @@ const supportedModules = new SafeSet(new SafeArrayIterator([
'constants',
'crypto',
// 'dgram',
// 'diagnostics_channel',
'diagnostics_channel',
// 'dns',
// 'dns/promises',
// 'domain',
@@ -60,7 +60,7 @@ const supportedModules = new SafeSet(new SafeArrayIterator([
// 'https',
// 'inspector',
// 'module',
// 'net',
'net',
'os',
'path',
'path/posix',

View File

@@ -131,20 +131,9 @@ const noop = () => {};
const kPerfHooksNetConnectContext = Symbol('kPerfHooksNetConnectContext');
let netClientSocketChannel;
let netServerSocketChannel;
function lazyChannels() {
// TODO(joyeecheung): support diagnostics channels in the snapshot.
// For now it is fine to create them lazily when there isn't a snapshot to
// build. If users need the channels they would have to create them first
// before invoking any built-ins that would publish to these channels
// anyway.
if (netClientSocketChannel === undefined) {
const dc = require('diagnostics_channel');
netClientSocketChannel = dc.channel('net.client.socket');
netServerSocketChannel = dc.channel('net.server.socket');
}
}
const dc = require('diagnostics_channel');
const netClientSocketChannel = dc.channel('net.client.socket');
const netServerSocketChannel = dc.channel('net.server.socket');
const {
hasObserver,
@@ -217,7 +206,7 @@ function connect(...args) {
const options = normalized[0];
debug('createConnection', normalized);
const socket = new Socket(options);
lazyChannels();
if (netClientSocketChannel.hasSubscribers) {
netClientSocketChannel.publish({
socket,
@@ -1761,7 +1750,6 @@ function onconnection(err, clientHandle) {
socket.server = self;
socket._server = self;
self.emit('connection', socket);
lazyChannels();
if (netServerSocketChannel.hasSubscribers) {
netServerSocketChannel.publish({
socket,

60
test/fixtures/snapshot/server.js vendored Normal file
View File

@@ -0,0 +1,60 @@
'use strict';
const net = require('net');
const {
setDeserializeMainFunction
} = require('v8').startupSnapshot;
const dc = require('diagnostics_channel');
const echoServer = net.Server(function(connection) {
connection.on('data', function(chunk) {
connection.write(chunk);
});
connection.on('end', function() {
connection.end();
});
});
const kNumChars = 256;
const buffer = new Uint8Array(kNumChars);
for (let i = 0; i < kNumChars; ++i) {
buffer[i] = i;
}
let recv = '';
echoServer.on('listening', function() {
const port = this.address().port;
console.log(`server port`, port);
const c = net.createConnection({ host: '127.0.0.1', port });
c.on('data', function(chunk) {
recv += chunk.toString('latin1');
if (recv.length === buffer.length) {
c.end();
}
});
c.on('connect', function() {
c.write(buffer);
});
c.on('close', function() {
console.log(`recv.length: ${recv.length}`);
echoServer.close();
});
});
dc.subscribe('net.server.socket', (({ socket }) => {
console.log(`From server diagnostics channel:`, socket.localPort);
}));
dc.subscribe('net.client.socket', (({ socket }) => {
console.log(`From client diagnostics channel`);
}));
setDeserializeMainFunction(() => {
echoServer.listen(0);
});

View File

@@ -46,6 +46,7 @@ const expectedModules = new Set([
'Internal Binding wasm_web_api',
'Internal Binding worker',
'NativeModule buffer',
'NativeModule diagnostics_channel',
'NativeModule events',
'NativeModule fs',
'NativeModule internal/abort_controller',

View File

@@ -0,0 +1,65 @@
'use strict';
// This tests that a local TCP server can be snapshotted and the
// diagnostics channels work across serialization.
require('../common');
const assert = require('assert');
const { spawnSync } = require('child_process');
const tmpdir = require('../common/tmpdir');
const fixtures = require('../common/fixtures');
const path = require('path');
const fs = require('fs');
tmpdir.refresh();
const blobPath = path.join(tmpdir.path, 'snapshot.blob');
const entry = fixtures.path('snapshot', 'server.js');
{
const child = spawnSync(process.execPath, [
'--snapshot-blob',
blobPath,
'--build-snapshot',
entry,
], {
cwd: tmpdir.path
});
if (child.status !== 0) {
console.log(child.stderr.toString());
console.log(child.stdout.toString());
assert.strictEqual(child.status, 0);
}
const stats = fs.statSync(path.join(tmpdir.path, 'snapshot.blob'));
assert(stats.isFile());
}
{
const child = spawnSync(process.execPath, [
'--snapshot-blob',
blobPath,
], {
cwd: tmpdir.path,
env: {
...process.env,
}
});
const stdout = child.stdout.toString().trim();
console.log(`[stdout]:\n${stdout}\n`);
const stderr = child.stderr.toString().trim();
console.log(`[stderr]:\n${stderr}\n`);
assert.strictEqual(child.status, 0);
const lines = stdout.split('\n');
assert.strictEqual(lines.length, 4);
// The log should look like this:
// server port ${port}
// From client diagnostics channel
// From server diagnostics channel: ${port}
// recv.length: 256
assert.match(lines[0], /server port (\d+)/);
const port = lines[0].match(/server port (\d+)/)[1];
assert.match(lines[1], /From client diagnostics channel/);
assert.match(lines[2], new RegExp(`From server diagnostics channel: ${port}`));
assert.match(lines[3], /recv\.length: 256/);
}