Skip to content

Commit b095e37

Browse files
committed
move unbounded_queue_max_capacity to ctor
1 parent bea2105 commit b095e37

10 files changed

+38
-67
lines changed

examples/bounded_dropping_queue_frontend.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ struct CustomFrontendOptions
2727
static constexpr size_t initial_queue_capacity = 256;
2828

2929
static constexpr uint32_t blocking_queue_retry_interval_ns = 800;
30+
static constexpr size_t unbounded_queue_max_capacity = 2ull * 1024u * 1024u * 1024u;
3031
static constexpr quill::HugePagesPolicy huge_pages_policy = quill::HugePagesPolicy::Never;
3132
};
3233

examples/custom_frontend_options.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ struct CustomFrontendOptions
1616
static constexpr quill::QueueType queue_type = quill::QueueType::BoundedDropping;
1717
static constexpr size_t initial_queue_capacity = 131'072;
1818
static constexpr uint32_t blocking_queue_retry_interval_ns = 800;
19+
static constexpr size_t unbounded_queue_max_capacity = 2ull * 1024u * 1024u * 1024u;
1920
static constexpr quill::HugePagesPolicy huge_pages_policy = quill::HugePagesPolicy::Never;
2021
};
2122

include/quill/Logger.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,7 @@ class LoggerImpl : public detail::LoggerBase
362362
if constexpr (using_unbounded_queue)
363363
{
364364
// MSVC doesn't like the template keyword, but every other compiler requires it
365-
#if defined(_MSC_VER)
366-
return thread_context->get_spsc_queue<frontend_options_t::queue_type>().prepare_write(
367-
total_size, frontend_options_t::unbounded_queue_max_capacity);
368-
#else
369-
return thread_context->get_spsc_queue<frontend_options_t::queue_type>()
370-
.template prepare_write<frontend_options_t::unbounded_queue_max_capacity>(total_size);
371-
#endif
365+
return thread_context->get_spsc_queue<frontend_options_t::queue_type>().prepare_write(total_size);
372366
}
373367
else
374368
{

include/quill/core/ThreadContextManager.h

+9-5
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,14 @@ class ThreadContext
5959

6060
public:
6161
/***/
62-
ThreadContext(QueueType queue_type, size_t initial_queue_capacity, HugePagesPolicy huge_pages_policy)
62+
ThreadContext(QueueType queue_type, size_t initial_queue_capacity,
63+
QUILL_MAYBE_UNUSED size_t unbounded_queue_max_capacity, HugePagesPolicy huge_pages_policy)
6364
: _queue_type(queue_type)
6465
{
6566
if (has_unbounded_queue_type())
6667
{
6768
new (&_spsc_queue_union.unbounded_spsc_queue)
68-
UnboundedSPSCQueue{initial_queue_capacity, huge_pages_policy};
69+
UnboundedSPSCQueue{initial_queue_capacity, unbounded_queue_max_capacity, huge_pages_policy};
6970
}
7071
else if (has_bounded_queue_type())
7172
{
@@ -330,8 +331,10 @@ class ScopedThreadContext
330331
{
331332
public:
332333
/***/
333-
ScopedThreadContext(QueueType queue_type, uint32_t spsc_queue_capacity, HugePagesPolicy huge_pages_policy)
334-
: _thread_context(std::make_shared<ThreadContext>(queue_type, spsc_queue_capacity, huge_pages_policy))
334+
ScopedThreadContext(QueueType queue_type, size_t initial_queue_capacity,
335+
size_t unbounded_queue_max_capacity, HugePagesPolicy huge_pages_policy)
336+
: _thread_context(std::make_shared<ThreadContext>(
337+
queue_type, initial_queue_capacity, unbounded_queue_max_capacity, huge_pages_policy))
335338
{
336339
#ifndef NDEBUG
337340
// Thread-local flag to track if an instance has been created for this thread.
@@ -389,7 +392,8 @@ template <typename TFrontendOptions>
389392
QUILL_NODISCARD QUILL_ATTRIBUTE_HOT ThreadContext* get_local_thread_context() noexcept
390393
{
391394
thread_local ScopedThreadContext scoped_thread_context{
392-
TFrontendOptions::queue_type, TFrontendOptions::initial_queue_capacity, TFrontendOptions::huge_pages_policy};
395+
TFrontendOptions::queue_type, TFrontendOptions::initial_queue_capacity,
396+
TFrontendOptions::unbounded_queue_max_capacity, TFrontendOptions::huge_pages_policy};
393397

394398
return scoped_thread_context.get_thread_context();
395399
}

include/quill/core/UnboundedSPSCQueue.h

+11-22
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,11 @@ class UnboundedSPSCQueue
7171
/**
7272
* Constructor
7373
*/
74-
explicit UnboundedSPSCQueue(size_t initial_bounded_queue_capacity,
75-
HugePagesPolicy huge_pages_policy = quill::HugePagesPolicy::Never)
76-
: _producer(new Node(initial_bounded_queue_capacity, huge_pages_policy)), _consumer(_producer)
74+
UnboundedSPSCQueue(size_t initial_bounded_queue_capacity, size_t max_capacity,
75+
HugePagesPolicy huge_pages_policy = quill::HugePagesPolicy::Never)
76+
: _max_capacity(max_capacity),
77+
_producer(new Node(initial_bounded_queue_capacity, huge_pages_policy)),
78+
_consumer(_producer)
7779
{
7880
}
7981

@@ -105,13 +107,7 @@ class UnboundedSPSCQueue
105107
* making it visible to the consumer.
106108
* @return a valid point to the buffer
107109
*/
108-
#if defined(_MSC_VER)
109-
// MSVC doesn't like this as template <QueueType queue_type> when called from Logger, while it compiles on MSVC there will be false positives from clang-tidy
110-
QUILL_NODISCARD QUILL_ATTRIBUTE_HOT std::byte* prepare_write(size_t nbytes, size_t unbounded_queue_max_capacity)
111-
#else
112-
template <size_t unbounded_queue_max_capacity>
113110
QUILL_NODISCARD QUILL_ATTRIBUTE_HOT std::byte* prepare_write(size_t nbytes)
114-
#endif
115111
{
116112
// Try to reserve the bounded queue
117113
std::byte* write_pos = _producer->bounded_queue.prepare_write(nbytes);
@@ -121,11 +117,7 @@ class UnboundedSPSCQueue
121117
return write_pos;
122118
}
123119

124-
#if defined(_MSC_VER)
125-
return _handle_full_queue(nbytes, unbounded_queue_max_capacity);
126-
#else
127-
return _handle_full_queue<unbounded_queue_max_capacity>(nbytes);
128-
#endif
120+
return _handle_full_queue(nbytes);
129121
}
130122

131123
/**
@@ -244,12 +236,7 @@ class UnboundedSPSCQueue
244236

245237
private:
246238
/***/
247-
#if defined(_MSC_VER)
248-
QUILL_NODISCARD std::byte* _handle_full_queue(size_t nbytes, size_t unbounded_queue_max_capacity)
249-
#else
250-
template <size_t unbounded_queue_max_capacity>
251239
QUILL_NODISCARD std::byte* _handle_full_queue(size_t nbytes)
252-
#endif
253240
{
254241
// Then it means the queue doesn't have enough size
255242
size_t capacity = _producer->bounded_queue.capacity() * 2ull;
@@ -258,9 +245,9 @@ class UnboundedSPSCQueue
258245
capacity = capacity * 2ull;
259246
}
260247

261-
if (QUILL_UNLIKELY(capacity > unbounded_queue_max_capacity))
248+
if (QUILL_UNLIKELY(capacity > _max_capacity))
262249
{
263-
if (nbytes > unbounded_queue_max_capacity)
250+
if (nbytes > _max_capacity)
264251
{
265252
QUILL_THROW(
266253
QuillError{"Logging single messages larger than the configured maximum queue capacity "
@@ -274,7 +261,7 @@ class UnboundedSPSCQueue
274261
std::to_string(capacity) +
275262
" bytes\n"
276263
"Configured maximum queue capacity: " +
277-
std::to_string(unbounded_queue_max_capacity) + " bytes"});
264+
std::to_string(_max_capacity) + " bytes"});
278265
}
279266

280267
// we reached the unbounded_queue_max_capacity we won't be allocating more
@@ -335,6 +322,8 @@ class UnboundedSPSCQueue
335322
}
336323

337324
private:
325+
size_t _max_capacity;
326+
338327
/** Modified by either the producer or consumer but never both */
339328
alignas(QUILL_CACHE_LINE_ALIGNED) Node* _producer{nullptr};
340329
alignas(QUILL_CACHE_LINE_ALIGNED) Node* _consumer{nullptr};

test/integration_tests/BoundedBlockingQueueTest.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct CustomFrontendOptions
1717
static constexpr quill::QueueType queue_type = quill::QueueType::BoundedBlocking;
1818
static constexpr size_t initial_queue_capacity = 131'072;
1919
static constexpr uint32_t blocking_queue_retry_interval_ns = 800;
20+
static constexpr size_t unbounded_queue_max_capacity = 2ull * 1024u * 1024u * 1024u;
2021
static constexpr quill::HugePagesPolicy huge_pages_policy = quill::HugePagesPolicy::Never;
2122
};
2223

