bounded_ordering_queue.hpp 11 KB

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