Skip to content

Commit

Permalink
optimize: take action (#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyj1991 authored Oct 8, 2022
1 parent 27f95d0 commit 7d3fbb2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 30 deletions.
47 changes: 18 additions & 29 deletions src/commands/dump.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ const DependentMap dependent_map = {
namespace {
uv_thread_t uv_profiling_callback_thread;
ActionMap action_map;
RequestMap request_map;
std::string cpuprofile_filepath = "";
std::string sampling_heapprofile_filepath = "";
std::string heapsnapshot_filepath = "";
Expand Down Expand Up @@ -123,13 +122,6 @@ void DependentActionRunning(DumpAction action, XpfError& err) {
}
}

void TransactionDone(string thread_name, string unique_key, XpfError& err) {
if (request_map.find(unique_key) != request_map.end()) {
err = XpfError::Failure("<%s> %s has been executed by other thread.",
thread_name.c_str(), unique_key.c_str());
}
}

template <typename T>
T* GetProfilingData(void* data, string notify_type, string unique_key) {
Isolate* isolate = Isolate::GetCurrent();
Expand All @@ -140,13 +132,6 @@ T* GetProfilingData(void* data, string notify_type, string unique_key) {
return dump_data;
}

template <typename T>
T* GetDumpData(void* data) {
T* dump_data = static_cast<T*>(data);
if (!dump_data->run_once) dump_data->run_once = true;
return dump_data;
}

void AfterDumpFile(string& filepath, string notify_type, string unique_key) {
Isolate* isolate = Isolate::GetCurrent();
EnvironmentData* env_data = EnvironmentData::GetCurrent(isolate);
Expand All @@ -157,11 +142,17 @@ void AfterDumpFile(string& filepath, string notify_type, string unique_key) {

} // namespace

#define CLEAR_DATA \
DebugT(module_type, env_data->thread_id(), "<%s> %s dump_data cleared.", \
notify_type.c_str(), unique_key.c_str()); \
delete dump_data;

#define CHECK_ERR(func) \
func; \
if (err.Fail()) { \
DebugT(module_type, env_data->thread_id(), "<%s> %s error: %s", \
notify_type.c_str(), unique_key.c_str(), err.GetErrMessage()); \
CLEAR_DATA; \
return; \
}

Expand All @@ -174,21 +165,8 @@ void HandleAction(v8::Isolate* isolate, void* data, string notify_type) {
// check transaction has been done
XpfError err;
string unique_key = traceid + "::" + Action2String(action);
TransactionDone(notify_type, unique_key, err);
if (err.Fail()) {
DebugT(module_type, env_data->thread_id(), "%s", err.GetErrMessage());
request_map.erase(unique_key);
// clear dump_data
if (dump_data->run_once) {
DebugT(module_type, env_data->thread_id(), "<%s> %s dump_data cleared.",
notify_type.c_str(), unique_key.c_str());
delete dump_data;
}
return;
}

// set action executing flag
request_map.insert(make_pair(unique_key, true));
DebugT(module_type, env_data->thread_id(), "<%s> %s handled.",
notify_type.c_str(), unique_key.c_str());

Expand All @@ -207,7 +185,8 @@ void HandleAction(v8::Isolate* isolate, void* data, string notify_type) {
break;
}
case STOP_CPU_PROFILING: {
CpuProfilerDumpData* tmp = GetDumpData<CpuProfilerDumpData>(data);
dump_data->run_once = true;
CpuProfilerDumpData* tmp = static_cast<CpuProfilerDumpData*>(data);
CpuProfiler::StopProfiling(isolate, tmp->title, cpuprofile_filepath);
AfterDumpFile(cpuprofile_filepath, notify_type, unique_key);
action_map.erase(START_CPU_PROFILING);
Expand All @@ -225,6 +204,7 @@ void HandleAction(v8::Isolate* isolate, void* data, string notify_type) {
break;
}
case STOP_SAMPLING_HEAP_PROFILING: {
dump_data->run_once = true;
SamplingHeapProfiler::StopSamplingHeapProfiling(
isolate, sampling_heapprofile_filepath);
AfterDumpFile(sampling_heapprofile_filepath, notify_type, unique_key);
Expand All @@ -237,6 +217,7 @@ void HandleAction(v8::Isolate* isolate, void* data, string notify_type) {
break;
}
case STOP_GC_PROFILING: {
dump_data->run_once = true;
GcProfiler::StopGCProfiling(isolate);
AfterDumpFile(gcprofile_filepath, notify_type, unique_key);
action_map.erase(START_GC_PROFILING);
Expand All @@ -260,9 +241,16 @@ void HandleAction(v8::Isolate* isolate, void* data, string notify_type) {
action);
break;
}

// clear dump_data
if (dump_data->run_once) {
CLEAR_DATA;
}
return;
}

#undef CHECK_ERR
#undef CLEAR_DATA

static void WaitForProfile(uint64_t profiling_time) {
uint64_t start = uv_hrtime();
Expand Down Expand Up @@ -440,6 +428,7 @@ static json DoDumpAction(json command, DumpAction action, string prefix,
data, profiling, err); \
if (err.Fail()) { \
error(format("%s", err.GetErrMessage())); \
delete data; \
return; \
} \
success(result); \
Expand Down
1 change: 0 additions & 1 deletion src/commands/dump.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ enum DumpAction {
};

using ActionMap = std::unordered_map<int, bool>;
using RequestMap = std::unordered_map<std::string, bool>;
using ConflictMap = std::unordered_map<int, std::vector<DumpAction>>;
using DependentMap = std::unordered_map<int, DumpAction>;

Expand Down

0 comments on commit 7d3fbb2

Please sign in to comment.