io_uring_service.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. //
  2. // detail/io_uring_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_IO_URING_SERVICE_HPP
  11. #define BOOST_ASIO_DETAIL_IO_URING_SERVICE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/config.hpp>
  16. #if defined(BOOST_ASIO_HAS_IO_URING)
  17. #include <liburing.h>
  18. #include <boost/asio/detail/atomic_count.hpp>
  19. #include <boost/asio/detail/buffer_sequence_adapter.hpp>
  20. #include <boost/asio/detail/conditionally_enabled_mutex.hpp>
  21. #include <boost/asio/detail/io_uring_operation.hpp>
  22. #include <boost/asio/detail/limits.hpp>
  23. #include <boost/asio/detail/object_pool.hpp>
  24. #include <boost/asio/detail/op_queue.hpp>
  25. #include <boost/asio/detail/reactor.hpp>
  26. #include <boost/asio/detail/scheduler_task.hpp>
  27. #include <boost/asio/detail/timer_queue_base.hpp>
  28. #include <boost/asio/detail/timer_queue_set.hpp>
  29. #include <boost/asio/detail/wait_op.hpp>
  30. #include <boost/asio/execution_context.hpp>
  31. #include <boost/asio/detail/push_options.hpp>
  32. namespace boost {
  33. namespace asio {
  34. namespace detail {
  35. class io_uring_service
  36. : public execution_context_service_base<io_uring_service>,
  37. public scheduler_task
  38. {
  39. private:
  40. // The mutex type used by this reactor.
  41. typedef conditionally_enabled_mutex mutex;
  42. public:
  43. enum op_types { read_op = 0, write_op = 1, except_op = 2, max_ops = 3 };
  44. class io_object;
  45. // An I/O queue stores operations that must run serially.
  46. class io_queue : operation
  47. {
  48. friend class io_uring_service;
  49. io_object* io_object_;
  50. op_queue<io_uring_operation> op_queue_;
  51. bool cancel_requested_;
  52. BOOST_ASIO_DECL io_queue();
  53. void set_result(int r) { task_result_ = static_cast<unsigned>(r); }
  54. BOOST_ASIO_DECL operation* perform_io(int result);
  55. BOOST_ASIO_DECL static void do_complete(void* owner, operation* base,
  56. const boost::system::error_code& ec, std::size_t bytes_transferred);
  57. };
  58. // Per I/O object state.
  59. class io_object
  60. {
  61. friend class io_uring_service;
  62. friend class object_pool_access;
  63. io_object* next_;
  64. io_object* prev_;
  65. mutex mutex_;
  66. io_uring_service* service_;
  67. io_queue queues_[max_ops];
  68. bool shutdown_;
  69. BOOST_ASIO_DECL io_object(bool locking);
  70. };
  71. // Per I/O object data.
  72. typedef io_object* per_io_object_data;
  73. // Constructor.
  74. BOOST_ASIO_DECL io_uring_service(boost::asio::execution_context& ctx);
  75. // Destructor.
  76. BOOST_ASIO_DECL ~io_uring_service();
  77. // Destroy all user-defined handler objects owned by the service.
  78. BOOST_ASIO_DECL void shutdown();
  79. // Recreate internal state following a fork.
  80. BOOST_ASIO_DECL void notify_fork(
  81. boost::asio::execution_context::fork_event fork_ev);
  82. // Initialise the task.
  83. BOOST_ASIO_DECL void init_task();
  84. // Register an I/O object with io_uring.
  85. BOOST_ASIO_DECL void register_io_object(io_object*& io_obj);
  86. // Register an internal I/O object with io_uring.
  87. BOOST_ASIO_DECL void register_internal_io_object(
  88. io_object*& io_obj, int op_type, io_uring_operation* op);
  89. // Register buffers with io_uring.
  90. BOOST_ASIO_DECL void register_buffers(const ::iovec* v, unsigned n);
  91. // Unregister buffers from io_uring.
  92. BOOST_ASIO_DECL void unregister_buffers();
  93. // Post an operation for immediate completion.
  94. void post_immediate_completion(operation* op, bool is_continuation);
  95. // Start a new operation. The operation will be prepared and submitted to the
  96. // io_uring when it is at the head of its I/O operation queue.
  97. BOOST_ASIO_DECL void start_op(int op_type, per_io_object_data& io_obj,
  98. io_uring_operation* op, bool is_continuation);
  99. // Cancel all operations associated with the given I/O object. The handlers
  100. // associated with the I/O object will be invoked with the operation_aborted
  101. // error.
  102. BOOST_ASIO_DECL void cancel_ops(per_io_object_data& io_obj);
  103. // Cancel all operations associated with the given I/O object and key. The
  104. // handlers associated with the object and key will be invoked with the
  105. // operation_aborted error.
  106. BOOST_ASIO_DECL void cancel_ops_by_key(per_io_object_data& io_obj,
  107. int op_type, void* cancellation_key);
  108. // Cancel any operations that are running against the I/O object and remove
  109. // its registration from the service. The service resources associated with
  110. // the I/O object must be released by calling cleanup_io_object.
  111. BOOST_ASIO_DECL void deregister_io_object(per_io_object_data& io_obj);
  112. // Perform any post-deregistration cleanup tasks associated with the I/O
  113. // object.
  114. BOOST_ASIO_DECL void cleanup_io_object(per_io_object_data& io_obj);
  115. // Add a new timer queue to the reactor.
  116. template <typename Time_Traits>
  117. void add_timer_queue(timer_queue<Time_Traits>& timer_queue);
  118. // Remove a timer queue from the reactor.
  119. template <typename Time_Traits>
  120. void remove_timer_queue(timer_queue<Time_Traits>& timer_queue);
  121. // Schedule a new operation in the given timer queue to expire at the
  122. // specified absolute time.
  123. template <typename Time_Traits>
  124. void schedule_timer(timer_queue<Time_Traits>& queue,
  125. const typename Time_Traits::time_type& time,
  126. typename timer_queue<Time_Traits>::per_timer_data& timer, wait_op* op);
  127. // Cancel the timer operations associated with the given token. Returns the
  128. // number of operations that have been posted or dispatched.
  129. template <typename Time_Traits>
  130. std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
  131. typename timer_queue<Time_Traits>::per_timer_data& timer,
  132. std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
  133. // Cancel the timer operations associated with the given key.
  134. template <typename Time_Traits>
  135. void cancel_timer_by_key(timer_queue<Time_Traits>& queue,
  136. typename timer_queue<Time_Traits>::per_timer_data* timer,
  137. void* cancellation_key);
  138. // Move the timer operations associated with the given timer.
  139. template <typename Time_Traits>
  140. void move_timer(timer_queue<Time_Traits>& queue,
  141. typename timer_queue<Time_Traits>::per_timer_data& target,
  142. typename timer_queue<Time_Traits>::per_timer_data& source);
  143. // Wait on io_uring once until interrupted or events are ready to be
  144. // dispatched.
  145. BOOST_ASIO_DECL void run(long usec, op_queue<operation>& ops);
  146. // Interrupt the io_uring wait.
  147. BOOST_ASIO_DECL void interrupt();
  148. private:
  149. // The hint to pass to io_uring_queue_init to size its data structures.
  150. enum { ring_size = 16384 };
  151. // The number of operations to submit in a batch.
  152. enum { submit_batch_size = 128 };
  153. // The number of operations to complete in a batch.
  154. enum { complete_batch_size = 128 };
  155. // The type used for processing eventfd readiness notifications.
  156. class event_fd_read_op;
  157. // Initialise the ring.
  158. BOOST_ASIO_DECL void init_ring();
  159. // Register the eventfd descriptor for readiness notifications.
  160. BOOST_ASIO_DECL void register_with_reactor();
  161. // Allocate a new I/O object.
  162. BOOST_ASIO_DECL io_object* allocate_io_object();
  163. // Free an existing I/O object.
  164. BOOST_ASIO_DECL void free_io_object(io_object* s);
  165. // Helper function to cancel all operations associated with the given I/O
  166. // object. This function must be called while the I/O object's mutex is held.
  167. // Returns true if there are operations for which cancellation is pending.
  168. BOOST_ASIO_DECL bool do_cancel_ops(
  169. per_io_object_data& io_obj, op_queue<operation>& ops);
  170. // Helper function to add a new timer queue.
  171. BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
  172. // Helper function to remove a timer queue.
  173. BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
  174. // Called to recalculate and update the timeout.
  175. BOOST_ASIO_DECL void update_timeout();
  176. // Get the current timeout value.
  177. BOOST_ASIO_DECL __kernel_timespec get_timeout() const;
  178. // Get a new submission queue entry, flushing the queue if necessary.
  179. BOOST_ASIO_DECL ::io_uring_sqe* get_sqe();
  180. // Submit pending submission queue entries.
  181. BOOST_ASIO_DECL void submit_sqes();
  182. // Post an operation to submit the pending submission queue entries.
  183. BOOST_ASIO_DECL void post_submit_sqes_op(mutex::scoped_lock& lock);
  184. // Push an operation to submit the pending submission queue entries.
  185. BOOST_ASIO_DECL void push_submit_sqes_op(op_queue<operation>& ops);
  186. // Helper operation to submit pending submission queue entries.
  187. class submit_sqes_op : operation
  188. {
  189. friend class io_uring_service;
  190. io_uring_service* service_;
  191. BOOST_ASIO_DECL submit_sqes_op(io_uring_service* s);
  192. BOOST_ASIO_DECL static void do_complete(void* owner, operation* base,
  193. const boost::system::error_code& ec, std::size_t bytes_transferred);
  194. };
  195. // The scheduler implementation used to post completions.
  196. scheduler& scheduler_;
  197. // Mutex to protect access to internal data.
  198. mutex mutex_;
  199. // The ring.
  200. ::io_uring ring_;
  201. // The count of unfinished work.
  202. atomic_count outstanding_work_;
  203. // The operation used to submit the pending submission queue entries.
  204. submit_sqes_op submit_sqes_op_;
  205. // The number of pending submission queue entries_.
  206. int pending_sqes_;
  207. // Whether there is a pending submission operation.
  208. bool pending_submit_sqes_op_;
  209. // Whether the service has been shut down.
  210. bool shutdown_;
  211. // The timer queues.
  212. timer_queue_set timer_queues_;
  213. // The timespec for the pending timeout operation. Must remain valid while the
  214. // operation is outstanding.
  215. __kernel_timespec timeout_;
  216. // Mutex to protect access to the registered I/O objects.
  217. mutex registration_mutex_;
  218. // Keep track of all registered I/O objects.
  219. object_pool<io_object> registered_io_objects_;
  220. // Helper class to do post-perform_io cleanup.
  221. struct perform_io_cleanup_on_block_exit;
  222. friend struct perform_io_cleanup_on_block_exit;
  223. // The reactor used to register for eventfd readiness.
  224. reactor& reactor_;
  225. // The per-descriptor reactor data used for the eventfd.
  226. reactor::per_descriptor_data reactor_data_;
  227. // The eventfd descriptor used to wait for readiness.
  228. int event_fd_;
  229. };
  230. } // namespace detail
  231. } // namespace asio
  232. } // namespace boost
  233. #include <boost/asio/detail/pop_options.hpp>
  234. #include <boost/asio/detail/impl/io_uring_service.hpp>
  235. #if defined(BOOST_ASIO_HEADER_ONLY)
  236. # include <boost/asio/detail/impl/io_uring_service.ipp>
  237. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  238. #endif // defined(BOOST_ASIO_HAS_IO_URING)
  239. #endif // BOOST_ASIO_DETAIL_IO_URING_SERVICE_HPP