From 53d301c29bc49f10bbac21137e715a933d6a6d8a Mon Sep 17 00:00:00 2001 From: Tom Cherry Date: Wed, 22 Apr 2020 11:37:26 -0700 Subject: [PATCH] Remove thread safety from libbase logging / liblog There are no libbase users that require thread safety for SetLogger, SetAborter, or SetDefaultTag and the equivalent liblog symbols are unreleased, thus have effectively no users. It is hard to imagine a scenario where a user would need to use these functions in a multi-threaded program, and it is unreasonable for all users to pay for thread safety for a vast minority of potential scenarios. Thread safety implies less efficiency and necessarily means that these functions are neither fork safe nor async-signal safe, and we do have users who depend on those characteristics. It is always possible for users of the non-thread safe versions of these functions to build thread safe versions on top of them. For example, if a user needs a thread safe SetLogger(), they can use the non-thread safe SetLogger at the start of their program to register a logger that has its own lock and pointer to a logger function. Bug: 119867234 Test: logging unit tests Change-Id: I8afffec1a6957d3bda95502a4c59493e0c5049ce --- base/logging.cpp | 34 +++++++--------------------------- liblog/logger_write.cpp | 12 ------------ liblog/logger_write.h | 5 +---- liblog/properties.cpp | 3 --- 4 files changed, 8 insertions(+), 46 deletions(-) diff --git a/base/logging.cpp b/base/logging.cpp index cd460eb46..3c73fea1a 100644 --- a/base/logging.cpp +++ b/base/logging.cpp @@ -195,7 +195,6 @@ static std::mutex& LoggingLock() { return logging_lock; } -// Only used for Q fallback. static LogFunction& Logger() { #ifdef __ANDROID__ static auto& logger = *new LogFunction(LogdLogger()); @@ -205,7 +204,6 @@ static LogFunction& Logger() { return logger; } -// Only used for Q fallback. static AbortFunction& Aborter() { static auto& aborter = *new AbortFunction(DefaultAborter); return aborter; @@ -416,45 +414,27 @@ void InitLogging(char* argv[], LogFunction&& logger, AbortFunction&& aborter) { } void SetLogger(LogFunction&& logger) { + Logger() = std::move(logger); + static auto& liblog_functions = GetLibLogFunctions(); if (liblog_functions) { - // We need to atomically swap the old and new pointers since other threads may be logging. - // We know all threads will be using the new logger after __android_log_set_logger() returns, - // so we can delete it then. - // This leaks one std::function<> per instance of libbase if multiple copies of libbase within a - // single process call SetLogger(). That is the same cost as having a static - // std::function<>, which is the not-thread-safe alternative. - static std::atomic logger_function(nullptr); - auto* old_logger_function = logger_function.exchange(new LogFunction(logger)); liblog_functions->__android_log_set_logger([](const struct __android_log_message* log_message) { auto log_id = log_id_tToLogId(log_message->buffer_id); auto severity = PriorityToLogSeverity(log_message->priority); - auto& function = *logger_function.load(std::memory_order_acquire); - function(log_id, severity, log_message->tag, log_message->file, log_message->line, + Logger()(log_id, severity, log_message->tag, log_message->file, log_message->line, log_message->message); }); - delete old_logger_function; - } else { - std::lock_guard lock(LoggingLock()); - Logger() = std::move(logger); } } void SetAborter(AbortFunction&& aborter) { + Aborter() = std::move(aborter); + static auto& liblog_functions = GetLibLogFunctions(); if (liblog_functions) { - // See the comment in SetLogger(). - static std::atomic abort_function(nullptr); - auto* old_abort_function = abort_function.exchange(new AbortFunction(aborter)); - liblog_functions->__android_log_set_aborter([](const char* abort_message) { - auto& function = *abort_function.load(std::memory_order_acquire); - function(abort_message); - }); - delete old_abort_function; - } else { - std::lock_guard lock(LoggingLock()); - Aborter() = std::move(aborter); + liblog_functions->__android_log_set_aborter( + [](const char* abort_message) { Aborter()(abort_message); }); } } diff --git a/liblog/logger_write.cpp b/liblog/logger_write.cpp index d15b3679b..3a75fa3e9 100644 --- a/liblog/logger_write.cpp +++ b/liblog/logger_write.cpp @@ -28,7 +28,6 @@ #endif #include -#include #include #include @@ -38,7 +37,6 @@ #include "android/log.h" #include "log/log_read.h" #include "logger.h" -#include "rwlock.h" #include "uio.h" #ifdef __ANDROID__ @@ -142,10 +140,8 @@ std::string& GetDefaultTag() { static std::string default_tag = getprogname(); return default_tag; } -RwLock default_tag_lock; void __android_log_set_default_tag(const char* tag) { - auto lock = std::unique_lock{default_tag_lock}; GetDefaultTag().assign(tag, 0, LOGGER_ENTRY_MAX_PAYLOAD); } @@ -163,10 +159,8 @@ static __android_logger_function logger_function = __android_log_logd_logger; #else static __android_logger_function logger_function = __android_log_stderr_logger; #endif -static RwLock logger_function_lock; void __android_log_set_logger(__android_logger_function logger) { - auto lock = std::unique_lock{logger_function_lock}; logger_function = logger; } @@ -180,15 +174,12 @@ void __android_log_default_aborter(const char* abort_message) { } static __android_aborter_function aborter_function = __android_log_default_aborter; -static RwLock aborter_function_lock; void __android_log_set_aborter(__android_aborter_function aborter) { - auto lock = std::unique_lock{aborter_function_lock}; aborter_function = aborter; } void __android_log_call_aborter(const char* abort_message) { - auto lock = std::shared_lock{aborter_function_lock}; aborter_function(abort_message); } @@ -310,9 +301,7 @@ void __android_log_write_log_message(__android_log_message* log_message) { return; } - auto tag_lock = std::shared_lock{default_tag_lock, std::defer_lock}; if (log_message->tag == nullptr) { - tag_lock.lock(); log_message->tag = GetDefaultTag().c_str(); } @@ -322,7 +311,6 @@ void __android_log_write_log_message(__android_log_message* log_message) { } #endif - auto lock = std::shared_lock{logger_function_lock}; logger_function(log_message); } diff --git a/liblog/logger_write.h b/liblog/logger_write.h index 065fd55fd..eee277821 100644 --- a/liblog/logger_write.h +++ b/liblog/logger_write.h @@ -18,7 +18,4 @@ #include -#include "rwlock.h" - -std::string& GetDefaultTag(); // Must read lock default_tag_lock -extern RwLock default_tag_lock; \ No newline at end of file +std::string& GetDefaultTag(); diff --git a/liblog/properties.cpp b/liblog/properties.cpp index abd48fcd4..37670ecd6 100644 --- a/liblog/properties.cpp +++ b/liblog/properties.cpp @@ -23,7 +23,6 @@ #include #include -#include #include @@ -99,9 +98,7 @@ static int __android_log_level(const char* tag, size_t len) { static const char log_namespace[] = "persist.log.tag."; static const size_t base_offset = 8; /* skip "persist." */ - auto tag_lock = std::shared_lock{default_tag_lock, std::defer_lock}; if (tag == nullptr || len == 0) { - tag_lock.lock(); auto& tag_string = GetDefaultTag(); tag = tag_string.c_str(); len = tag_string.size();