Skip to content

Commit

Permalink
perf: expose node.http trace flag inside array
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed May 21, 2023
1 parent 85ac915 commit 3bd9f56
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 4 deletions.
5 changes: 3 additions & 2 deletions lib/internal/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const {
} = primordials;

const { setUnrefTimeout } = require('internal/timers');
const { trace, isTraceCategoryEnabled } = internalBinding('trace_events');
const { trace } = internalBinding('trace_events');
const tracing = internalBinding('tracing');
const {
CHAR_LOWERCASE_B,
CHAR_LOWERCASE_E,
Expand Down Expand Up @@ -38,7 +39,7 @@ function getNextTraceEventId() {
}

function isTraceHTTPEnabled() {
return isTraceCategoryEnabled('node.http');
return tracing.tracingCategories[0] > 0;
}

const traceEventCategory = 'node,node.http';
Expand Down
2 changes: 2 additions & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
'src/node_symbols.cc',
'src/node_task_queue.cc',
'src/node_trace_events.cc',
'src/node_tracing.cc',
'src/node_types.cc',
'src/node_url.cc',
'src/node_util.cc',
Expand Down Expand Up @@ -248,6 +249,7 @@
'src/node_sockaddr-inl.h',
'src/node_stat_watcher.h',
'src/node_union_bytes.h',
'src/node_tracing.h',
'src/node_url.h',
'src/node_util.h',
'src/node_version.h',
Expand Down
3 changes: 2 additions & 1 deletion src/base_object_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ namespace node {
V(blob_binding_data, BlobBindingData) \
V(process_binding_data, process::BindingData) \
V(timers_binding_data, timers::BindingData) \
V(url_binding_data, url::BindingData)
V(url_binding_data, url::BindingData) \
V(tracing_binding_data, tracing::BindingData)

#define UNSERIALIZABLE_BINDING_TYPES(V) \
V(http2_binding_data, http2::BindingData) \
Expand Down
1 change: 1 addition & 0 deletions src/node_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
V(task_queue) \
V(tcp_wrap) \
V(timers) \
V(tracing) \
V(trace_events) \
V(tty_wrap) \
V(types) \
Expand Down
2 changes: 1 addition & 1 deletion src/node_builtins.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ BuiltinLoader::BuiltinCategories BuiltinLoader::GetBuiltinCategories() const {
#endif // !HAVE_INSPECTOR

#if !NODE_USE_V8_PLATFORM || !defined(NODE_HAVE_I18N_SUPPORT)
"trace_events",
"tracing", "trace_events",
#endif // !NODE_USE_V8_PLATFORM || !defined(NODE_HAVE_I18N_SUPPORT)

#if !HAVE_OPENSSL
Expand Down
1 change: 1 addition & 0 deletions src/node_external_reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class ExternalReferenceRegistry {
V(string_decoder) \
V(stream_wrap) \
V(signal_wrap) \
V(tracing) \
V(trace_events) \
V(timers) \
V(types) \
Expand Down
1 change: 1 addition & 0 deletions src/node_snapshotable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "node_metadata.h"
#include "node_process.h"
#include "node_snapshot_builder.h"
#include "node_tracing.h"
#include "node_url.h"
#include "node_util.h"
#include "node_v8.h"
Expand Down
105 changes: 105 additions & 0 deletions src/node_tracing.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include "node_tracing.h"
#include "base_object-inl.h"
#include "env-inl.h"
#include "memory_tracker-inl.h"
#include "node.h"
#include "node_external_reference.h"
#include "node_internals.h"
#include "node_v8_platform-inl.h"
#include "tracing/agent.h"
#include "util-inl.h"
#include "v8.h"

#include <set>
#include <string>
#include <numeric>

namespace node {
namespace tracing {

using v8::Context;
using v8::FunctionTemplate;
using v8::HandleScope;
using v8::Isolate;
using v8::Local;
using v8::Object;
using v8::ObjectTemplate;
using v8::String;
using v8::Value;

void BindingData::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackField("tracing_categories_buffer", tracing_categories_buffer_);
}

BindingData::BindingData(Realm* realm, v8::Local<v8::Object> object)
: SnapshotableObject(realm, object, type_int),
tracing_categories_buffer_(realm->isolate(), kTracingCategoriesLength) {
object
->Set(realm->context(),
FIXED_ONE_BYTE_STRING(realm->isolate(), "tracingCategories"),
tracing_categories_buffer_.GetJSArray())
.Check();

// get the pointer of the memory for the flag that store if trace is enabled for http
// the pointer will always be the same and if the category does not exist, it creates: https://github.com/nodejs/node/blob/6bbf2a57fcf33266c5859497f8cc32e1389a358a/deps/v8/src/libplatform/tracing/tracing-controller.cc#L322-L342
tracing_categories_buffer_[0] = const_cast<uint8_t*>(TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED("node.http"));
}

bool BindingData::PrepareForSerialization(v8::Local<v8::Context> context,
v8::SnapshotCreator* creator) {
// We'll just re-initialize the buffers in the constructor since their
// contents can be thrown away once consumed in the previous call.
tracing_categories_buffer_.Release();
// Return true because we need to maintain the reference to the binding from
// JS land.
return true;
}

InternalFieldInfoBase* BindingData::Serialize(int index) {
DCHECK_EQ(index, BaseObject::kEmbedderType);
InternalFieldInfo* info =
InternalFieldInfoBase::New<InternalFieldInfo>(type());
return info;
}

void BindingData::Deserialize(v8::Local<v8::Context> context,
v8::Local<v8::Object> holder,
int index,
InternalFieldInfoBase* info) {
DCHECK_EQ(index, BaseObject::kEmbedderType);
v8::HandleScope scope(context->GetIsolate());
Realm* realm = Realm::GetCurrent(context);
BindingData* binding = realm->AddBindingData<BindingData>(context, holder);
CHECK_NOT_NULL(binding);
}

void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
Local<FunctionTemplate> ctor) {
Isolate* isolate = isolate_data->isolate();
Local<ObjectTemplate> target = ctor->InstanceTemplate();

}

void BindingData::CreatePerContextProperties(Local<Object> target,
Local<Value> unused,
Local<Context> context,
void* priv) {
Realm* realm = Realm::GetCurrent(context);
realm->AddBindingData<BindingData>(context, target);
}

void BindingData::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {

}

} // namespace tracing

} // node

NODE_BINDING_CONTEXT_AWARE_INTERNAL(
tracing, node::tracing::BindingData::CreatePerContextProperties)
NODE_BINDING_PER_ISOLATE_INIT(
tracing, node::tracing::BindingData::CreatePerIsolateProperties)
NODE_BINDING_EXTERNAL_REFERENCE(
tracing, node::tracing::BindingData::RegisterExternalReferences)
49 changes: 49 additions & 0 deletions src/node_tracing.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#ifndef SRC_NODE_TRACING_H_
#define SRC_NODE_TRACING_H_

#include <cinttypes>
#include "aliased_buffer.h"
#include "node.h"
#include "node_snapshotable.h"
#include "util.h"
#include "v8-fast-api-calls.h"
#include "v8.h"

#include <string>

namespace node {
class ExternalReferenceRegistry;

namespace tracing {

class BindingData : public SnapshotableObject {
public:
BindingData(Realm* realm, v8::Local<v8::Object> obj);

using InternalFieldInfo = InternalFieldInfoBase;

SERIALIZABLE_OBJECT_METHODS()
SET_BINDING_ID(tracing_binding_data)

void MemoryInfo(MemoryTracker* tracker) const override;
SET_SELF_SIZE(BindingData)
SET_MEMORY_INFO_NAME(BindingData)

static void CreatePerIsolateProperties(IsolateData* isolate_data,
v8::Local<v8::FunctionTemplate> ctor);
static void CreatePerContextProperties(v8::Local<v8::Object> target,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv);
static void RegisterExternalReferences(ExternalReferenceRegistry* registry);

private:
static constexpr size_t kTracingCategoriesLength = 1;
AliasedUint8Array tracing_categories_buffer_;
};

} // namespace tracing

} // namespace node

#endif // SRC_NODE_TRACING_H_

0 comments on commit 3bd9f56

Please sign in to comment.