From 0887c50c8fedc54d07cab6f9baafb14b5fee07c8 Mon Sep 17 00:00:00 2001 From: Jan Steemann Date: Wed, 2 Mar 2016 11:31:41 +0100 Subject: [PATCH 1/4] fixed logger initialization and shutdown --- lib/Basics/Logger.cpp | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/Basics/Logger.cpp b/lib/Basics/Logger.cpp index e06e97ca40..02915ee61b 100644 --- a/lib/Basics/Logger.cpp +++ b/lib/Basics/Logger.cpp @@ -98,6 +98,12 @@ static std::atomic LoggingActive(false); static Mutex LogTopicNamesLock; static std::map LogTopicNames; +//////////////////////////////////////////////////////////////////////////////// +/// @brief mutex to initialize, flush and shutdown logging +//////////////////////////////////////////////////////////////////////////////// + +static Mutex InitializeMutex; + //////////////////////////////////////////////////////////////////////////////// /// @brief message container //////////////////////////////////////////////////////////////////////////////// @@ -1124,15 +1130,16 @@ std::vector Logger::bufferedEntries(LogLevel level, uint64_t start, /// @brief initializes the logging component //////////////////////////////////////////////////////////////////////////////// -static std::atomic IsInitialized(false); static std::atomic ShutdownInitalized(false); static std::unique_ptr LoggingThread; static void ShutdownLogging() { Logger::shutdown(true); } void Logger::initialize(bool threaded) { - if (!IsInitialized) { - IsInitialized = true; + MUTEX_LOCKER(locker, InitializeMutex); + + if (LoggingActive.load(std::memory_order_seq_cst)) { + return; } // logging is now active @@ -1159,27 +1166,22 @@ void Logger::initialize(bool threaded) { //////////////////////////////////////////////////////////////////////////////// void Logger::shutdown(bool clearBuffers) { - if (!IsInitialized) { + MUTEX_LOCKER(locker, InitializeMutex); + + if (!LoggingActive.load(std::memory_order_seq_cst)) { + // if logging not activated or already shutdown, then we can abort here return; } // logging is now inactive (this will terminate the logging thread) - LoggingActive.store(false, std::memory_order_relaxed); + LoggingActive.store(false, std::memory_order_seq_cst); // join with the logging thread if (ThreadedLogging) { - LoggingActive.store(false, std::memory_order_relaxed); - // ignore all errors for now as we cannot log them anywhere... LoggingThread->beginShutdown(); - for (size_t i = 0; i < 10; ++i) { - if (LoggingThread->isRunning()) { - break; - } - - usleep(100 * 1000); - } + LoggingThread.reset(); } // cleanup appenders @@ -1222,7 +1224,10 @@ void Logger::reopen() { //////////////////////////////////////////////////////////////////////////////// void Logger::flush() { - if (!IsInitialized) { + MUTEX_LOCKER(locker, InitializeMutex); + + if (!LoggingActive.load(std::memory_order_seq_cst)) { + // logging not (or not yet) initialized return; } From c719beccc439f56dfc15e0f80936286674e93afb Mon Sep 17 00:00:00 2001 From: Frank Celler Date: Wed, 2 Mar 2016 12:45:04 +0100 Subject: [PATCH 2/4] added BASE_LD_FLAGS and BASE_LIBS --- CMakeLists.txt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c498ea5270..9c912f44e1 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -182,6 +182,8 @@ else () set(BASE_FLAGS) set(BASE_C_FLAGS ${CMAKE_C_FLAGS}) set(BASE_CXX_FLAGS ${CMAKE_CXX_FLAGS}) + set(BASE_LD_FLAGS $ENV{LDFLAGS}) + set(BASE_LIBS $ENV{LIBS}) endif () if (SOLARIS) @@ -226,7 +228,11 @@ if (MSVC) set(MSVC_LIBS crypt32.lib;WINMM.LIB;Ws2_32.lib;getopt;regex) set(CMAKE_EXE_LINKER_FLAGS - "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:CONSOLE /LTCG /SAFESEH:NO /MACHINE:x64 /ignore:4099" + "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:CONSOLE /LTCG /SAFESEH:NO /MACHINE:x64 /ignore:4099 ${BASE_LD_FLAGS}" + ) +else () + set(CMAKE_EXE_LINKER_FLAGS + "${CMAKE_EXE_LINKER_FLAGS} ${BASE_LD_FLAGS}" ) endif () @@ -680,6 +686,7 @@ list(INSERT SYSTEM_LIBRARIES 0 ${ZLIB_LIBS} ${ICU_LIBS} ${OPENSSL_LIBRARIES} + ${BASE_LIBS} ${CMAKE_THREAD_LIBS_INIT} ${CMAKE_DL_LIBS} ) From 9342b0bab9a47734eeb86fb49de8329eb6d96f5e Mon Sep 17 00:00:00 2001 From: Frank Celler Date: Wed, 2 Mar 2016 14:08:18 +0100 Subject: [PATCH 3/4] be verbose --- CMakeLists.txt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9c912f44e1..27553ed3bf 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -463,6 +463,14 @@ if (VERBOSE) message(STATUS "${BASE_CXX_FLAGS}") message(STATUS) + message(STATUS "Info BASE_LD_FLAGS") + message(STATUS "${BASE_LD_FLAGS}") + message(STATUS) + + message(STATUS "Info BASE_LIBS") + message(STATUS "${BASE_LIBS}") + message(STATUS) + message(STATUS "Info CMAKE_C_FLAGS") message(STATUS "${CMAKE_C_FLAGS}") message(STATUS) From 90bf212f54a25679639fbc0392b1debbe67a271d Mon Sep 17 00:00:00 2001 From: Jan Steemann Date: Wed, 2 Mar 2016 14:56:30 +0100 Subject: [PATCH 4/4] fixed shutdown issues --- arangod/RestServer/ArangoServer.cpp | 2 +- lib/Basics/Logger.cpp | 7 ++++--- lib/Basics/Thread.cpp | 2 ++ 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/arangod/RestServer/ArangoServer.cpp b/arangod/RestServer/ArangoServer.cpp index 305eb8cdf8..c264fb0f37 100644 --- a/arangod/RestServer/ArangoServer.cpp +++ b/arangod/RestServer/ArangoServer.cpp @@ -448,6 +448,7 @@ int ArangoServer::start() { } else if (_daemonMode) { return startupDaemon(); } else { + InitializeWorkMonitor(); _applicationServer->setupLogging(true, false, false); if (!_pidFile.empty()) { @@ -1360,7 +1361,6 @@ void ArangoServer::buildApplicationServer() { } int ArangoServer::startupServer() { - InitializeWorkMonitor(); TRI_InitializeStatistics(); OperationMode::server_operation_mode_e mode = diff --git a/lib/Basics/Logger.cpp b/lib/Basics/Logger.cpp index 02915ee61b..d927fd9496 100644 --- a/lib/Basics/Logger.cpp +++ b/lib/Basics/Logger.cpp @@ -722,7 +722,7 @@ static void QueueMessage(char const* function, char const* file, long int line, class LogThread : public Thread { public: explicit LogThread(std::string const& name) : Thread(name) {} - ~LogThread() {shutdown();} + ~LogThread() { shutdown(); } public: void run(); @@ -748,6 +748,7 @@ void LogThread::run() { delete msg; } } + //////////////////////////////////////////////////////////////////////////////// /// @brief LogTopic //////////////////////////////////////////////////////////////////////////////// @@ -1146,7 +1147,7 @@ void Logger::initialize(bool threaded) { LoggingActive.store(true); // generate threaded logging? - ThreadedLogging.store(threaded, std::memory_order_relaxed); + ThreadedLogging.store(threaded, std::memory_order_seq_cst); if (threaded) { LoggingThread = std::make_unique("Logging"); @@ -1173,9 +1174,9 @@ void Logger::shutdown(bool clearBuffers) { return; } - // logging is now inactive (this will terminate the logging thread) LoggingActive.store(false, std::memory_order_seq_cst); + // logging is now inactive (this will terminate the logging thread) // join with the logging thread if (ThreadedLogging) { // ignore all errors for now as we cannot log them anywhere... diff --git a/lib/Basics/Thread.cpp b/lib/Basics/Thread.cpp index 7199dc3247..080401d9ef 100644 --- a/lib/Basics/Thread.cpp +++ b/lib/Basics/Thread.cpp @@ -156,10 +156,12 @@ Thread::~Thread() { #endif } +#ifdef TRI_HAVE_POSIX_THREADS if (_state.load() != ThreadState::DETACHED) { LOG(FATAL) << "thread is not detached, hard shutdown"; FATAL_ERROR_EXIT(); } +#endif } ////////////////////////////////////////////////////////////////////////////////