From 9b5a65b819c28b3292550b536acbe3404eb9f25c Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Tue, 4 Aug 2020 17:51:44 +0800 Subject: [PATCH] refine init signal handler meg dumper (#25911) --- paddle/fluid/platform/init.cc | 11 +++++------ paddle/fluid/platform/init.h | 25 ++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/paddle/fluid/platform/init.cc b/paddle/fluid/platform/init.cc index 8a28292fb7..261f6e807a 100644 --- a/paddle/fluid/platform/init.cc +++ b/paddle/fluid/platform/init.cc @@ -58,7 +58,6 @@ namespace framework { std::once_flag gflags_init_flag; std::once_flag glog_init_flag; std::once_flag p2p_init_flag; -std::once_flag glog_warning_once_flag; bool InitGflags(std::vector args) { bool successed = false; @@ -260,22 +259,22 @@ const char *ParseSignalErrorString(const std::string &str) { } // Handle SIGSEGV, SIGILL, SIGFPE, SIGABRT, SIGBUS, and SIGTERM. -std::ostringstream signal_msg_dumper; void SignalHandle(const char *data, int size) { try { // NOTE1: The glog FailureSignalHandler dumped messages // are deal with line by line + auto signal_msg_dunmer_ptr = SignalMessageDumper::Instance().Get(); // NOTE2: we only deal with the time info ane signal info, // the stack trace will generated by paddle self if (StartsWith(data, "*** Aborted at")) { - signal_msg_dumper << " [TimeInfo: " << std::string(data, size - 1) - << "]\n"; + *signal_msg_dunmer_ptr << " [TimeInfo: " << std::string(data, size - 1) + << "]\n"; } else if (StartsWith(data, "***")) { std::string signal_info(data, size - 1); std::string useless_substr("; stack trace:"); size_t start_pos = signal_info.rfind(useless_substr); signal_info.replace(start_pos, useless_substr.length(), ""); - signal_msg_dumper << " [SignalInfo: " << signal_info << "]\n"; + *signal_msg_dunmer_ptr << " [SignalInfo: " << signal_info << "]\n"; // NOTE3: Here does not throw an exception, // otherwise it will casue "terminate called recursively" auto exp = platform::EnforceNotMet( @@ -283,7 +282,7 @@ void SignalHandle(const char *data, int size) { "A serious error (%s) is detected by the operating system.", ParseSignalErrorString(signal_info)), __FILE__, __LINE__); - std::cout << exp.what() << signal_msg_dumper.str() << std::endl; + std::cout << exp.what() << (*signal_msg_dunmer_ptr).str() << std::endl; } } catch (...) { // Since the program has already triggered a system error, diff --git a/paddle/fluid/platform/init.h b/paddle/fluid/platform/init.h index 908a5943d4..5bd5a640ad 100644 --- a/paddle/fluid/platform/init.h +++ b/paddle/fluid/platform/init.h @@ -12,6 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #pragma once +#include #include // NOLINT #include #include @@ -22,7 +23,7 @@ limitations under the License. */ namespace paddle { namespace platform { -void ParseCommandLineFlags(int argc, char **argv, bool remove); +void ParseCommandLineFlags(int argc, char** argv, bool remove); } // namespace platform } // namespace paddle @@ -32,14 +33,32 @@ namespace framework { bool InitGflags(std::vector argv); -void InitGLOG(const std::string &prog_name); +void InitGLOG(const std::string& prog_name); void InitDevices(bool init_p2p); void InitDevices(bool init_p2p, const std::vector devices); #ifndef _WIN32 -void SignalHandle(const char *data, int size); +class SignalMessageDumper { + public: + ~SignalMessageDumper() {} + SignalMessageDumper(const SignalMessageDumper& o) = delete; + const SignalMessageDumper& operator=(const SignalMessageDumper& o) = delete; + + static SignalMessageDumper& Instance() { + static SignalMessageDumper instance; + return instance; + } + + std::shared_ptr Get() { return dumper_; } + + private: + SignalMessageDumper() : dumper_(new std::ostringstream()) {} + std::shared_ptr dumper_; +}; + +void SignalHandle(const char* data, int size); #endif } // namespace framework -- GitLab