unbounded_ordering_queue.hpp 7.5 KB

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