Skip to content

Commit

Permalink
src: add fast api for some push methods
Browse files Browse the repository at this point in the history
Specifically `pushClientBucket` and `pushDnsBucket`.
  • Loading branch information
santigimeno committed Dec 5, 2023
1 parent 21459cb commit 7b498cd
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 35 deletions.
7 changes: 4 additions & 3 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ const {
TypedArrayPrototypeSlice,
} = primordials;


const nsolidApi = internalBinding('nsolid_api');
const {
nsolid_counts,
nsolid_tracer_s,
nsolid_span_id_s,
nsolid_consts,
pushClientBucket,
} = internalBinding('nsolid_api');
} = nsolidApi;

const net = require('net');
const assert = require('internal/assert');
Expand Down Expand Up @@ -736,7 +737,7 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
}

nsolid_counts[kHttpClientCount]++;
pushClientBucket(now() - req[nsolid_tracer_s]);
nsolidApi.pushClientBucket(now() - req[nsolid_tracer_s]);

if (req[kClientRequestStatistics] && hasObserver('http')) {
stopPerf(req, kClientRequestStatistics, {
Expand Down
10 changes: 5 additions & 5 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ const {
Symbol,
} = primordials;

const nsolidApi = internalBinding('nsolid_api');
const {
nsolid_counts,
nsolid_tracer_s,
nsolid_span_id_s,
nsolid_consts,
pushDnsBucket,
} = internalBinding('nsolid_api');
} = nsolidApi;

const cares = internalBinding('cares_wrap');
const { isIP } = require('internal/net');
Expand Down Expand Up @@ -134,7 +134,7 @@ let promises = null; // Lazy loaded

function onlookup(err, addresses) {
const now = perfNow();
pushDnsBucket(now - this[nsolid_tracer_s]);
nsolidApi.pushDnsBucket(now - this[nsolid_tracer_s]);

const span = this[nsolid_span_id_s];
if (err) {
Expand All @@ -160,7 +160,7 @@ function onlookup(err, addresses) {

function onlookupall(err, addresses) {
const now = perfNow();
pushDnsBucket(now - this[nsolid_tracer_s]);
nsolidApi.pushDnsBucket(now - this[nsolid_tracer_s]);

const span = this[nsolid_span_id_s];
if (err) {
Expand Down Expand Up @@ -326,7 +326,7 @@ ObjectDefineProperty(lookup, customPromisifyArgs,

function onlookupservice(err, hostname, service) {
const now = perfNow();
pushDnsBucket(now - this[nsolid_tracer_s]);
nsolidApi.pushDnsBucket(now - this[nsolid_tracer_s]);

const span = this[nsolid_span_id_s];
if (err) {
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/dns/callback_resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ const {
JSONStringify,
} = primordials;

const nsolidApi = internalBinding('nsolid_api');
const {
nsolid_consts,
nsolid_counts,
nsolid_tracer_s,
nsolid_span_id_s,
pushDnsBucket,
} = internalBinding('nsolid_api');
} = nsolidApi;

const { now: perfNow } = require('internal/perf/utils');

Expand Down Expand Up @@ -67,7 +67,7 @@ const kPerfHooksDnsLookupResolveContext = Symbol('kPerfHooksDnsLookupResolveCont

function onresolve(err, result, ttls) {
const now = perfNow();
pushDnsBucket(now - this[nsolid_tracer_s]);
nsolidApi.pushDnsBucket(now - this[nsolid_tracer_s]);
if (ttls && this.ttl)
result = ArrayPrototypeMap(
result, (address, index) => ({ address, ttl: ttls[index] }));
Expand Down
12 changes: 6 additions & 6 deletions lib/internal/dns/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ const {
Symbol,
} = primordials;

const nsolidApi = internalBinding('nsolid_api');
const {
nsolid_counts,
nsolid_tracer_s,
nsolid_span_id_s,
nsolid_consts,
pushDnsBucket,
} = internalBinding('nsolid_api');
} = nsolidApi;

const {
bindDefaultResolver,
Expand Down Expand Up @@ -111,7 +111,7 @@ const {

function onlookup(err, addresses) {
const now = perfNow();
pushDnsBucket(now - this[nsolid_tracer_s]);
nsolidApi.pushDnsBucket(now - this[nsolid_tracer_s]);

const span = this[nsolid_span_id_s];
if (err) {
Expand All @@ -138,7 +138,7 @@ function onlookup(err, addresses) {

function onlookupall(err, addresses) {
const now = perfNow();
pushDnsBucket(now - this[nsolid_tracer_s]);
nsolidApi.pushDnsBucket(now - this[nsolid_tracer_s]);

const span = this[nsolid_span_id_s];
if (err) {
Expand Down Expand Up @@ -302,7 +302,7 @@ function lookup(hostname, options) {

function onlookupservice(err, hostname, service) {
const now = perfNow();
pushDnsBucket(now - this[nsolid_tracer_s]);
nsolidApi.pushDnsBucket(now - this[nsolid_tracer_s]);

const span = this[nsolid_span_id_s];
if (err) {
Expand Down Expand Up @@ -391,7 +391,7 @@ function lookupService(address, port) {

function onresolve(err, result, ttls) {
const now = perfNow();
pushDnsBucket(now - this[nsolid_tracer_s]);
nsolidApi.pushDnsBucket(now - this[nsolid_tracer_s]);

const span = this[nsolid_span_id_s];
if (err) {
Expand Down
75 changes: 57 additions & 18 deletions src/nsolid/nsolid_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2043,11 +2043,41 @@ static void AgentId(const FunctionCallbackInfo<Value>& args) {
}


static void PushClientBucket(const FunctionCallbackInfo<Value>& args) {
EnvInst* envinst = EnvInst::GetEnvLocalInst(args.GetIsolate());
CHECK_NE(envinst, nullptr);
CHECK(args[0]->IsNumber());
envinst->PushClientBucket(args[0].As<Number>()->Value());
void BindingData::SlowPushClientBucket(
const FunctionCallbackInfo<Value>& args) {
DCHECK(args[0]->IsNumber());
PushClientBucketImpl(PrincipalRealm::GetBindingData<BindingData>(args),
args[0].As<Number>()->Value());
}


void BindingData::FastPushClientBucket(v8::Local<v8::Object> receiver,
double val) {
PushClientBucketImpl(FromJSObject<BindingData>(receiver), val);
}


void BindingData::PushClientBucketImpl(BindingData* data, double val) {
data->env()->envinst_->PushClientBucket(val);
}


void BindingData::SlowPushDnsBucket(
const FunctionCallbackInfo<Value>& args) {
DCHECK(args[0]->IsNumber());
PushDnsBucketImpl(PrincipalRealm::GetBindingData<BindingData>(args),
args[0].As<Number>()->Value());
}


void BindingData::FastPushDnsBucket(v8::Local<v8::Object> receiver,
double val) {
PushDnsBucketImpl(FromJSObject<BindingData>(receiver), val);
}


void BindingData::PushDnsBucketImpl(BindingData* data, double val) {
data->env()->envinst_->PushDnsBucket(val);
}


Expand All @@ -2070,14 +2100,6 @@ void BindingData::PushServerBucketImpl(BindingData* data, double val) {
}


static void PushDnsBucket(const FunctionCallbackInfo<Value>& args) {
EnvInst* envinst = EnvInst::GetEnvLocalInst(args.GetIsolate());
CHECK_NE(envinst, nullptr);
CHECK(args[0]->IsNumber());
envinst->PushDnsBucket(args[0].As<Number>()->Value());
}


void BindingData::SlowPushSpanDataDouble(
const FunctionCallbackInfo<Value>& args) {
DCHECK_EQ(args.Length(), 3);
Expand Down Expand Up @@ -2584,7 +2606,10 @@ void BindingData::Deserialize(Local<Context> context,
CHECK_NOT_NULL(binding);
}


v8::CFunction BindingData::fast_push_client_bucket_(
v8::CFunction::Make(FastPushClientBucket));
v8::CFunction BindingData::fast_push_dns_bucket_(
v8::CFunction::Make(FastPushDnsBucket));
v8::CFunction BindingData::fast_push_server_bucket_(
v8::CFunction::Make(FastPushServerBucket));
v8::CFunction BindingData::fast_push_span_data_double_(
Expand All @@ -2605,6 +2630,16 @@ void BindingData::Initialize(Local<Object> target,
realm->AddBindingData<BindingData>(target);
if (binding_data == nullptr) return;

SetFastMethod(context,
target,
"pushClientBucket",
SlowPushClientBucket,
&fast_push_client_bucket_);
SetFastMethod(context,
target,
"pushDnsBucket",
SlowPushDnsBucket,
&fast_push_dns_bucket_);
SetFastMethod(context,
target,
"pushServerBucket",
Expand All @@ -2622,8 +2657,6 @@ void BindingData::Initialize(Local<Object> target,
&fast_push_span_data_uint64_);

SetMethod(context, target, "agentId", AgentId);
SetMethod(context, target, "pushClientBucket", PushClientBucket);
SetMethod(context, target, "pushDnsBucket", PushDnsBucket);
SetMethod(context, target, "pushSpanDataString", PushSpanDataString);
SetMethod(context, target, "getEnvMetrics", GetEnvMetrics);
SetMethod(context, target, "getProcessMetrics", GetProcessMetrics);
Expand Down Expand Up @@ -2731,6 +2764,14 @@ void BindingData::Initialize(Local<Object> target,
void BindingData::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {

registry->Register(SlowPushClientBucket);
registry->Register(FastPushClientBucket);
registry->Register(fast_push_client_bucket_.GetTypeInfo());

registry->Register(SlowPushDnsBucket);
registry->Register(FastPushDnsBucket);
registry->Register(fast_push_dns_bucket_.GetTypeInfo());

registry->Register(SlowPushServerBucket);
registry->Register(FastPushServerBucket);
registry->Register(fast_push_server_bucket_.GetTypeInfo());
Expand All @@ -2744,8 +2785,6 @@ void BindingData::RegisterExternalReferences(
registry->Register(fast_push_span_data_uint64_.GetTypeInfo());

registry->Register(AgentId);
registry->Register(PushClientBucket);
registry->Register(PushDnsBucket);
registry->Register(PushSpanDataString);
registry->Register(GetEnvMetrics);
registry->Register(GetProcessMetrics);
Expand Down
12 changes: 12 additions & 0 deletions src/nsolid/nsolid_bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ class BindingData : public SnapshotableObject {
SET_SELF_SIZE(BindingData)
SET_MEMORY_INFO_NAME(BindingData)

static void SlowPushClientBucket(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void FastPushClientBucket(v8::Local<v8::Object> receiver, double val);
static void PushClientBucketImpl(BindingData* data, double val);

static void SlowPushDnsBucket(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void FastPushDnsBucket(v8::Local<v8::Object> receiver, double val);
static void PushDnsBucketImpl(BindingData* data, double val);

static void SlowPushServerBucket(
const v8::FunctionCallbackInfo<v8::Value>& args);
static void FastPushServerBucket(v8::Local<v8::Object> receiver, double val);
Expand Down Expand Up @@ -55,6 +65,8 @@ class BindingData : public SnapshotableObject {
ExternalReferenceRegistry* registry);

private:
static v8::CFunction fast_push_client_bucket_;
static v8::CFunction fast_push_dns_bucket_;
static v8::CFunction fast_push_server_bucket_;
static v8::CFunction fast_push_span_data_double_;
static v8::CFunction fast_push_span_data_uint64_;
Expand Down

0 comments on commit 7b498cd

Please sign in to comment.