9 #include "quill/core/Attributes.h" 10 #include "quill/core/BoundedSPSCQueue.h" 11 #include "quill/core/Common.h" 12 #include "quill/core/QuillError.h" 23 #if defined(_WIN32) && defined(_MSC_VER) && !defined(__GNUC__) 25 #pragma warning(disable : 4324) 55 explicit Node(
size_t bounded_queue_capacity, HugePagesPolicy huge_pages_policy)
56 : bounded_queue(bounded_queue_capacity, huge_pages_policy)
61 std::atomic<Node*> next{
nullptr};
68 explicit ReadResult(std::byte* read_position) : read_pos(read_position) {}
71 size_t previous_capacity{0};
72 size_t new_capacity{0};
73 bool allocation{
false};
80 HugePagesPolicy huge_pages_policy = quill::HugePagesPolicy::Never)
81 : _max_capacity(max_capacity),
82 _producer(new Node(initial_bounded_queue_capacity, huge_pages_policy)),
99 Node
const* current_node = _consumer;
102 while (current_node !=
nullptr)
104 auto const to_delete = current_node;
105 current_node = current_node->next;
118 std::byte* write_pos = _producer->bounded_queue.prepare_write(nbytes);
120 if (QUILL_LIKELY(write_pos !=
nullptr))
125 return _handle_full_queue(nbytes);
134 _producer->bounded_queue.finish_write(nbytes);
140 QUILL_ATTRIBUTE_HOT
void commit_write() noexcept { _producer->bounded_queue.commit_write(); }
158 return _producer->bounded_queue.capacity();
168 if (capacity > (_producer->bounded_queue.capacity() >> 1))
176 auto const next_node =
new Node{
capacity, _producer->bounded_queue.huge_pages_policy()};
179 _producer->next.store(next_node, std::memory_order_release);
182 _producer = next_node;
192 ReadResult read_result{_consumer->bounded_queue.prepare_read()};
194 if (read_result.read_pos !=
nullptr)
200 Node*
const next_node = _consumer->next.load(std::memory_order_acquire);
204 return _read_next_queue(next_node);
217 _consumer->bounded_queue.finish_read(nbytes);
223 QUILL_ATTRIBUTE_HOT
void commit_read() noexcept { _consumer->bounded_queue.commit_read(); }
230 QUILL_NODISCARD
size_t capacity() const noexcept {
return _consumer->bounded_queue.capacity(); }
237 QUILL_NODISCARD QUILL_ATTRIBUTE_HOT
bool empty() const noexcept
239 return _consumer->bounded_queue.empty() && (_consumer->next.load(std::memory_order_relaxed) ==
nullptr);
244 QUILL_NODISCARD std::byte* _handle_full_queue(
size_t nbytes)
247 size_t capacity = _producer->bounded_queue.capacity() * 2ull;
248 while (capacity < nbytes)
250 capacity = capacity * 2ull;
253 if (QUILL_UNLIKELY(capacity > _max_capacity))
255 if (nbytes > _max_capacity)
258 QuillError{
"Logging single messages larger than the configured maximum queue capacity " 260 "To log single messages exceeding this limit, consider increasing " 261 "FrontendOptions::unbounded_queue_max_capacity.\n" 263 std::to_string(nbytes) +
265 "Required queue capacity: " +
266 std::to_string(capacity) +
268 "Configured maximum queue capacity: " +
269 std::to_string(_max_capacity) +
" bytes"});
278 _producer->bounded_queue.commit_write();
281 auto const next_node =
new Node{
capacity, _producer->bounded_queue.huge_pages_policy()};
284 _producer->next.store(next_node, std::memory_order_release);
287 _producer = next_node;
290 std::byte*
const write_pos = _producer->bounded_queue.prepare_write(nbytes);
294 "write_pos is nullptr after allocating new node in UnboundedSPSCQueue::prepare_write()");
300 QUILL_NODISCARD
ReadResult _read_next_queue(Node* next_node)
305 ReadResult read_result{_consumer->bounded_queue.prepare_read()};
307 if (read_result.read_pos)
314 _consumer->bounded_queue.commit_read();
317 auto const previous_capacity = _consumer->bounded_queue.capacity();
320 _consumer = next_node;
321 read_result.read_pos = _consumer->bounded_queue.prepare_read();
324 read_result.allocation =
true;
325 read_result.new_capacity = _consumer->bounded_queue.capacity();
326 read_result.previous_capacity = previous_capacity;
332 size_t _max_capacity;
335 alignas(QUILL_CACHE_LINE_ALIGNED) Node* _producer{
nullptr};
336 alignas(QUILL_CACHE_LINE_ALIGNED) Node* _consumer{
nullptr};
339 #if defined(_WIN32) && defined(_MSC_VER) && !defined(__GNUC__)
A single-producer single-consumer FIFO circular buffer.
Definition: UnboundedSPSCQueue.h:42
~UnboundedSPSCQueue()
Destructor.
Definition: UnboundedSPSCQueue.h:96
Definition: UnboundedSPSCQueue.h:66
QUILL_NODISCARD QUILL_ATTRIBUTE_HOT std::byte * prepare_write(size_t nbytes)
Reserve contiguous space for the producer without making it visible to the consumer.
Definition: UnboundedSPSCQueue.h:115
QUILL_NODISCARD size_t capacity() const noexcept
Return the current buffer's capacity.
Definition: UnboundedSPSCQueue.h:230
UnboundedSPSCQueue(size_t initial_bounded_queue_capacity, size_t max_capacity, HugePagesPolicy huge_pages_policy=quill::HugePagesPolicy::Never)
Constructor.
Definition: UnboundedSPSCQueue.h:79
QUILL_ATTRIBUTE_HOT void finish_write(size_t nbytes) noexcept
Complement to reserve producer space that makes nbytes starting from the return of reserve producer s...
Definition: UnboundedSPSCQueue.h:132
QUILL_NODISCARD QUILL_ATTRIBUTE_HOT bool empty() const noexcept
checks if the queue is empty
Definition: UnboundedSPSCQueue.h:237
Setups a signal handler to handle fatal signals.
Definition: BackendManager.h:24
QUILL_ATTRIBUTE_HOT void finish_read(size_t nbytes) noexcept
Consumes the next nbytes in the buffer and frees it back for the producer to reuse.
Definition: UnboundedSPSCQueue.h:215
QUILL_NODISCARD size_t producer_capacity() const noexcept
Return the current buffer's capacity.
Definition: UnboundedSPSCQueue.h:156
QUILL_ATTRIBUTE_HOT void commit_write() noexcept
Commit write to notify the consumer bytes are ready to read.
Definition: UnboundedSPSCQueue.h:140
custom exception
Definition: QuillError.h:45
QUILL_NODISCARD QUILL_ATTRIBUTE_HOT ReadResult prepare_read()
Prepare to read from the buffer a callback used for notifications to the user.
Definition: UnboundedSPSCQueue.h:190
QUILL_ATTRIBUTE_HOT void commit_read() noexcept
Commit the read to indicate that the bytes are read and are now free to be reused.
Definition: UnboundedSPSCQueue.h:223
void shrink(size_t capacity)
Shrinks the queue if capacity is a valid smaller power of 2.
Definition: UnboundedSPSCQueue.h:166
QUILL_ATTRIBUTE_HOT void finish_and_commit_write(size_t nbytes) noexcept
Finish and commit write as a single function.
Definition: UnboundedSPSCQueue.h:145