unbounded_ordering_queue.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 unbounded_ordering_queue.hpp
  9. * \author Andrey Semashev
  10. * \date 24.07.2011
  11. *
  12. * The header contains implementation of unbounded ordering record queueing strategy for
  13. * the asynchronous sink frontend.
  14. */
  15. #ifndef BOOST_LOG_SINKS_UNBOUNDED_ORDERING_QUEUE_HPP_INCLUDED_
  16. #define BOOST_LOG_SINKS_UNBOUNDED_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 <queue>
  25. #include <vector>
  26. #include <boost/cstdint.hpp>
  27. #include <boost/thread/locks.hpp>
  28. #include <boost/thread/mutex.hpp>
  29. #include <boost/thread/condition_variable.hpp>
  30. #include <boost/thread/thread_time.hpp>
  31. #include <boost/date_time/posix_time/posix_time_types.hpp>
  32. #include <boost/log/detail/timestamp.hpp>
  33. #include <boost/log/detail/enqueued_record.hpp>
  34. #include <boost/log/keywords/order.hpp>
  35. #include <boost/log/keywords/ordering_window.hpp>
  36. #include <boost/log/core/record_view.hpp>
  37. #include <boost/log/detail/header.hpp>
  38. namespace boost {
  39. BOOST_LOG_OPEN_NAMESPACE
  40. namespace sinks {
  41. /*!
  42. * \brief Unbounded ordering log record queueing strategy
  43. *
  44. * The \c unbounded_ordering_queue class is intended to be used with
  45. * the \c asynchronous_sink frontend as a log record queueing strategy.
  46. *
  47. * This strategy provides the following properties to the record queueing mechanism:
  48. *
  49. * \li The queue has no size limits.
  50. * \li The queue has a fixed latency window. This means that each log record put
  51. * into the queue will normally not be dequeued for a certain period of time.
  52. * \li The queue performs stable record ordering within the latency window.
  53. * The ordering predicate can be specified in the \c OrderT template parameter.
  54. *
  55. * Since this queue has no size limits, it may grow uncontrollably if sink backends
  56. * dequeue log records not fast enough. When this is an issue, it is recommended to
  57. * use one of the bounded strategies.
  58. */
  59. template< typename OrderT >
  60. class unbounded_ordering_queue
  61. {
  62. private:
  63. typedef boost::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, in milliseconds
  72. const uint64_t m_ordering_window;
  73. //! Synchronization mutex
  74. mutex_type m_mutex;
  75. //! Condition for blocking
  76. condition_variable m_cond;
  77. //! Thread-safe 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. posix_time::time_duration get_ordering_window() const
  86. {
  87. return posix_time::milliseconds(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 posix_time::time_duration get_default_ordering_window()
  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 posix_time::milliseconds(30);
  103. }
  104. protected:
  105. //! Initializing constructor
  106. template< typename ArgsT >
  107. explicit unbounded_ordering_queue(ArgsT const& args) :
  108. m_ordering_window(args[keywords::ordering_window || &unbounded_ordering_queue::get_default_ordering_window].total_milliseconds()),
  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. lock_guard< mutex_type > lock(m_mutex);
  117. enqueue_unlocked(rec);
  118. }
  119. //! Attempts to enqueue log record to the queue
  120. bool try_enqueue(record_view const& rec)
  121. {
  122. unique_lock< mutex_type > lock(m_mutex, try_to_lock);
  123. if (lock.owns_lock())
  124. {
  125. enqueue_unlocked(rec);
  126. return true;
  127. }
  128. else
  129. return false;
  130. }
  131. //! Attempts to dequeue a log record ready for processing from the queue, does not block if no log records are ready to be processed
  132. bool try_dequeue_ready(record_view& rec)
  133. {
  134. lock_guard< mutex_type > lock(m_mutex);
  135. if (!m_queue.empty())
  136. {
  137. const boost::log::aux::timestamp now = boost::log::aux::get_timestamp();
  138. enqueued_record const& elem = m_queue.top();
  139. if (static_cast< uint64_t >((now - elem.m_timestamp).milliseconds()) >= m_ordering_window)
  140. {
  141. // We got a new element
  142. rec = elem.m_record;
  143. m_queue.pop();
  144. return true;
  145. }
  146. }
  147. return false;
  148. }
  149. //! Attempts to dequeue log record from the queue, does not block.
  150. bool try_dequeue(record_view& rec)
  151. {
  152. lock_guard< mutex_type > lock(m_mutex);
  153. if (!m_queue.empty())
  154. {
  155. enqueued_record const& elem = m_queue.top();
  156. rec = elem.m_record;
  157. m_queue.pop();
  158. return true;
  159. }
  160. return false;
  161. }
  162. //! Dequeues log record from the queue, blocks if no log records are ready to be processed
  163. bool dequeue_ready(record_view& rec)
  164. {
  165. unique_lock< mutex_type > lock(m_mutex);
  166. while (!m_interruption_requested)
  167. {
  168. if (!m_queue.empty())
  169. {
  170. const boost::log::aux::timestamp now = boost::log::aux::get_timestamp();
  171. enqueued_record const& elem = m_queue.top();
  172. const uint64_t difference = (now - elem.m_timestamp).milliseconds();
  173. if (difference >= m_ordering_window)
  174. {
  175. // We got a new element
  176. rec = elem.m_record;
  177. m_queue.pop();
  178. return true;
  179. }
  180. else
  181. {
  182. // Wait until the element becomes ready to be processed
  183. m_cond.timed_wait(lock, posix_time::milliseconds(m_ordering_window - difference));
  184. }
  185. }
  186. else
  187. {
  188. // Wait for an element to come
  189. m_cond.wait(lock);
  190. }
  191. }
  192. m_interruption_requested = false;
  193. return false;
  194. }
  195. //! Wakes a thread possibly blocked in the \c dequeue method
  196. void interrupt_dequeue()
  197. {
  198. lock_guard< mutex_type > lock(m_mutex);
  199. m_interruption_requested = true;
  200. m_cond.notify_one();
  201. }
  202. private:
  203. //! Enqueues a log record
  204. void enqueue_unlocked(record_view const& rec)
  205. {
  206. const bool was_empty = m_queue.empty();
  207. m_queue.push(enqueued_record(rec));
  208. if (was_empty)
  209. m_cond.notify_one();
  210. }
  211. };
  212. } // namespace sinks
  213. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  214. } // namespace boost
  215. #include <boost/log/detail/footer.hpp>
  216. #endif // BOOST_LOG_SINKS_UNBOUNDED_ORDERING_QUEUE_HPP_INCLUDED_