Skip to content

Commit afb9e39

Browse files
Crypt-iQdergoeggestickies-v
committed
log: introduce LogRateLimiter, LogLimitStats, Status
LogRateLimiter will be used to keep track of source locations and our current time-based logging window. It contains an unordered_map and a m_suppressions_active bool to track source locations. The map is keyed by std::source_location, so a custom Hash function (SourceLocationHasher) and custom KeyEqual function (SourceLocationEqual) is provided. SourceLocationHasher uses CSipHasher(0,0) under the hood to get a uniform distribution. A public Reset method is provided so that a scheduler (e.g. the "b-scheduler" thread) can periodically reset LogRateLimiter's state when the time window has elapsed. The LogRateLimiter::Consume method checks if we have enough available bytes in our rate limiting budget to log an additional string. It returns a Status enum that denotes the rate limiting status and can be used by the caller to emit a warning, skip logging, etc. The Status enum has three states: - UNSUPPRESSED (logging was successful) - NEWLY_SUPPRESSED (logging was succcesful, next log will be suppressed) - STILL_SUPPRESSED (logging was unsuccessful) LogLimitStats counts the available bytes left for logging per source location for the current logging window. It does not track actual source locations; it is used as a value in m_source_locations. Also exposes a SuppressionsActive() method so the logger can use that in a later commit to prefix [*] to logs whenenever suppressions are active. Co-Authored-By: Niklas Gogge <[email protected]> Co-Authored-By: stickies-v <[email protected]>
1 parent df7972a commit afb9e39

File tree

3 files changed

+227
-0
lines changed

3 files changed

+227
-0
lines changed

src/logging.cpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,30 @@ static size_t MemUsage(const BCLog::Logger::BufferedLog& buflog)
367367
return buflog.str.size() + buflog.logging_function.size() + buflog.source_file.size() + buflog.threadname.size() + memusage::MallocUsage(sizeof(memusage::list_node<BCLog::Logger::BufferedLog>));
368368
}
369369

370+
BCLog::LogRateLimiter::LogRateLimiter(
371+
SchedulerFunction scheduler_func,
372+
uint64_t max_bytes,
373+
std::chrono::seconds reset_window) : m_max_bytes{max_bytes}, m_reset_window{reset_window}
374+
{
375+
scheduler_func([this] { Reset(); }, reset_window);
376+
}
377+
378+
BCLog::LogRateLimiter::Status BCLog::LogRateLimiter::Consume(
379+
const std::source_location& source_loc,
380+
const std::string& str)
381+
{
382+
StdLockGuard scoped_lock(m_mutex);
383+
auto& counter{m_source_locations.try_emplace(source_loc, m_max_bytes).first->second};
384+
Status status{counter.GetDroppedBytes() > 0 ? Status::STILL_SUPPRESSED : Status::UNSUPPRESSED};
385+
386+
if (!counter.Consume(str.size()) && status == Status::UNSUPPRESSED) {
387+
status = Status::NEWLY_SUPPRESSED;
388+
m_suppression_active = true;
389+
}
390+
391+
return status;
392+
}
393+
370394
void BCLog::Logger::FormatLogStrInPlace(std::string& str, BCLog::LogFlags category, BCLog::Level level, std::string_view source_file, int source_line, std::string_view logging_function, std::string_view threadname, SystemClock::time_point now, std::chrono::seconds mocktime) const
371395
{
372396
if (!str.ends_with('\n')) str.push_back('\n');
@@ -492,6 +516,37 @@ void BCLog::Logger::ShrinkDebugFile()
492516
fclose(file);
493517
}
494518

