CPU.loadAvg = function() { CPU.last.walltime = proc.gettimeofday(); CPU.last.systime = proc.getsystime(); CPU.last.usertime = proc.getusertime(); // add a single sample var o = { at: CPU.last.walltime, walltime: CPU.last.walltime, systime: CPU.last.systime, usertime: CPU.last.usertime, }; CPU.samples.push(o); // collect the min and max times over 1, 5 and 10 s var max = o; var min = o; // get samples CPU.samples = CPU.samples.filter(function(s) { // microsecond = 1000 milliseconds, and 1000 milliseconds = 1 second // only min can change, since samples are from the past if( s.at < min.at ) { min = s; } // remove values older than 100 ms return (s.at > CPU.last.walltime - SAMPLE_SPEED * 10 * 1000); }); var result = CPU.calcUsage(min, max); CPU.load = result.percentage; };
CPU.getCPU = function() { return { real: proc.gettimeofday(), user: proc.getsystime(), sys: proc.getusertime(), percentage: CPU.load, } };