A C++ library for logging very fast and without allocating.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

194 líneas
5.7 KiB

  1. #pragma once
  2. #include <cstddef>
  3. #include <atomic>
  4. #include <new>
  5. #include <cassert>
  6. #include <optional>
  7. #ifdef __cpp_lib_hardware_interference_size
  8. static constexpr size_t max_interference_size = std::max(std::hardware_constructive_interference_size, std::hardware_destructive_interference_size);
  9. static constexpr size_t line_length = std::hardware_constructive_interference_size;
  10. #else
  11. static constexpr size_t max_interference_size = 128;
  12. static constexpr size_t line_length = 64;
  13. #endif
  14. template<typename T>
  15. struct alignas(max_interference_size) padded_atomic final {
  16. std::atomic<T> value;
  17. };
  18. using offset_t = size_t;
  19. extern const size_t page_size;
  20. enum class overflow_response_t {
  21. must_wait,
  22. must_overflow
  23. };
  24. #ifdef __cpp_concepts
  25. template<typename T>
  26. concept OverflowStrategyType = requires (T strategy) {
  27. {T::on_overflow} -> std::same_as<overflow_response_t>;
  28. {strategy.wait()};
  29. };
  30. #endif
  31. struct force_contiguous_mode {};
  32. struct token_t {offset_t start; offset_t end;};
  33. struct disruptor_exception : public std::runtime_error {
  34. disruptor_exception() : std::runtime_error("Unknown error disruptor") {}
  35. explicit disruptor_exception(const char* str) : std::runtime_error(str) {}
  36. };
  37. template<typename OverflowStrategy>
  38. #ifdef __cpp_concepts
  39. requires OverflowStrategyType<OverflowStrategy>
  40. #endif
  41. class disruptor
  42. {
  43. char* buffer;
  44. size_t buffer_size;
  45. std::atomic<offset_t>& read_trailer;
  46. std::atomic<offset_t>& read_lead;
  47. std::atomic<offset_t>& write_trailer;
  48. std::atomic<offset_t>& write_lead;
  49. offset_t& max_offset;
  50. char* data_buffer;
  51. public:
  52. disruptor(char* _buffer, const size_t _buffer_size)
  53. : buffer(_buffer)
  54. , buffer_size(_buffer_size)
  55. , read_trailer (*reinterpret_cast<std::atomic<offset_t>*>(_buffer+max_interference_size*0))
  56. , read_lead (*reinterpret_cast<std::atomic<offset_t>*>(_buffer+max_interference_size*1))
  57. , write_trailer(*reinterpret_cast<std::atomic<offset_t>*>(_buffer+max_interference_size*2))
  58. , write_lead (*reinterpret_cast<std::atomic<offset_t>*>(_buffer+max_interference_size*3))
  59. , max_offset (*reinterpret_cast<offset_t*> (_buffer+max_interference_size*4))
  60. {
  61. if(buffer_size <= page_size) throw disruptor_exception("buffer size too small to build a disruptor");
  62. data_buffer = buffer+max_interference_size*5;
  63. if(data_buffer <= buffer+page_size) {
  64. data_buffer = buffer+page_size;
  65. max_offset = buffer_size - page_size;
  66. } else if(data_buffer > buffer+page_size) {
  67. size_t ctr = 0;
  68. do {
  69. ctr++;
  70. } while(data_buffer > buffer+page_size*ctr);
  71. data_buffer = buffer+page_size*ctr;
  72. max_offset = buffer_size - page_size*ctr;
  73. }
  74. }
  75. template<bool must_be_contiguous = false>
  76. std::optional<token_t> try_advance(
  77. std::atomic<offset_t>& lead,
  78. std::atomic<offset_t> fence,
  79. offset_t amount
  80. ) {
  81. offset_t old_offset = lead.load(std::memory_order_relaxed);
  82. offset_t new_offset = old_offset + amount;
  83. offset_t fence_v = fence.load(std::memory_order_relaxed);
  84. // Check if we jumped the fence
  85. if(fence_v <= new_offset && fence_v > old_offset) {
  86. goto handle_fence;
  87. }
  88. // Check if we jumped the fence while overflowing
  89. if(new_offset >= max_offset) {
  90. if constexpr(must_be_contiguous) {
  91. new_offset = amount;
  92. } else {
  93. new_offset %= max_offset;
  94. }
  95. if(fence_v <= new_offset) {
  96. goto handle_fence;
  97. }
  98. }
  99. goto handle_fence_end;
  100. handle_fence:
  101. if constexpr (OverflowStrategy::on_overflow == overflow_response_t::must_wait) {
  102. return std::nullopt;
  103. } else if constexpr (OverflowStrategy::on_overflow == overflow_response_t::must_overflow) {
  104. if(!fence.compare_exchange_weak(fence_v, new_offset, std::memory_order_release, std::memory_order_relaxed)) {
  105. return std::nullopt;
  106. }
  107. } else {
  108. static_assert(
  109. OverflowStrategy::on_overflow == overflow_response_t::must_wait
  110. || OverflowStrategy::on_overflow == overflow_response_t::must_overflow
  111. );
  112. }
  113. handle_fence_end:
  114. if(!lead.compare_exchange_weak(old_offset, new_offset, std::memory_order_acquire, std::memory_order_relaxed)) {
  115. return std::nullopt;
  116. }
  117. return token_t{old_offset, new_offset};
  118. }
  119. token_t reserve_write(size_t sz) {
  120. std::optional<token_t> tok;
  121. OverflowStrategy waiter;
  122. while(true) {
  123. tok = try_advance(write_lead, read_trailer, sz);
  124. if(tok) break;
  125. waiter.wait();
  126. }
  127. return tok.value();
  128. }
  129. token_t reserve_write(size_t sz, force_contiguous_mode) {
  130. std::optional<token_t> tok;
  131. OverflowStrategy waiter;
  132. while(true) {
  133. tok = try_advance<true>(write_lead, read_trailer, sz);
  134. if(tok) break;
  135. waiter.wait();
  136. }
  137. return tok.value();
  138. }
  139. void conclude_write(token_t tok) noexcept(std::is_nothrow_invocable_v<typename OverflowStrategy::wait>) {
  140. OverflowStrategy waiter;
  141. while(!write_trailer.compare_exchange_weak(tok.start, tok.end, std::memory_order_release, std::memory_order_relaxed)) {
  142. waiter.wait();
  143. }
  144. }
  145. token_t reserve_read() {
  146. offset_t old_offset = read_lead.load(std::memory_order_relaxed);
  147. offset_t new_offset = write_trailer.load(std::memory_order_relaxed);
  148. if(old_offset > new_offset) new_offset = 0;
  149. OverflowStrategy waiter;
  150. while(!read_lead.compare_exchange_weak(old_offset, new_offset, std::memory_order_acquire, std::memory_order_relaxed)) {
  151. waiter.wait();
  152. }
  153. return token_t{old_offset, new_offset};
  154. }
  155. void conclude_read(token_t tok) noexcept(std::is_nothrow_invocable_v<typename OverflowStrategy::wait>) {
  156. OverflowStrategy waiter;
  157. while(!read_trailer.compare_exchange_weak(tok.start, tok.end, std::memory_order_release, std::memory_order_relaxed)) {
  158. waiter.wait();
  159. }
  160. }
  161. };
  162. struct OverflowWait {
  163. static constexpr overflow_response_t on_overflow = overflow_response_t::must_wait;
  164. void wait() {
  165. #ifdef __clang__ or __GNUC__
  166. __asm__("nop\n\t");
  167. #elif _MSC_VER
  168. __nop;
  169. #endif
  170. }
  171. };