Skip to content

Commit

Permalink
optimize: logic of dump actions
Browse files Browse the repository at this point in the history
PR-URL: #101
Reviewed-BY: hyj1991 <[email protected]>
  • Loading branch information
hyj1991 authored Aug 3, 2021
1 parent d4265d2 commit 383aacd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 49 deletions.
81 changes: 33 additions & 48 deletions src/commands/dump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ void UnrefDumpActionAsyncHandle() {

template <typename T>
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
Expand Down Expand Up @@ -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"];
Expand All @@ -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<data_type##dump_data_t>( \
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<data_type##dump_data_t>( \
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

Expand Down
2 changes: 1 addition & 1 deletion src/commands/dump.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 383aacd

Please sign in to comment.