简体   繁体   中英

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.

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 }
});

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM