Determine which core is running Node.js process at runtime
Process are not attached to specific core in any operating systems (except, maybe, in some real-time oriented one).
Processors (and cores) are resources that can be assigned to any process whenever it needs. One thread can be executed only in a single core at once, but cores are shared by all processes. The operating system is responsible to schedule processes over available cores. So, when any process is "paused" due to let execute (or continue execution of) another process in the same core, there is no reason to expect that process to be resumed in the same core next time.
This is slightly observable when you have single process with a high cpu cosumtion in a (multi-core) machine with a relative low cpu activity by simply executing htop
. Then you could see that there is always a highly occupied core, but which core is it will change regularly.
While trying to think about why you would want to know this kind of information, I think I understand the core of your confusion. Note that a process may switch cores multiple times per millisecond. The OS does a very good job of scheduling this, and I cannot imagine that performance is so much an issue that you would need to improve on this (because in this case you would be writing your own OS layer, and not in node). There will be absolutely NO observable delay between a process that runs on a core and that has to be started on that core, except possibly on embedded hardware with a custom OS.
Note that modern systems can even switch cores on and off. So imagine a webserver with 2 cores and 2 node processes serving requests. At night when there is not much work, the OS switches core 2 off, and both processes run happily both on core 1, serving the occasional request. Then when it gets busier, the OS starts the second core and most of the time both node processes will run at the same time, each on its own core. However note that the OS also has plenty of other processes (for instance, updating the realtime-clock). So it may very well be that at some point node process A runs on core 1, and node process B on core 2. Then core 1 is used to update the clock, while core 2 is still running B. Halfway through updating the clock however process B is stopped on core 2, and process A is started there. When the clock update is done, process B is started again on core 1. All of this happens in less than a microsecond.
So context switches happen millions of times a second on modern architectures, don't worry about them. Even if you find that at some point the 2 node processes run on different cores, there is no guarantee this is still true a microsecond later. And, again, probably the OS will do a much better job optimising this than you.
Now a thing that you could be interested in, is knowing if not for some reason the two processes ALWAYS run on the same core (e.g. because secretly they are not two processes, but one process, such as different requests to the same node server). This is something you only need to check for once. Just load both node instances fully, and check in top/process explorer/etc if their combined CPU usage is above 100%. If so, you can assume they are at least capable of running on different cores, and further assume that the OS will schedule them to different cores if they would benefit from this.
Possible linux way:
function getPSR( pid, callback ) {
var exec = require('child_process').execSync;
var command = 'ps -A -o pid,psr -p ' + pid + ' | grep ' + pid + ' | grep -v grep |head -n 1 | awk \'{print $2}\'';
var result = exec( command );
return result.toString("utf-8").trim();
}
function getTIDs( pid ) {
var exec = require('child_process').execSync;
var command = 'ps -mo tid,psr -p ' + pid + ' | grep -v grep | awk \'/[0-9]/ {print("{\\042id\\042 : ", $1, ",\\042psr\\042:", $2, " }," )}\'';
var tids = '[ ' + exec(command) + '{"id": false} ]';
return JSON.parse(tids);
}
function setPSR( pid, psr ) {
var exec = require('child_process').execSync;
var command = 'taskset -pc ' + psr + ' ' + pid + '; kill -STOP ' + pid + '; kill -CONT ' + pid;
var result = exec(command);
return result.toString("utf-8").trim();
}
function setTIDsPSR( pid, psr ) {
var tids = getTIDs(pid);
console.log(tids);
for (var i in tids) {
if (tids[i].id) {
console.log( setPSR( tids[i].id, psr ) );
}
}
}