Skip to content

Commit 5172196

Browse files
jasnelladuh95
authored andcommitted
src: add a couple fast apis in node_os
PR-URL: #58210 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]>
1 parent 7d00fc2 commit 5172196

File tree

3 files changed

+71
-4
lines changed

3 files changed

+71
-4
lines changed

src/node_external_reference.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ using CFunctionCallbackWithUint8ArrayUint32Int64Bool =
5858
bool);
5959
using CFunctionWithUint32 = uint32_t (*)(v8::Local<v8::Value>,
6060
const uint32_t input);
61+
using CFunctionWithReturnUint32 = uint32_t (*)(v8::Local<v8::Value>);
62+
using CFunctionWithReturnDouble = double (*)(v8::Local<v8::Value>);
6163
using CFunctionWithDoubleReturnDouble = double (*)(v8::Local<v8::Value>,
6264
v8::Local<v8::Value>,
6365
const double);
@@ -96,6 +98,7 @@ class ExternalReferenceRegistry {
9698
V(CFunctionCallbackReturnBool) \
9799
V(CFunctionCallbackReturnDouble) \
98100
V(CFunctionCallbackReturnInt32) \
101+
V(CFunctionWithReturnUint32) \
99102
V(CFunctionCallbackValueReturnDouble) \
100103
V(CFunctionCallbackValueReturnDoubleUnusedReceiver) \
101104
V(CFunctionCallbackWithInt64) \

src/node_os.cc

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
// USE OR OTHER DEALINGS IN THE SOFTWARE.
2121

2222
#include "env-inl.h"
23+
#include "node_debug.h"
2324
#include "node_external_reference.h"
2425
#include "string_bytes.h"
2526

@@ -148,12 +149,26 @@ static void GetFreeMemory(const FunctionCallbackInfo<Value>& args) {
148149
args.GetReturnValue().Set(amount);
149150
}
150151

152+
static double FastGetFreeMemory(Local<Value> receiver) {
153+
TRACK_V8_FAST_API_CALL("os.freemem");
154+
return static_cast<double>(uv_get_free_memory());
155+
}
156+
157+
static v8::CFunction fast_get_free_memory(
158+
v8::CFunction::Make(FastGetFreeMemory));
151159

152160
static void GetTotalMemory(const FunctionCallbackInfo<Value>& args) {
153161
double amount = static_cast<double>(uv_get_total_memory());
154162
args.GetReturnValue().Set(amount);
155163
}
156164

165+
double FastGetTotalMemory(Local<Value> receiver) {
166+
TRACK_V8_FAST_API_CALL("os.totalmem");
167+
return static_cast<double>(uv_get_total_memory());
168+
}
169+
170+
static v8::CFunction fast_get_total_memory(
171+
v8::CFunction::Make(FastGetTotalMemory));
157172

158173
static void GetUptime(const FunctionCallbackInfo<Value>& args) {
159174
Environment* env = Environment::GetCurrent(args);
@@ -406,6 +421,14 @@ static void GetAvailableParallelism(const FunctionCallbackInfo<Value>& args) {
406421
args.GetReturnValue().Set(parallelism);
407422
}
408423

424+
uint32_t FastGetAvailableParallelism(v8::Local<v8::Value> receiver) {
425+
TRACK_V8_FAST_API_CALL("os.availableParallelism");
426+
return uv_available_parallelism();
427+
}
428+
429+
static v8::CFunction fast_get_available_parallelism(
430+
v8::CFunction::Make(FastGetAvailableParallelism));
431+
409432
void Initialize(Local<Object> target,
410433
Local<Value> unused,
411434
Local<Context> context,
@@ -414,16 +437,21 @@ void Initialize(Local<Object> target,
414437
SetMethod(context, target, "getHostname", GetHostname);
415438
SetMethod(context, target, "getLoadAvg", GetLoadAvg);
416439
SetMethod(context, target, "getUptime", GetUptime);
417-
SetMethod(context, target, "getTotalMem", GetTotalMemory);
418-
SetMethod(context, target, "getFreeMem", GetFreeMemory);
440+
SetFastMethodNoSideEffect(
441+
context, target, "getTotalMem", GetTotalMemory, &fast_get_total_memory);
442+
SetFastMethodNoSideEffect(
443+
context, target, "getFreeMem", GetFreeMemory, &fast_get_free_memory);
419444
SetMethod(context, target, "getCPUs", GetCPUInfo);
420445
SetMethod(context, target, "getInterfaceAddresses", GetInterfaceAddresses);
421446
SetMethod(context, target, "getHomeDirectory", GetHomeDirectory);
422447
SetMethod(context, target, "getUserInfo", GetUserInfo);
423448
SetMethod(context, target, "setPriority", SetPriority);
424449
SetMethod(context, target, "getPriority", GetPriority);
425-
SetMethod(
426-
context, target, "getAvailableParallelism", GetAvailableParallelism);
450+
SetFastMethodNoSideEffect(context,
451+
target,
452+
"getAvailableParallelism",
453+
GetAvailableParallelism,
454+
&fast_get_available_parallelism);
427455
SetMethod(context, target, "getOSInformation", GetOSInformation);
428456
target
429457
->Set(context,
@@ -437,14 +465,20 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
437465
registry->Register(GetLoadAvg);
438466
registry->Register(GetUptime);
439467
registry->Register(GetTotalMemory);
468+
registry->Register(FastGetTotalMemory);
469+
registry->Register(fast_get_total_memory.GetTypeInfo());
440470
registry->Register(GetFreeMemory);
471+
registry->Register(FastGetFreeMemory);
472+
registry->Register(fast_get_free_memory.GetTypeInfo());
441473
registry->Register(GetCPUInfo);
442474
registry->Register(GetInterfaceAddresses);
443475
registry->Register(GetHomeDirectory);
444476
registry->Register(GetUserInfo);
445477
registry->Register(SetPriority);
446478
registry->Register(GetPriority);
447479
registry->Register(GetAvailableParallelism);
480+
registry->Register(FastGetAvailableParallelism);
481+
registry->Register(fast_get_available_parallelism.GetTypeInfo());
448482
registry->Register(GetOSInformation);
449483
}
450484

test/parallel/test-os-fast.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Flags: --expose-internals --no-warnings --allow-natives-syntax
2+
'use strict';
3+
4+
const common = require('../common');
5+
const assert = require('assert');
6+
const {
7+
totalmem,
8+
freemem,
9+
availableParallelism,
10+
} = require('os');
11+
12+
const { internalBinding } = require('internal/test/binding');
13+
14+
function testFastOs() {
15+
assert.strictEqual(typeof totalmem(), 'number');
16+
assert.strictEqual(typeof freemem(), 'number');
17+
assert.strictEqual(typeof availableParallelism(), 'number');
18+
}
19+
20+
eval('%PrepareFunctionForOptimization(testFastOs)');
21+
testFastOs();
22+
eval('%OptimizeFunctionOnNextCall(testFastOs)');
23+
testFastOs();
24+
25+
if (common.isDebug) {
26+
const { getV8FastApiCallCount } = internalBinding('debug');
27+
assert.strictEqual(getV8FastApiCallCount('os.totalmem'), 1);
28+
assert.strictEqual(getV8FastApiCallCount('os.freemem'), 1);
29+
assert.strictEqual(getV8FastApiCallCount('os.availableParallelism'), 1);
30+
}

0 commit comments

Comments
 (0)