Skip to content

perf_hooks: couple of perf hooks improvements #58209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmark/worker/bench-eventlooputil.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if (process.argv[2] === 'idle cats') {
}

const bench = common.createBenchmark(main, {
n: [1e6],
n: [1e7],
method: [
'ELU_simple',
'ELU_passed',
Expand Down
1 change: 1 addition & 0 deletions lib/internal/perf/nodetiming.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ class PerformanceNodeTiming {
loopStart: this.loopStart,
loopExit: this.loopExit,
idleTime: this.idleTime,
uvMetricsInfo: this.uvMetricsInfo,
};
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/node_external_reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ using CFunctionCallbackReturnInt32 =
v8::FastApiCallbackOptions& options);
using CFunctionCallbackValueReturnDouble =
double (*)(v8::Local<v8::Value> receiver);
using CFunctionCallbackValueReturnDoubleOptions =
double (*)(v8::Local<v8::Value>,
// NOLINTNEXTLINE(runtime/references) This is V8 api.
v8::FastApiCallbackOptions&);
using CFunctionCallbackValueReturnDoubleUnusedReceiver =
double (*)(v8::Local<v8::Value> unused, v8::Local<v8::Value> receiver);
using CFunctionCallbackWithInt64 = void (*)(v8::Local<v8::Object> unused,
Expand Down Expand Up @@ -111,6 +115,7 @@ class ExternalReferenceRegistry {
V(CFunctionCallbackReturnInt32) \
V(CFunctionWithReturnUint32) \
V(CFunctionCallbackValueReturnDouble) \
V(CFunctionCallbackValueReturnDoubleOptions) \
V(CFunctionCallbackValueReturnDoubleUnusedReceiver) \
V(CFunctionCallbackWithInt64) \
V(CFunctionCallbackWithBool) \
Expand Down
18 changes: 17 additions & 1 deletion src/node_perf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "histogram-inl.h"
#include "memory_tracker-inl.h"
#include "node_buffer.h"
#include "node_debug.h"
#include "node_external_reference.h"
#include "node_internals.h"
#include "node_process-inl.h"
Expand All @@ -17,6 +18,7 @@ namespace performance {
using v8::Array;
using v8::Context;
using v8::DontDelete;
using v8::FastApiCallbackOptions;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::GCCallbackFlags;
Expand Down Expand Up @@ -263,6 +265,17 @@ void LoopIdleTime(const FunctionCallbackInfo<Value>& args) {
args.GetReturnValue().Set(1.0 * idle_time / NANOS_PER_MILLIS);
}

static double FastLoopIdleTime(
v8::Local<v8::Value> receiver,
FastApiCallbackOptions& options) { // NOLINT(runtime/references)
TRACK_V8_FAST_API_CALL("performance.loopIdleTime");
Environment* env = Environment::GetCurrent(options.isolate);
uint64_t idle_time = uv_metrics_idle_time(env->event_loop());
return 1.0 * idle_time / NANOS_PER_MILLIS;
}

static v8::CFunction fast_loop_idle_time(v8::CFunction::Make(FastLoopIdleTime));

void UvMetricsInfo(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
Expand Down Expand Up @@ -338,7 +351,8 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data,
"removeGarbageCollectionTracking",
RemoveGarbageCollectionTracking);
SetMethod(isolate, target, "notify", Notify);
SetMethod(isolate, target, "loopIdleTime", LoopIdleTime);
SetFastMethodNoSideEffect(
isolate, target, "loopIdleTime", LoopIdleTime, &fast_loop_idle_time);
SetMethod(isolate, target, "createELDHistogram", CreateELDHistogram);
SetMethod(isolate, target, "markBootstrapComplete", MarkBootstrapComplete);
SetMethod(isolate, target, "uvMetricsInfo", UvMetricsInfo);
Expand Down Expand Up @@ -406,6 +420,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(RemoveGarbageCollectionTracking);
registry->Register(Notify);
registry->Register(LoopIdleTime);
registry->Register(FastLoopIdleTime);
registry->Register(fast_loop_idle_time.GetTypeInfo());
registry->Register(CreateELDHistogram);
registry->Register(MarkBootstrapComplete);
registry->Register(UvMetricsInfo);
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-perf-hooks-fast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Flags: --expose-internals --no-warnings --allow-natives-syntax
'use strict';

const common = require('../common');
const assert = require('assert');
const {
performance
} = require('perf_hooks');

const { internalBinding } = require('internal/test/binding');

function testFastPerf() {
const obj = performance.eventLoopUtilization();
assert.strictEqual(typeof obj.idle, 'number');
assert.strictEqual(typeof obj.active, 'number');
assert.strictEqual(typeof obj.utilization, 'number');
}

eval('%PrepareFunctionForOptimization(testFastPerf)');
testFastPerf();
eval('%OptimizeFunctionOnNextCall(testFastPerf)');
testFastPerf();

if (common.isDebug) {
const { getV8FastApiCallCount } = internalBinding('debug');
assert.strictEqual(getV8FastApiCallCount('performance.loopIdleTime'), 1);
}
Loading