bounded_ordering_queue.hpp 8.7 KB

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