Skip to content
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

refactor: js bindings #209

Merged
merged 10 commits into from
Oct 28, 2022
Merged
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
11 changes: 8 additions & 3 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"src/process_data.cc",
"src/xpf_thread.cc",
"src/xprofiler.cc",
"src/configure.cc",
"src/logger.cc",
"src/util.cc",
"src/library/json.hpp",
"src/library/error.cc",
"src/library/common.cc",
Expand Down Expand Up @@ -43,9 +43,14 @@
"src/commands/report/uv_statistics.cc",
"src/commands/report/system_statistics.cc",
"src/commands/coredumper/coredumper.cc",
"src/hooks/set_hooks.cc",
"src/hooks/fatal_error.cc",
"src/util.cc",
"src/jsapi/export_environment.cc",
"src/jsapi/export_configure.cc",
"src/jsapi/export_logger.cc",
"src/jsapi/export_hooks.cc",
"src/jsapi/export_http.cc",
"src/jsapi/export_thread_logbypass.cc",
"src/jsapi/export_thread_listener.cc",
],
"include_dirs": [
'src',
Expand Down
29 changes: 9 additions & 20 deletions src/commands/listener.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,11 @@
#include "commands/parser.h"
#include "environment_data.h"
#include "logger.h"
#include "nan.h"
#include "platform/platform.h"
#include "uv.h"
#include "xpf_mutex-inl.h"

namespace xprofiler {
using Nan::False;
using Nan::FunctionCallbackInfo;
using Nan::ThrowTypeError;
using Nan::True;
using v8::Value;

namespace per_process {
Mutex command_listener_mutex;
Expand All @@ -24,25 +18,20 @@ static void CreateCommandsListenerThread(void* unused) {
CreateIpcServer(ParseCmd);
}

void RunCommandsListener(const FunctionCallbackInfo<Value>& info) {
int StartCommandsListener(EnvironmentData* env_data) {
Mutex::ScopedLock lock(per_process::command_listener_mutex);
if (per_process::command_listener_thread_created) {
info.GetReturnValue().Set(True());
return;
return 0;
}
int rc = 0;

// init commands listener thread
rc = uv_thread_create(&per_process::uv_commands_listener_thread,
CreateCommandsListenerThread, nullptr);
if (rc != 0) {
ThrowTypeError("xprofiler: create uv commands listener thread failed!");
info.GetReturnValue().Set(False());
return;
int rc = uv_thread_create(&per_process::uv_commands_listener_thread,
CreateCommandsListenerThread, nullptr);
if (rc == 0) {
InfoT("init", env_data->thread_id(),
"commands listener: listener thread created.");
}
EnvironmentData* env_data = EnvironmentData::GetCurrent(info);
InfoT("init", env_data->thread_id(),
"commands listener: listener thread created.");

info.GetReturnValue().Set(True());
return rc;
}
} // namespace xprofiler
11 changes: 3 additions & 8 deletions src/commands/listener.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
#ifndef XPROFILER_SRC_COMMANDS_LISTENER_H
#define XPROFILER_SRC_COMMANDS_LISTENER_H

#include "nan.h"
#include "environment_data.h"

namespace xprofiler {
using Nan::FunctionCallbackInfo;
using v8::Value;

// javascript-accessible
void RunCommandsListener(const FunctionCallbackInfo<Value>& info);
} // namespace xprofiler
int StartCommandsListener(EnvironmentData* env_data);
}

#endif /* XPROFILER_SRC_COMMANDS_LISTENER_H */
4 changes: 2 additions & 2 deletions src/commands/send.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "../library/json.hpp"
#include "../platform/platform.h"
#include "library/json.hpp"
#include "platform/platform.h"

namespace xprofiler {
using nlohmann::json;
Expand Down
7 changes: 0 additions & 7 deletions src/configure.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#ifndef XPROFILER_SRC_CONFIGURE_H
#define XPROFILER_SRC_CONFIGURE_H

#include "library/common.h"
#include "library/error.h"
#include "logger.h"
#include "nan.h"

namespace xprofiler {

Expand All @@ -25,10 +22,6 @@ inline void SetLogLevel(LOG_LEVEL value);
inline void SetLogType(LOG_TYPE value);
inline void SetEnableLogUvHandles(bool value);

// javascript accessible
void Configure(const Nan::FunctionCallbackInfo<v8::Value>& info);
void GetConfig(const Nan::FunctionCallbackInfo<v8::Value>& info);

class ConfigStore {
// TODO(legendecas): accessors.
public:
Expand Down
39 changes: 8 additions & 31 deletions src/environment_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@
#include "xpf_v8.h"

namespace xprofiler {
using v8::Boolean;
using v8::Context;
using v8::Isolate;
using v8::Local;
using v8::Number;
using v8::Object;

namespace per_thread {
thread_local EnvironmentData* environment_data = nullptr;
Expand Down Expand Up @@ -185,34 +180,16 @@ void EnvironmentData::CollectStatistics(uv_async_t* handle) {
CollectLibuvHandleStatistics(env_data);
}

// javascript accessible
// static
void EnvironmentData::JsSetupEnvironmentData(
const Nan::FunctionCallbackInfo<v8::Value>& info) {
Isolate* isolate = info.GetIsolate();
void EnvironmentData::JsSetupEnvironmentData(Isolate* isolate,
bool is_main_thread,
ThreadId thread_id,
std::string node_version) {
EnvironmentData* env_data = EnvironmentData::GetCurrent(isolate);
HandleScope scope(isolate);
Local<Context> context = isolate->GetCurrentContext();

Local<Object> data = info[0].As<Object>();
Local<Number> thread_id =
data->Get(context, OneByteString(isolate, "threadId"))
.ToLocalChecked()
.As<Number>();
Local<Boolean> is_main_thread =
data->Get(context, OneByteString(isolate, "isMainThread"))
.ToLocalChecked()
.As<Boolean>();
Local<v8::String> node_version =
data->Get(context, OneByteString(isolate, "nodeVersion"))
.ToLocalChecked()
.As<v8::String>();

env_data->thread_id_ = thread_id->Value();
env_data->is_main_thread_ = is_main_thread->Value();

Nan::Utf8String node_version_string(node_version);
env_data->node_version_ = (*node_version_string);

env_data->is_main_thread_ = is_main_thread;
env_data->thread_id_ = thread_id;
env_data->node_version_ = node_version;
}

} // namespace xprofiler
5 changes: 3 additions & 2 deletions src/environment_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ class EnvironmentData {
static EnvironmentData* TryGetCurrent();
static void Create(v8::Isolate* isolate);

static void JsSetupEnvironmentData(
const Nan::FunctionCallbackInfo<v8::Value>& info);
static void JsSetupEnvironmentData(v8::Isolate* isolate, bool is_main_thread,
ThreadId thread_id,
std::string node_version);

void SendCollectStatistics();

Expand Down
10 changes: 0 additions & 10 deletions src/hooks/set_hooks.h

This file was deleted.

2 changes: 1 addition & 1 deletion src/configure.cc → src/jsapi/export_configure.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "configure.h"
#include "export_configure.h"

#include "process_data.h"
#include "util-inl.h"
Expand Down
11 changes: 11 additions & 0 deletions src/jsapi/export_configure.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef XPROFILER_SRC_JSAPI_CONFIGURE_H
#define XPROFILER_SRC_JSAPI_CONFIGURE_H

#include "nan.h"

namespace xprofiler {
void Configure(const Nan::FunctionCallbackInfo<v8::Value>& info);
void GetConfig(const Nan::FunctionCallbackInfo<v8::Value>& info);
} // namespace xprofiler

#endif /* XPROFILER_SRC_JSAPI_CONFIGURE_H */
40 changes: 40 additions & 0 deletions src/jsapi/export_environment.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "export_environment.h"

#include "environment_data.h"
#include "util-inl.h"
#include "xpf_v8.h"

namespace xprofiler {
using v8::Boolean;
using v8::Context;
using v8::Isolate;
using v8::Local;
using v8::Number;
using v8::Object;

void JsSetupEnvironmentData(const Nan::FunctionCallbackInfo<v8::Value>& info) {
Isolate* isolate = info.GetIsolate();
HandleScope scope(isolate);
Local<Context> context = isolate->GetCurrentContext();

Local<Object> data = info[0].As<Object>();
Local<Number> thread_id =
data->Get(context, OneByteString(isolate, "threadId"))
.ToLocalChecked()
.As<Number>();
Local<Boolean> is_main_thread =
data->Get(context, OneByteString(isolate, "isMainThread"))
.ToLocalChecked()
.As<Boolean>();
Local<v8::String> node_version =
data->Get(context, OneByteString(isolate, "nodeVersion"))
.ToLocalChecked()
.As<v8::String>();

Nan::Utf8String node_version_string(node_version);

EnvironmentData::JsSetupEnvironmentData(isolate, is_main_thread->Value(),
thread_id->Value(),
(*node_version_string));
}
} // namespace xprofiler
9 changes: 9 additions & 0 deletions src/jsapi/export_environment.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef XPROFILER_SRC_JSAPI_ENVIRONMENT_H
#define XPROFILER_SRC_JSAPI_ENVIRONMENT_H
#include "nan.h"

namespace xprofiler {
void JsSetupEnvironmentData(const Nan::FunctionCallbackInfo<v8::Value>& info);
}

#endif /* XPROFILER_SRC_JSAPI_ENVIRONMENT_H */
4 changes: 2 additions & 2 deletions src/hooks/set_hooks.cc → src/jsapi/export_hooks.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "set_hooks.h"
#include "export_hooks.h"

#include "configure-inl.h"
#include "fatal_error.h"
#include "hooks/fatal_error.h"

namespace xprofiler {
using Nan::FunctionCallbackInfo;
Expand Down
10 changes: 10 additions & 0 deletions src/jsapi/export_hooks.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef XPROFILER_SRC_JSAPI_HOOKS_H
#define XPROFILER_SRC_JSAPI_HOOKS_H

#include "nan.h"

namespace xprofiler {
void SetHooks(const Nan::FunctionCallbackInfo<v8::Value>& info);
}

#endif /* XPROFILER_SRC_JSAPI_HOOKS_H */
81 changes: 81 additions & 0 deletions src/jsapi/export_http.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "export_http.h"

#include "environment_data.h"
#include "logger.h"

namespace xprofiler {
using Nan::FunctionCallbackInfo;
using v8::Value;

constexpr char module_type[] = "http";

void AddLiveRequest(const FunctionCallbackInfo<Value>& info) {
EnvironmentData* env_data = EnvironmentData::GetCurrent(info);
if (env_data == nullptr) {
return;
}
HttpStatistics* http_statistics = env_data->http_statistics();
Mutex::ScopedLock lock(http_statistics->mutex);
http_statistics->live_http_request++;
}

void AddCloseRequest(const FunctionCallbackInfo<Value>& info) {
EnvironmentData* env_data = EnvironmentData::GetCurrent(info);
if (env_data == nullptr) {
return;
}
HttpStatistics* http_statistics = env_data->http_statistics();
Mutex::ScopedLock lock(http_statistics->mutex);
http_statistics->http_response_close++;
}

void AddSentRequest(const FunctionCallbackInfo<Value>& info) {
EnvironmentData* env_data = EnvironmentData::GetCurrent(info);
if (env_data == nullptr) {
return;
}
HttpStatistics* http_statistics = env_data->http_statistics();

if (!info[0]->IsNumber()) {
ErrorT(module_type, env_data->thread_id(), "request cost must be number!");
return;
}

uint32_t cost = Nan::To<uint32_t>(info[0]).ToChecked();

Mutex::ScopedLock lock(http_statistics->mutex);
http_statistics->http_response_sent++;
http_statistics->http_rt += cost;
}

void AddRequestTimeout(const FunctionCallbackInfo<Value>& info) {
EnvironmentData* env_data = EnvironmentData::GetCurrent(info);
if (env_data == nullptr) {
return;
}
HttpStatistics* http_statistics = env_data->http_statistics();
Mutex::ScopedLock lock(http_statistics->mutex);
http_statistics->http_request_timeout++;
}

void AddHttpStatusCode(const FunctionCallbackInfo<Value>& info) {
EnvironmentData* env_data = EnvironmentData::GetCurrent(info);
if (env_data == nullptr) {
return;
}
HttpStatistics* http_statistics = env_data->http_statistics();

if (!info[0]->IsNumber()) {
ErrorT(module_type, env_data->thread_id(), "request cost must be number!");
return;
}

uint32_t status_code = Nan::To<uint32_t>(info[0]).ToChecked();
if (status_code >= kMaxHttpStatusCode) {
return;
}

Mutex::ScopedLock lock(http_statistics->mutex);
http_statistics->status_codes[status_code]++;
}
} // namespace xprofiler
15 changes: 15 additions & 0 deletions src/jsapi/export_http.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef XPROFILER_SRC_JSAPI_HTTP_H
#define XPROFILER_SRC_JSAPI_HTTP_H

#include "nan.h"
#include "xpf_mutex-inl.h"

namespace xprofiler {
void AddLiveRequest(const Nan::FunctionCallbackInfo<v8::Value>& info);
void AddCloseRequest(const Nan::FunctionCallbackInfo<v8::Value>& info);
void AddSentRequest(const Nan::FunctionCallbackInfo<v8::Value>& info);
void AddRequestTimeout(const Nan::FunctionCallbackInfo<v8::Value>& info);
void AddHttpStatusCode(const Nan::FunctionCallbackInfo<v8::Value>& info);
} // namespace xprofiler

#endif /* XPROFILER_SRC_LOGBYPASS_HTTP_H */
Loading