Logging: reduce locking frequency by fixing TODO.

Bug: webrtc:11567
Change-Id: I0cd5062c3a088e3781d009242db32069193fbe82
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/176902
Commit-Queue: Markus Handell <handellm@webrtc.org>
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#31657}
This commit is contained in:
Markus Handell 2020-07-08 08:52:48 +02:00 committed by Commit Bot
parent a827a30bb7
commit ce1ff6f8a7
2 changed files with 11 additions and 6 deletions

View File

@ -90,6 +90,7 @@ bool LogMessage::log_to_stderr_ = true;
// cleanup by setting to null, or let it leak (safe at program exit). // cleanup by setting to null, or let it leak (safe at program exit).
ABSL_CONST_INIT LogSink* LogMessage::streams_ RTC_GUARDED_BY(g_log_mutex_) = ABSL_CONST_INIT LogSink* LogMessage::streams_ RTC_GUARDED_BY(g_log_mutex_) =
nullptr; nullptr;
ABSL_CONST_INIT std::atomic<bool> LogMessage::streams_empty_ = {true};
// Boolean options default to false (0) // Boolean options default to false (0)
bool LogMessage::thread_, LogMessage::timestamp_; bool LogMessage::thread_, LogMessage::timestamp_;
@ -269,6 +270,7 @@ void LogMessage::AddLogToStream(LogSink* stream, LoggingSeverity min_sev) {
stream->min_severity_ = min_sev; stream->min_severity_ = min_sev;
stream->next_ = streams_; stream->next_ = streams_;
streams_ = stream; streams_ = stream;
streams_empty_.store(false, std::memory_order_relaxed);
UpdateMinLogSeverity(); UpdateMinLogSeverity();
} }
@ -281,6 +283,7 @@ void LogMessage::RemoveLogToStream(LogSink* stream) {
break; break;
} }
} }
streams_empty_.store(streams_ == nullptr, std::memory_order_relaxed);
UpdateMinLogSeverity(); UpdateMinLogSeverity();
} }
@ -438,12 +441,7 @@ void LogMessage::OutputToDebug(const std::string& str,
bool LogMessage::IsNoop(LoggingSeverity severity) { bool LogMessage::IsNoop(LoggingSeverity severity) {
if (severity >= g_dbg_sev || severity >= g_min_sev) if (severity >= g_dbg_sev || severity >= g_min_sev)
return false; return false;
return streams_empty_.load(std::memory_order_relaxed);
// TODO(tommi): We're grabbing this lock for every LogMessage instance that
// is going to be logged. This introduces unnecessary synchronization for
// a feature that's mostly used for testing.
webrtc::MutexLock lock(&g_log_mutex_);
return streams_ == nullptr;
} }
void LogMessage::FinishPrintStream() { void LogMessage::FinishPrintStream() {

View File

@ -46,6 +46,7 @@
#include <errno.h> #include <errno.h>
#include <atomic>
#include <sstream> // no-presubmit-check TODO(webrtc:8982) #include <sstream> // no-presubmit-check TODO(webrtc:8982)
#include <string> #include <string>
#include <utility> #include <utility>
@ -557,6 +558,12 @@ class LogMessage {
// The output streams and their associated severities // The output streams and their associated severities
static LogSink* streams_; static LogSink* streams_;
// Holds true with high probability if |streams_| is empty, false with high
// probability otherwise. Operated on with std::memory_order_relaxed because
// it's ok to loose or log some additional statements near the instant streams
// are added/removed.
static std::atomic<bool> streams_empty_;
// Flags for formatting options // Flags for formatting options
static bool thread_, timestamp_; static bool thread_, timestamp_;