Skip to content

Commit

Permalink
src: support diagnostics channel in the snapshot
Browse files Browse the repository at this point in the history
PR-URL: nodejs#44193
Refs: nodejs#44014
Refs: nodejs#37476
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Chengzhong Wu <[email protected]>
  • Loading branch information
joyeecheung authored and Fyko committed Sep 15, 2022
1 parent deb03c4 commit 82f22ef
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 18 deletions.
4 changes: 2 additions & 2 deletions lib/internal/main/mksnapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const supportedModules = new SafeSet(new SafeArrayIterator([
'constants',
'crypto',
// 'dgram',
// 'diagnostics_channel',
'diagnostics_channel',
// 'dns',
// 'dns/promises',
// 'domain',
Expand All @@ -60,7 +60,7 @@ const supportedModules = new SafeSet(new SafeArrayIterator([
// 'https',
// 'inspector',
// 'module',
// 'net',
'net',
'os',
'path',
'path/posix',
Expand Down
20 changes: 4 additions & 16 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
60 changes: 60 additions & 0 deletions test/fixtures/snapshot/server.js
Original file line number Diff line number Diff line change
@@ -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);
});
1 change: 1 addition & 0 deletions test/parallel/test-bootstrap-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
65 changes: 65 additions & 0 deletions test/parallel/test-snapshot-net.js
Original file line number Diff line number Diff line change
@@ -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/);
}

0 comments on commit 82f22ef

Please sign in to comment.