Comparing version 0.2.2 to 0.2.3
@@ -1,13 +0,10 @@ | ||
var usage = require('./'); | ||
var usage = require('../'); | ||
usage.setKeepHistory(true); | ||
checkUsage(); | ||
setInterval(checkUsage, 1000); | ||
setInterval(function() { | ||
function checkUsage() { | ||
usage.lookup(parseInt(process.argv[2]), function(err, stat) { | ||
usage.lookup(process.pid, prt); | ||
} | ||
function prt () { | ||
console.log(arguments); | ||
} | ||
console.log(err, stat); | ||
}); | ||
}, 2000); |
var sysinfo = require('bindings')('sysinfo'); | ||
var fs = require('fs'); | ||
console.log(sysinfo); | ||
var UPTIME_FILE = '/proc/uptime'; | ||
@@ -15,4 +13,9 @@ var STAT_FILE = '/proc/$pid/stat'; | ||
function usageLookup(pid, callback) { | ||
var keepHistory = false; | ||
var historyCpuUsage = {}; | ||
function usageLookup(pid, callback, useHistory) { | ||
useHistory = useHistory || true; | ||
if(!sysinfo.IS_SUPPORTED) { | ||
@@ -39,6 +42,20 @@ return callback(new Error('NOT_SUPPORTED_ON_THIS_PLATFORM')); | ||
var usageData = { | ||
cpu: calculateCpuUsage(uptime, stat), | ||
memory: calculateMemoryUsage(stat) | ||
}; | ||
if(historyCpuUsage[pid] && useHistory) { | ||
usageData['cpu'] = calculateCpuUsageFromHistory(uptime, stat, historyCpuUsage[pid]); | ||
} else { | ||
usageData['cpu'] = calculateCpuUsage(uptime, stat); | ||
} | ||
if(keepHistory) { | ||
//save totalTime in history | ||
historyCpuUsage[pid] = { | ||
timestamp: Date.now(), | ||
stat: stat, | ||
uptime: uptime | ||
}; | ||
} | ||
callback(null, usageData); | ||
@@ -57,2 +74,14 @@ } | ||
function calculateCpuUsageFromHistory(uptime, stat, lastUsage) { | ||
var totalTime = (stat.stime + stat.utime) / sysinfo.HERTZ; | ||
var lastTotalTime = (lastUsage.stat.stime + lastUsage.stat.utime) / sysinfo.HERTZ; | ||
var usedTimeSinceLast = totalTime - lastTotalTime; | ||
var timeSpent = uptime - lastUsage.uptime; | ||
var pcpu = (usedTimeSinceLast / timeSpent) * 100; | ||
return pcpu; | ||
} | ||
function calculateMemoryUsage(stat) { | ||
@@ -101,4 +130,20 @@ | ||
function setKeepHistory (value) { | ||
keepHistory = value; | ||
} | ||
function clearHistory (pid) { | ||
if(pid) { | ||
historyCpuUsage[pid] = null; | ||
} else { | ||
historyCpuUsage = {}; | ||
} | ||
} | ||
exports.lookup = usageLookup; | ||
exports.setKeepHistory = setKeepHistory; | ||
exports.clearHistory = clearHistory; | ||
exports._getUptime= getUptime; | ||
exports._getStat = getStat; |
{ | ||
"name": "usage", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "simple way to lookup linux process usage", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -29,2 +29,20 @@ [![Build Status](https://travis-ci.org/arunoda/node-usage.png?branch=master)](https://travis-ci.org/arunoda/node-usage) | ||
## Average CPU usage vs Current CPU usage | ||
By default CPU Percentage provided is a average from the starting time of the process. It does not correctly reflect the current CPU usage. (this is also a problem with linux `ps` utility) | ||
But If you call `usage.lookup()` continuously for a given pid, you can turn on **keepHistory** flag and you'll get the CPU usage since last time you track the usage. This reflects the current CPU usage. | ||
see following example to enable keepHistory flag | ||
~~~js | ||
usage.setKeepHistory(true); | ||
~~~ | ||
you can clear history cached too | ||
~~~js | ||
usage.clearHistory(pid); //clear history for the given pid | ||
usage.clearHistory(); //clean history for all pids | ||
~~~ | ||
## Motivation | ||
@@ -31,0 +49,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8261
152
52
12