Skip to content

Commit

Permalink
refactor: optimize macro definitions
Browse files Browse the repository at this point in the history
PR-URL: https://github.com/hyj1991/xprofiler/pull/43
Reviewed-By: hyj1991 <yeekwanvong@gmail.com>
  • Loading branch information
hyj1991 authored Dec 27, 2019
1 parent 654941c commit 4f7fc1f
Show file tree
Hide file tree
Showing 14 changed files with 292 additions and 313 deletions.
46 changes: 26 additions & 20 deletions src/commands/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ using nlohmann::json;
using std::exception;
using std::string;

#define HANDLE_COMMANDS(cmd_str, handle) \
if (strcmp(cmd.c_str(), #cmd_str) == 0) { \
handle( \
parsed, FmtMessage, \
[traceid](json data) { SuccessValue(traceid, data); }, \
[traceid](string message) { ErrorValue(traceid, message); }); \
handled = true; \
}

void ParseCmd(char *command) {
Debug("parser", "received command: %s", command);
json parsed;
Expand All @@ -36,33 +45,30 @@ void ParseCmd(char *command) {
return;
}

#define V(cmd_str, handle) \
if (strcmp(cmd.c_str(), #cmd_str) == 0) { \
handle( \
parsed, FmtMessage, \
[traceid](json data) { SuccessValue(traceid, data); }, \
[traceid](string message) { ErrorValue(traceid, message); }); \
handled = true; \
}
// get version
V(check_version, GetXprofilerVersion)
HANDLE_COMMANDS(check_version, GetXprofilerVersion)

// get/set config
V(get_config, GetXprofilerConfig)
V(set_config, SetXprofilerConfig)
HANDLE_COMMANDS(get_config, GetXprofilerConfig)
HANDLE_COMMANDS(set_config, SetXprofilerConfig)

// cpu profiling
V(start_cpu_profiling, StartCpuProfiling)
V(stop_cpu_profiling, StopCpuProfiling)
HANDLE_COMMANDS(start_cpu_profiling, StartCpuProfiling)
HANDLE_COMMANDS(stop_cpu_profiling, StopCpuProfiling)

// heapdump
V(heapdump, Heapdump)
HANDLE_COMMANDS(heapdump, Heapdump)

// sampling heap profiling
V(start_heap_profiling, StartSamplingHeapProfiling)
V(stop_heap_profiling, StopSamplingHeapProfiling)
HANDLE_COMMANDS(start_heap_profiling, StartSamplingHeapProfiling)
HANDLE_COMMANDS(stop_heap_profiling, StopSamplingHeapProfiling)

// gc profiling
V(start_gc_profiling, StartGcProfiling)
V(stop_gc_profiling, StopGcProfiling)
HANDLE_COMMANDS(start_gc_profiling, StartGcProfiling)
HANDLE_COMMANDS(stop_gc_profiling, StopGcProfiling)

// node report
V(diag_report, GetNodeReport)
#undef V
HANDLE_COMMANDS(diag_report, GetNodeReport)

// not match any commands
if (!handled) {
Expand Down
3 changes: 2 additions & 1 deletion src/commands/report/javascript_stack.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ void SetJavaScriptStack(JSONWriter* writer) {
else
writer->json_keyvalue("pcAddress", "nullptr");

#if (NODE_MODULE_VERSION >= 0x0040)
#if (NODE_VERSION_AT_LEAST(10, 12, 0))
// needs v8 version >= 6.8
Local<StackFrame> frame = stack->GetFrame(isolate, i);
#else
Local<StackFrame> frame = stack->GetFrame(i);
Expand Down
8 changes: 3 additions & 5 deletions src/commands/send.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@ namespace xprofiler {
using nlohmann::json;
using std::string;

#define V(ok, res) \
#define SEND_VALUE(ok, res) \
result["ok"] = ok; \
result["traceid"] = traceid; \
result[#res] = res; \
CreateIpcClient(const_cast<char *>(result.dump().c_str()));

void ErrorValue(string traceid, string message) {
json result;
V(false, message);
SEND_VALUE(false, message);
}

void SuccessValue(string traceid, json data) {
json result;
V(true, data);
SEND_VALUE(true, data);
}

#undef V
} // namespace xprofiler
38 changes: 20 additions & 18 deletions src/commands/simple/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ namespace xprofiler {
using nlohmann::json;
using std::exception;

#define HANDLE_CONFIG_SETTING(ret, key, func) \
if (options.find(#key) != options.end()) { \
ret value; \
XpfError err; \
value = GetJsonValue<ret>(options, #key, err); \
if (err.Fail()) { \
error(format("%s", err.GetErrMessage())); \
return; \
} \
Set##func(value); \
setted = true; \
data[#key] = Get##func(); \
}

COMMAND_CALLBACK(GetXprofilerConfig) {
json data;
data["log_dir"] = GetLogDir();
Expand All @@ -22,24 +36,12 @@ COMMAND_CALLBACK(SetXprofilerConfig) {
json options = command["options"];
bool setted = false;
json data;
#define V(ret, key, func) \
if (options.find(#key) != options.end()) { \
ret value; \
XpfError err; \
value = GetJsonValue<ret>(options, #key, err); \
if (err.Fail()) { \
error(format("%s", err.GetErrMessage())); \
return; \
} \
Set##func(value); \
setted = true; \
data[#key] = Get##func(); \
}
V(bool, enable_log_uv_handles, EnableLogUvHandles)
V(LOG_LEVEL, log_level, LogLevel)
V(LOG_TYPE, log_type, LogType)
V(bool, enable_fatal_error_hook, EnableFatalErrorHook)
#undef V

HANDLE_CONFIG_SETTING(LOG_LEVEL, log_level, LogLevel)
HANDLE_CONFIG_SETTING(LOG_TYPE, log_type, LogType)
HANDLE_CONFIG_SETTING(bool, enable_log_uv_handles, EnableLogUvHandles)
HANDLE_CONFIG_SETTING(bool, enable_fatal_error_hook, EnableFatalErrorHook)

if (!setted)
error(format("not support setting config %s", options.dump().c_str()));
else
Expand Down
99 changes: 27 additions & 72 deletions src/configure.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "library/common.h"
#include "nan.h"
#include "configure.h"

namespace xprofiler {
using Nan::FunctionCallbackInfo;
Expand All @@ -19,94 +18,50 @@ using v8::Value;

static string log_dir = "/tmp";
static uint32_t log_interval = 60;
static bool enable_log_uv_handles = true;
static bool log_format_alinode = false;
static LOG_LEVEL log_level = LOG_ERROR;
static LOG_TYPE log_type = LOG_TO_FILE;
static bool enable_log_uv_handles = true;
static bool log_format_alinode = false;
static bool enable_fatal_error_hook = true;

#define COVERT_STRING(key) \
if (key##_value->IsString()) { \
Local<String> key##_string = To<String>(key##_value).ToLocalChecked(); \
Utf8String key##_utf8string(key##_string); \
key = *key##_utf8string; \
}

#define CONVERT_UINT32(key) \
if (key##_value->IsUint32()) key = To<uint32_t>(key##_value).ToChecked();

#define CONVERT_UINT32_V2(key, type) \
if (key##_value->IsUint32()) \
key = static_cast<type>(To<uint32_t>(key##_value).ToChecked());

#define CONVERT_BOOL(key) \
if (key##_value->IsBoolean()) key = To<bool>(key##_value).ToChecked();

void Configure(const FunctionCallbackInfo<Value> &info) {
if (!info[0]->IsObject()) {
ThrowTypeError(New<String>("config must be object!").ToLocalChecked());
return;
}
Local<Object> config = To<Object>(info[0]).ToLocalChecked();
#define S(key) \
Local<Value> key##_value = \
Get(config, New<String>(#key).ToLocalChecked()).ToLocalChecked();
#define V(key, cvrt) S(key) cvrt(key)
#define W(key, cvrt, type) S(key) cvrt(key, type)
// set log dir
V(log_dir, COVERT_STRING)

// set log interval
V(log_interval, CONVERT_UINT32)

// enable collecting uv handles
V(enable_log_uv_handles, CONVERT_BOOL)

// log format: standard or alinode
V(log_format_alinode, CONVERT_BOOL)

// log level: 0 info, 1 error, 2 debug
W(log_level, CONVERT_UINT32_V2, LOG_LEVEL)
COVERT_STRING(log_dir)
CONVERT_UINT32(log_interval)
CONVERT_UINT32_WITH_TYPE(log_level, LOG_LEVEL)
CONVERT_UINT32_WITH_TYPE(log_type, LOG_TYPE)
CONVERT_BOOL(enable_log_uv_handles)
CONVERT_BOOL(log_format_alinode)
CONVERT_BOOL(enable_fatal_error_hook)

// log type: 0 file, 1 ttl
W(log_type, CONVERT_UINT32_V2, LOG_TYPE)

// enable fatal error hook
V(enable_fatal_error_hook, CONVERT_BOOL)
#undef S
#undef V
#undef W
info.GetReturnValue().Set(New<Boolean>(true));
}

void GetConfig(const FunctionCallbackInfo<Value> &info) {
Local<Object> config = New<Object>();
#define V(key, type) \
Set(config, New<String>(#key).ToLocalChecked(), \
New<type>(key).ToLocalChecked());
#define W(key, type) \
Set(config, New<String>(#key).ToLocalChecked(), New<type>(key));
V(log_dir, String)
W(log_interval, Number)
W(enable_log_uv_handles, Boolean)
W(log_format_alinode, Boolean)
W(log_level, Number)
W(log_type, Number)
W(enable_fatal_error_hook, Boolean)
#undef V
#undef W

CONFIG_LOCAL_STRING(log_dir, String)
CONFIG_NATIVE_NUMBER(log_interval, Number)
CONFIG_NATIVE_NUMBER(log_level, Number)
CONFIG_NATIVE_NUMBER(log_type, Number)
CONFIG_NATIVE_NUMBER(enable_log_uv_handles, Boolean)
CONFIG_NATIVE_NUMBER(log_format_alinode, Boolean)
CONFIG_NATIVE_NUMBER(enable_fatal_error_hook, Boolean)

info.GetReturnValue().Set(config);
}

#define V(ret, func, vari) \
ret Get##func() { return vari; } \
void Set##func(ret value) { vari = value; }
V(string, LogDir, log_dir)
V(uint32_t, LogInterval, log_interval)
V(bool, FormatAsAlinode, log_format_alinode)
V(bool, EnableLogUvHandles, enable_log_uv_handles)
V(LOG_LEVEL, LogLevel, log_level)
V(LOG_TYPE, LogType, log_type)
V(bool, EnableFatalErrorHook, enable_fatal_error_hook)
#undef V
// define getter / setter
DEFINE_GET_SET_FUNCTION(LogDir, string, log_dir)
DEFINE_GET_SET_FUNCTION(LogInterval, uint32_t, log_interval)
DEFINE_GET_SET_FUNCTION(LogLevel, LOG_LEVEL, log_level)
DEFINE_GET_SET_FUNCTION(LogType, LOG_TYPE, log_type)
DEFINE_GET_SET_FUNCTION(FormatAsAlinode, bool, log_format_alinode)
DEFINE_GET_SET_FUNCTION(EnableLogUvHandles, bool, enable_log_uv_handles)
DEFINE_GET_SET_FUNCTION(EnableFatalErrorHook, bool, enable_fatal_error_hook)
} // namespace xprofiler
64 changes: 52 additions & 12 deletions src/configure.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,58 @@ using Nan::FunctionCallbackInfo;
using std::string;
using v8::Value;

// normal external
#define V(ret, func) \
ret Get##func(); \
void Set##func(ret value);
V(string, LogDir)
V(uint32_t, LogInterval)
V(bool, FormatAsAlinode)
V(bool, EnableLogUvHandles)
V(LOG_LEVEL, LogLevel)
V(LOG_TYPE, LogType)
V(bool, EnableFatalErrorHook)
#undef V
#define DECLARE_GETTER_FUNCTION(func_name, type) type Get##func_name()

#define DECLARE_SETTER_FUNCTION(func_name, type) void Set##func_name(type value)

#define DECLARE_GET_SET_FUNCTION(func_name, type) \
DECLARE_GETTER_FUNCTION(func_name, type); \
DECLARE_SETTER_FUNCTION(func_name, type);

#define DEFINE_GET_SET_FUNCTION(func_name, type, vari) \
DECLARE_GETTER_FUNCTION(func_name, type) { return vari; } \
DECLARE_SETTER_FUNCTION(func_name, type) { vari = value; }

#define LOCAL_VALUE(key) \
Local<Value> key##_value = \
Get(config, New<String>(#key).ToLocalChecked()).ToLocalChecked();

#define COVERT_STRING(key) \
LOCAL_VALUE(key) \
if (key##_value->IsString()) { \
Local<String> key##_string = To<String>(key##_value).ToLocalChecked(); \
Utf8String key##_utf8string(key##_string); \
key = *key##_utf8string; \
}

#define CONVERT_UINT32(key) \
LOCAL_VALUE(key) \
if (key##_value->IsUint32()) key = To<uint32_t>(key##_value).ToChecked();

#define CONVERT_UINT32_WITH_TYPE(key, type) \
LOCAL_VALUE(key) \
if (key##_value->IsUint32()) \
key = static_cast<type>(To<uint32_t>(key##_value).ToChecked());

#define CONVERT_BOOL(key) \
LOCAL_VALUE(key) \
if (key##_value->IsBoolean()) key = To<bool>(key##_value).ToChecked();

#define CONFIG_LOCAL_STRING(key, type) \
Set(config, New<String>(#key).ToLocalChecked(), \
New<type>(key).ToLocalChecked());

#define CONFIG_NATIVE_NUMBER(key, type) \
Set(config, New<String>(#key).ToLocalChecked(), New<type>(key));

// declare getter / setter
DECLARE_GET_SET_FUNCTION(LogDir, string)
DECLARE_GET_SET_FUNCTION(LogInterval, uint32_t)
DECLARE_GET_SET_FUNCTION(LogLevel, LOG_LEVEL)
DECLARE_GET_SET_FUNCTION(LogType, LOG_TYPE)
DECLARE_GET_SET_FUNCTION(FormatAsAlinode, bool)
DECLARE_GET_SET_FUNCTION(EnableLogUvHandles, bool)
DECLARE_GET_SET_FUNCTION(EnableFatalErrorHook, bool)

// javascript accessible
void Configure(const FunctionCallbackInfo<Value> &info);
Expand Down
Loading

0 comments on commit 4f7fc1f

Please sign in to comment.