custom_tracking.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // custom_tracking.hpp
  3. // ~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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 CUSTOM_TRACKING_HPP
  11. #define CUSTOM_TRACKING_HPP
  12. #include <cinttypes>
  13. #include <cstdint>
  14. #include <cstdio>
  15. # define BOOST_ASIO_INHERIT_TRACKED_HANDLER \
  16. : public ::custom_tracking::tracked_handler
  17. # define BOOST_ASIO_ALSO_INHERIT_TRACKED_HANDLER \
  18. , public ::custom_tracking::tracked_handler
  19. # define BOOST_ASIO_HANDLER_TRACKING_INIT \
  20. ::custom_tracking::init()
  21. # define BOOST_ASIO_HANDLER_CREATION(args) \
  22. ::custom_tracking::creation args
  23. # define BOOST_ASIO_HANDLER_COMPLETION(args) \
  24. ::custom_tracking::completion tracked_completion args
  25. # define BOOST_ASIO_HANDLER_INVOCATION_BEGIN(args) \
  26. tracked_completion.invocation_begin args
  27. # define BOOST_ASIO_HANDLER_INVOCATION_END \
  28. tracked_completion.invocation_end()
  29. # define BOOST_ASIO_HANDLER_OPERATION(args) \
  30. ::custom_tracking::operation args
  31. # define BOOST_ASIO_HANDLER_REACTOR_REGISTRATION(args) \
  32. ::custom_tracking::reactor_registration args
  33. # define BOOST_ASIO_HANDLER_REACTOR_DEREGISTRATION(args) \
  34. ::custom_tracking::reactor_deregistration args
  35. # define BOOST_ASIO_HANDLER_REACTOR_READ_EVENT 1
  36. # define BOOST_ASIO_HANDLER_REACTOR_WRITE_EVENT 2
  37. # define BOOST_ASIO_HANDLER_REACTOR_ERROR_EVENT 4
  38. # define BOOST_ASIO_HANDLER_REACTOR_EVENTS(args) \
  39. ::custom_tracking::reactor_events args
  40. # define BOOST_ASIO_HANDLER_REACTOR_OPERATION(args) \
  41. ::custom_tracking::reactor_operation args
  42. struct custom_tracking
  43. {
  44. // Base class for objects containing tracked handlers.
  45. struct tracked_handler
  46. {
  47. std::uintmax_t handler_id_ = 0; // To uniquely identify a handler.
  48. std::uintmax_t tree_id_ = 0; // To identify related handlers.
  49. const char* object_type_; // The object type associated with the handler.
  50. std::uintmax_t native_handle_; // Native handle, if any.
  51. };
  52. // Initialise the tracking system.
  53. static void init()
  54. {
  55. }
  56. // Record the creation of a tracked handler.
  57. static void creation(boost::asio::execution_context& /*ctx*/,
  58. tracked_handler& h, const char* object_type, void* /*object*/,
  59. std::uintmax_t native_handle, const char* op_name)
  60. {
  61. // Generate a unique id for the new handler.
  62. static std::atomic<std::uintmax_t> next_handler_id{1};
  63. h.handler_id_ = next_handler_id++;
  64. // Copy the tree identifier forward from the current handler.
  65. if (*current_completion())
  66. h.tree_id_ = (*current_completion())->handler_.tree_id_;
  67. // Store various attributes of the operation to use in later output.
  68. h.object_type_ = object_type;
  69. h.native_handle_ = native_handle;
  70. std::printf(
  71. "Starting operation %s.%s for native_handle = %" PRIuMAX
  72. ", handler = %" PRIuMAX ", tree = %" PRIuMAX "\n",
  73. object_type, op_name, h.native_handle_, h.handler_id_, h.tree_id_);
  74. }
  75. struct completion
  76. {
  77. explicit completion(const tracked_handler& h)
  78. : handler_(h),
  79. next_(*current_completion())
  80. {
  81. *current_completion() = this;
  82. }
  83. completion(const completion&) = delete;
  84. completion& operator=(const completion&) = delete;
  85. // Destructor records only when an exception is thrown from the handler, or
  86. // if the memory is being freed without the handler having been invoked.
  87. ~completion()
  88. {
  89. *current_completion() = next_;
  90. }
  91. // Records that handler is to be invoked with the specified arguments.
  92. template <class... Args>
  93. void invocation_begin(Args&&... /*args*/)
  94. {
  95. std::printf("Entering handler %" PRIuMAX " in tree %" PRIuMAX "\n",
  96. handler_.handler_id_, handler_.tree_id_);
  97. }
  98. // Record that handler invocation has ended.
  99. void invocation_end()
  100. {
  101. std::printf("Leaving handler %" PRIuMAX " in tree %" PRIuMAX "\n",
  102. handler_.handler_id_, handler_.tree_id_);
  103. }
  104. tracked_handler handler_;
  105. // Completions may nest. Here we stash a pointer to the outer completion.
  106. completion* next_;
  107. };
  108. static completion** current_completion()
  109. {
  110. static BOOST_ASIO_THREAD_KEYWORD completion* current = nullptr;
  111. return &current;
  112. }
  113. // Record an operation that is not directly associated with a handler.
  114. static void operation(boost::asio::execution_context& /*ctx*/,
  115. const char* /*object_type*/, void* /*object*/,
  116. std::uintmax_t /*native_handle*/, const char* /*op_name*/)
  117. {
  118. }
  119. // Record that a descriptor has been registered with the reactor.
  120. static void reactor_registration(boost::asio::execution_context& context,
  121. uintmax_t native_handle, uintmax_t registration)
  122. {
  123. std::printf("Adding to reactor native_handle = %" PRIuMAX
  124. ", registration = %" PRIuMAX "\n", native_handle, registration);
  125. }
  126. // Record that a descriptor has been deregistered from the reactor.
  127. static void reactor_deregistration(boost::asio::execution_context& context,
  128. uintmax_t native_handle, uintmax_t registration)
  129. {
  130. std::printf("Removing from reactor native_handle = %" PRIuMAX
  131. ", registration = %" PRIuMAX "\n", native_handle, registration);
  132. }
  133. // Record reactor-based readiness events associated with a descriptor.
  134. static void reactor_events(boost::asio::execution_context& context,
  135. uintmax_t registration, unsigned events)
  136. {
  137. std::printf(
  138. "Reactor readiness for registration = %" PRIuMAX ", events =%s%s%s\n",
  139. registration,
  140. (events & BOOST_ASIO_HANDLER_REACTOR_READ_EVENT) ? " read" : "",
  141. (events & BOOST_ASIO_HANDLER_REACTOR_WRITE_EVENT) ? " write" : "",
  142. (events & BOOST_ASIO_HANDLER_REACTOR_ERROR_EVENT) ? " error" : "");
  143. }
  144. // Record a reactor-based operation that is associated with a handler.
  145. static void reactor_operation(const tracked_handler& h,
  146. const char* op_name, const boost::system::error_code& ec)
  147. {
  148. std::printf(
  149. "Performed operation %s.%s for native_handle = %" PRIuMAX
  150. ", ec = %s:%d\n", h.object_type_, op_name, h.native_handle_,
  151. ec.category().name(), ec.value());
  152. }
  153. // Record a reactor-based operation that is associated with a handler.
  154. static void reactor_operation(const tracked_handler& h,
  155. const char* op_name, const boost::system::error_code& ec,
  156. std::size_t bytes_transferred)
  157. {
  158. std::printf(
  159. "Performed operation %s.%s for native_handle = %" PRIuMAX
  160. ", ec = %s:%d, n = %" PRIuMAX "\n", h.object_type_, op_name,
  161. h.native_handle_, ec.category().name(), ec.value(),
  162. static_cast<uintmax_t>(bytes_transferred));
  163. }
  164. };
  165. #endif // CUSTOM_TRACKING_HPP