How to get CPU and memory consumption in Total.js?
- it will work in Linux and macOS
- operations are supported in Total.js
+v2.4.0
.
Create an operation
The operation below reads CPU
and RSS
(memory) information from terminal, it's the information from the operation system.
E.g. /definitions/operations.js
:
const Exec = require('child_process').exec;
NEWOPERATION('consumption', function(error, value, callback) {
var async = [];
var output = { cpu: 0, rss: 0 };
Exec('ps -p {0} -o %cpu,rss,etime'.format(process.pid), function(err, response) {
err && error.push(err);
response.parseTerminal(['%CPU', 'RSS'], function(line) {
output.cpu = U.parseFloat(line[0]);
output.rss = U.parseFloat(line[1]) * 1024; // bytes
});
callback(output);
});
});
Usage
You can execute the code below anywhere in your Total.js application:
OPERATION('consumption', function(err, response) {
console.log(err, response.cpu + '%', response.rss.filesize();
// null '24.3%' '40.45 MB'
});