Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CPU usage of a process in NodeJS

I have a NodeJS application that spawns processes on a server with 24 cores. As a rule, I do not spawn more than one process per core (for a total of 24 processes at most). I would like to know the CPU usage of the core where each process is spawn. Is it possible to investigate something like this? I know I can use os.cpus(), but this returns a more general info on all the CPUs in the machine rather than the one where the process I'm querying is working on.

Is it possible to do something like this with what we have in NodeJS? I wouldn't like to rely on executing (heavy) bash scripts from NodeJS to know something like that.

like image 801
Masiar Avatar asked Jan 23 '13 15:01

Masiar


2 Answers

Try node-usage for tracking CPU using the PID

You can use node-usage like below

var spawn = require('child_process').spawn;
var usage = require('usage');

var ffmpeg = spawn('ffmpeg');
usage.lookup(ffmpeg.pid, function(err, usageInfo) {

    console.log(usageInfo); // { cpu: 2.4, memory: 100065280 }
});
like image 147
Arunoda Susiripala Avatar answered Oct 03 '22 22:10

Arunoda Susiripala


Of course I could be wrong, but I'm not sure there is an API for worker-process CPU usage, but you can get per core for all cores: http://nodejs.org/api/os.html#os_os_cpus

On another note: For such power, you might want to consider checking that you have access to all of the system's memory. V8 limits to a default that requires re-compiling in order to have "deeper-pockets' for memory access.

like image 24
astone26 Avatar answered Oct 03 '22 22:10

astone26