Skip to content

Commit

Permalink
os: improve loadavg() performance
Browse files Browse the repository at this point in the history
PR-URL: nodejs#11516
Reviewed-By: Jackson Tian <shyvo1987@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
  • Loading branch information
mscdex authored and italoacasas committed Feb 27, 2017
1 parent fe7a722 commit db06c73
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
17 changes: 17 additions & 0 deletions benchmark/os/loadavg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const common = require('../common.js');
const loadavg = require('os').loadavg;

const bench = common.createBenchmark(main, {
n: [5e6]
});

function main(conf) {
const n = +conf.n;

bench.start();
for (var i = 0; i < n; ++i)
loadavg();
bench.end(n);
}
8 changes: 7 additions & 1 deletion lib/os.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

const binding = process.binding('os');
const getLoadAvg = binding.getLoadAvg;
const constants = process.binding('constants').os;
const internalUtil = require('internal/util');
const isWindows = process.platform === 'win32';

exports.hostname = binding.getHostname;
exports.loadavg = binding.getLoadAvg;
exports.uptime = binding.getUptime;
exports.freemem = binding.getFreeMem;
exports.totalmem = binding.getTotalMem;
Expand All @@ -17,6 +17,12 @@ exports.networkInterfaces = binding.getInterfaceAddresses;
exports.homedir = binding.getHomeDirectory;
exports.userInfo = binding.getUserInfo;

const avgValues = new Float64Array(3);
exports.loadavg = function loadavg() {
getLoadAvg(avgValues);
return [avgValues[0], avgValues[1], avgValues[2]];
};

Object.defineProperty(exports, 'constants', {
configurable: false,
enumerable: true,
Expand Down
14 changes: 7 additions & 7 deletions src/node_os.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ namespace node {
namespace os {

using v8::Array;
using v8::ArrayBuffer;
using v8::Boolean;
using v8::Context;
using v8::Float64Array;
using v8::FunctionCallbackInfo;
using v8::Integer;
using v8::Local;
Expand Down Expand Up @@ -182,14 +184,12 @@ static void GetUptime(const FunctionCallbackInfo<Value>& args) {


static void GetLoadAvg(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
double loadavg[3];
CHECK(args[0]->IsFloat64Array());
Local<Float64Array> array = args[0].As<Float64Array>();
CHECK_EQ(array->Length(), 3);
Local<ArrayBuffer> ab = array->Buffer();
double* loadavg = static_cast<double*>(ab->GetContents().Data());
uv_loadavg(loadavg);
Local<Array> loads = Array::New(env->isolate(), 3);
loads->Set(0, Number::New(env->isolate(), loadavg[0]));
loads->Set(1, Number::New(env->isolate(), loadavg[1]));
loads->Set(2, Number::New(env->isolate(), loadavg[2]));
args.GetReturnValue().Set(loads);
}


Expand Down

0 comments on commit db06c73

Please sign in to comment.