test/integration_tests/BoundedDroppingQueueDropMessagesTest.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct CustomFrontendOptions
1717
static constexpr quill::QueueType queue_type = quill::QueueType::BoundedDropping;
1818
static constexpr size_t initial_queue_capacity = 1024;
1919
static constexpr uint32_t blocking_queue_retry_interval_ns = 800;
20+
static constexpr size_t unbounded_queue_max_capacity = 2ull * 1024u * 1024u * 1024u;
2021
static constexpr quill::HugePagesPolicy huge_pages_policy = quill::HugePagesPolicy::Never;
2122
};
2223

test/integration_tests/BoundedDroppingQueueTest.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct CustomFrontendOptions
1717
static constexpr quill::QueueType queue_type = quill::QueueType::BoundedDropping;
1818
static constexpr size_t initial_queue_capacity = 131'072;
1919
static constexpr uint32_t blocking_queue_retry_interval_ns = 800;
20+
static constexpr size_t unbounded_queue_max_capacity = 2ull * 1024u * 1024u * 1024u;
2021
static constexpr quill::HugePagesPolicy huge_pages_policy = quill::HugePagesPolicy::Never;
2122
};
2223

test/integration_tests/CsvWritingCustomFrontendTest.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ struct CustomFrontendOptions
1919
static constexpr quill::QueueType queue_type = quill::QueueType::BoundedBlocking;
2020
static constexpr size_t initial_queue_capacity = 131'072;
2121
static constexpr uint32_t blocking_queue_retry_interval_ns = 800;
22+
static constexpr size_t unbounded_queue_max_capacity = 2ull * 1024u * 1024u * 1024u;
2223
static constexpr quill::HugePagesPolicy huge_pages_policy = quill::HugePagesPolicy::Never;
2324
};
2425

