sc_tracing/logging/stderr_writer.rs
1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! This module contains a buffered semi-asynchronous stderr writer.
20//!
21//! Depending on how we were started writing to stderr can take a surprisingly long time.
22//!
23//! If the other side takes their sweet sweet time reading whatever we send them then writing
24//! to stderr might block for a long time, since it is effectively a synchronous operation.
25//! And every time we write to stderr we need to grab a global lock, which affects every thread
26//! which also tries to log something at the same time.
27//!
28//! Of course we *will* be ultimately limited by how fast the recipient can ingest our logs,
29//! but it's not like logging is the only thing we're doing. And we still can't entirely
30//! avoid the problem of multiple threads contending for the same lock. (Well, technically
31//! we could employ something like a lock-free circular buffer, but that might be like
32//! killing a fly with a sledgehammer considering the complexity involved; this is only
33//! a logger after all.)
34//!
35//! But we can try to make things a little better. We can offload actually writing to stderr
36//! to another thread and flush the logs in bulk instead of doing it per-line, which should
37//! reduce the amount of CPU time we waste on making syscalls and on spinning waiting for locks.
38//!
39//! How much this helps depends on a multitude of factors, including the hardware we're running on,
40//! how much we're logging, from how many threads, which exact set of threads are logging, to what
41//! stderr is actually connected to (is it a terminal emulator? a file? an UDP socket?), etc.
42//!
43//! In general this can reduce the real time execution time as much as 75% in certain cases, or it
44//! can make absolutely no difference in others.
45
46use parking_lot::{Condvar, Mutex, Once};
47use std::{
48 io::Write,
49 sync::atomic::{AtomicBool, Ordering},
50 time::Duration,
51};
52use tracing::{Level, Metadata};
53
54/// How many bytes of buffered logs will trigger an async flush on another thread?
55const ASYNC_FLUSH_THRESHOLD: usize = 16 * 1024;
56
57/// How many bytes of buffered logs will trigger a sync flush on the current thread?
58const SYNC_FLUSH_THRESHOLD: usize = 768 * 1024;
59
60/// How many bytes can be buffered at maximum?
61const EMERGENCY_FLUSH_THRESHOLD: usize = 2 * 1024 * 1024;
62
63/// If there isn't enough printed out this is how often the logs will be automatically flushed.
64const AUTOFLUSH_EVERY: Duration = Duration::from_millis(50);
65
66/// The least serious level at which a synchronous flush will be triggered.
67const SYNC_FLUSH_LEVEL_THRESHOLD: Level = Level::ERROR;
68
69/// The amount of time we'll block until the buffer is fully flushed on exit.
70///
71/// This should be completely unnecessary in normal circumstances.
72const ON_EXIT_FLUSH_TIMEOUT: Duration = Duration::from_secs(5);
73
74/// A global buffer to which we'll append all of our logs before flushing them out to stderr.
75static BUFFER: Mutex<Vec<u8>> = parking_lot::const_mutex(Vec::new());
76
77/// A spare buffer which we'll swap with the main buffer on each flush to minimize lock contention.
78static SPARE_BUFFER: Mutex<Vec<u8>> = parking_lot::const_mutex(Vec::new());
79
80/// A conditional variable used to forcefully trigger asynchronous flushes.
81static ASYNC_FLUSH_CONDVAR: Condvar = Condvar::new();
82
83static ENABLE_ASYNC_LOGGING: AtomicBool = AtomicBool::new(true);
84
85fn flush_logs(mut buffer: parking_lot::lock_api::MutexGuard<parking_lot::RawMutex, Vec<u8>>) {
86 let mut spare_buffer = SPARE_BUFFER.lock();
87 std::mem::swap(&mut *spare_buffer, &mut *buffer);
88 std::mem::drop(buffer);
89
90 let stderr = std::io::stderr();
91 let mut stderr_lock = stderr.lock();
92 let _ = stderr_lock.write_all(&spare_buffer);
93 std::mem::drop(stderr_lock);
94
95 spare_buffer.clear();
96}
97
98fn log_autoflush_thread() {
99 let mut buffer = BUFFER.lock();
100 loop {
101 ASYNC_FLUSH_CONDVAR.wait_for(&mut buffer, AUTOFLUSH_EVERY);
102 loop {
103 flush_logs(buffer);
104
105 buffer = BUFFER.lock();
106 if buffer.len() >= ASYNC_FLUSH_THRESHOLD {
107 // While we were busy flushing we picked up enough logs to do another flush.
108 continue
109 } else {
110 break
111 }
112 }
113 }
114}
115
116#[cold]
117fn initialize() {
118 std::thread::Builder::new()
119 .name("log-autoflush".to_owned())
120 .spawn(log_autoflush_thread)
121 .expect("thread spawning doesn't normally fail; qed");
122
123 // SAFETY: This is safe since we pass a valid pointer to `atexit`.
124 let errcode = unsafe { libc::atexit(on_exit) };
125 assert_eq!(errcode, 0, "atexit failed while setting up the logger: {}", errcode);
126}
127
128extern "C" fn on_exit() {
129 ENABLE_ASYNC_LOGGING.store(false, Ordering::SeqCst);
130
131 if let Some(buffer) = BUFFER.try_lock_for(ON_EXIT_FLUSH_TIMEOUT) {
132 flush_logs(buffer);
133 }
134}
135
136/// A drop-in replacement for [`std::io::stderr`] for use anywhere
137/// a [`tracing_subscriber::fmt::MakeWriter`] is accepted.
138pub struct MakeStderrWriter {
139 // A dummy field so that the structure is not publicly constructible.
140 _dummy: (),
141}
142
143impl Default for MakeStderrWriter {
144 fn default() -> Self {
145 static ONCE: Once = Once::new();
146 ONCE.call_once(initialize);
147 MakeStderrWriter { _dummy: () }
148 }
149}
150
151impl tracing_subscriber::fmt::MakeWriter<'_> for MakeStderrWriter {
152 type Writer = StderrWriter;
153
154 fn make_writer(&self) -> Self::Writer {
155 StderrWriter::new(false)
156 }
157
158 // The `tracing-subscriber` crate calls this for every line logged.
159 fn make_writer_for(&self, meta: &Metadata<'_>) -> Self::Writer {
160 StderrWriter::new(*meta.level() <= SYNC_FLUSH_LEVEL_THRESHOLD)
161 }
162}
163
164pub struct StderrWriter {
165 buffer: Option<parking_lot::lock_api::MutexGuard<'static, parking_lot::RawMutex, Vec<u8>>>,
166 sync_flush_on_drop: bool,
167 original_len: usize,
168}
169
170impl StderrWriter {
171 fn new(mut sync_flush_on_drop: bool) -> Self {
172 if !ENABLE_ASYNC_LOGGING.load(Ordering::Relaxed) {
173 sync_flush_on_drop = true;
174 }
175
176 // This lock isn't as expensive as it might look, since this is only called once the full
177 // line to be logged is already serialized into a thread-local buffer inside of the
178 // `tracing-subscriber` crate, and basically the only thing we'll do when holding this lock
179 // is to copy that over to our global shared buffer in one go in `Write::write_all` and be
180 // immediately dropped.
181 let buffer = BUFFER.lock();
182 StderrWriter { original_len: buffer.len(), buffer: Some(buffer), sync_flush_on_drop }
183 }
184}
185
186#[cold]
187fn emergency_flush(buffer: &mut Vec<u8>, input: &[u8]) {
188 let stderr = std::io::stderr();
189 let mut stderr_lock = stderr.lock();
190 let _ = stderr_lock.write_all(buffer);
191 buffer.clear();
192
193 let _ = stderr_lock.write_all(input);
194}
195
196impl Write for StderrWriter {
197 fn write(&mut self, input: &[u8]) -> Result<usize, std::io::Error> {
198 let buffer = self.buffer.as_mut().expect("buffer is only None after `drop`; qed");
199 if buffer.len() + input.len() >= EMERGENCY_FLUSH_THRESHOLD {
200 // Make sure we don't blow our memory budget. Normally this should never happen,
201 // but there are cases where we directly print out untrusted user input which
202 // can potentially be megabytes in size.
203 emergency_flush(buffer, input);
204 } else {
205 buffer.extend_from_slice(input);
206 }
207 Ok(input.len())
208 }
209
210 fn write_all(&mut self, input: &[u8]) -> Result<(), std::io::Error> {
211 self.write(input).map(|_| ())
212 }
213
214 fn flush(&mut self) -> Result<(), std::io::Error> {
215 Ok(())
216 }
217}
218
219impl Drop for StderrWriter {
220 fn drop(&mut self) {
221 let buf = self.buffer.take().expect("buffer is only None after `drop`; qed");
222 if self.sync_flush_on_drop || buf.len() >= SYNC_FLUSH_THRESHOLD {
223 flush_logs(buf);
224 } else if self.original_len < ASYNC_FLUSH_THRESHOLD && buf.len() >= ASYNC_FLUSH_THRESHOLD {
225 ASYNC_FLUSH_CONDVAR.notify_one();
226 }
227 }
228}