io_uring_service.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. //
  2. // detail/io_uring_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2025 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. struct io_object
  60. {
  61. io_object* next_;
  62. io_object* prev_;
  63. mutex mutex_;
  64. io_uring_service* service_;
  65. io_queue queues_[max_ops];
  66. bool shutdown_;
  67. BOOST_ASIO_DECL io_object(bool locking, int spin_count);
  68. };
  69. // Per I/O object data.
  70. typedef io_object* per_io_object_data;
  71. // Constructor.
  72. BOOST_ASIO_DECL io_uring_service(boost::asio::execution_context& ctx);
  73. // Destructor.
  74. BOOST_ASIO_DECL ~io_uring_service();
  75. // Destroy all user-defined handler objects owned by the service.
  76. BOOST_ASIO_DECL void shutdown();
  77. // Recreate internal state following a fork.
  78. BOOST_ASIO_DECL void notify_fork(
  79. boost::asio::execution_context::fork_event fork_ev);
  80. // Initialise the task.
  81. BOOST_ASIO_DECL void init_task();
  82. // Register an I/O object with io_uring.
  83. BOOST_ASIO_DECL void register_io_object(io_object*& io_obj);
  84. // Register an internal I/O object with io_uring.
  85. BOOST_ASIO_DECL void register_internal_io_object(
  86. io_object*& io_obj, int op_type, io_uring_operation* op);
  87. // Register buffers with io_uring.
  88. BOOST_ASIO_DECL void register_buffers(const ::iovec* v, unsigned n);
  89. // Unregister buffers from io_uring.
  90. BOOST_ASIO_DECL void unregister_buffers();
  91. // Post an operation for immediate completion.
  92. void post_immediate_completion(operation* op, bool is_continuation);
  93. // Start a new operation. The operation will be prepared and submitted to the
  94. // io_uring when it is at the head of its I/O operation queue.
  95. BOOST_ASIO_DECL void start_op(int op_type, per_io_object_data& io_obj,
  96. io_uring_operation* op, bool is_continuation);
  97. // Cancel all operations associated with the given I/O object. The handlers
  98. // associated with the I/O object will be invoked with the operation_aborted
  99. // error.
  100. BOOST_ASIO_DECL void cancel_ops(per_io_object_data& io_obj);
  101. // Cancel all operations associated with the given I/O object and key. The
  102. // handlers associated with the object and key will be invoked with the
  103. // operation_aborted error.
  104. BOOST_ASIO_DECL void cancel_ops_by_key(per_io_object_data& io_obj,
  105. int op_type, void* cancellation_key);
  106. // Cancel any operations that are running against the I/O object and remove
  107. // its registration from the service. The service resources associated with
  108. // the I/O object must be released by calling cleanup_io_object.
  109. BOOST_ASIO_DECL void deregister_io_object(per_io_object_data& io_obj);
  110. // Perform any post-deregistration cleanup tasks associated with the I/O
  111. // object.
  112. BOOST_ASIO_DECL void cleanup_io_object(per_io_object_data& io_obj);
  113. // Add a new timer queue to the reactor.
  114. template <typename TimeTraits, typename Allocator>
  115. void add_timer_queue(timer_queue<TimeTraits, Allocator>& timer_queue);
  116. // Remove a timer queue from the reactor.
  117. template <typename TimeTraits, typename Allocator>
  118. void remove_timer_queue(timer_queue<TimeTraits, Allocator>& timer_queue);
  119. // Schedule a new operation in the given timer queue to expire at the
  120. // specified absolute time.
  121. template <typename TimeTraits, typename Allocator>
  122. void schedule_timer(timer_queue<TimeTraits, Allocator>& queue,
  123. const typename TimeTraits::time_type& time,
  124. typename timer_queue<TimeTraits, Allocator>::per_timer_data& timer,
  125. wait_op* op);
  126. // Cancel the timer operations associated with the given token. Returns the
  127. // number of operations that have been posted or dispatched.
  128. template <typename TimeTraits, typename Allocator>
  129. std::size_t cancel_timer(timer_queue<TimeTraits, Allocator>& queue,
  130. typename timer_queue<TimeTraits, Allocator>::per_timer_data& timer,
  131. std::size_t max_cancelled = (std::numeric_limits<std::size_t>::max)());
  132. // Cancel the timer operations associated with the given key.
  133. template <typename TimeTraits, typename Allocator>
  134. void cancel_timer_by_key(timer_queue<TimeTraits, Allocator>& queue,
  135. typename timer_queue<TimeTraits, Allocator>::per_timer_data* timer,
  136. void* cancellation_key);
  137. // Move the timer operations associated with the given timer.
  138. template <typename TimeTraits, typename Allocator>
  139. void move_timer(timer_queue<TimeTraits, Allocator>& queue,
  140. typename timer_queue<TimeTraits, Allocator>::per_timer_data& target,
  141. typename timer_queue<TimeTraits, Allocator>::per_timer_data& source);
  142. // Wait on io_uring once until interrupted or events are ready to be
  143. // dispatched.
  144. BOOST_ASIO_DECL void run(long usec, op_queue<operation>& ops);
  145. // Interrupt the io_uring wait.
  146. BOOST_ASIO_DECL void interrupt();
  147. private:
  148. // The hint to pass to io_uring_queue_init to size its data structures.
  149. enum { ring_size = 16384 };
  150. // The number of operations to submit in a batch.
  151. enum { submit_batch_size = 128 };
  152. // The number of operations to complete in a batch.
  153. enum { complete_batch_size = 128 };
  154. // The type used for processing eventfd readiness notifications.
  155. class event_fd_read_op;
  156. // Initialise the ring.
  157. BOOST_ASIO_DECL void init_ring();
  158. // Register the eventfd descriptor for readiness notifications.
  159. BOOST_ASIO_DECL void register_with_reactor();
  160. // Allocate a new I/O object.
  161. BOOST_ASIO_DECL io_object* allocate_io_object();
  162. // Free an existing I/O object.
  163. BOOST_ASIO_DECL void free_io_object(io_object* s);
  164. // Helper function to cancel all operations associated with the given I/O
  165. // object. This function must be called while the I/O object's mutex is held.
  166. // Returns true if there are operations for which cancellation is pending.
  167. BOOST_ASIO_DECL bool do_cancel_ops(
  168. per_io_object_data& io_obj, op_queue<operation>& ops);
  169. // Helper function to add a new timer queue.
  170. BOOST_ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
  171. // Helper function to remove a timer queue.
  172. BOOST_ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
  173. // Called to recalculate and update the timeout.
  174. BOOST_ASIO_DECL void update_timeout();
  175. // Get the current timeout value.
  176. BOOST_ASIO_DECL __kernel_timespec get_timeout() const;
  177. // Get a new submission queue entry, flushing the queue if necessary.
  178. BOOST_ASIO_DECL ::io_uring_sqe* get_sqe();
  179. // Submit pending submission queue entries.
  180. BOOST_ASIO_DECL void submit_sqes();
  181. // Post an operation to submit the pending submission queue entries.
  182. BOOST_ASIO_DECL void post_submit_sqes_op(mutex::scoped_lock& lock);
  183. // Push an operation to submit the pending submission queue entries.
  184. BOOST_ASIO_DECL void push_submit_sqes_op(op_queue<operation>& ops);
  185. // Helper operation to submit pending submission queue entries.
  186. class submit_sqes_op : operation
  187. {
  188. friend class io_uring_service;
  189. io_uring_service* service_;
  190. BOOST_ASIO_DECL submit_sqes_op(io_uring_service* s);
  191. BOOST_ASIO_DECL static void do_complete(void* owner, operation* base,
  192. const boost::system::error_code& ec, std::size_t bytes_transferred);
  193. };
  194. // The scheduler implementation used to post completions.
  195. scheduler& scheduler_;
  196. // Mutex to protect access to internal data.
  197. mutex mutex_;
  198. // The ring.
  199. ::io_uring ring_;
  200. // The count of unfinished work.
  201. atomic_count outstanding_work_;
  202. // The operation used to submit the pending submission queue entries.
  203. submit_sqes_op submit_sqes_op_;
  204. // The number of pending submission queue entries_.
  205. int pending_sqes_;
  206. // Whether there is a pending submission operation.
  207. bool pending_submit_sqes_op_;
  208. // Whether the service has been shut down.
  209. bool shutdown_;
  210. // Whether I/O locking is enabled.
  211. const bool io_locking_;
  212. // How any times to spin waiting for the I/O mutex.
  213. const int io_locking_spin_count_;
  214. // The timer queues.
  215. timer_queue_set timer_queues_;
  216. // The timespec for the pending timeout operation. Must remain valid while the
  217. // operation is outstanding.
  218. __kernel_timespec timeout_;
  219. // Mutex to protect access to the registered I/O objects.
  220. mutex registration_mutex_;
  221. // Keep track of all registered I/O objects.
  222. object_pool<io_object, execution_context::allocator<void>>
  223. registered_io_objects_;
  224. // Helper class to do post-perform_io cleanup.
  225. struct perform_io_cleanup_on_block_exit;
  226. friend struct perform_io_cleanup_on_block_exit;
  227. // The reactor used to register for eventfd readiness.
  228. reactor& reactor_;
  229. // The per-descriptor reactor data used for the eventfd.
  230. reactor::per_descriptor_data reactor_data_;
  231. // The eventfd descriptor used to wait for readiness.
  232. int event_fd_;
  233. };
  234. } // namespace detail
  235. } // namespace asio
  236. } // namespace boost
  237. #include <boost/asio/detail/pop_options.hpp>
  238. #include <boost/asio/detail/impl/io_uring_service.hpp>
  239. #if defined(BOOST_ASIO_HEADER_ONLY)
  240. # include <boost/asio/detail/impl/io_uring_service.ipp>
  241. #endif // defined(BOOST_ASIO_HEADER_ONLY)
  242. #endif // defined(BOOST_ASIO_HAS_IO_URING)
  243. #endif // BOOST_ASIO_DETAIL_IO_URING_SERVICE_HPP