From e9f370d5fa76012e4def13a03a16ae1c67e81f6f Mon Sep 17 00:00:00 2001 From: hyj1991 Date: Thu, 27 Oct 2022 16:46:35 +0800 Subject: [PATCH 01/10] refactor jsapi - http --- binding.gyp | 1 + src/jsapi/http.cc | 81 +++++++++++++++++++++++++++++++++++++++++++ src/jsapi/http.h | 15 ++++++++ src/logbypass/http.cc | 76 ---------------------------------------- src/logbypass/http.h | 8 ----- src/xprofiler.cc | 4 ++- 6 files changed, 100 insertions(+), 85 deletions(-) create mode 100644 src/jsapi/http.cc create mode 100644 src/jsapi/http.h diff --git a/binding.gyp b/binding.gyp index 42c491c..c8554d3 100644 --- a/binding.gyp +++ b/binding.gyp @@ -45,6 +45,7 @@ "src/commands/coredumper/coredumper.cc", "src/hooks/set_hooks.cc", "src/hooks/fatal_error.cc", + "src/jsapi/http.cc", "src/util.cc", ], "include_dirs": [ diff --git a/src/jsapi/http.cc b/src/jsapi/http.cc new file mode 100644 index 0000000..b8d5f3a --- /dev/null +++ b/src/jsapi/http.cc @@ -0,0 +1,81 @@ +#include "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& 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& 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& 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(info[0]).ToChecked(); + + Mutex::ScopedLock lock(http_statistics->mutex); + http_statistics->http_response_sent++; + http_statistics->http_rt += cost; +} + +void AddRequestTimeout(const FunctionCallbackInfo& 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& 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(info[0]).ToChecked(); + if (status_code >= kMaxHttpStatusCode) { + return; + } + + Mutex::ScopedLock lock(http_statistics->mutex); + http_statistics->status_codes[status_code]++; +} +} // namespace xprofiler \ No newline at end of file diff --git a/src/jsapi/http.h b/src/jsapi/http.h new file mode 100644 index 0000000..88854b3 --- /dev/null +++ b/src/jsapi/http.h @@ -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& info); +void AddCloseRequest(const Nan::FunctionCallbackInfo& info); +void AddSentRequest(const Nan::FunctionCallbackInfo& info); +void AddRequestTimeout(const Nan::FunctionCallbackInfo& info); +void AddHttpStatusCode(const Nan::FunctionCallbackInfo& info); +} // namespace xprofiler + +#endif /* XPROFILER_SRC_LOGBYPASS_HTTP_H */ diff --git a/src/logbypass/http.cc b/src/logbypass/http.cc index 86ca269..bc16365 100644 --- a/src/logbypass/http.cc +++ b/src/logbypass/http.cc @@ -2,84 +2,8 @@ #include "environment_data.h" #include "logger.h" -#include "uv.h" namespace xprofiler { -using Nan::FunctionCallbackInfo; -using v8::Value; - -constexpr char module_type[] = "http"; - -void AddLiveRequest(const FunctionCallbackInfo& 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& 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& 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(info[0]).ToChecked(); - - Mutex::ScopedLock lock(http_statistics->mutex); - http_statistics->http_response_sent++; - http_statistics->http_rt += cost; -} - -void AddRequestTimeout(const FunctionCallbackInfo& 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& 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(info[0]).ToChecked(); - if (status_code >= kMaxHttpStatusCode) { - return; - } - - Mutex::ScopedLock lock(http_statistics->mutex); - http_statistics->status_codes[status_code]++; -} - void WriteHttpStatus(EnvironmentData* env_data, bool log_format_alinode, uint32_t http_patch_timeout) { HttpStatistics* http_statistics = env_data->http_statistics(); diff --git a/src/logbypass/http.h b/src/logbypass/http.h index 834c510..1626a87 100644 --- a/src/logbypass/http.h +++ b/src/logbypass/http.h @@ -1,7 +1,6 @@ #ifndef XPROFILER_SRC_LOGBYPASS_HTTP_H #define XPROFILER_SRC_LOGBYPASS_HTTP_H -#include "nan.h" #include "xpf_mutex-inl.h" namespace xprofiler { @@ -24,13 +23,6 @@ struct HttpStatistics { void WriteHttpStatus(EnvironmentData* env_data, bool log_format_alinode, uint32_t http_patch_timeout); - -// javascript-accessible -void AddLiveRequest(const Nan::FunctionCallbackInfo& info); -void AddCloseRequest(const Nan::FunctionCallbackInfo& info); -void AddSentRequest(const Nan::FunctionCallbackInfo& info); -void AddRequestTimeout(const Nan::FunctionCallbackInfo& info); -void AddHttpStatusCode(const Nan::FunctionCallbackInfo& info); } // namespace xprofiler #endif /* XPROFILER_SRC_LOGBYPASS_HTTP_H */ diff --git a/src/xprofiler.cc b/src/xprofiler.cc index 50db456..2f4de7d 100644 --- a/src/xprofiler.cc +++ b/src/xprofiler.cc @@ -2,8 +2,8 @@ #include "configure.h" #include "environment_data.h" #include "hooks/set_hooks.h" +#include "jsapi/http.h" #include "library/common.h" -#include "logbypass/http.h" #include "logbypass/log.h" #include "logger.h" #include "nan.h" @@ -58,6 +58,8 @@ NAN_MODULE_INIT(Initialize) { CREATE_JS_BINDING(addSentRequest, AddSentRequest); CREATE_JS_BINDING(addRequestTimeout, AddRequestTimeout); CREATE_JS_BINDING(addHttpStatusCode, AddHttpStatusCode); + + // js api } NODE_MODULE_CONTEXT_AWARE(xprofiler, Initialize) From 3037b6f16d2e476822277d7fca14ea9df7a73bea Mon Sep 17 00:00:00 2001 From: hyj1991 Date: Thu, 27 Oct 2022 17:03:50 +0800 Subject: [PATCH 02/10] refactor jsapi - configure --- binding.gyp | 4 ++-- src/configure.h | 7 ------- src/{ => jsapi}/configure.cc | 2 +- src/jsapi/http.cc | 2 +- src/jsapi/include/configure.h | 13 +++++++++++++ src/jsapi/{ => include}/http.h | 0 src/xprofiler.cc | 6 ++---- 7 files changed, 19 insertions(+), 15 deletions(-) rename src/{ => jsapi}/configure.cc (99%) create mode 100644 src/jsapi/include/configure.h rename src/jsapi/{ => include}/http.h (100%) diff --git a/binding.gyp b/binding.gyp index c8554d3..29e103f 100644 --- a/binding.gyp +++ b/binding.gyp @@ -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", @@ -46,7 +46,7 @@ "src/hooks/set_hooks.cc", "src/hooks/fatal_error.cc", "src/jsapi/http.cc", - "src/util.cc", + "src/jsapi/configure.cc", ], "include_dirs": [ 'src', diff --git a/src/configure.h b/src/configure.h index 921c499..2089ac1 100644 --- a/src/configure.h +++ b/src/configure.h @@ -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 { @@ -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& info); -void GetConfig(const Nan::FunctionCallbackInfo& info); - class ConfigStore { // TODO(legendecas): accessors. public: diff --git a/src/configure.cc b/src/jsapi/configure.cc similarity index 99% rename from src/configure.cc rename to src/jsapi/configure.cc index 13d344e..f7c7513 100644 --- a/src/configure.cc +++ b/src/jsapi/configure.cc @@ -1,4 +1,4 @@ -#include "configure.h" +#include "include/configure.h" #include "process_data.h" #include "util-inl.h" diff --git a/src/jsapi/http.cc b/src/jsapi/http.cc index b8d5f3a..0935c57 100644 --- a/src/jsapi/http.cc +++ b/src/jsapi/http.cc @@ -1,4 +1,4 @@ -#include "http.h" +#include "include/http.h" #include "environment_data.h" #include "logger.h" diff --git a/src/jsapi/include/configure.h b/src/jsapi/include/configure.h new file mode 100644 index 0000000..2f5c7e2 --- /dev/null +++ b/src/jsapi/include/configure.h @@ -0,0 +1,13 @@ +#ifndef XPROFILER_SRC_JSAPI_CONFIGURE_H +#define XPROFILER_SRC_JSAPI_CONFIGURE_H + +#include "nan.h" + +namespace xprofiler { +// javascript accessible +void Configure(const Nan::FunctionCallbackInfo& info); +void GetConfig(const Nan::FunctionCallbackInfo& info); + +} // namespace xprofiler + +#endif /* XPROFILER_SRC_JSAPI_CONFIGURE_H */ diff --git a/src/jsapi/http.h b/src/jsapi/include/http.h similarity index 100% rename from src/jsapi/http.h rename to src/jsapi/include/http.h diff --git a/src/xprofiler.cc b/src/xprofiler.cc index 2f4de7d..2654247 100644 --- a/src/xprofiler.cc +++ b/src/xprofiler.cc @@ -1,8 +1,8 @@ #include "commands/listener.h" -#include "configure.h" #include "environment_data.h" #include "hooks/set_hooks.h" -#include "jsapi/http.h" +#include "jsapi/include/configure.h" +#include "jsapi/include/http.h" #include "library/common.h" #include "logbypass/log.h" #include "logger.h" @@ -58,8 +58,6 @@ NAN_MODULE_INIT(Initialize) { CREATE_JS_BINDING(addSentRequest, AddSentRequest); CREATE_JS_BINDING(addRequestTimeout, AddRequestTimeout); CREATE_JS_BINDING(addHttpStatusCode, AddHttpStatusCode); - - // js api } NODE_MODULE_CONTEXT_AWARE(xprofiler, Initialize) From 177ee5063541f63933868ee44dbc9e3b25d55444 Mon Sep 17 00:00:00 2001 From: hyj1991 Date: Thu, 27 Oct 2022 17:34:06 +0800 Subject: [PATCH 03/10] refactor jsapi - logger --- binding.gyp | 3 ++- src/jsapi/export_logger.cc | 43 +++++++++++++++++++++++++++++++ src/jsapi/include/export_logger.h | 13 ++++++++++ src/logger.cc | 29 --------------------- src/logger.h | 6 ----- src/xprofiler.cc | 2 +- 6 files changed, 59 insertions(+), 37 deletions(-) create mode 100644 src/jsapi/export_logger.cc create mode 100644 src/jsapi/include/export_logger.h diff --git a/binding.gyp b/binding.gyp index 29e103f..0f71001 100644 --- a/binding.gyp +++ b/binding.gyp @@ -45,8 +45,9 @@ "src/commands/coredumper/coredumper.cc", "src/hooks/set_hooks.cc", "src/hooks/fatal_error.cc", - "src/jsapi/http.cc", "src/jsapi/configure.cc", + "src/jsapi/export_logger.cc", + "src/jsapi/http.cc", ], "include_dirs": [ 'src', diff --git a/src/jsapi/export_logger.cc b/src/jsapi/export_logger.cc new file mode 100644 index 0000000..9a9d467 --- /dev/null +++ b/src/jsapi/export_logger.cc @@ -0,0 +1,43 @@ +#include "include/export_logger.h" + +#include "environment_data.h" + +namespace xprofiler { +using Nan::FunctionCallbackInfo; +using Nan::New; +using Nan::ThrowTypeError; +using Nan::To; +using Nan::Utf8String; +using std::string; +using std::to_string; +using v8::Local; +using v8::String; +using v8::Value; + +#define JS_LOG_WITH_LEVEL(level) \ + if (!info[0]->IsString() || !info[1]->IsString()) { \ + ThrowTypeError( \ + New("log type and content must be string!").ToLocalChecked()); \ + return; \ + } \ + EnvironmentData* env_data = EnvironmentData::GetCurrent(info); \ + \ + Local component_string = To(info[0]).ToLocalChecked(); \ + Utf8String component(component_string); \ + Local log_content_string = To(info[1]).ToLocalChecked(); \ + Utf8String log_content(log_content_string); \ + Log(level, *component, env_data->thread_id(), *log_content); + +void JsInfo(const FunctionCallbackInfo& info) { + JS_LOG_WITH_LEVEL(LOG_INFO) +} + +void JsError(const FunctionCallbackInfo& info) { + JS_LOG_WITH_LEVEL(LOG_ERROR) +} + +void JsDebug(const FunctionCallbackInfo& info) { + JS_LOG_WITH_LEVEL(LOG_DEBUG) +} + +}; // namespace xprofiler diff --git a/src/jsapi/include/export_logger.h b/src/jsapi/include/export_logger.h new file mode 100644 index 0000000..f725ed7 --- /dev/null +++ b/src/jsapi/include/export_logger.h @@ -0,0 +1,13 @@ +#ifndef XPROFILER_SRC_JSAPI_LOGGER_H +#define XPROFILER_SRC_JSAPI_LOGGER_H + +#include "logger.h" +#include "nan.h" + +namespace xprofiler { +void JsInfo(const Nan::FunctionCallbackInfo& info); +void JsError(const Nan::FunctionCallbackInfo& info); +void JsDebug(const Nan::FunctionCallbackInfo& info); +} // namespace xprofiler + +#endif /* XPROFILER_SRC_JSAPI_LOGGER_H */ diff --git a/src/logger.cc b/src/logger.cc index 05c2c54..ac9d27c 100644 --- a/src/logger.cc +++ b/src/logger.cc @@ -8,11 +8,9 @@ #include #include "configure-inl.h" -#include "environment_data.h" #include "platform/platform.h" #include "process_data.h" #include "util.h" -#include "uv.h" #include "xpf_mutex-inl.h" namespace xprofiler { @@ -137,31 +135,4 @@ void Log(const LOG_LEVEL output_level, const char* type, ThreadId thread_id, } } -#define JS_LOG_WITH_LEVEL(level) \ - if (!info[0]->IsString() || !info[1]->IsString()) { \ - ThrowTypeError( \ - New("log type and content must be string!").ToLocalChecked()); \ - return; \ - } \ - EnvironmentData* env_data = EnvironmentData::GetCurrent(info); \ - \ - Local component_string = To(info[0]).ToLocalChecked(); \ - Utf8String component(component_string); \ - Local log_content_string = To(info[1]).ToLocalChecked(); \ - Utf8String log_content(log_content_string); \ - Log(level, *component, env_data->thread_id(), *log_content); - -/* js binding logger */ -void JsInfo(const FunctionCallbackInfo& info) { - JS_LOG_WITH_LEVEL(LOG_INFO) -} - -void JsError(const FunctionCallbackInfo& info) { - JS_LOG_WITH_LEVEL(LOG_ERROR) -} - -void JsDebug(const FunctionCallbackInfo& info) { - JS_LOG_WITH_LEVEL(LOG_DEBUG) -} - }; // namespace xprofiler diff --git a/src/logger.h b/src/logger.h index 83f2b93..a18c5fc 100644 --- a/src/logger.h +++ b/src/logger.h @@ -3,7 +3,6 @@ #include "library/common.h" #include "library/printf-inl.h" -#include "nan.h" namespace xprofiler { // xprofiler logger @@ -37,11 +36,6 @@ NATIVE_LOGGERS(DEFINE_LOGGER); } NATIVE_LOGGERS(DEFINE_LOGGER); #undef DEFINE_LOGGER - -// javascript accessible -void JsInfo(const Nan::FunctionCallbackInfo& info); -void JsError(const Nan::FunctionCallbackInfo& info); -void JsDebug(const Nan::FunctionCallbackInfo& info); } // namespace xprofiler #endif /* XPROFILER_SRC_LOGGER_H */ diff --git a/src/xprofiler.cc b/src/xprofiler.cc index 2654247..c0d0cab 100644 --- a/src/xprofiler.cc +++ b/src/xprofiler.cc @@ -2,10 +2,10 @@ #include "environment_data.h" #include "hooks/set_hooks.h" #include "jsapi/include/configure.h" +#include "jsapi/include/export_logger.h" #include "jsapi/include/http.h" #include "library/common.h" #include "logbypass/log.h" -#include "logger.h" #include "nan.h" #include "platform/platform.h" #include "process_data.h" From 290d2156b68b74d55925b8c281b03d32bab9c78b Mon Sep 17 00:00:00 2001 From: hyj1991 Date: Thu, 27 Oct 2022 17:36:02 +0800 Subject: [PATCH 04/10] fix --- binding.gyp | 4 ++-- src/jsapi/{configure.cc => export_configure.cc} | 2 +- src/jsapi/{http.cc => export_http.cc} | 2 +- src/jsapi/include/{configure.h => export_configure.h} | 0 src/jsapi/include/{http.h => export_http.h} | 0 src/xprofiler.cc | 4 ++-- 6 files changed, 6 insertions(+), 6 deletions(-) rename src/jsapi/{configure.cc => export_configure.cc} (98%) rename src/jsapi/{http.cc => export_http.cc} (98%) rename src/jsapi/include/{configure.h => export_configure.h} (100%) rename src/jsapi/include/{http.h => export_http.h} (100%) diff --git a/binding.gyp b/binding.gyp index 0f71001..1f327af 100644 --- a/binding.gyp +++ b/binding.gyp @@ -45,9 +45,9 @@ "src/commands/coredumper/coredumper.cc", "src/hooks/set_hooks.cc", "src/hooks/fatal_error.cc", - "src/jsapi/configure.cc", + "src/jsapi/export_configure.cc", "src/jsapi/export_logger.cc", - "src/jsapi/http.cc", + "src/jsapi/export_http.cc", ], "include_dirs": [ 'src', diff --git a/src/jsapi/configure.cc b/src/jsapi/export_configure.cc similarity index 98% rename from src/jsapi/configure.cc rename to src/jsapi/export_configure.cc index f7c7513..ef2339a 100644 --- a/src/jsapi/configure.cc +++ b/src/jsapi/export_configure.cc @@ -1,4 +1,4 @@ -#include "include/configure.h" +#include "include/export_configure.h" #include "process_data.h" #include "util-inl.h" diff --git a/src/jsapi/http.cc b/src/jsapi/export_http.cc similarity index 98% rename from src/jsapi/http.cc rename to src/jsapi/export_http.cc index 0935c57..d6d0bba 100644 --- a/src/jsapi/http.cc +++ b/src/jsapi/export_http.cc @@ -1,4 +1,4 @@ -#include "include/http.h" +#include "include/export_http.h" #include "environment_data.h" #include "logger.h" diff --git a/src/jsapi/include/configure.h b/src/jsapi/include/export_configure.h similarity index 100% rename from src/jsapi/include/configure.h rename to src/jsapi/include/export_configure.h diff --git a/src/jsapi/include/http.h b/src/jsapi/include/export_http.h similarity index 100% rename from src/jsapi/include/http.h rename to src/jsapi/include/export_http.h diff --git a/src/xprofiler.cc b/src/xprofiler.cc index c0d0cab..7d9e7d4 100644 --- a/src/xprofiler.cc +++ b/src/xprofiler.cc @@ -1,9 +1,9 @@ #include "commands/listener.h" #include "environment_data.h" #include "hooks/set_hooks.h" -#include "jsapi/include/configure.h" +#include "jsapi/include/export_configure.h" +#include "jsapi/include/export_http.h" #include "jsapi/include/export_logger.h" -#include "jsapi/include/http.h" #include "library/common.h" #include "logbypass/log.h" #include "nan.h" From 50fb24ccf4ebd296f94de1a86df468fde279ca46 Mon Sep 17 00:00:00 2001 From: hyj1991 Date: Thu, 27 Oct 2022 19:47:51 +0800 Subject: [PATCH 05/10] refactor jsapi - environment --- binding.gyp | 1 + src/environment_data.cc | 39 ++++++------------------- src/environment_data.h | 5 ++-- src/jsapi/export_environment.cc | 40 ++++++++++++++++++++++++++ src/jsapi/export_logger.cc | 2 -- src/jsapi/include/export_environment.h | 9 ++++++ src/logger.cc | 8 ------ src/xprofiler.cc | 23 ++++++++------- 8 files changed, 73 insertions(+), 54 deletions(-) create mode 100644 src/jsapi/export_environment.cc create mode 100644 src/jsapi/include/export_environment.h diff --git a/binding.gyp b/binding.gyp index 1f327af..8853673 100644 --- a/binding.gyp +++ b/binding.gyp @@ -45,6 +45,7 @@ "src/commands/coredumper/coredumper.cc", "src/hooks/set_hooks.cc", "src/hooks/fatal_error.cc", + "src/jsapi/export_environment.cc", "src/jsapi/export_configure.cc", "src/jsapi/export_logger.cc", "src/jsapi/export_http.cc", diff --git a/src/environment_data.cc b/src/environment_data.cc index 518ee6a..d6d7c02 100644 --- a/src/environment_data.cc +++ b/src/environment_data.cc @@ -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; @@ -185,34 +180,16 @@ void EnvironmentData::CollectStatistics(uv_async_t* handle) { CollectLibuvHandleStatistics(env_data); } -// javascript accessible // static -void EnvironmentData::JsSetupEnvironmentData( - const Nan::FunctionCallbackInfo& 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 = isolate->GetCurrentContext(); - - Local data = info[0].As(); - Local thread_id = - data->Get(context, OneByteString(isolate, "threadId")) - .ToLocalChecked() - .As(); - Local is_main_thread = - data->Get(context, OneByteString(isolate, "isMainThread")) - .ToLocalChecked() - .As(); - Local node_version = - data->Get(context, OneByteString(isolate, "nodeVersion")) - .ToLocalChecked() - .As(); - - 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 diff --git a/src/environment_data.h b/src/environment_data.h index 17b9cab..0f6dba3 100644 --- a/src/environment_data.h +++ b/src/environment_data.h @@ -34,8 +34,9 @@ class EnvironmentData { static EnvironmentData* TryGetCurrent(); static void Create(v8::Isolate* isolate); - static void JsSetupEnvironmentData( - const Nan::FunctionCallbackInfo& info); + static void JsSetupEnvironmentData(v8::Isolate* isolate, bool is_main_thread, + ThreadId thread_id, + std::string node_version); void SendCollectStatistics(); diff --git a/src/jsapi/export_environment.cc b/src/jsapi/export_environment.cc new file mode 100644 index 0000000..892485c --- /dev/null +++ b/src/jsapi/export_environment.cc @@ -0,0 +1,40 @@ +#include "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& info) { + Isolate* isolate = info.GetIsolate(); + HandleScope scope(isolate); + Local context = isolate->GetCurrentContext(); + + Local data = info[0].As(); + Local thread_id = + data->Get(context, OneByteString(isolate, "threadId")) + .ToLocalChecked() + .As(); + Local is_main_thread = + data->Get(context, OneByteString(isolate, "isMainThread")) + .ToLocalChecked() + .As(); + Local node_version = + data->Get(context, OneByteString(isolate, "nodeVersion")) + .ToLocalChecked() + .As(); + + Nan::Utf8String node_version_string(node_version); + + EnvironmentData::JsSetupEnvironmentData(isolate, is_main_thread->Value(), + thread_id->Value(), + (*node_version_string)); +} +} // namespace xprofiler \ No newline at end of file diff --git a/src/jsapi/export_logger.cc b/src/jsapi/export_logger.cc index 9a9d467..0836f33 100644 --- a/src/jsapi/export_logger.cc +++ b/src/jsapi/export_logger.cc @@ -8,8 +8,6 @@ using Nan::New; using Nan::ThrowTypeError; using Nan::To; using Nan::Utf8String; -using std::string; -using std::to_string; using v8::Local; using v8::String; using v8::Value; diff --git a/src/jsapi/include/export_environment.h b/src/jsapi/include/export_environment.h new file mode 100644 index 0000000..99e8ab4 --- /dev/null +++ b/src/jsapi/include/export_environment.h @@ -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& info); +} + +#endif /* XPROFILER_SRC_JSAPI_ENVIRONMENT_H */ \ No newline at end of file diff --git a/src/logger.cc b/src/logger.cc index ac9d27c..3a91879 100644 --- a/src/logger.cc +++ b/src/logger.cc @@ -14,16 +14,8 @@ #include "xpf_mutex-inl.h" namespace xprofiler { -using Nan::FunctionCallbackInfo; -using Nan::New; -using Nan::ThrowTypeError; -using Nan::To; -using Nan::Utf8String; using std::string; using std::to_string; -using v8::Local; -using v8::String; -using v8::Value; static const int kMaxFormatLength = 2048; diff --git a/src/xprofiler.cc b/src/xprofiler.cc index 7d9e7d4..e9f2258 100644 --- a/src/xprofiler.cc +++ b/src/xprofiler.cc @@ -1,7 +1,7 @@ #include "commands/listener.h" -#include "environment_data.h" #include "hooks/set_hooks.h" #include "jsapi/include/export_configure.h" +#include "jsapi/include/export_environment.h" #include "jsapi/include/export_http.h" #include "jsapi/include/export_logger.h" #include "library/common.h" @@ -31,24 +31,18 @@ NAN_MODULE_INIT(Initialize) { Isolate* isolate = target->GetIsolate(); EnvironmentData::Create(isolate); + // environment + CREATE_JS_BINDING(setup, JsSetupEnvironmentData); + // config CREATE_JS_BINDING(configure, Configure); CREATE_JS_BINDING(getConfig, GetConfig); - // js logger + // logger CREATE_JS_BINDING(info, JsInfo); CREATE_JS_BINDING(error, JsError); CREATE_JS_BINDING(debug, JsDebug); - CREATE_JS_BINDING(setup, EnvironmentData::JsSetupEnvironmentData); - - // performance log - CREATE_JS_BINDING(runLogBypass, RunLogBypass); - - // commands listener - CREATE_JS_BINDING(checkSocketPath, CheckSocketPath); - CREATE_JS_BINDING(runCommandsListener, RunCommandsListener); - // set hooks CREATE_JS_BINDING(setHooks, SetHooks); @@ -58,6 +52,13 @@ NAN_MODULE_INIT(Initialize) { CREATE_JS_BINDING(addSentRequest, AddSentRequest); CREATE_JS_BINDING(addRequestTimeout, AddRequestTimeout); CREATE_JS_BINDING(addHttpStatusCode, AddHttpStatusCode); + + // performance log + CREATE_JS_BINDING(runLogBypass, RunLogBypass); + + // commands listener + CREATE_JS_BINDING(checkSocketPath, CheckSocketPath); + CREATE_JS_BINDING(runCommandsListener, RunCommandsListener); } NODE_MODULE_CONTEXT_AWARE(xprofiler, Initialize) From 767d8ce618f5c962926a353842e310d16875522c Mon Sep 17 00:00:00 2001 From: hyj1991 Date: Thu, 27 Oct 2022 20:01:36 +0800 Subject: [PATCH 06/10] refactor jsapi - hooks --- binding.gyp | 2 +- src/hooks/set_hooks.h | 10 ---------- src/{hooks/set_hooks.cc => jsapi/export_hooks.cc} | 4 ++-- src/jsapi/include/export_configure.h | 1 - src/jsapi/include/export_hooks.h | 10 ++++++++++ src/xprofiler.cc | 2 +- test/fixtures/cases/unfinished.js | 9 +++++++++ test/unfinished.test.js | 10 ++++++---- 8 files changed, 29 insertions(+), 19 deletions(-) delete mode 100644 src/hooks/set_hooks.h rename src/{hooks/set_hooks.cc => jsapi/export_hooks.cc} (82%) create mode 100644 src/jsapi/include/export_hooks.h diff --git a/binding.gyp b/binding.gyp index 8853673..23206fe 100644 --- a/binding.gyp +++ b/binding.gyp @@ -43,11 +43,11 @@ "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/jsapi/export_environment.cc", "src/jsapi/export_configure.cc", "src/jsapi/export_logger.cc", + "src/jsapi/export_hooks.cc", "src/jsapi/export_http.cc", ], "include_dirs": [ diff --git a/src/hooks/set_hooks.h b/src/hooks/set_hooks.h deleted file mode 100644 index 3c4e858..0000000 --- a/src/hooks/set_hooks.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef XPROFILER_SRC_HOOKS_SET_HOOKS_H -#define XPROFILER_SRC_HOOKS_SET_HOOKS_H - -#include "nan.h" - -namespace xprofiler { -void SetHooks(const Nan::FunctionCallbackInfo& info); -} // namespace xprofiler - -#endif /* XPROFILER_SRC_HOOKS_SET_HOOKS_H */ diff --git a/src/hooks/set_hooks.cc b/src/jsapi/export_hooks.cc similarity index 82% rename from src/hooks/set_hooks.cc rename to src/jsapi/export_hooks.cc index 8b96a56..067db03 100644 --- a/src/hooks/set_hooks.cc +++ b/src/jsapi/export_hooks.cc @@ -1,7 +1,7 @@ -#include "set_hooks.h" +#include "include/export_hooks.h" #include "configure-inl.h" -#include "fatal_error.h" +#include "hooks/fatal_error.h" namespace xprofiler { using Nan::FunctionCallbackInfo; diff --git a/src/jsapi/include/export_configure.h b/src/jsapi/include/export_configure.h index 2f5c7e2..a7305c8 100644 --- a/src/jsapi/include/export_configure.h +++ b/src/jsapi/include/export_configure.h @@ -4,7 +4,6 @@ #include "nan.h" namespace xprofiler { -// javascript accessible void Configure(const Nan::FunctionCallbackInfo& info); void GetConfig(const Nan::FunctionCallbackInfo& info); diff --git a/src/jsapi/include/export_hooks.h b/src/jsapi/include/export_hooks.h new file mode 100644 index 0000000..6e921e2 --- /dev/null +++ b/src/jsapi/include/export_hooks.h @@ -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& info); +} + +#endif /* XPROFILER_SRC_JSAPI_HOOKS_H */ diff --git a/src/xprofiler.cc b/src/xprofiler.cc index e9f2258..e290f9f 100644 --- a/src/xprofiler.cc +++ b/src/xprofiler.cc @@ -1,7 +1,7 @@ #include "commands/listener.h" -#include "hooks/set_hooks.h" #include "jsapi/include/export_configure.h" #include "jsapi/include/export_environment.h" +#include "jsapi/include/export_hooks.h" #include "jsapi/include/export_http.h" #include "jsapi/include/export_logger.h" #include "library/common.h" diff --git a/test/fixtures/cases/unfinished.js b/test/fixtures/cases/unfinished.js index a849827..115781d 100644 --- a/test/fixtures/cases/unfinished.js +++ b/test/fixtures/cases/unfinished.js @@ -15,6 +15,7 @@ exports = module.exports = function () { jspath: exitFatalErrorScriptPath, tid: 0, cmd: 'start_cpu_profiling', + skip: true, options: { profiling_time: 5 * 60 * 1000 }, checkExitInfo: false, check(file) { @@ -27,6 +28,7 @@ exports = module.exports = function () { jspath: exitFatalErrorScriptPath, tid: 0, cmd: 'start_heap_profiling', + skip: true, options: { profiling_time: 5 * 60 * 1000 }, checkExitInfo: false, check(file) { @@ -39,6 +41,7 @@ exports = module.exports = function () { jspath: exitFatalErrorScriptPath, tid: 0, cmd: 'start_gc_profiling', + skip: true, options: { profiling_time: 5 * 60 * 1000 }, checkExitInfo: false, check(file) { @@ -53,6 +56,7 @@ exports = module.exports = function () { jspath: exitNormalScriptPath, tid: 0, cmd: 'start_cpu_profiling', + skip: false, options: { profiling_time: 5 * 60 * 1000 }, checkExitInfo: true, check(file) { @@ -65,6 +69,7 @@ exports = module.exports = function () { jspath: exitNormalScriptPath, tid: 0, cmd: 'start_heap_profiling', + skip: false, options: { profiling_time: 5 * 60 * 1000 }, checkExitInfo: true, check(file) { @@ -77,6 +82,7 @@ exports = module.exports = function () { jspath: exitNormalScriptPath, tid: 0, cmd: 'start_gc_profiling', + skip: false, options: { profiling_time: 5 * 60 * 1000 }, checkExitInfo: true, check(file) { @@ -91,6 +97,7 @@ exports = module.exports = function () { jspath: exitNormalScriptPath, tid: 0, cmd: 'start_cpu_profiling', + skip: false, options: undefined, checkExitInfo: true, check(file) { @@ -103,6 +110,7 @@ exports = module.exports = function () { jspath: exitNormalScriptPath, tid: 0, cmd: 'start_heap_profiling', + skip: false, options: undefined, checkExitInfo: true, check(file) { @@ -115,6 +123,7 @@ exports = module.exports = function () { jspath: exitNormalScriptPath, tid: 0, cmd: 'start_gc_profiling', + skip: false, options: undefined, checkExitInfo: true, check(file) { diff --git a/test/unfinished.test.js b/test/unfinished.test.js index f5db033..d485664 100644 --- a/test/unfinished.test.js +++ b/test/unfinished.test.js @@ -66,12 +66,14 @@ describe('unfinished sampling before process exit', function () { } }); - (cse.checkExitInfo ? it : it.skip)(`child process should be exited with code 0`, function () { + (cse.skip ? it.skip : it)(`child process should be exited with code 0`, function () { console.log(`[${moment().format('YYYY-MM-DD HH:mm:ss')}]`, `exit info: ${JSON.stringify(exitInfo)}`); - utils.checkChildProcessExitInfo(expect, exitInfo); + if (cse.checkExitInfo) { + utils.checkChildProcessExitInfo(expect, exitInfo); + } }); - (cse.checkExitInfo ? it : it.skip)(`sampling file should be exists when process exit`, function () { + (cse.skip ? it.skip : it)(`sampling file should be exists when process exit`, function () { console.log('xtcl:', JSON.stringify(resByXctl)); expect(resByXctl.ok); const filepath = resByXctl.data.filepath; @@ -79,7 +81,7 @@ describe('unfinished sampling before process exit', function () { expect(fs.existsSync(filepath)); }); - (cse.checkExitInfo ? it : it.skip)('value should be ok', async function () { + (cse.skip ? it.skip : it)('value should be ok', async function () { describe(`it has expected structure`, function () { if (typeof cse.check !== 'function') { return; From df9b7f2f5a6b1815169e8820bac9780984fb1247 Mon Sep 17 00:00:00 2001 From: hyj1991 Date: Thu, 27 Oct 2022 20:18:06 +0800 Subject: [PATCH 07/10] refactor jsapi - logbypass --- binding.gyp | 1 + src/jsapi/export_thread_logbypass.cc | 17 +++++++++++++++++ src/jsapi/include/export_thread_logbypass.h | 10 ++++++++++ src/logbypass/log.cc | 11 +---------- src/logbypass/log.h | 3 +-- src/xprofiler.cc | 2 +- 6 files changed, 31 insertions(+), 13 deletions(-) create mode 100644 src/jsapi/export_thread_logbypass.cc create mode 100644 src/jsapi/include/export_thread_logbypass.h diff --git a/binding.gyp b/binding.gyp index 23206fe..21f234d 100644 --- a/binding.gyp +++ b/binding.gyp @@ -49,6 +49,7 @@ "src/jsapi/export_logger.cc", "src/jsapi/export_hooks.cc", "src/jsapi/export_http.cc", + "src/jsapi/export_thread_logbypass.cc", ], "include_dirs": [ 'src', diff --git a/src/jsapi/export_thread_logbypass.cc b/src/jsapi/export_thread_logbypass.cc new file mode 100644 index 0000000..4d6b775 --- /dev/null +++ b/src/jsapi/export_thread_logbypass.cc @@ -0,0 +1,17 @@ +#include "include/export_thread_logbypass.h" + +#include "environment_data.h" +#include "logbypass/log.h" + +namespace xprofiler { +using Nan::FunctionCallbackInfo; +using Nan::True; +using v8::Value; + +void RunLogBypass(const FunctionCallbackInfo& info) { + EnvironmentData* env_data = EnvironmentData::GetCurrent(info); + StartLogThread(env_data); + info.GetReturnValue().Set(True()); +} + +} // namespace xprofiler diff --git a/src/jsapi/include/export_thread_logbypass.h b/src/jsapi/include/export_thread_logbypass.h new file mode 100644 index 0000000..76ccfb8 --- /dev/null +++ b/src/jsapi/include/export_thread_logbypass.h @@ -0,0 +1,10 @@ +#ifndef XPROFILER_SRC_JSAPI_THREAD_LOGBYPASS_H +#define XPROFILER_SRC_JSAPI_THREAD_LOGBYPASS_H + +#include "nan.h" + +namespace xprofiler { +void RunLogBypass(const Nan::FunctionCallbackInfo& info); +} + +#endif /* XPROFILER_SRC_JSAPI_THREAD_LOGBYPASS_H */ diff --git a/src/logbypass/log.cc b/src/logbypass/log.cc index 8826fcf..2fc6ad6 100644 --- a/src/logbypass/log.cc +++ b/src/logbypass/log.cc @@ -9,18 +9,12 @@ #include "gc.h" #include "heap.h" #include "http.h" -#include "library/utils.h" #include "libuv.h" #include "logger.h" #include "process_data.h" #include "uv.h" namespace xprofiler { -using Nan::False; -using Nan::FunctionCallbackInfo; -using Nan::ThrowTypeError; -using Nan::True; -using v8::Value; void LogByPass::ThreadEntry(uv_loop_t* loop) { CHECK_EQ(0, uv_timer_init(loop, &cpu_interval_)); @@ -101,8 +95,7 @@ void LogByPass::Write(EnvironmentData* env_data, bool log_format_alinode) { WriteHttpStatus(env_data, log_format_alinode, GetPatchHttpTimeout()); } -void RunLogBypass(const FunctionCallbackInfo& info) { - EnvironmentData* env_data = EnvironmentData::GetCurrent(info); +void StartLogThread(EnvironmentData* env_data) { ThreadId thread_id = env_data->thread_id(); // init gc hooks InitGcStatusHooks(env_data); @@ -116,8 +109,6 @@ void RunLogBypass(const FunctionCallbackInfo& info) { data->log_by_pass->StartIfNeeded(); InfoT("init", thread_id, "logbypass: log thread created."); } - - info.GetReturnValue().Set(True()); } } // namespace xprofiler diff --git a/src/logbypass/log.h b/src/logbypass/log.h index d553823..f4b7cb1 100644 --- a/src/logbypass/log.h +++ b/src/logbypass/log.h @@ -28,8 +28,7 @@ class LogByPass final : public XpfThread { bool next_log_ = false; }; -// javascript-accessible -void RunLogBypass(const Nan::FunctionCallbackInfo& info); +void StartLogThread(EnvironmentData* env_data); } // namespace xprofiler #endif /* XPROFILER_SRC_LOGBYPASS_LOG_H */ diff --git a/src/xprofiler.cc b/src/xprofiler.cc index e290f9f..ec195b8 100644 --- a/src/xprofiler.cc +++ b/src/xprofiler.cc @@ -4,8 +4,8 @@ #include "jsapi/include/export_hooks.h" #include "jsapi/include/export_http.h" #include "jsapi/include/export_logger.h" +#include "jsapi/include/export_thread_logbypass.h" #include "library/common.h" -#include "logbypass/log.h" #include "nan.h" #include "platform/platform.h" #include "process_data.h" From 5ed03999d94ac129fe3ecd2cd87ada088288b401 Mon Sep 17 00:00:00 2001 From: hyj1991 Date: Thu, 27 Oct 2022 20:36:26 +0800 Subject: [PATCH 08/10] refactor jsapi - command listener --- src/commands/listener.cc | 29 +++++++--------------- src/commands/listener.h | 11 +++----- src/jsapi/export_thread_listener.cc | 21 ++++++++++++++++ src/jsapi/include/export_thread_listener.h | 10 ++++++++ src/xprofiler.cc | 2 +- 5 files changed, 44 insertions(+), 29 deletions(-) create mode 100644 src/jsapi/export_thread_listener.cc create mode 100644 src/jsapi/include/export_thread_listener.h diff --git a/src/commands/listener.cc b/src/commands/listener.cc index 842abae..7699840 100644 --- a/src/commands/listener.cc +++ b/src/commands/listener.cc @@ -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; @@ -24,25 +18,20 @@ static void CreateCommandsListenerThread(void* unused) { CreateIpcServer(ParseCmd); } -void RunCommandsListener(const FunctionCallbackInfo& 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 diff --git a/src/commands/listener.h b/src/commands/listener.h index 1b9f1aa..31db894 100644 --- a/src/commands/listener.h +++ b/src/commands/listener.h @@ -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& info); -} // namespace xprofiler +int StartCommandsListener(EnvironmentData* env_data); +} #endif /* XPROFILER_SRC_COMMANDS_LISTENER_H */ diff --git a/src/jsapi/export_thread_listener.cc b/src/jsapi/export_thread_listener.cc new file mode 100644 index 0000000..a5894e1 --- /dev/null +++ b/src/jsapi/export_thread_listener.cc @@ -0,0 +1,21 @@ +#include "include/export_thread_listener.h" + +#include "commands/listener.h" +#include "environment_data.h" + +namespace xprofiler { +using Nan::False; +using Nan::FunctionCallbackInfo; +using Nan::ThrowTypeError; +using Nan::True; +using v8::Value; + +void RunCommandsListener(const FunctionCallbackInfo& info) { + EnvironmentData* env_data = EnvironmentData::GetCurrent(info); + int rc = StartCommandsListener(env_data); + if (rc != 0) { + ThrowTypeError("xprofiler: create uv commands listener thread failed!"); + } + info.GetReturnValue().Set(rc == 0 ? True() : False()); +} +} // namespace xprofiler diff --git a/src/jsapi/include/export_thread_listener.h b/src/jsapi/include/export_thread_listener.h new file mode 100644 index 0000000..9530b70 --- /dev/null +++ b/src/jsapi/include/export_thread_listener.h @@ -0,0 +1,10 @@ +#ifndef XPROFILER_SRC_JSAPI_THREAD_LISTENER_H +#define XPROFILER_SRC_JSAPI_THREAD_LISTENER_H + +#include "nan.h" + +namespace xprofiler { +void RunCommandsListener(const Nan::FunctionCallbackInfo& info); +} + +#endif /* XPROFILER_SRC_JSAPI_THREAD_LISTENER_H */ diff --git a/src/xprofiler.cc b/src/xprofiler.cc index ec195b8..7c7fcb9 100644 --- a/src/xprofiler.cc +++ b/src/xprofiler.cc @@ -1,9 +1,9 @@ -#include "commands/listener.h" #include "jsapi/include/export_configure.h" #include "jsapi/include/export_environment.h" #include "jsapi/include/export_hooks.h" #include "jsapi/include/export_http.h" #include "jsapi/include/export_logger.h" +#include "jsapi/include/export_thread_listener.h" #include "jsapi/include/export_thread_logbypass.h" #include "library/common.h" #include "nan.h" From b87afdcd02c3647ddb79cabd15fd5f4884eb4901 Mon Sep 17 00:00:00 2001 From: hyj1991 Date: Thu, 27 Oct 2022 20:51:54 +0800 Subject: [PATCH 09/10] refactor jsapi - platform --- binding.gyp | 1 + src/commands/send.cc | 4 ++-- src/jsapi/include/export_thread_logbypass.h | 2 +- src/jsapi/include/export_utils.h | 10 ++++++++++ src/logbypass/cpu.cc | 4 ++-- src/platform/platform.h | 4 ---- src/platform/unix/ipc.cc | 2 +- src/platform/unix/report.cc | 2 +- src/platform/unix/utils.cc | 3 +-- src/platform/win/ipc_win.cc | 2 +- src/platform/win/report_win.cc | 2 +- src/platform/win/utils_win.cc | 2 +- src/xprofiler.cc | 22 ++++++++++----------- 13 files changed, 33 insertions(+), 27 deletions(-) create mode 100644 src/jsapi/include/export_utils.h diff --git a/binding.gyp b/binding.gyp index 21f234d..afdb8da 100644 --- a/binding.gyp +++ b/binding.gyp @@ -50,6 +50,7 @@ "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', diff --git a/src/commands/send.cc b/src/commands/send.cc index 3642dcf..5ba5f8c 100644 --- a/src/commands/send.cc +++ b/src/commands/send.cc @@ -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; diff --git a/src/jsapi/include/export_thread_logbypass.h b/src/jsapi/include/export_thread_logbypass.h index 76ccfb8..dd7cd55 100644 --- a/src/jsapi/include/export_thread_logbypass.h +++ b/src/jsapi/include/export_thread_logbypass.h @@ -4,7 +4,7 @@ #include "nan.h" namespace xprofiler { -void RunLogBypass(const Nan::FunctionCallbackInfo& info); +void CheckSocketPath(const Nan::FunctionCallbackInfo& info); } #endif /* XPROFILER_SRC_JSAPI_THREAD_LOGBYPASS_H */ diff --git a/src/jsapi/include/export_utils.h b/src/jsapi/include/export_utils.h new file mode 100644 index 0000000..7c9cb9c --- /dev/null +++ b/src/jsapi/include/export_utils.h @@ -0,0 +1,10 @@ +#ifndef XPROFILER_SRC_JSAPI_UTILS_H +#define XPROFILER_SRC_JSAPI_UTILS_H + +#include "nan.h" + +namespace xprofiler { +void RunLogBypass(const Nan::FunctionCallbackInfo& info); +} + +#endif /* XPROFILER_SRC_JSAPI_UTILS_H */ diff --git a/src/logbypass/cpu.cc b/src/logbypass/cpu.cc index fec64ea..4183f10 100644 --- a/src/logbypass/cpu.cc +++ b/src/logbypass/cpu.cc @@ -1,5 +1,5 @@ -#include "../logger.h" -#include "../platform/platform.h" +#include "logger.h" +#include "platform/platform.h" namespace xprofiler { #define EXTRA_SYMBOL diff --git a/src/platform/platform.h b/src/platform/platform.h index b651a78..b750d3b 100644 --- a/src/platform/platform.h +++ b/src/platform/platform.h @@ -3,7 +3,6 @@ #include #include "library/writer.h" -#include "nan.h" namespace xprofiler { @@ -29,9 +28,6 @@ void PrintLoadedLibraries(JSONWriter* writer); // coredumper void WriteCore(std::string filename); - -// js binding -void CheckSocketPath(const Nan::FunctionCallbackInfo& info); } // namespace xprofiler #endif /* XPROFILER_SRC_PLATFORM_PLATFORM_H */ diff --git a/src/platform/unix/ipc.cc b/src/platform/unix/ipc.cc index b633117..c8fb653 100644 --- a/src/platform/unix/ipc.cc +++ b/src/platform/unix/ipc.cc @@ -4,7 +4,7 @@ #include #include -#include "../../logger.h" +#include "logger.h" #include "configure-inl.h" namespace xprofiler { diff --git a/src/platform/unix/report.cc b/src/platform/unix/report.cc index 1718dc5..9d4bb14 100644 --- a/src/platform/unix/report.cc +++ b/src/platform/unix/report.cc @@ -32,7 +32,7 @@ #include #endif -#include "../../library/writer.h" +#include "library/writer.h" extern char** environ; diff --git a/src/platform/unix/utils.cc b/src/platform/unix/utils.cc index 8e84320..4c702b6 100644 --- a/src/platform/unix/utils.cc +++ b/src/platform/unix/utils.cc @@ -2,8 +2,7 @@ #include #include -#include "../platform.h" -#include "uv.h" +#include "platform/platform.h" namespace xprofiler { void SleepCrossPlatform(int seconds) { sleep(seconds); } diff --git a/src/platform/win/ipc_win.cc b/src/platform/win/ipc_win.cc index 62e676e..4018d40 100644 --- a/src/platform/win/ipc_win.cc +++ b/src/platform/win/ipc_win.cc @@ -1,7 +1,7 @@ #ifdef _WIN32 #include -#include "../../logger.h" +#include "logger.h" #include "configure-inl.h" #include "uv.h" diff --git a/src/platform/win/report_win.cc b/src/platform/win/report_win.cc index a47b4e7..69ffb8a 100644 --- a/src/platform/win/report_win.cc +++ b/src/platform/win/report_win.cc @@ -7,7 +7,7 @@ #include #include -#include "../../library/writer.h" +#include "library/writer.h" namespace xprofiler { using std::ostringstream; diff --git a/src/platform/win/utils_win.cc b/src/platform/win/utils_win.cc index 0747256..1242132 100644 --- a/src/platform/win/utils_win.cc +++ b/src/platform/win/utils_win.cc @@ -1,7 +1,7 @@ #ifdef _WIN32 #include -#include "../platform.h" +#include "platform/platform.h" #include "uv.h" namespace xprofiler { diff --git a/src/xprofiler.cc b/src/xprofiler.cc index 7c7fcb9..06dccae 100644 --- a/src/xprofiler.cc +++ b/src/xprofiler.cc @@ -5,9 +5,9 @@ #include "jsapi/include/export_logger.h" #include "jsapi/include/export_thread_listener.h" #include "jsapi/include/export_thread_logbypass.h" +#include "jsapi/include/export_utils.h" #include "library/common.h" #include "nan.h" -#include "platform/platform.h" #include "process_data.h" namespace xprofiler { @@ -34,31 +34,31 @@ NAN_MODULE_INIT(Initialize) { // environment CREATE_JS_BINDING(setup, JsSetupEnvironmentData); + // set hooks + CREATE_JS_BINDING(setHooks, SetHooks); + + // utils + CREATE_JS_BINDING(checkSocketPath, CheckSocketPath); + // config CREATE_JS_BINDING(configure, Configure); CREATE_JS_BINDING(getConfig, GetConfig); + // uv thread + CREATE_JS_BINDING(runLogBypass, RunLogBypass); + CREATE_JS_BINDING(runCommandsListener, RunCommandsListener); + // logger CREATE_JS_BINDING(info, JsInfo); CREATE_JS_BINDING(error, JsError); CREATE_JS_BINDING(debug, JsDebug); - // set hooks - CREATE_JS_BINDING(setHooks, SetHooks); - // http status CREATE_JS_BINDING(addLiveRequest, AddLiveRequest); CREATE_JS_BINDING(addCloseRequest, AddCloseRequest); CREATE_JS_BINDING(addSentRequest, AddSentRequest); CREATE_JS_BINDING(addRequestTimeout, AddRequestTimeout); CREATE_JS_BINDING(addHttpStatusCode, AddHttpStatusCode); - - // performance log - CREATE_JS_BINDING(runLogBypass, RunLogBypass); - - // commands listener - CREATE_JS_BINDING(checkSocketPath, CheckSocketPath); - CREATE_JS_BINDING(runCommandsListener, RunCommandsListener); } NODE_MODULE_CONTEXT_AWARE(xprofiler, Initialize) From 7d62a3a3eda69f5b57470f3999d35c0786b2c1a4 Mon Sep 17 00:00:00 2001 From: hyj1991 Date: Fri, 28 Oct 2022 09:29:24 +0800 Subject: [PATCH 10/10] fixup --- src/jsapi/export_configure.cc | 2 +- src/jsapi/{include => }/export_configure.h | 1 - src/jsapi/export_environment.cc | 2 +- src/jsapi/{include => }/export_environment.h | 0 src/jsapi/export_hooks.cc | 2 +- src/jsapi/{include => }/export_hooks.h | 0 src/jsapi/export_http.cc | 2 +- src/jsapi/{include => }/export_http.h | 0 src/jsapi/export_logger.cc | 2 +- src/jsapi/{include => }/export_logger.h | 0 src/jsapi/export_thread_listener.cc | 2 +- src/jsapi/{include => }/export_thread_listener.h | 0 src/jsapi/export_thread_logbypass.cc | 2 +- .../{include => }/export_thread_logbypass.h | 2 +- src/jsapi/{include => }/export_utils.h | 2 +- src/platform/unix/ipc.cc | 2 +- src/platform/win/ipc_win.cc | 2 +- src/xprofiler.cc | 16 ++++++++-------- 18 files changed, 19 insertions(+), 20 deletions(-) rename src/jsapi/{include => }/export_configure.h (99%) rename src/jsapi/{include => }/export_environment.h (100%) rename src/jsapi/{include => }/export_hooks.h (100%) rename src/jsapi/{include => }/export_http.h (100%) rename src/jsapi/{include => }/export_logger.h (100%) rename src/jsapi/{include => }/export_thread_listener.h (100%) rename src/jsapi/{include => }/export_thread_logbypass.h (72%) rename src/jsapi/{include => }/export_utils.h (68%) diff --git a/src/jsapi/export_configure.cc b/src/jsapi/export_configure.cc index ef2339a..7004b4e 100644 --- a/src/jsapi/export_configure.cc +++ b/src/jsapi/export_configure.cc @@ -1,4 +1,4 @@ -#include "include/export_configure.h" +#include "export_configure.h" #include "process_data.h" #include "util-inl.h" diff --git a/src/jsapi/include/export_configure.h b/src/jsapi/export_configure.h similarity index 99% rename from src/jsapi/include/export_configure.h rename to src/jsapi/export_configure.h index a7305c8..5fa6c8b 100644 --- a/src/jsapi/include/export_configure.h +++ b/src/jsapi/export_configure.h @@ -6,7 +6,6 @@ namespace xprofiler { void Configure(const Nan::FunctionCallbackInfo& info); void GetConfig(const Nan::FunctionCallbackInfo& info); - } // namespace xprofiler #endif /* XPROFILER_SRC_JSAPI_CONFIGURE_H */ diff --git a/src/jsapi/export_environment.cc b/src/jsapi/export_environment.cc index 892485c..2d1ca1c 100644 --- a/src/jsapi/export_environment.cc +++ b/src/jsapi/export_environment.cc @@ -1,4 +1,4 @@ -#include "include/export_environment.h" +#include "export_environment.h" #include "environment_data.h" #include "util-inl.h" diff --git a/src/jsapi/include/export_environment.h b/src/jsapi/export_environment.h similarity index 100% rename from src/jsapi/include/export_environment.h rename to src/jsapi/export_environment.h diff --git a/src/jsapi/export_hooks.cc b/src/jsapi/export_hooks.cc index 067db03..ca26c48 100644 --- a/src/jsapi/export_hooks.cc +++ b/src/jsapi/export_hooks.cc @@ -1,4 +1,4 @@ -#include "include/export_hooks.h" +#include "export_hooks.h" #include "configure-inl.h" #include "hooks/fatal_error.h" diff --git a/src/jsapi/include/export_hooks.h b/src/jsapi/export_hooks.h similarity index 100% rename from src/jsapi/include/export_hooks.h rename to src/jsapi/export_hooks.h diff --git a/src/jsapi/export_http.cc b/src/jsapi/export_http.cc index d6d0bba..8e50524 100644 --- a/src/jsapi/export_http.cc +++ b/src/jsapi/export_http.cc @@ -1,4 +1,4 @@ -#include "include/export_http.h" +#include "export_http.h" #include "environment_data.h" #include "logger.h" diff --git a/src/jsapi/include/export_http.h b/src/jsapi/export_http.h similarity index 100% rename from src/jsapi/include/export_http.h rename to src/jsapi/export_http.h diff --git a/src/jsapi/export_logger.cc b/src/jsapi/export_logger.cc index 0836f33..1be5925 100644 --- a/src/jsapi/export_logger.cc +++ b/src/jsapi/export_logger.cc @@ -1,4 +1,4 @@ -#include "include/export_logger.h" +#include "export_logger.h" #include "environment_data.h" diff --git a/src/jsapi/include/export_logger.h b/src/jsapi/export_logger.h similarity index 100% rename from src/jsapi/include/export_logger.h rename to src/jsapi/export_logger.h diff --git a/src/jsapi/export_thread_listener.cc b/src/jsapi/export_thread_listener.cc index a5894e1..59c84c1 100644 --- a/src/jsapi/export_thread_listener.cc +++ b/src/jsapi/export_thread_listener.cc @@ -1,4 +1,4 @@ -#include "include/export_thread_listener.h" +#include "export_thread_listener.h" #include "commands/listener.h" #include "environment_data.h" diff --git a/src/jsapi/include/export_thread_listener.h b/src/jsapi/export_thread_listener.h similarity index 100% rename from src/jsapi/include/export_thread_listener.h rename to src/jsapi/export_thread_listener.h diff --git a/src/jsapi/export_thread_logbypass.cc b/src/jsapi/export_thread_logbypass.cc index 4d6b775..5c44601 100644 --- a/src/jsapi/export_thread_logbypass.cc +++ b/src/jsapi/export_thread_logbypass.cc @@ -1,4 +1,4 @@ -#include "include/export_thread_logbypass.h" +#include "export_thread_logbypass.h" #include "environment_data.h" #include "logbypass/log.h" diff --git a/src/jsapi/include/export_thread_logbypass.h b/src/jsapi/export_thread_logbypass.h similarity index 72% rename from src/jsapi/include/export_thread_logbypass.h rename to src/jsapi/export_thread_logbypass.h index dd7cd55..76ccfb8 100644 --- a/src/jsapi/include/export_thread_logbypass.h +++ b/src/jsapi/export_thread_logbypass.h @@ -4,7 +4,7 @@ #include "nan.h" namespace xprofiler { -void CheckSocketPath(const Nan::FunctionCallbackInfo& info); +void RunLogBypass(const Nan::FunctionCallbackInfo& info); } #endif /* XPROFILER_SRC_JSAPI_THREAD_LOGBYPASS_H */ diff --git a/src/jsapi/include/export_utils.h b/src/jsapi/export_utils.h similarity index 68% rename from src/jsapi/include/export_utils.h rename to src/jsapi/export_utils.h index 7c9cb9c..dbb3d83 100644 --- a/src/jsapi/include/export_utils.h +++ b/src/jsapi/export_utils.h @@ -4,7 +4,7 @@ #include "nan.h" namespace xprofiler { -void RunLogBypass(const Nan::FunctionCallbackInfo& info); +void CheckSocketPath(const Nan::FunctionCallbackInfo& info); } #endif /* XPROFILER_SRC_JSAPI_UTILS_H */ diff --git a/src/platform/unix/ipc.cc b/src/platform/unix/ipc.cc index c8fb653..fafa68d 100644 --- a/src/platform/unix/ipc.cc +++ b/src/platform/unix/ipc.cc @@ -4,8 +4,8 @@ #include #include -#include "logger.h" #include "configure-inl.h" +#include "logger.h" namespace xprofiler { using Nan::FunctionCallbackInfo; diff --git a/src/platform/win/ipc_win.cc b/src/platform/win/ipc_win.cc index 4018d40..a136058 100644 --- a/src/platform/win/ipc_win.cc +++ b/src/platform/win/ipc_win.cc @@ -1,8 +1,8 @@ #ifdef _WIN32 #include -#include "logger.h" #include "configure-inl.h" +#include "logger.h" #include "uv.h" namespace xprofiler { diff --git a/src/xprofiler.cc b/src/xprofiler.cc index 06dccae..b5fe8b8 100644 --- a/src/xprofiler.cc +++ b/src/xprofiler.cc @@ -1,11 +1,11 @@ -#include "jsapi/include/export_configure.h" -#include "jsapi/include/export_environment.h" -#include "jsapi/include/export_hooks.h" -#include "jsapi/include/export_http.h" -#include "jsapi/include/export_logger.h" -#include "jsapi/include/export_thread_listener.h" -#include "jsapi/include/export_thread_logbypass.h" -#include "jsapi/include/export_utils.h" +#include "jsapi/export_configure.h" +#include "jsapi/export_environment.h" +#include "jsapi/export_hooks.h" +#include "jsapi/export_http.h" +#include "jsapi/export_logger.h" +#include "jsapi/export_thread_listener.h" +#include "jsapi/export_thread_logbypass.h" +#include "jsapi/export_utils.h" #include "library/common.h" #include "nan.h" #include "process_data.h"