test/unit_tests/UnboundedQueueTest.cpp

+11-33
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,13 @@ TEST_CASE("unbounded_queue_shrink")
1515
constexpr size_t CHUNK{256};
1616
constexpr size_t INITIAL_SIZE{1024};
1717

18-
UnboundedSPSCQueue buffer{INITIAL_SIZE};
18+
UnboundedSPSCQueue buffer{INITIAL_SIZE, quill::FrontendOptions::unbounded_queue_max_capacity};
1919
REQUIRE_EQ(buffer.producer_capacity(), INITIAL_SIZE);
2020

2121
// This queue will grow as we request 5 * 256
2222
for (uint32_t i = 0; i < 5; ++i)
2323
{
24-
#if defined(_MSC_VER)
25-
auto* write_buffer = buffer.prepare_write(CHUNK, quill::FrontendOptions::unbounded_queue_max_capacity);
26-
#else
27-
auto* write_buffer = buffer.prepare_write<quill::FrontendOptions::unbounded_queue_max_capacity>(CHUNK);
28-
#endif
24+
auto* write_buffer = buffer.prepare_write(CHUNK);
2925

3026
REQUIRE(write_buffer);
3127
buffer.finish_write(CHUNK);
@@ -69,19 +65,15 @@ TEST_CASE("unbounded_queue_shrink")
6965

7066
TEST_CASE("unbounded_queue_allocation_within_limit")
7167
{
72-
UnboundedSPSCQueue buffer{1024};
68+
UnboundedSPSCQueue buffer{1024, std::numeric_limits<uint64_t>::max()};
7369

7470
static constexpr size_t two_mb = 2u * 1024u * 1024u;
7571

7672
// Attempt to allocate a buffer size that exceeds the default limit,
7773
// ensuring that allocation within configurable bounds does not throw.
7874
auto func = [&buffer]()
7975
{
80-
#if defined(_MSC_VER)
81-
auto* write_buffer_z = buffer.prepare_write(2 * two_mb, std::numeric_limits<uint64_t>::max());
82-
#else
83-
auto* write_buffer_z = buffer.prepare_write<std::numeric_limits<uint64_t>::max()>(2 * two_mb);
84-
#endif
76+
auto* write_buffer_z = buffer.prepare_write(2 * two_mb);
8577
return write_buffer_z;
8678
};
8779

@@ -91,19 +83,15 @@ TEST_CASE("unbounded_queue_allocation_within_limit")
9183

9284
TEST_CASE("unbounded_queue_allocation_exceeds_limit")
9385
{
94-
UnboundedSPSCQueue buffer{1024};
95-
9686
constexpr static uint64_t two_mb = 2u * 1024u * 1024u;
9787

88+
UnboundedSPSCQueue buffer{1024, two_mb};
89+
9890
// Attempt to allocate a buffer size that exceeds the specified capacity,
9991
// which should trigger an exception.
10092
auto func = [&buffer]()
10193
{
102-
#if defined(_MSC_VER)
103-
auto* write_buffer_z = buffer.prepare_write(2 * two_mb, two_mb);
104-
#else
105-
auto* write_buffer_z = buffer.prepare_write<two_mb>(2 * two_mb);
106-
#endif
94+
auto* write_buffer_z = buffer.prepare_write(2 * two_mb);
10795
return write_buffer_z;
10896
};
10997

@@ -113,7 +101,7 @@ TEST_CASE("unbounded_queue_allocation_exceeds_limit")
113101

114102
TEST_CASE("unbounded_queue_read_write_multithreaded_plain_ints")
115103
{
116-
UnboundedSPSCQueue buffer{1024};
104+
UnboundedSPSCQueue buffer{1024, quill::FrontendOptions::unbounded_queue_max_capacity};
117105

118106
std::thread producer_thread(
119107
[&buffer]()
@@ -122,25 +110,15 @@ TEST_CASE("unbounded_queue_read_write_multithreaded_plain_ints")
122110
{
123111
for (uint32_t i = 0; i < 8192; ++i)
124112
{
125-
#if defined(_MSC_VER)
126-
auto* write_buffer =
127-
buffer.prepare_write(sizeof(uint32_t), quill::FrontendOptions::unbounded_queue_max_capacity);
128-
#else
129113
auto* write_buffer =
130-
buffer.prepare_write<quill::FrontendOptions::unbounded_queue_max_capacity>(sizeof(uint32_t));
131-
#endif
114+
buffer.prepare_write(sizeof(uint32_t));
132115

133116
while (!write_buffer)
134117
{
135118
std::this_thread::sleep_for(std::chrono::microseconds{2});
136-
#if defined(_MSC_VER)
137-
write_buffer =
138-
buffer.prepare_write(sizeof(uint32_t), quill::FrontendOptions::unbounded_queue_max_capacity);
139-
#else
119+
140120
write_buffer =
141-
buffer.prepare_write<quill::FrontendOptions::unbounded_queue_max_capacity>(
142-
sizeof(uint32_t));
143-
#endif
121+
buffer.prepare_write(sizeof(uint32_t));
144122
}
145123

146124
std::memcpy(write_buffer, &i, sizeof(uint32_t));

0 commit comments

Comments
 (0)