Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js - targeting a cpu core

Tags:

node.js

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.

like image 845
UpTheCreek Avatar asked Sep 14 '12 21:09

UpTheCreek


People also ask

How do I use all cores in CPU Nodejs?

You may run your node. js application on multiple cores by using the cluster module on combination with os module which may be used to detect how many CPUs you have. Show activity on this post. I'm using Node worker to run processes in a simple way from my main process.

Can we use node JS for CPU intensive applications?

js being single-threaded. The single-threaded implementation makes Node a bad choice for CPU-intensive programs. When a time-consuming task is running in the program it blocks the event loop from moving forward for a longer period.

Does Node benefit from multiple cores?

Introduction. A single instance of a Node. js application runs on only one thread and, therefore, doesn't take full advantage of multi-core systems. There will be times when you may want to launch a cluster of Node.

What is Libuv and how does node js use it?

libuv: libuv is a C library originally written for Node. js to abstract non-blocking I/O operations. Event-driven asynchronous I/O model is integrated. It allows the CPU and other resources to be used simultaneously while still performing I/O operations, thereby resulting in efficient use of resources and network.


2 Answers

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
like image 181
Dave Dopson Avatar answered Oct 23 '22 14:10

Dave Dopson


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.

like image 25
JohnnyHK Avatar answered Oct 23 '22 15:10

JohnnyHK