From 383aacd25550cfbcbfe3a0b480efe0f3ced0b638 Mon Sep 17 00:00:00 2001 From: hyj1991 Date: Tue, 3 Aug 2021 14:25:30 +0800 Subject: [PATCH] optimize: logic of dump actions PR-URL: https://github.com/X-Profiler/xprofiler/pull/101 Reviewed-BY: hyj1991 --- src/commands/dump.cc | 81 ++++++++++++++++++-------------------------- src/commands/dump.h | 2 +- 2 files changed, 34 insertions(+), 49 deletions(-) diff --git a/src/commands/dump.cc b/src/commands/dump.cc index a268958..8367357 100644 --- a/src/commands/dump.cc +++ b/src/commands/dump.cc @@ -341,7 +341,7 @@ void UnrefDumpActionAsyncHandle() { template static json DoDumpAction(json command, DumpAction action, string prefix, - string ext, T *data, bool profliling, XpfError &err) { + string ext, T *data, bool profiling, XpfError &err) { json result; // get traceid @@ -401,7 +401,7 @@ static json DoDumpAction(json command, DumpAction action, string prefix, // send data NoticeMainJsThread(data); - if (!profliling) return result; + if (!profiling) return result; // get profiling time json options = command["options"]; @@ -418,60 +418,45 @@ static json DoDumpAction(json command, DumpAction action, string prefix, return result; } -#define ACTION_HANDLE(action, data_type, profliling, prefix, ext) \ - XpfError err; \ - json result = DoDumpAction( \ - command, action, #prefix, #ext, data, profliling, err); \ - if (err.Fail()) { \ - error(format("%s", err.GetErrMessage())); \ - return; \ - } \ +#define ACTION_HANDLE(action, data_type, profiling, prefix, ext) \ + XpfError err; \ + json result = DoDumpAction( \ + command, action, #prefix, #ext, data, profiling, err); \ + if (err.Fail()) { \ + error(format("%s", err.GetErrMessage())); \ + return; \ + } \ success(result); -COMMAND_CALLBACK(StartCpuProfiling) { - cpuprofile_dump_data_t *data = new cpuprofile_dump_data_t; - data->title = "xprofiler"; - ACTION_HANDLE(START_CPU_PROFILING, cpuprofile_, true, cpuprofile, cpuprofile) -} - -COMMAND_CALLBACK(StopCpuProfiling) { - cpuprofile_dump_data_t *data = new cpuprofile_dump_data_t; - ACTION_HANDLE(STOP_CPU_PROFILING, cpuprofile_, false, cpuprofile, cpuprofile) -} +#define V(func, data_type, action, profiling, prefix, ext) \ + COMMAND_CALLBACK(func) { \ + data_type##dump_data_t *data = new data_type##dump_data_t; \ + ACTION_HANDLE(action, data_type, profiling, prefix, ext) \ + } -COMMAND_CALLBACK(Heapdump) { - heapdump_data_t *data = new heapdump_data_t; - ACTION_HANDLE(HEAPDUMP, heap, false, heapdump, heapsnapshot) -} +// cpu profiling +V(StartCpuProfiling, cpuprofile_, START_CPU_PROFILING, true, cpuprofile, + cpuprofile) +V(StopCpuProfiling, cpuprofile_, STOP_CPU_PROFILING, false, cpuprofile, + cpuprofile) -COMMAND_CALLBACK(StartSamplingHeapProfiling) { - sampling_heapprofiler_dump_data_t *data = - new sampling_heapprofiler_dump_data_t; - ACTION_HANDLE(START_SAMPLING_HEAP_PROFILING, sampling_heapprofiler_, true, - heapprofile, heapprofile) -} +// sampling heap profiling +V(StartSamplingHeapProfiling, sampling_heapprofiler_, + START_SAMPLING_HEAP_PROFILING, true, heapprofile, heapprofile) +V(StopSamplingHeapProfiling, sampling_heapprofiler_, + STOP_SAMPLING_HEAP_PROFILING, false, heapprofile, heapprofile) -COMMAND_CALLBACK(StopSamplingHeapProfiling) { - sampling_heapprofiler_dump_data_t *data = - new sampling_heapprofiler_dump_data_t; - ACTION_HANDLE(STOP_SAMPLING_HEAP_PROFILING, sampling_heapprofiler_, false, - heapprofile, heapprofile) -} +// gc profiling +V(StartGcProfiling, gcprofiler_, START_GC_PROFILING, true, gcprofile, gcprofile) +V(StopGcProfiling, gcprofiler_, STOP_GC_PROFILING, false, gcprofile, gcprofile) -COMMAND_CALLBACK(StartGcProfiling) { - gcprofiler_dump_data_t *data = new gcprofiler_dump_data_t; - ACTION_HANDLE(START_GC_PROFILING, gcprofiler_, true, gcprofile, gcprofile) -} +// heapdump +V(Heapdump, heap, HEAPDUMP, false, heapdump, heapsnapshot) -COMMAND_CALLBACK(StopGcProfiling) { - gcprofiler_dump_data_t *data = new gcprofiler_dump_data_t; - ACTION_HANDLE(STOP_GC_PROFILING, gcprofiler_, false, gcprofile, gcprofile) -} +// dynamic report +V(GetNodeReport, node_report_, NODE_REPORT, false, diagreport, diag) -COMMAND_CALLBACK(GetNodeReport) { - node_report_dump_data_t *data = new node_report_dump_data_t; - ACTION_HANDLE(NODE_REPORT, node_report_, false, diagreport, diag) -} +#undef V #undef ACTION_HANDLE diff --git a/src/commands/dump.h b/src/commands/dump.h index 189d93c..f3e5772 100644 --- a/src/commands/dump.h +++ b/src/commands/dump.h @@ -34,7 +34,7 @@ typedef struct BaseDumpData { } dump_data_t; typedef struct CpuProfilerDumpData : BaseDumpData { - string title; + string title = "xprofiler"; } cpuprofile_dump_data_t; typedef struct HeapdumpData : BaseDumpData {