io_uring_service.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //
  2. // detail/io_uring_service.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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 ASIO_DETAIL_IO_URING_SERVICE_HPP
  11. #define 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 "asio/detail/config.hpp"
  16. #if defined(ASIO_HAS_IO_URING)
  17. #include <liburing.h>
  18. #include "asio/detail/atomic_count.hpp"
  19. #include "asio/detail/buffer_sequence_adapter.hpp"
  20. #include "asio/detail/conditionally_enabled_mutex.hpp"
  21. #include "asio/detail/io_uring_operation.hpp"
  22. #include "asio/detail/limits.hpp"
  23. #include "asio/detail/object_pool.hpp"
  24. #include "asio/detail/op_queue.hpp"
  25. #include "asio/detail/reactor.hpp"
  26. #include "asio/detail/scheduler_task.hpp"
  27. #include "asio/detail/timer_queue_base.hpp"
  28. #include "asio/detail/timer_queue_set.hpp"
  29. #include "asio/detail/wait_op.hpp"
  30. #include "asio/execution_context.hpp"
  31. #include "asio/detail/push_options.hpp"
  32. namespace asio {
  33. namespace detail {
  34. class io_uring_service
  35. : public execution_context_service_base<io_uring_service>,
  36. public scheduler_task
  37. {
  38. private:
  39. // The mutex type used by this reactor.
  40. typedef conditionally_enabled_mutex mutex;
  41. public:
  42. enum op_types { read_op = 0, write_op = 1, except_op = 2, max_ops = 3 };
  43. class io_object;
  44. // An I/O queue stores operations that must run serially.
  45. class io_queue : operation
  46. {
  47. friend class io_uring_service;
  48. io_object* io_object_;
  49. op_queue<io_uring_operation> op_queue_;
  50. bool cancel_requested_;
  51. ASIO_DECL io_queue();
  52. void set_result(int r) { task_result_ = static_cast<unsigned>(r); }
  53. ASIO_DECL operation* perform_io(int result);
  54. ASIO_DECL static void do_complete(void* owner, operation* base,
  55. const asio::error_code& ec, std::size_t bytes_transferred);
  56. };
  57. // Per I/O object state.
  58. class io_object
  59. {
  60. friend class io_uring_service;
  61. friend class object_pool_access;
  62. io_object* next_;
  63. io_object* prev_;
  64. mutex mutex_;
  65. io_uring_service* service_;
  66. io_queue queues_[max_ops];
  67. bool shutdown_;
  68. ASIO_DECL io_object(bool locking);
  69. };
  70. // Per I/O object data.
  71. typedef io_object* per_io_object_data;
  72. // Constructor.
  73. ASIO_DECL io_uring_service(asio::execution_context& ctx);
  74. // Destructor.
  75. ASIO_DECL ~io_uring_service();
  76. // Destroy all user-defined handler objects owned by the service.
  77. ASIO_DECL void shutdown();
  78. // Recreate internal state following a fork.
  79. ASIO_DECL void notify_fork(
  80. asio::execution_context::fork_event fork_ev);
  81. // Initialise the task.
  82. ASIO_DECL void init_task();
  83. // Register an I/O object with io_uring.
  84. ASIO_DECL void register_io_object(io_object*& io_obj);
  85. // Register an internal I/O object with io_uring.
  86. ASIO_DECL void register_internal_io_object(
  87. io_object*& io_obj, int op_type, io_uring_operation* op);
  88. // Register buffers with io_uring.
  89. ASIO_DECL void register_buffers(const ::iovec* v, unsigned n);
  90. // Unregister buffers from io_uring.
  91. ASIO_DECL void unregister_buffers();
  92. // Post an operation for immediate completion.
  93. void post_immediate_completion(operation* op, bool is_continuation);
  94. // Start a new operation. The operation will be prepared and submitted to the
  95. // io_uring when it is at the head of its I/O operation queue.
  96. ASIO_DECL void start_op(int op_type, per_io_object_data& io_obj,
  97. io_uring_operation* op, bool is_continuation);
  98. // Cancel all operations associated with the given I/O object. The handlers
  99. // associated with the I/O object will be invoked with the operation_aborted
  100. // error.
  101. ASIO_DECL void cancel_ops(per_io_object_data& io_obj);
  102. // Cancel all operations associated with the given I/O object and key. The
  103. // handlers associated with the object and key will be invoked with the
  104. // operation_aborted error.
  105. ASIO_DECL void cancel_ops_by_key(per_io_object_data& io_obj,
  106. int op_type, void* cancellation_key);
  107. // Cancel any operations that are running against the I/O object and remove
  108. // its registration from the service. The service resources associated with
  109. // the I/O object must be released by calling cleanup_io_object.
  110. ASIO_DECL void deregister_io_object(per_io_object_data& io_obj);
  111. // Perform any post-deregistration cleanup tasks associated with the I/O
  112. // object.
  113. ASIO_DECL void cleanup_io_object(per_io_object_data& io_obj);
  114. // Add a new timer queue to the reactor.
  115. template <typename Time_Traits>
  116. void add_timer_queue(timer_queue<Time_Traits>& timer_queue);
  117. // Remove a timer queue from the reactor.
  118. template <typename Time_Traits>
  119. void remove_timer_queue(timer_queue<Time_Traits>& timer_queue);
  120. // Schedule a new operation in the given timer queue to expire at the
  121. // specified absolute time.
  122. template <typename Time_Traits>
  123. void schedule_timer(timer_queue<Time_Traits>& queue,
  124. const typename Time_Traits::time_type& time,
  125. typename timer_queue<Time_Traits>::per_timer_data& timer, 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 Time_Traits>
  129. std::size_t cancel_timer(timer_queue<Time_Traits>& queue,
  130. typename timer_queue<Time_Traits>::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 Time_Traits>
  134. void cancel_timer_by_key(timer_queue<Time_Traits>& queue,
  135. typename timer_queue<Time_Traits>::per_timer_data* timer,
  136. void* cancellation_key);
  137. // Move the timer operations associated with the given timer.
  138. template <typename Time_Traits>
  139. void move_timer(timer_queue<Time_Traits>& queue,
  140. typename timer_queue<Time_Traits>::per_timer_data& target,
  141. typename timer_queue<Time_Traits>::per_timer_data& source);
  142. // Wait on io_uring once until interrupted or events are ready to be
  143. // dispatched.
  144. ASIO_DECL void run(long usec, op_queue<operation>& ops);
  145. // Interrupt the io_uring wait.
  146. 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. ASIO_DECL void init_ring();
  158. // Register the eventfd descriptor for readiness notifications.
  159. ASIO_DECL void register_with_reactor();
  160. // Allocate a new I/O object.
  161. ASIO_DECL io_object* allocate_io_object();
  162. // Free an existing I/O object.
  163. 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. 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. ASIO_DECL void do_add_timer_queue(timer_queue_base& queue);
  171. // Helper function to remove a timer queue.
  172. ASIO_DECL void do_remove_timer_queue(timer_queue_base& queue);
  173. // Called to recalculate and update the timeout.
  174. ASIO_DECL void update_timeout();
  175. // Get the current timeout value.
  176. ASIO_DECL __kernel_timespec get_timeout() const;
  177. // Get a new submission queue entry, flushing the queue if necessary.
  178. ASIO_DECL ::io_uring_sqe* get_sqe();
  179. // Submit pending submission queue entries.
  180. ASIO_DECL void submit_sqes();
  181. // Post an operation to submit the pending submission queue entries.
  182. ASIO_DECL void post_submit_sqes_op(mutex::scoped_lock& lock);
  183. // Push an operation to submit the pending submission queue entries.
  184. 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. ASIO_DECL submit_sqes_op(io_uring_service* s);
  191. ASIO_DECL static void do_complete(void* owner, operation* base,
  192. const asio::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. // The timer queues.
  211. timer_queue_set timer_queues_;
  212. // The timespec for the pending timeout operation. Must remain valid while the
  213. // operation is outstanding.
  214. __kernel_timespec timeout_;
  215. // Mutex to protect access to the registered I/O objects.
  216. mutex registration_mutex_;
  217. // Keep track of all registered I/O objects.
  218. object_pool<io_object> registered_io_objects_;
  219. // Helper class to do post-perform_io cleanup.
  220. struct perform_io_cleanup_on_block_exit;
  221. friend struct perform_io_cleanup_on_block_exit;
  222. // The reactor used to register for eventfd readiness.
  223. reactor& reactor_;
  224. // The per-descriptor reactor data used for the eventfd.
  225. reactor::per_descriptor_data reactor_data_;
  226. // The eventfd descriptor used to wait for readiness.
  227. int event_fd_;
  228. };
  229. } // namespace detail
  230. } // namespace asio
  231. #include "asio/detail/pop_options.hpp"
  232. #include "asio/detail/impl/io_uring_service.hpp"
  233. #if defined(ASIO_HEADER_ONLY)
  234. # include "asio/detail/impl/io_uring_service.ipp"
  235. #endif // defined(ASIO_HEADER_ONLY)
  236. #endif // defined(ASIO_HAS_IO_URING)
  237. #endif // ASIO_DETAIL_IO_URING_SERVICE_HPP