9 #include "quill/core/Attributes.h" 10 #include "quill/core/BoundedSPSCQueue.h" 11 #include "quill/core/Common.h" 12 #include "quill/core/QuillError.h" 50 explicit Node(
size_t bounded_queue_capacity, HugePagesPolicy huge_pages_policy)
51 : bounded_queue(bounded_queue_capacity, huge_pages_policy)
56 std::atomic<Node*> next{
nullptr};
63 explicit ReadResult(std::byte* read_position) : read_pos(read_position) {}
66 size_t previous_capacity{0};
67 size_t new_capacity{0};
68 bool allocation{
false};
75 HugePagesPolicy huge_pages_policy = quill::HugePagesPolicy::Never)
76 : _max_capacity(max_capacity),
77 _producer(new Node(initial_bounded_queue_capacity, huge_pages_policy)),
94 Node
const* current_node = _consumer;
97 while (current_node !=
nullptr)
99 auto const to_delete = current_node;
100 current_node = current_node->next;
113 std::byte* write_pos = _producer->bounded_queue.prepare_write(nbytes);
115 if (QUILL_LIKELY(write_pos !=
nullptr))
120 return _handle_full_queue(nbytes);
129 _producer->bounded_queue.finish_write(nbytes);
135 QUILL_ATTRIBUTE_HOT
void commit_write() noexcept { _producer->bounded_queue.commit_write(); }
153 return _producer->bounded_queue.capacity();
163 if (capacity > (_producer->bounded_queue.capacity() >> 1))
171 auto const next_node =
new Node{
capacity, _producer->bounded_queue.huge_pages_policy()};
174 _producer->next.store(next_node, std::memory_order_release);
177 _producer = next_node;
187 ReadResult read_result{_consumer->bounded_queue.prepare_read()};
189 if (read_result.read_pos !=
nullptr)
195 Node*
const next_node = _consumer->next.load(std::memory_order_acquire);
199 return _read_next_queue(next_node);
212 _consumer->bounded_queue.finish_read(nbytes);
218 QUILL_ATTRIBUTE_HOT
void commit_read() noexcept { _consumer->bounded_queue.commit_read(); }
225 QUILL_NODISCARD
size_t capacity() const noexcept {
return _consumer->bounded_queue.capacity(); }
232 QUILL_NODISCARD QUILL_ATTRIBUTE_HOT
bool empty() const noexcept
234 return _consumer->bounded_queue.empty() && (_consumer->next.load(std::memory_order_relaxed) ==
nullptr);
239 QUILL_NODISCARD std::byte* _handle_full_queue(
size_t nbytes)
242 size_t capacity = _producer->bounded_queue.capacity() * 2ull;
243 while (capacity < nbytes)
245 capacity = capacity * 2ull;
248 if (QUILL_UNLIKELY(capacity > _max_capacity))
250 if (nbytes > _max_capacity)
253 QuillError{
"Logging single messages larger than the configured maximum queue capacity " 255 "To log single messages exceeding this limit, consider increasing " 256 "FrontendOptions::unbounded_queue_max_capacity.\n" 258 std::to_string(nbytes) +
260 "Required queue capacity: " +
261 std::to_string(capacity) +
263 "Configured maximum queue capacity: " +
264 std::to_string(_max_capacity) +
" bytes"});
273 _producer->bounded_queue.commit_write();
276 auto const next_node =
new Node{
capacity, _producer->bounded_queue.huge_pages_policy()};
279 _producer->next.store(next_node, std::memory_order_release);
282 _producer = next_node;
285 std::byte*
const write_pos = _producer->bounded_queue.prepare_write(nbytes);
287 assert(write_pos &&
"write_pos is nullptr");
293 QUILL_NODISCARD
ReadResult _read_next_queue(Node* next_node)
298 ReadResult read_result{_consumer->bounded_queue.prepare_read()};
300 if (read_result.read_pos)
307 _consumer->bounded_queue.commit_read();
310 auto const previous_capacity = _consumer->bounded_queue.capacity();
313 _consumer = next_node;
314 read_result.read_pos = _consumer->bounded_queue.prepare_read();
317 read_result.allocation =
true;
318 read_result.new_capacity = _consumer->bounded_queue.capacity();
319 read_result.previous_capacity = previous_capacity;
325 size_t _max_capacity;
328 alignas(QUILL_CACHE_LINE_ALIGNED) Node* _producer{
nullptr};
329 alignas(QUILL_CACHE_LINE_ALIGNED) Node* _consumer{
nullptr};
A single-producer single-consumer FIFO circular buffer.
Definition: UnboundedSPSCQueue.h:37
~UnboundedSPSCQueue()
Destructor.
Definition: UnboundedSPSCQueue.h:91
Definition: UnboundedSPSCQueue.h:61
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:110
QUILL_NODISCARD size_t capacity() const noexcept
Return the current buffer's capacity.
Definition: UnboundedSPSCQueue.h:225
UnboundedSPSCQueue(size_t initial_bounded_queue_capacity, size_t max_capacity, HugePagesPolicy huge_pages_policy=quill::HugePagesPolicy::Never)
Constructor.
Definition: UnboundedSPSCQueue.h:74
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:127
QUILL_NODISCARD QUILL_ATTRIBUTE_HOT bool empty() const noexcept
checks if the queue is empty
Definition: UnboundedSPSCQueue.h:232
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:210
QUILL_NODISCARD size_t producer_capacity() const noexcept
Return the current buffer's capacity.
Definition: UnboundedSPSCQueue.h:151
QUILL_ATTRIBUTE_HOT void commit_write() noexcept
Commit write to notify the consumer bytes are ready to read.
Definition: UnboundedSPSCQueue.h:135
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:185
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:218
void shrink(size_t capacity)
Shrinks the queue if capacity is a valid smaller power of 2.
Definition: UnboundedSPSCQueue.h:161
QUILL_ATTRIBUTE_HOT void finish_and_commit_write(size_t nbytes) noexcept
Finish and commit write as a single function.
Definition: UnboundedSPSCQueue.h:140