bounded_ordering_queue.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file bounded_ordering_queue.hpp
  9. * \author Andrey Semashev
  10. * \date 06.01.2012
  11. *
  12. * The header contains implementation of bounded ordering queueing strategy for
  13. * the asynchronous sink frontend.
  14. */
  15. #ifndef BOOST_LOG_SINKS_BOUNDED_ORDERING_QUEUE_HPP_INCLUDED_
  16. #define BOOST_LOG_SINKS_BOUNDED_ORDERING_QUEUE_HPP_INCLUDED_
  17. #include <boost/log/detail/config.hpp>
  18. #ifdef BOOST_HAS_PRAGMA_ONCE
  19. #pragma once
  20. #endif
  21. #if defined(BOOST_LOG_NO_THREADS)
  22. #error Boost.Log: This header content is only supported in multithreaded environment
  23. #endif
  24. #include <cstddef>
  25. #include <queue>
  26. #include <vector>
  27. #include <chrono>
  28. #include <mutex>
  29. #include <condition_variable>
  30. #include <boost/log/detail/enqueued_record.hpp>
  31. #include <boost/log/keywords/order.hpp>
  32. #include <boost/log/keywords/ordering_window.hpp>
  33. #include <boost/log/core/record_view.hpp>
  34. #include <boost/log/detail/header.hpp>
  35. namespace boost {
  36. BOOST_LOG_OPEN_NAMESPACE
  37. namespace sinks {
  38. /*!
  39. * \brief Bounded ordering log record queueing strategy
  40. *
  41. * The \c bounded_ordering_queue class is intended to be used with
  42. * the \c asynchronous_sink frontend as a log record queueing strategy.
  43. *
  44. * This strategy provides the following properties to the record queueing mechanism:
  45. *
  46. * \li The queue has limited capacity specified by the \c MaxQueueSizeV template parameter.
  47. * \li Upon reaching the size limit, the queue invokes the overflow handling strategy
  48. * specified in the \c OverflowStrategyT template parameter to handle the situation.
  49. * The library provides overflow handling strategies for most common cases:
  50. * \c drop_on_overflow will silently discard the log record, and \c block_on_overflow
  51. * will put the enqueueing thread to wait until there is space in the queue.
  52. * \li The queue has a fixed latency window. This means that each log record put
  53. * into the queue will normally not be dequeued for a certain period of time.
  54. * \li The queue performs stable record ordering within the latency window.
  55. * The ordering predicate can be specified in the \c OrderT template parameter.
  56. */
  57. template< typename OrderT, std::size_t MaxQueueSizeV, typename OverflowStrategyT >
  58. class bounded_ordering_queue :
  59. private OverflowStrategyT
  60. {
  61. private:
  62. typedef OverflowStrategyT overflow_strategy;
  63. typedef std::mutex mutex_type;
  64. typedef sinks::aux::enqueued_record enqueued_record;
  65. typedef std::priority_queue<
  66. enqueued_record,
  67. std::vector< enqueued_record >,
  68. enqueued_record::order< OrderT >
  69. > queue_type;
  70. private:
  71. //! Ordering window duration
  72. const std::chrono::steady_clock::duration m_ordering_window;
  73. //! Synchronization primitive
  74. mutex_type m_mutex;
  75. //! Condition to block the consuming thread on
  76. std::condition_variable m_cond;
  77. //! Log record queue
  78. queue_type m_queue;
  79. //! Interruption flag
  80. bool m_interruption_requested;
  81. public:
  82. /*!
  83. * Returns ordering window size specified during initialization
  84. */
  85. std::chrono::steady_clock::duration get_ordering_window() const
  86. {
  87. return m_ordering_window;
  88. }
  89. /*!
  90. * Returns default ordering window size.
  91. * The default window size is specific to the operating system thread scheduling mechanism.
  92. */
  93. static BOOST_CONSTEXPR std::chrono::steady_clock::duration get_default_ordering_window() BOOST_NOEXCEPT
  94. {
  95. // The main idea behind this parameter is that the ordering window should be large enough
  96. // to allow the frontend to order records from different threads on an attribute
  97. // that contains system time. Thus this value should be:
  98. // * No less than the minimum time resolution quant that Boost.DateTime provides on the current OS.
  99. // For instance, on Windows it defaults to around 15-16 ms.
  100. // * No less than thread switching quant on the current OS. For now 30 ms is large enough window size to
  101. // switch threads on any known OS. It can be tuned for other platforms as needed.
  102. return std::chrono::milliseconds(30);
  103. }
  104. protected:
  105. //! Initializing constructor
  106. template< typename ArgsT >
  107. explicit bounded_ordering_queue(ArgsT const& args) :
  108. m_ordering_window(std::chrono::duration_cast< std::chrono::steady_clock::duration >(args[keywords::ordering_window || &bounded_ordering_queue::get_default_ordering_window])),
  109. m_queue(args[keywords::order]),
  110. m_interruption_requested(false)
  111. {
  112. }
  113. //! Enqueues log record to the queue
  114. void enqueue(record_view const& rec)
  115. {
  116. std::unique_lock< mutex_type > lock(m_mutex);
  117. std::size_t size = m_queue.size();
  118. for (; size >= MaxQueueSizeV; size = m_queue.size())
  119. {
  120. if (!overflow_strategy::on_overflow(rec, lock))
  121. return;
  122. }
  123. m_queue.push(enqueued_record(rec));
  124. if (size == 0)
  125. m_cond.notify_one();
  126. }
  127. //! Attempts to enqueue log record to the queue
  128. bool try_enqueue(record_view const& rec)
  129. {
  130. std::unique_lock< mutex_type > lock(m_mutex, std::try_to_lock);
  131. if (lock.owns_lock())
  132. {
  133. const std::size_t size = m_queue.size();
  134. // Do not invoke the bounding strategy in case of overflow as it may block
  135. if (size < MaxQueueSizeV)
  136. {
  137. m_queue.push(enqueued_record(rec));
  138. if (size == 0)
  139. m_cond.notify_one();
  140. return true;
  141. }
  142. }
  143. return false;
  144. }
  145. //! Attempts to dequeue a log record ready for processing from the queue, does not block if the queue is empty
  146. bool try_dequeue_ready(record_view& rec)
  147. {
  148. std::lock_guard< mutex_type > lock(m_mutex);
  149. const std::size_t size = m_queue.size();
  150. if (size > 0)
  151. {
  152. const auto now = std::chrono::steady_clock::now();
  153. enqueued_record const& elem = m_queue.top();
  154. if ((now - elem.m_timestamp) >= m_ordering_window)
  155. {
  156. // We got a new element
  157. rec = elem.m_record;
  158. m_queue.pop();
  159. overflow_strategy::on_queue_space_available();
  160. return true;
  161. }
  162. }
  163. return false;
  164. }
  165. //! Attempts to dequeue log record from the queue, does not block if the queue is empty
  166. bool try_dequeue(record_view& rec)
  167. {
  168. std::lock_guard< mutex_type > lock(m_mutex);
  169. const std::size_t size = m_queue.size();
  170. if (size > 0)
  171. {
  172. enqueued_record const& elem = m_queue.top();
  173. rec = elem.m_record;
  174. m_queue.pop();
  175. overflow_strategy::on_queue_space_available();
  176. return true;
  177. }
  178. return false;
  179. }
  180. //! Dequeues log record from the queue, blocks if the queue is empty
  181. bool dequeue_ready(record_view& rec)
  182. {
  183. std::unique_lock< mutex_type > lock(m_mutex);
  184. while (!m_interruption_requested)
  185. {
  186. const std::size_t size = m_queue.size();
  187. if (size > 0)
  188. {
  189. const auto now = std::chrono::steady_clock::now();
  190. enqueued_record const& elem = m_queue.top();
  191. const auto difference = now - elem.m_timestamp;
  192. if (difference >= m_ordering_window)
  193. {
  194. rec = elem.m_record;
  195. m_queue.pop();
  196. overflow_strategy::on_queue_space_available();
  197. return true;
  198. }
  199. else
  200. {
  201. // Wait until the element becomes ready to be processed
  202. m_cond.wait_for(lock, m_ordering_window - difference);
  203. }
  204. }
  205. else
  206. {
  207. m_cond.wait(lock);
  208. }
  209. }
  210. m_interruption_requested = false;
  211. return false;
  212. }
  213. //! Wakes a thread possibly blocked in the \c dequeue method
  214. void interrupt_dequeue()
  215. {
  216. std::lock_guard< mutex_type > lock(m_mutex);
  217. m_interruption_requested = true;
  218. overflow_strategy::interrupt();
  219. m_cond.notify_one();
  220. }
  221. };
  222. } // namespace sinks
  223. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  224. } // namespace boost
  225. #include <boost/log/detail/footer.hpp>
  226. #endif // BOOST_LOG_SINKS_BOUNDED_ORDERING_QUEUE_HPP_INCLUDED_