From 79caed66676cacfc44f5681bb5e8032a4b78a80e Mon Sep 17 00:00:00 2001 From: wangchaochaohu Date: Tue, 26 May 2020 19:43:33 +0800 Subject: [PATCH] fix the print error of PE record_event and framework overhead in profiler test=develop (#24744) --- paddle/fluid/platform/device_tracer.cc | 43 ++++++++++++++------------ paddle/fluid/platform/device_tracer.h | 2 +- paddle/fluid/platform/profiler.cc | 3 +- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/paddle/fluid/platform/device_tracer.cc b/paddle/fluid/platform/device_tracer.cc index 6be8ed25e3f..f2266bd78ce 100644 --- a/paddle/fluid/platform/device_tracer.cc +++ b/paddle/fluid/platform/device_tracer.cc @@ -641,22 +641,24 @@ DeviceTracer *GetDeviceTracer() { return tracer; } -std::string SetCurAnnotation(Event *event) { +// In order to record PE time, we add main_thread_annotation_stack +// for all event between PE run, we treat it as PE's child Event, +// so when event is not in same thread of PE event, we need add +// father event(PE::run event) for this event +void SetCurAnnotation(Event *event) { std::string ret; - if (!annotation_stack.empty() && event->role() != EventRole::kSpecial) { + if (!annotation_stack.empty()) { event->set_parent(annotation_stack.back()); event->set_name(annotation_stack.back()->name() + "/" + event->name()); } - + if (annotation_stack.empty() && !main_thread_annotation_stack.empty() && + main_thread_annotation_stack.back()->thread_id() != event->thread_id()) { + event->set_parent(main_thread_annotation_stack.back()); + event->set_name(main_thread_annotation_stack.back()->name() + "/" + + event->name()); + } annotation_stack.push_back(event); - if (!main_thread_annotation_stack_name.empty() && !annotation_stack.empty() && - main_thread_annotation_stack.back()->thread_id() != - annotation_stack.back()->thread_id()) { - ret = main_thread_annotation_stack_name.back() + "/" + event->name(); - } else { - ret = event->name(); - } if (event->role() == EventRole::kSpecial) { std::string name = event->name(); if (!main_thread_annotation_stack_name.empty()) { @@ -665,22 +667,23 @@ std::string SetCurAnnotation(Event *event) { main_thread_annotation_stack_name.push_back(name); main_thread_annotation_stack.push_back(event); } - - return ret; } void ClearCurAnnotation() { - if (!main_thread_annotation_stack_name.empty() && !annotation_stack.empty() && - main_thread_annotation_stack.back()->thread_id() != - annotation_stack.back()->thread_id()) { - annotation_stack.back()->set_name(main_thread_annotation_stack_name.back() + - "/" + annotation_stack.back()->name()); - } if (!main_thread_annotation_stack.empty() && main_thread_annotation_stack.back()->name() == annotation_stack.back()->name()) { - main_thread_annotation_stack_name.pop_back(); - main_thread_annotation_stack.pop_back(); + std::string name = annotation_stack.back()->name(); + std::string main_name = main_thread_annotation_stack.back()->name(); + int main_name_len = main_name.length(); + int name_len = name.length(); + int prefix_len = main_name_len - name_len; + + if (prefix_len >= 0 && main_name.at(prefix_len) == '/' && + name == main_name.substr(prefix_len, name_len)) { + main_thread_annotation_stack_name.pop_back(); + main_thread_annotation_stack.pop_back(); + } } annotation_stack.pop_back(); } diff --git a/paddle/fluid/platform/device_tracer.h b/paddle/fluid/platform/device_tracer.h index 44b7af149ef..85168a046fb 100644 --- a/paddle/fluid/platform/device_tracer.h +++ b/paddle/fluid/platform/device_tracer.h @@ -137,7 +137,7 @@ class DeviceTracer { DeviceTracer* GetDeviceTracer(); // Set a name for the cuda kernel operation being launched by the thread. -std::string SetCurAnnotation(Event* event); +void SetCurAnnotation(Event* event); // Clear the name after the operation is done. void ClearCurAnnotation(); // Current name of the operation being run in the thread. diff --git a/paddle/fluid/platform/profiler.cc b/paddle/fluid/platform/profiler.cc index be655255bd8..22e00b89239 100644 --- a/paddle/fluid/platform/profiler.cc +++ b/paddle/fluid/platform/profiler.cc @@ -73,7 +73,8 @@ RecordEvent::RecordEvent(const std::string &name, const EventRole role) { // lock is not needed, the code below is thread-safe Event *e = PushEvent(name, role); // Maybe need the same push/pop behavior. - name_ = SetCurAnnotation(e); + SetCurAnnotation(e); + name_ = e->name(); } RecordEvent::~RecordEvent() { -- GitLab