help icon indicating copy to clipboard operation
help copied to clipboard

How expensive to pass handle to child process

Open ORESoftware opened this issue 8 years ago • 6 comments

In the Node.js docs, it shows that you can pass the handle to a request/response using code like so:

const k = cp.fork('./child.js');

app.use(function(req,res,next){
   k.send(`handle`, req.socket);
});

I was wondering how performant that is? Is an expensive process?

ORESoftware avatar Jul 06 '18 20:07 ORESoftware

parent.js

const proc = cp.fork('./child.js');

http.createServer((req, res) => {
  console.log(Date.now());
  proc.send('handle', res.socket);
}).listen(1e3);

child.js

process.on('message', (msg, handle) => {
  console.log(Date.now());
});

Output

> node parent.js
1531082139300
1531082139307

I was wondering how performant that is? Is an expensive process?

It's unclear what you mean by "expensive". It takes 7ms on average.

ghost avatar Jul 08 '18 20:07 ghost

Yeah I think more computation complexity, like how much burden on the CPU. But also useful to know how much time it takes. Do you think that time measurement is accurate?

ORESoftware avatar Jul 08 '18 22:07 ORESoftware

ORESoftware, its going to be dominated by the cost of forking a process, surely. Its not a particularly computationally intense activity - at least not in userland. But, there is administrative costs borne by the kernal/system calls to achieve establishing the new process.

shellberg avatar Jul 09 '18 10:07 shellberg

Well you prob wouldn't fork a new process for each request, you could re-use a child process, but yeah, thx

ORESoftware avatar Jul 10 '18 06:07 ORESoftware

So here we go!

$ cat 1372.js

const cp = require('child_process')
const k = cp.fork('./child.js')
const http = require('http')

http.createServer((req, res) => {
  setTimeout(() => {
  k.send('handle', res.socket);
  }, 3000)
}).listen(12000, () => {
  http.get('http://localhost:12000', (m) => {
    m.on('data', (d) => {
      console.log(d.toString())
    })
  })
})

$ cat child.js

process.on('message', (msg, handle) => {
  console.log('got it')
});

$ strace -f -tttT node 1372.js

relevant section:

PID 20014 = parent, PID 20020: child

// parent waiting (server)
[pid 20014] 1533050332.998961 <... epoll_wait resumed> [], 1024, 2999) = 0 <3.002233>

// parent got request, initiate sending the handle. First, send a protocol message (NODE_HANDLE is coming)..
[pid 20014] 1533050333.000182 sendmsg(12, {msg_name(0)=NULL, msg_iov(1)=[{"{\"cmd\":\"NODE_HANDLE\",\"type\":\"net"..., 76}], msg_controllen=24, [{cmsg_len=20, cmsg_level=SOL_SOCKET, cmsg_type=SCM_RIGHTS, [15]}], msg_flags=0}, 0) = 76 <0.000076>

// child receives the message
[pid 20020] 1533050333.000301 <... epoll_wait resumed> [{EPOLLIN, {u32=3, u64=3}}], 1024, -1) = 1 <2.939558>
[pid 20020] 1533050333.000351 recvmsg(3, {msg_name(0)=NULL, msg_iov(1)=[{"{\"cmd\":\"NODE_HANDLE\",\"type\":\"net"..., 65536}], msg_controllen=24, [{cmsg_len=20, cmsg_level=SOL_SOCKET, cmsg_type=SCM_RIGHTS, [13]}], msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 76 <0.000020>

// child parses the handle
[pid 20020] 1533050333.000425 getsockname(13, {sa_family=AF_INET6, sin6_port=htons(12000), inet_pton(AF_INET6, "::ffff:127.0.0.1", &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, [28]) = 0 <0.000040>
[pid 20020] 1533050333.000497 getsockopt(13, SOL_SOCKET, SO_TYPE, [1], [4]) = 0 <0.000042>

//parent waits.
[pid 20014] 1533050333.000595 epoll_ctl(3, EPOLL_CTL_MOD, 12, {EPOLLIN, {u32=12, u64=12}}) = 0 <0.000022>
[pid 20014] 1533050333.000652 epoll_wait(3, [], 1024, 0) = 0 <0.000012>
[pid 20014] 1533050333.000693 epoll_wait(3,  <unfinished ...>

// child writes an acknowledgement - handle received, thank you!
[pid 20020] 1533050333.002349 write(3, "{\"cmd\":\"NODE_HANDLE_ACK\"}\n", 26) = 26 <0.000050>

// parent received it.
[pid 20014] 1533050333.002419 <... epoll_wait resumed> [{EPOLLIN, {u32=12, u64=12}}], 1024, 116992) = 1 <0.001720>
[pid 20014] 1533050333.002446 recvmsg(12, {msg_name(0)=NULL, msg_iov(1)=[{"{\"cmd\":\"NODE_HANDLE_ACK\"}\n", 65536}], msg_controllen=0, msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 26 <0.000015>

//child code runs
[pid 20020] 1533050333.005049 write(11, "got it\n", 7got it = 7 <0.000018>

so the overall time I would say is 1533050333.002446 - 1533050333.000182 = 2264 microseconds (~2ms). Relative timestamp is the second token in each line, and time spent in each system call is in the angle brackets.

gireeshpunathil avatar Jul 31 '18 15:07 gireeshpunathil

closing as answered, let me know if there is anything else pending on this thread

gireeshpunathil avatar Aug 10 '18 06:08 gireeshpunathil