From 863f704f479038a1b7f47a5cbb2fe32bd44b922d Mon Sep 17 00:00:00 2001 From: Luiz Siqueira Date: Tue, 14 Aug 2018 08:51:20 -0300 Subject: [PATCH 1/3] increment counter every time we overrid a message in async mode. --- include/spdlog/details/circular_q.h | 8 ++++++++ include/spdlog/details/mpmc_blocking_q.h | 5 +++++ include/spdlog/details/thread_pool.h | 5 +++++ tests/test_async.cpp | 4 ++++ 4 files changed, 22 insertions(+) diff --git a/include/spdlog/details/circular_q.h b/include/spdlog/details/circular_q.h index 6f3433a2..cea12174 100644 --- a/include/spdlog/details/circular_q.h +++ b/include/spdlog/details/circular_q.h @@ -31,6 +31,7 @@ public: if (tail_ == head_) // overrun last item if full { head_ = (head_ + 1) % max_items_; + ++overrun_counter_; } } @@ -53,12 +54,19 @@ public: return ((tail_ + 1) % max_items_) == head_; } + int overrun_counter() const + { + return overrun_counter_; + } + private: size_t max_items_; typename std::vector::size_type head_ = 0; typename std::vector::size_type tail_ = 0; std::vector v_; + + int overrun_counter_ = 0; }; } // namespace details } // namespace spdlog diff --git a/include/spdlog/details/mpmc_blocking_q.h b/include/spdlog/details/mpmc_blocking_q.h index d607da2c..3aa9fa1b 100644 --- a/include/spdlog/details/mpmc_blocking_q.h +++ b/include/spdlog/details/mpmc_blocking_q.h @@ -30,6 +30,11 @@ public: { } + int overrun_counter() const + { + return q_.overrun_counter(); + } + #ifndef __MINGW32__ // try to enqueue and block if no room left void enqueue(T &&item) diff --git a/include/spdlog/details/thread_pool.h b/include/spdlog/details/thread_pool.h index b45b6d2b..fd47eed2 100644 --- a/include/spdlog/details/thread_pool.h +++ b/include/spdlog/details/thread_pool.h @@ -157,6 +157,11 @@ public: post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy); } + int overrun_counter() const + { + return q_.overrun_counter(); + } + private: q_type q_; diff --git a/tests/test_async.cpp b/tests/test_async.cpp index c4324df4..91be8bd9 100644 --- a/tests/test_async.cpp +++ b/tests/test_async.cpp @@ -7,6 +7,7 @@ TEST_CASE("basic async test ", "[async]") { using namespace spdlog; auto test_sink = std::make_shared(); + int overrun_counter = 0; size_t queue_size = 128; size_t messages = 256; { @@ -17,9 +18,11 @@ TEST_CASE("basic async test ", "[async]") logger->info("Hello message #{}", i); } logger->flush(); + overrun_counter = tp->overrun_counter(); } REQUIRE(test_sink->msg_counter() == messages); REQUIRE(test_sink->flush_counter() == 1); + REQUIRE(overrun_counter == 0); } TEST_CASE("discard policy ", "[async]") @@ -37,6 +40,7 @@ TEST_CASE("discard policy ", "[async]") logger->info("Hello message"); } REQUIRE(test_sink->msg_counter() < messages); + REQUIRE(tp->overrun_counter() > 0); } TEST_CASE("discard policy using factory ", "[async]") From c543985cf4a060c4c038b303c3c8bcc6db13657c Mon Sep 17 00:00:00 2001 From: Luiz Siqueira Date: Tue, 14 Aug 2018 09:21:52 -0300 Subject: [PATCH 2/3] use size_t instead of int for overrun counter --- include/spdlog/details/circular_q.h | 4 ++-- include/spdlog/details/mpmc_blocking_q.h | 2 +- include/spdlog/details/thread_pool.h | 2 +- tests/test_async.cpp | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/include/spdlog/details/circular_q.h b/include/spdlog/details/circular_q.h index cea12174..b78af247 100644 --- a/include/spdlog/details/circular_q.h +++ b/include/spdlog/details/circular_q.h @@ -54,7 +54,7 @@ public: return ((tail_ + 1) % max_items_) == head_; } - int overrun_counter() const + size_t overrun_counter() const { return overrun_counter_; } @@ -66,7 +66,7 @@ private: std::vector v_; - int overrun_counter_ = 0; + size_t overrun_counter_ = 0; }; } // namespace details } // namespace spdlog diff --git a/include/spdlog/details/mpmc_blocking_q.h b/include/spdlog/details/mpmc_blocking_q.h index 3aa9fa1b..9a922e04 100644 --- a/include/spdlog/details/mpmc_blocking_q.h +++ b/include/spdlog/details/mpmc_blocking_q.h @@ -30,7 +30,7 @@ public: { } - int overrun_counter() const + size_t overrun_counter() const { return q_.overrun_counter(); } diff --git a/include/spdlog/details/thread_pool.h b/include/spdlog/details/thread_pool.h index fd47eed2..aab089f4 100644 --- a/include/spdlog/details/thread_pool.h +++ b/include/spdlog/details/thread_pool.h @@ -157,7 +157,7 @@ public: post_async_msg_(async_msg(std::move(worker_ptr), async_msg_type::flush), overflow_policy); } - int overrun_counter() const + size_t overrun_counter() const { return q_.overrun_counter(); } diff --git a/tests/test_async.cpp b/tests/test_async.cpp index 91be8bd9..6f86cf6c 100644 --- a/tests/test_async.cpp +++ b/tests/test_async.cpp @@ -7,7 +7,7 @@ TEST_CASE("basic async test ", "[async]") { using namespace spdlog; auto test_sink = std::make_shared(); - int overrun_counter = 0; + size_t overrun_counter = 0; size_t queue_size = 128; size_t messages = 256; { From 4eb80dd8d273d4785c780cf10e1bad9433ab8a35 Mon Sep 17 00:00:00 2001 From: Luiz Siqueira Date: Tue, 14 Aug 2018 10:11:03 -0300 Subject: [PATCH 3/3] acquire lock before reading overrun_counter --- include/spdlog/details/mpmc_blocking_q.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/spdlog/details/mpmc_blocking_q.h b/include/spdlog/details/mpmc_blocking_q.h index 9a922e04..cf34d225 100644 --- a/include/spdlog/details/mpmc_blocking_q.h +++ b/include/spdlog/details/mpmc_blocking_q.h @@ -32,6 +32,7 @@ public: size_t overrun_counter() const { + std::unique_lock lock(queue_mutex_); return q_.overrun_counter(); }