| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,17 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + const common = require('../common.js'); | ||
| 4 | + const cpus = require('os').cpus; | ||
| 5 | + | ||
| 6 | + const bench = common.createBenchmark(main, { | ||
| 7 | + n: [3e4] | ||
| 8 | + }); | ||
| 9 | + | ||
| 10 | + function main(conf) { | ||
| 11 | + const n = +conf.n; | ||
| 12 | + | ||
| 13 | + bench.start(); | ||
| 14 | + for (var i = 0; i < n; ++i) | ||
| 15 | + cpus(); | ||
| 16 | + bench.end(n); | ||
| 17 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,7 +1,9 @@ | |||
| 1 | 1 | 'use strict'; | |
| 2 | 2 | ||
| 3 | 3 | const binding = process.binding('os'); | |
| 4 | + const getCPUs = binding.getCPUs; | ||
| 4 | 5 | const getLoadAvg = binding.getLoadAvg; | |
| 6 | + const pushValToArrayMax = process.binding('util').pushValToArrayMax; | ||
| 5 | 7 | const constants = process.binding('constants').os; | |
| 6 | 8 | const internalUtil = require('internal/util'); | |
| 7 | 9 | const isWindows = process.platform === 'win32'; | |
@@ -10,7 +12,6 @@ exports.hostname = binding.getHostname; | |||
| 10 | 12 | exports.uptime = binding.getUptime; | |
| 11 | 13 | exports.freemem = binding.getFreeMem; | |
| 12 | 14 | exports.totalmem = binding.getTotalMem; | |
| 13 | - exports.cpus = binding.getCPUs; | ||
| 14 | 15 | exports.type = binding.getOSType; | |
| 15 | 16 | exports.release = binding.getOSRelease; | |
| 16 | 17 | exports.networkInterfaces = binding.getInterfaceAddresses; | |
@@ -23,6 +24,26 @@ exports.loadavg = function loadavg() { | |||
| 23 | 24 | return [avgValues[0], avgValues[1], avgValues[2]]; | |
| 24 | 25 | }; | |
| 25 | 26 | ||
| 27 | + const cpuValues = new Float64Array(6 * pushValToArrayMax); | ||
| 28 | + function addCPUInfo() { | ||
| 29 | + for (var i = 0, c = 0; i < arguments.length; ++i, c += 6) { | ||
| 30 | + this[this.length] = { | ||
| 31 | + model: arguments[i], | ||
| 32 | + speed: cpuValues[c], | ||
| 33 | + times: { | ||
| 34 | + user: cpuValues[c + 1], | ||
| 35 | + nice: cpuValues[c + 2], | ||
| 36 | + sys: cpuValues[c + 3], | ||
| 37 | + idle: cpuValues[c + 4], | ||
| 38 | + irq: cpuValues[c + 5] | ||
| 39 | + } | ||
| 40 | + }; | ||
| 41 | + } | ||
| 42 | + } | ||
| 43 | + exports.cpus = function cpus() { | ||
| 44 | + return getCPUs(addCPUInfo, cpuValues, []); | ||
| 45 | + }; | ||
| 46 | + | ||
| 26 | 47 | Object.defineProperty(exports, 'constants', { | |
| 27 | 48 | configurable: false, | |
| 28 | 49 | enumerable: true, | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,6 +32,7 @@ using v8::ArrayBuffer; | |||
| 32 | 32 | using v8::Boolean; | |
| 33 | 33 | using v8::Context; | |
| 34 | 34 | using v8::Float64Array; | |
| 35 | + using v8::Function; | ||
| 35 | 36 | using v8::FunctionCallbackInfo; | |
| 36 | 37 | using v8::Integer; | |
| 37 | 38 | using v8::Local; | |
@@ -122,36 +123,47 @@ static void GetOSRelease(const FunctionCallbackInfo<Value>& args) { | |||
| 122 | 123 | static void GetCPUInfo(const FunctionCallbackInfo<Value>& args) { | |
| 123 | 124 | Environment* env = Environment::GetCurrent(args); | |
| 124 | 125 | uv_cpu_info_t* cpu_infos; | |
| 125 | - int count, i; | ||
| 126 | + int count, i, field_idx; | ||
| 126 | 127 | ||
| 127 | 128 | int err = uv_cpu_info(&cpu_infos, &count); | |
| 128 | 129 | if (err) | |
| 129 | 130 | return; | |
| 130 | 131 | ||
| 131 | - Local<Array> cpus = Array::New(env->isolate()); | ||
| 132 | - for (i = 0; i < count; i++) { | ||
| 132 | + CHECK(args[0]->IsFunction()); | ||
| 133 | + Local<Function> addfn = args[0].As<Function>(); | ||
| 134 | + | ||
| 135 | + CHECK(args[1]->IsFloat64Array()); | ||
| 136 | + Local<Float64Array> array = args[1].As<Float64Array>(); | ||
| 137 | + CHECK_EQ(array->Length(), 6 * NODE_PUSH_VAL_TO_ARRAY_MAX); | ||
| 138 | + Local<ArrayBuffer> ab = array->Buffer(); | ||
| 139 | + double* fields = static_cast<double*>(ab->GetContents().Data()); | ||
| 140 | + | ||
| 141 | + CHECK(args[2]->IsArray()); | ||
| 142 | + Local<Array> cpus = args[2].As<Array>(); | ||
| 143 | + | ||
| 144 | + Local<Value> model_argv[NODE_PUSH_VAL_TO_ARRAY_MAX]; | ||
| 145 | + int model_idx = 0; | ||
| 146 | + | ||
| 147 | + for (i = 0, field_idx = 0; i < count; i++) { | ||
| 133 | 148 | uv_cpu_info_t* ci = cpu_infos + i; | |
| 134 | 149 | ||
| 135 | - Local<Object> times_info = Object::New(env->isolate()); | ||
| 136 | - times_info->Set(env->user_string(), | ||
| 137 | - Number::New(env->isolate(), ci->cpu_times.user)); | ||
| 138 | - times_info->Set(env->nice_string(), | ||
| 139 | - Number::New(env->isolate(), ci->cpu_times.nice)); | ||
| 140 | - times_info->Set(env->sys_string(), | ||
| 141 | - Number::New(env->isolate(), ci->cpu_times.sys)); | ||
| 142 | - times_info->Set(env->idle_string(), | ||
| 143 | - Number::New(env->isolate(), ci->cpu_times.idle)); | ||
| 144 | - times_info->Set(env->irq_string(), | ||
| 145 | - Number::New(env->isolate(), ci->cpu_times.irq)); | ||
| 146 | - | ||
| 147 | - Local<Object> cpu_info = Object::New(env->isolate()); | ||
| 148 | - cpu_info->Set(env->model_string(), | ||
| 149 | - OneByteString(env->isolate(), ci->model)); | ||
| 150 | - cpu_info->Set(env->speed_string(), | ||
| 151 | - Number::New(env->isolate(), ci->speed)); | ||
| 152 | - cpu_info->Set(env->times_string(), times_info); | ||
| 153 | - | ||
| 154 | - (*cpus)->Set(i, cpu_info); | ||
| 150 | + fields[field_idx++] = ci->speed; | ||
| 151 | + fields[field_idx++] = ci->cpu_times.user; | ||
| 152 | + fields[field_idx++] = ci->cpu_times.nice; | ||
| 153 | + fields[field_idx++] = ci->cpu_times.sys; | ||
| 154 | + fields[field_idx++] = ci->cpu_times.idle; | ||
| 155 | + fields[field_idx++] = ci->cpu_times.irq; | ||
| 156 | + model_argv[model_idx++] = OneByteString(env->isolate(), ci->model); | ||
| 157 | + | ||
| 158 | + if (model_idx >= NODE_PUSH_VAL_TO_ARRAY_MAX) { | ||
| 159 | + addfn->Call(env->context(), cpus, model_idx, model_argv).ToLocalChecked(); | ||
| 160 | + model_idx = 0; | ||
| 161 | + field_idx = 0; | ||
| 162 | + } | ||
| 163 | + } | ||
| 164 | + | ||
| 165 | + if (model_idx > 0) { | ||
| 166 | + addfn->Call(env->context(), cpus, model_idx, model_argv).ToLocalChecked(); | ||
| 155 | 167 | } | |
| 156 | 168 | ||
| 157 | 169 | uv_free_cpu_info(cpu_infos, count); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -141,6 +141,12 @@ void Initialize(Local<Object> target, | |||
| 141 | 141 | } | |
| 142 | 142 | #undef V | |
| 143 | 143 | ||
| 144 | + target->DefineOwnProperty( | ||
| 145 | + env->context(), | ||
| 146 | + OneByteString(env->isolate(), "pushValToArrayMax"), | ||
| 147 | + Integer::NewFromUnsigned(env->isolate(), NODE_PUSH_VAL_TO_ARRAY_MAX), | ||
| 148 | + v8::ReadOnly).FromJust(); | ||
| 149 | + | ||
| 144 | 150 | env->SetMethod(target, "getHiddenValue", GetHiddenValue); | |
| 145 | 151 | env->SetMethod(target, "setHiddenValue", SetHiddenValue); | |
| 146 | 152 | env->SetMethod(target, "getProxyDetails", GetProxyDetails); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments