diff --git a/paddle/fluid/platform/profiler.cc b/paddle/fluid/platform/profiler.cc index f6d9c8f64fd35a66e1d7fcb18a92e76c894402ff..476a1929101447e632e4d4422394fc685419c009 100644 --- a/paddle/fluid/platform/profiler.cc +++ b/paddle/fluid/platform/profiler.cc @@ -353,7 +353,7 @@ RecordEvent::RecordEvent(const char *name, const EventRole role) { #endif #endif if (UNLIKELY(g_enable_host_event_recorder_hook == false)) { - RecordEvent(name, role, "none"); + OriginalConstruct(name, role, "none"); return; } shallow_copy_name_ = name; @@ -371,7 +371,7 @@ RecordEvent::RecordEvent(const std::string &name, const EventRole role) { #endif #endif if (UNLIKELY(g_enable_host_event_recorder_hook == false)) { - RecordEvent(name, role, "none"); + OriginalConstruct(name, role, "none"); return; } name_ = new std::string(name); @@ -389,13 +389,18 @@ RecordEvent::RecordEvent(const std::string &name, const EventRole role, } #endif #endif - if (g_enable_host_event_recorder_hook) { - name_ = new std::string(name); - start_ns_ = PosixInNsec(); - attr_ = new std::string(attr); + if (UNLIKELY(g_enable_host_event_recorder_hook == false)) { + OriginalConstruct(name, role, attr); return; } + name_ = new std::string(name); + start_ns_ = PosixInNsec(); + attr_ = new std::string(attr); +} +void RecordEvent::OriginalConstruct(const std::string &name, + const EventRole role, + const std::string &attr) { if (g_state == ProfilerState::kDisabled || name.empty()) return; // do some initialization @@ -408,7 +413,7 @@ RecordEvent::RecordEvent(const std::string &name, const EventRole role, // Maybe need the same push/pop behavior. Event *e = PushEvent(name, role, attr); SetCurAnnotation(e); - // name_ = e->name(); + *name_ = e->name(); } RecordEvent::~RecordEvent() { @@ -431,10 +436,10 @@ RecordEvent::~RecordEvent() { } else { HostEventRecorder::GetInstance().RecordEvent(*name_, start_ns_, end_ns, role_, *attr_); + delete attr_; } + delete name_; } - delete name_; - delete attr_; return; } diff --git a/paddle/fluid/platform/profiler.h b/paddle/fluid/platform/profiler.h index 317991160b79882079b0f9b4436f11de8acf9564..9d0bdf2358900e75f83631f5fdea5f09c1327b32 100644 --- a/paddle/fluid/platform/profiler.h +++ b/paddle/fluid/platform/profiler.h @@ -139,17 +139,20 @@ struct RecordEvent { ~RecordEvent(); + void OriginalConstruct(const std::string& name, const EventRole role, + const std::string& attr); + bool is_enabled_{false}; bool is_pushed_{false}; // Event name - const std::string* name_{nullptr}; + std::string* name_{nullptr}; const char* shallow_copy_name_{nullptr}; uint64_t start_ns_; // Need to distinguish name by op type, block_id, program_id and perhaps // different kernel invocations within an op. // std::string full_name_; EventRole role_{EventRole::kOrdinary}; - const std::string* attr_{nullptr}; + std::string* attr_{nullptr}; }; /*class RecordRPCEvent {