18

How do you start a node process, targetting a specific CPU core?

I've seen node cluster, but I'm interested in starting two different processes, on different cores.

I was assuming there was a way of doing this when starting node from the command line, i.e:

node myapp.js

I'd be interested to know how to do this in both windows and linux, if there is a difference.

UpTheCreek
  • 31,444
  • 34
  • 152
  • 221

2 Answers2

16

In general, the scheduler will do a pretty good job of this without any help. In the wild, I've only seen one situation where this mattered....

We were deploying a node service on an 8-core box and during load testing we made the strangest observation... the service actually performed better with 7 workers than with 8. A bit of debugging later and we figured out that all network interrupts were being handled by core 0. I played with using 15 workers so that core0 would have a 1/2 share of the load compared to the other cores. Ultimately, I think we ended up going with 7 workers because it was simpler and more predictable and the complexity just wasn't worth it to get ~7% more theoretic throughput.

That being said, to set CPU affinity on linux:

$ taskset -pc 3089
pid 3089's current affinity list: 0,1

$ taskset -p 3089
pid 3089's current affinity mask: 3 # core 0 = 0x1, core 1 = 0x2


$ taskset -pc 1,2,3 3089 
pid 3089's current affinity list: 0,1
pid 3089's new affinity list: 1
Dave Dopson
  • 41,600
  • 19
  • 95
  • 85
  • In general, the scheduler will do a pretty good job of this without any help I see. I think I was getting too set on forcing the affinity manually. Do you know where the behaviour of the scheduler is documented? E.g. if you start 4 node services on 4 core machine, will each be executed on a different core, just like that? – UpTheCreek Sep 21 '12 at 16:39
  • There's actually multiple kernel schedulers, depending on OS version and with Linux, you can swap in a few alternatives that optimize for various cases. In general, if you have N things that want to run, and there are N cores, the scheduler has a pretty easy job. It's not foolproof, but what I've found is that it's about as easy to make perf WORSE as it is to make it better my messing with core affinity. It takes a lot of careful profiling to be able to understand what's actually going on and prove that you made things better by adding a constraint to the system. – Dave Dopson Sep 22 '12 at 23:12
  • "If you start 4 node services on a 4 core machine, will each be executed on a different core?" -- in a word, yes. And as fun as it is to geek out and measure things (been there, did that), you are unlikely to get much of a boost over what the system will do automatically. I'd wait until you have a demonstrable problem before you try to fix something not broken. Besides, you shouldn't be pushing these boxes anywhere near 100% load if they are servicing requests (queue workers are a different story). Even 50% load is really aggressive and will result in a lot of extra request latency. – Dave Dopson Sep 22 '12 at 23:15
  • thanks for the extra info. I don't really have much choice in pushing the boxes to a degree though, we're doing websocket messaging with quite high concurrency and frequency, which becomes an N+1 type problem :/ – UpTheCreek Sep 24 '12 at 06:48
  • the problem is that node creates multiple processes and it won't work with any of the child ones – Flash Thunder Jun 19 '19 at 14:58
  • Spending some quality time with htop, this no longer seems to be the case. I'm seeing the processes with the highest TIME (the event loop) bouncing from core to core to core. It's possible that pinning only happens when you are the only application running on the VM, but that is often not the case with Docker. – Jason Feb 03 '22 at 03:13
5

On linux you can use taskset to run node with a given CPU affinity. See this post for information on using the start command in Windows to do the same.

Community
  • 1
  • 1
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471