519+
void BCLog::LogRateLimiter::Reset()
520+
{
521+
decltype(m_source_locations) source_locations;
522+
{
523+
StdLockGuard scoped_lock(m_mutex);
524+
source_locations.swap(m_source_locations);
525+
m_suppression_active = false;
526+
}
527+
for (const auto& [source_loc, counter] : source_locations) {
528+
uint64_t dropped_bytes{counter.GetDroppedBytes()};
529+
if (dropped_bytes == 0) continue;
530+
LogPrintLevel_(
531+
LogFlags::ALL, Level::Info,
532+
"Restarting logging from %s:%d (%s): %d bytes were dropped during the last %ss.\n",
533+
source_loc.file_name(), source_loc.line(), source_loc.function_name(),
534+
dropped_bytes, Ticks<std::chrono::seconds>(m_reset_window));
535+
}
536+
}
537+
538+
bool BCLog::LogLimitStats::Consume(uint64_t bytes)
539+
{
540+
if (bytes > m_available_bytes) {
541+
m_dropped_bytes += bytes;
542+
m_available_bytes = 0;
543+
return false;
544+
}
545+
546+
m_available_bytes -= bytes;
547+
return true;
548+
}
549+
495550
bool BCLog::Logger::SetLogLevel(std::string_view level_str)
496551
{
497552
const auto level = GetLogLevel(level_str);

src/logging.h

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#ifndef BITCOIN_LOGGING_H
77
#define BITCOIN_LOGGING_H
88

9+
#include <crypto/siphash.h>
910
#include <threadsafety.h>
1011
#include <tinyformat.h>
1112
#include <util/fs.h>
@@ -14,11 +15,14 @@
1415

1516
#include <atomic>
1617
#include <cstdint>
18+
#include <cstring>
1719
#include <functional>
1820
#include <list>
1921
#include <mutex>
22+
#include <source_location>
2023
#include <string>
2124
#include <unordered_map>
25+
#include <unordered_set>
2226
#include <vector>
2327

2428
static const bool DEFAULT_LOGTIMEMICROS = false;
@@ -31,6 +35,24 @@ extern const char * const DEFAULT_DEBUGLOGFILE;
3135

3236
extern bool fLogIPs;
3337

38+
struct SourceLocationEqual {
39+
bool operator()(const std::source_location& lhs, const std::source_location& rhs) const noexcept
40+
{
41+
return lhs.line() == rhs.line() && std::string_view(lhs.file_name()) == std::string_view(rhs.file_name());
42+
}
43+
};
44+
45+
struct SourceLocationHasher {
46+
size_t operator()(const std::source_location& s) const noexcept
47+
{
48+
// Use CSipHasher(0, 0) as a simple way to get uniform distribution.
49+
return static_cast<size_t>(CSipHasher(0, 0)
50+
.Write(std::hash<std::string_view>{}(s.file_name()))
51+
.Write(s.line())
52+
.Finalize());
53+
}
54+
};
55+
3456
struct LogCategory {
3557
std::string category;
3658
bool active;
@@ -82,6 +104,79 @@ namespace BCLog {
82104
};
83105
constexpr auto DEFAULT_LOG_LEVEL{Level::Debug};
84106
constexpr size_t DEFAULT_MAX_LOG_BUFFER{1'000'000}; // buffer up to 1MB of log data prior to StartLogging
107+
constexpr uint64_t RATELIMIT_MAX_BYTES{1024 * 1024}; // maximum number of bytes that can be logged within one window
108+
109+
//! Keeps track of an individual source location and how many available bytes are left for logging from it.
110+
class LogLimitStats
111+
{
112+
private:
113+
//! Remaining bytes in the current window interval.
114+
uint64_t m_available_bytes;
115+
//! Number of bytes that were not consumed within the current window.
116+
uint64_t m_dropped_bytes{0};
117+
118+
public:
119+
LogLimitStats(uint64_t max_bytes) : m_available_bytes{max_bytes} {}
120+
//! Consume bytes from the window if enough bytes are available.
121+
//!
122+
//! Returns whether enough bytes were available.
123+
bool Consume(uint64_t bytes);
124+
125+
uint64_t GetAvailableBytes() const
126+
{
127+
return m_available_bytes;
128+
}
129+
130+
uint64_t GetDroppedBytes() const
131+
{
132+
return m_dropped_bytes;
133+
}
134+
};
135+
136+
/**
137+
* Fixed window rate limiter for logging.
138+
*/
139+
class LogRateLimiter
140+
{
141+
private:
142+
mutable StdMutex m_mutex;
143+
144+
//! Counters for each source location that has attempted to log something.
145+
std::unordered_map<std::source_location, LogLimitStats, SourceLocationHasher, SourceLocationEqual> m_source_locations GUARDED_BY(m_mutex);
146+
//! True if at least one log location is suppressed. Cached view on m_source_locations for performance reasons.
147+
std::atomic<bool> m_suppression_active{false};
148+
149+
public:
150+
using SchedulerFunction = std::function<void(std::function<void()>, std::chrono::milliseconds)>;
151+
/**
152+
* @param scheduler_func Callable object used to schedule resetting the window. The first
153+
* parameter is the function to be executed, and the second is the
154+
* reset_window interval.
155+
* @param max_bytes Maximum number of bytes that can be logged for each source
156+
* location.
157+
* @param reset_window Time window after which the byte counters are reset.
158+
*/
159+
LogRateLimiter(SchedulerFunction scheduler_func, uint64_t max_bytes, std::chrono::seconds reset_window);
160+
//! Maximum number of bytes logged per location per window.
161+
const uint64_t m_max_bytes;
162+
//! Interval after which the window is reset.
163+
const std::chrono::seconds m_reset_window;
164+
//! Suppression status of a source log location.
165+
enum class Status {
166+
UNSUPPRESSED, // string fits within the limit
167+
NEWLY_SUPPRESSED, // suppression has started since this string
168+
STILL_SUPPRESSED, // suppression is still ongoing
169+
};
170+
//! Consumes `source_loc`'s available bytes corresponding to the size of the (formatted)
171+
//! `str` and returns its status.
172+
[[nodiscard]] Status Consume(
173+
const std::source_location& source_loc,
174+
const std::string& str) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
175+
//! Resets all usage to zero. Called periodically by the scheduler.
176+
void Reset() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
177+
//! Returns true if any log locations are currently being suppressed.
178+
bool SuppressionsActive() const { return m_suppression_active; }
179+
};
85180

86181
class Logger
87182
{

src/test/logging_tests.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
#include <init/common.h>
66
#include <logging.h>
77
#include <logging/timer.h>
8+
#include <scheduler.h>
89
#include <test/util/setup_common.h>
910
#include <util/string.h>
1011

1112
#include <chrono>
1213
#include <fstream>
14+
#include <future>
1315
#include <iostream>
1416
#include <unordered_map>
1517
#include <utility>
@@ -276,4 +278,79 @@ BOOST_FIXTURE_TEST_CASE(logging_Conf, LogSetup)
276278
}
277279
}
278280

281+
void MockForwardAndSync(CScheduler& scheduler, std::chrono::seconds duration)
282+
{
283+
scheduler.MockForward(duration);
284+
std::promise<void> promise;
285+
scheduler.scheduleFromNow([&promise] { promise.set_value(); }, 0ms);
286+
promise.get_future().wait();
287+
}
288+
289+
BOOST_AUTO_TEST_CASE(logging_log_rate_limiter)
290+
{
291+
CScheduler scheduler{};
292+
scheduler.m_service_thread = std::thread([&scheduler] { scheduler.serviceQueue(); });
293+
uint64_t max_bytes{1024};
294+
auto reset_window{1min};
295+
auto sched_func = [&scheduler](auto func, auto window) { scheduler.scheduleEvery(std::move(func), window); };
296+
BCLog::LogRateLimiter limiter{sched_func, max_bytes, reset_window};
297+
298+
using Status = BCLog::LogRateLimiter::Status;
299+
auto source_loc_1{std::source_location::current()};
300+
auto source_loc_2{std::source_location::current()};
301+
302+
// A fresh limiter should not have any suppressions
303+
BOOST_CHECK(!limiter.SuppressionsActive());
304+
305+
// Resetting an unused limiter is fine
306+
limiter.Reset();
307+
BOOST_CHECK(!limiter.SuppressionsActive());
308+
309+
// No suppression should happen until more than max_bytes have been consumed
310+
BOOST_CHECK_EQUAL(limiter.Consume(source_loc_1, std::string(max_bytes - 1, 'a')), Status::UNSUPPRESSED);
311+
BOOST_CHECK_EQUAL(limiter.Consume(source_loc_1, "a"), Status::UNSUPPRESSED);
312+
BOOST_CHECK(!limiter.SuppressionsActive());
313+
BOOST_CHECK_EQUAL(limiter.Consume(source_loc_1, "a"), Status::NEWLY_SUPPRESSED);
314+
BOOST_CHECK(limiter.SuppressionsActive());
315+
BOOST_CHECK_EQUAL(limiter.Consume(source_loc_1, "a"), Status::STILL_SUPPRESSED);
316+
BOOST_CHECK(limiter.SuppressionsActive());
317+
318+
// Location 2 should not be affected by location 1's suppression
319+
BOOST_CHECK_EQUAL(limiter.Consume(source_loc_2, std::string(max_bytes, 'a')), Status::UNSUPPRESSED);
320+
BOOST_CHECK_EQUAL(limiter.Consume(source_loc_2, "a"), Status::NEWLY_SUPPRESSED);
321+
BOOST_CHECK(limiter.SuppressionsActive());
322+
323+
// After reset_window time has passed, all suppressions should be cleared.
324+
MockForwardAndSync(scheduler, reset_window);
325+
326+
BOOST_CHECK(!limiter.SuppressionsActive());
327+
BOOST_CHECK_EQUAL(limiter.Consume(source_loc_1, std::string(max_bytes, 'a')), Status::UNSUPPRESSED);
328+
BOOST_CHECK_EQUAL(limiter.Consume(source_loc_2, std::string(max_bytes, 'a')), Status::UNSUPPRESSED);
329+
330+
scheduler.stop();
331+
}
332+
333+
BOOST_AUTO_TEST_CASE(logging_log_limit_stats)
334+
{
335+
BCLog::LogLimitStats counter{BCLog::RATELIMIT_MAX_BYTES};
336+
337+
// Check that counter gets initialized correctly.
338+
BOOST_CHECK_EQUAL(counter.GetAvailableBytes(), BCLog::RATELIMIT_MAX_BYTES);
339+
BOOST_CHECK_EQUAL(counter.GetDroppedBytes(), 0ull);
340+
341+
const uint64_t MESSAGE_SIZE{512 * 1024};
342+
BOOST_CHECK(counter.Consume(MESSAGE_SIZE));
343+
BOOST_CHECK_EQUAL(counter.GetAvailableBytes(), BCLog::RATELIMIT_MAX_BYTES - MESSAGE_SIZE);
344+
BOOST_CHECK_EQUAL(counter.GetDroppedBytes(), 0ull);
345+
346+
BOOST_CHECK(counter.Consume(MESSAGE_SIZE));
347+
BOOST_CHECK_EQUAL(counter.GetAvailableBytes(), BCLog::RATELIMIT_MAX_BYTES - MESSAGE_SIZE * 2);
348+
BOOST_CHECK_EQUAL(counter.GetDroppedBytes(), 0ull);
349+
350+
// Consuming more bytes after already having consumed 1MB should fail.
351+
BOOST_CHECK(!counter.Consume(500));
352+
BOOST_CHECK_EQUAL(counter.GetAvailableBytes(), 0ull);
353+
BOOST_CHECK_EQUAL(counter.GetDroppedBytes(), 500ull);
354+
}
355+
279356
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)