Extend handler tracking to cover reactor-related events.
This commit is contained in:
parent
b2e85caeb3
commit
5bb7a7419a
@ -44,8 +44,18 @@ namespace detail {
|
||||
// - ASIO_HANDLER_INVOCATION_BEGIN(args)
|
||||
// - ASIO_HANDLER_INVOCATION_END
|
||||
// - ASIO_HANDLER_OPERATION(args)
|
||||
// - ASIO_HANDLER_REACTOR_REGISTRATION(args)
|
||||
// - ASIO_HANDLER_REACTOR_DEREGISTRATION(args)
|
||||
// - ASIO_HANDLER_REACTOR_READ_EVENT
|
||||
// - ASIO_HANDLER_REACTOR_WRITE_EVENT
|
||||
// - ASIO_HANDLER_REACTOR_ERROR_EVENT
|
||||
// - ASIO_HANDLER_REACTOR_EVENTS(args)
|
||||
// - ASIO_HANDLER_REACTOR_OPERATION(args)
|
||||
|
||||
# if !defined(ASIO_ENABLE_HANDLER_TRACKING)
|
||||
# define ASIO_ENABLE_HANDLER_TRACKING 1
|
||||
# endif /// !defined(ASIO_ENABLE_HANDLER_TRACKING)
|
||||
|
||||
#elif defined(ASIO_ENABLE_HANDLER_TRACKING)
|
||||
|
||||
class handler_tracking
|
||||
@ -122,6 +132,18 @@ public:
|
||||
const char* object_type, void* object,
|
||||
uintmax_t native_handle, const char* op_name);
|
||||
|
||||
// Record that a descriptor has been registered with the reactor.
|
||||
ASIO_DECL static void reactor_registration(execution_context& context,
|
||||
uintmax_t native_handle, uintmax_t registration);
|
||||
|
||||
// Record that a descriptor has been deregistered from the reactor.
|
||||
ASIO_DECL static void reactor_deregistration(execution_context& context,
|
||||
uintmax_t native_handle, uintmax_t registration);
|
||||
|
||||
// Record a reactor-based operation that is associated with a handler.
|
||||
ASIO_DECL static void reactor_events(execution_context& context,
|
||||
uintmax_t registration, unsigned events);
|
||||
|
||||
// Record a reactor-based operation that is associated with a handler.
|
||||
ASIO_DECL static void reactor_operation(
|
||||
const tracked_handler& h, const char* op_name,
|
||||
@ -164,6 +186,19 @@ private:
|
||||
# define ASIO_HANDLER_OPERATION(args) \
|
||||
asio::detail::handler_tracking::operation args
|
||||
|
||||
# define ASIO_HANDLER_REACTOR_REGISTRATION(args) \
|
||||
asio::detail::handler_tracking::reactor_registration args
|
||||
|
||||
# define ASIO_HANDLER_REACTOR_DEREGISTRATION(args) \
|
||||
asio::detail::handler_tracking::reactor_deregistration args
|
||||
|
||||
# define ASIO_HANDLER_REACTOR_READ_EVENT 1
|
||||
# define ASIO_HANDLER_REACTOR_WRITE_EVENT 2
|
||||
# define ASIO_HANDLER_REACTOR_ERROR_EVENT 4
|
||||
|
||||
# define ASIO_HANDLER_REACTOR_EVENTS(args) \
|
||||
asio::detail::handler_tracking::reactor_events args
|
||||
|
||||
# define ASIO_HANDLER_REACTOR_OPERATION(args) \
|
||||
asio::detail::handler_tracking::reactor_operation args
|
||||
|
||||
@ -177,6 +212,12 @@ private:
|
||||
# define ASIO_HANDLER_INVOCATION_BEGIN(args) (void)0
|
||||
# define ASIO_HANDLER_INVOCATION_END (void)0
|
||||
# define ASIO_HANDLER_OPERATION(args) (void)0
|
||||
# define ASIO_HANDLER_REACTOR_REGISTRATION(args) (void)0
|
||||
# define ASIO_HANDLER_REACTOR_DEREGISTRATION(args) (void)0
|
||||
# define ASIO_HANDLER_REACTOR_READ_EVENT 0
|
||||
# define ASIO_HANDLER_REACTOR_WRITE_EVENT 0
|
||||
# define ASIO_HANDLER_REACTOR_ERROR_EVENT 0
|
||||
# define ASIO_HANDLER_REACTOR_EVENTS(args) (void)0
|
||||
# define ASIO_HANDLER_REACTOR_OPERATION(args) (void)0
|
||||
|
||||
#endif // defined(ASIO_ENABLE_HANDLER_TRACKING)
|
||||
|
@ -150,6 +150,10 @@ int epoll_reactor::register_descriptor(socket_type descriptor,
|
||||
{
|
||||
descriptor_data = allocate_descriptor_state();
|
||||
|
||||
ASIO_HANDLER_REACTOR_REGISTRATION((
|
||||
context(), static_cast<uintmax_t>(descriptor),
|
||||
reinterpret_cast<uintmax_t>(descriptor_data)));
|
||||
|
||||
{
|
||||
mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
|
||||
|
||||
@ -175,6 +179,10 @@ int epoll_reactor::register_internal_descriptor(
|
||||
{
|
||||
descriptor_data = allocate_descriptor_state();
|
||||
|
||||
ASIO_HANDLER_REACTOR_REGISTRATION((
|
||||
context(), static_cast<uintmax_t>(descriptor),
|
||||
reinterpret_cast<uintmax_t>(descriptor_data)));
|
||||
|
||||
{
|
||||
mutex::scoped_lock descriptor_lock(descriptor_data->mutex_);
|
||||
|
||||
@ -335,6 +343,10 @@ void epoll_reactor::deregister_descriptor(socket_type descriptor,
|
||||
|
||||
descriptor_lock.unlock();
|
||||
|
||||
ASIO_HANDLER_REACTOR_DEREGISTRATION((
|
||||
context(), static_cast<uintmax_t>(descriptor),
|
||||
reinterpret_cast<uintmax_t>(descriptor_data)));
|
||||
|
||||
free_descriptor_state(descriptor_data);
|
||||
descriptor_data = 0;
|
||||
|
||||
@ -364,6 +376,10 @@ void epoll_reactor::deregister_internal_descriptor(socket_type descriptor,
|
||||
|
||||
descriptor_lock.unlock();
|
||||
|
||||
ASIO_HANDLER_REACTOR_DEREGISTRATION((
|
||||
context(), static_cast<uintmax_t>(descriptor),
|
||||
reinterpret_cast<uintmax_t>(descriptor_data)));
|
||||
|
||||
free_descriptor_state(descriptor_data);
|
||||
descriptor_data = 0;
|
||||
}
|
||||
@ -391,6 +407,35 @@ void epoll_reactor::run(bool block, op_queue<operation>& ops)
|
||||
epoll_event events[128];
|
||||
int num_events = epoll_wait(epoll_fd_, events, 128, timeout);
|
||||
|
||||
#if defined(ASIO_ENABLE_HANDLER_TRACKING)
|
||||
// Trace the waiting events.
|
||||
for (int i = 0; i < num_events; ++i)
|
||||
{
|
||||
void* ptr = events[i].data.ptr;
|
||||
if (ptr == &interrupter_)
|
||||
{
|
||||
// Ignore.
|
||||
}
|
||||
# if defined(ASIO_HAS_TIMERFD)
|
||||
else if (ptr == &timer_fd_)
|
||||
{
|
||||
// Ignore.
|
||||
}
|
||||
# endif // defined(ASIO_HAS_TIMERFD)
|
||||
{
|
||||
unsigned event_mask = 0;
|
||||
if ((events[i].events & EPOLLIN) != 0)
|
||||
event_mask |= ASIO_HANDLER_REACTOR_READ_EVENT;
|
||||
if ((events[i].events & EPOLLOUT))
|
||||
event_mask |= ASIO_HANDLER_REACTOR_WRITE_EVENT;
|
||||
if ((events[i].events & (EPOLLERR | EPOLLHUP)) != 0)
|
||||
event_mask |= ASIO_HANDLER_REACTOR_ERROR_EVENT;
|
||||
ASIO_HANDLER_REACTOR_EVENTS((context(),
|
||||
reinterpret_cast<uintmax_t>(ptr), event_mask));
|
||||
}
|
||||
}
|
||||
#endif // defined(ASIO_ENABLE_HANDLER_TRACKING)
|
||||
|
||||
#if defined(ASIO_HAS_TIMERFD)
|
||||
bool check_timers = (timer_fd_ == -1);
|
||||
#else // defined(ASIO_HAS_TIMERFD)
|
||||
|
@ -280,6 +280,21 @@ void handler_tracking::operation(execution_context&,
|
||||
current_id, object_type, object, op_name);
|
||||
}
|
||||
|
||||
void handler_tracking::reactor_registration(execution_context& /*context*/,
|
||||
uintmax_t /*native_handle*/, uintmax_t /*registration*/)
|
||||
{
|
||||
}
|
||||
|
||||
void handler_tracking::reactor_deregistration(execution_context& /*context*/,
|
||||
uintmax_t /*native_handle*/, uintmax_t /*registration*/)
|
||||
{
|
||||
}
|
||||
|
||||
void handler_tracking::reactor_events(execution_context& /*context*/,
|
||||
uintmax_t /*native_handle*/, unsigned /*events*/)
|
||||
{
|
||||
}
|
||||
|
||||
void handler_tracking::reactor_operation(
|
||||
const tracked_handler& h, const char* op_name,
|
||||
const asio::error_code& ec)
|
||||
|
@ -137,6 +137,10 @@ int kqueue_reactor::register_descriptor(socket_type descriptor,
|
||||
{
|
||||
descriptor_data = allocate_descriptor_state();
|
||||
|
||||
ASIO_HANDLER_REACTOR_REGISTRATION((
|
||||
context(), static_cast<uintmax_t>(descriptor),
|
||||
reinterpret_cast<uintmax_t>(descriptor_data)));
|
||||
|
||||
mutex::scoped_lock lock(descriptor_data->mutex_);
|
||||
|
||||
descriptor_data->descriptor_ = descriptor;
|
||||
@ -152,6 +156,10 @@ int kqueue_reactor::register_internal_descriptor(
|
||||
{
|
||||
descriptor_data = allocate_descriptor_state();
|
||||
|
||||
ASIO_HANDLER_REACTOR_REGISTRATION((
|
||||
context(), static_cast<uintmax_t>(descriptor),
|
||||
reinterpret_cast<uintmax_t>(descriptor_data)));
|
||||
|
||||
mutex::scoped_lock lock(descriptor_data->mutex_);
|
||||
|
||||
descriptor_data->descriptor_ = descriptor;
|
||||
@ -313,6 +321,10 @@ void kqueue_reactor::deregister_descriptor(socket_type descriptor,
|
||||
|
||||
descriptor_lock.unlock();
|
||||
|
||||
ASIO_HANDLER_REACTOR_DEREGISTRATION((
|
||||
context(), static_cast<uintmax_t>(descriptor),
|
||||
reinterpret_cast<uintmax_t>(descriptor_data)));
|
||||
|
||||
free_descriptor_state(descriptor_data);
|
||||
descriptor_data = 0;
|
||||
|
||||
@ -346,6 +358,10 @@ void kqueue_reactor::deregister_internal_descriptor(socket_type descriptor,
|
||||
|
||||
descriptor_lock.unlock();
|
||||
|
||||
ASIO_HANDLER_REACTOR_DEREGISTRATION((
|
||||
context(), static_cast<uintmax_t>(descriptor),
|
||||
reinterpret_cast<uintmax_t>(descriptor_data)));
|
||||
|
||||
free_descriptor_state(descriptor_data);
|
||||
descriptor_data = 0;
|
||||
}
|
||||
@ -365,6 +381,31 @@ void kqueue_reactor::run(bool block, op_queue<operation>& ops)
|
||||
struct kevent events[128];
|
||||
int num_events = kevent(kqueue_fd_, 0, 0, events, 128, timeout);
|
||||
|
||||
#if defined(ASIO_ENABLE_HANDLER_TRACKING)
|
||||
// Trace the waiting events.
|
||||
for (int i = 0; i < num_events; ++i)
|
||||
{
|
||||
void* ptr = reinterpret_cast<void*>(events[i].udata);
|
||||
if (ptr != &interrupter_)
|
||||
{
|
||||
unsigned event_mask = 0;
|
||||
switch (events[i].filter)
|
||||
{
|
||||
case EVFILT_READ:
|
||||
event_mask |= ASIO_HANDLER_REACTOR_READ_EVENT;
|
||||
break;
|
||||
case EVFILT_WRITE:
|
||||
event_mask |= ASIO_HANDLER_REACTOR_WRITE_EVENT;
|
||||
break;
|
||||
}
|
||||
if ((events[i].flags & (EV_ERROR | EV_OOBAND)) != 0)
|
||||
event_mask |= ASIO_HANDLER_REACTOR_ERROR_EVENT;
|
||||
ASIO_HANDLER_REACTOR_EVENTS((context(),
|
||||
reinterpret_cast<uintmax_t>(ptr), event_mask));
|
||||
}
|
||||
}
|
||||
#endif // defined(ASIO_ENABLE_HANDLER_TRACKING)
|
||||
|
||||
// Dispatch the waiting events.
|
||||
for (int i = 0; i < num_events; ++i)
|
||||
{
|
||||
|
@ -77,6 +77,7 @@ spawn_parallel_grep_LDADD = $(LDADD) -lboost_coroutine -lboost_context -lboost_s
|
||||
endif
|
||||
|
||||
EXTRA_DIST = \
|
||||
handler_tracking/custom_tracking.hpp \
|
||||
http/server/connection.hpp \
|
||||
http/server/connection_manager.hpp \
|
||||
http/server/header.hpp \
|
||||
|
201
asio/src/examples/cpp11/handler_tracking/custom_tracking.hpp
Normal file
201
asio/src/examples/cpp11/handler_tracking/custom_tracking.hpp
Normal file
@ -0,0 +1,201 @@
|
||||
//
|
||||
// custom_tracking.hpp
|
||||
// ~~~~~~~~~~~~~~~~~~~
|
||||
//
|
||||
// Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
//
|
||||
|
||||
#ifndef CUSTOM_TRACKING_HPP
|
||||
#define CUSTOM_TRACKING_HPP
|
||||
|
||||
#include <cinttypes>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
|
||||
# define ASIO_INHERIT_TRACKED_HANDLER \
|
||||
: public ::custom_tracking::tracked_handler
|
||||
|
||||
# define ASIO_ALSO_INHERIT_TRACKED_HANDLER \
|
||||
, public ::custom_tracking::tracked_handler
|
||||
|
||||
# define ASIO_HANDLER_TRACKING_INIT \
|
||||
::custom_tracking::init()
|
||||
|
||||
# define ASIO_HANDLER_CREATION(args) \
|
||||
::custom_tracking::creation args
|
||||
|
||||
# define ASIO_HANDLER_COMPLETION(args) \
|
||||
::custom_tracking::completion tracked_completion args
|
||||
|
||||
# define ASIO_HANDLER_INVOCATION_BEGIN(args) \
|
||||
tracked_completion.invocation_begin args
|
||||
|
||||
# define ASIO_HANDLER_INVOCATION_END \
|
||||
tracked_completion.invocation_end()
|
||||
|
||||
# define ASIO_HANDLER_OPERATION(args) \
|
||||
::custom_tracking::operation args
|
||||
|
||||
# define ASIO_HANDLER_REACTOR_REGISTRATION(args) \
|
||||
::custom_tracking::reactor_registration args
|
||||
|
||||
# define ASIO_HANDLER_REACTOR_DEREGISTRATION(args) \
|
||||
::custom_tracking::reactor_deregistration args
|
||||
|
||||
# define ASIO_HANDLER_REACTOR_READ_EVENT 1
|
||||
# define ASIO_HANDLER_REACTOR_WRITE_EVENT 2
|
||||
# define ASIO_HANDLER_REACTOR_ERROR_EVENT 4
|
||||
|
||||
# define ASIO_HANDLER_REACTOR_EVENTS(args) \
|
||||
::custom_tracking::reactor_events args
|
||||
|
||||
# define ASIO_HANDLER_REACTOR_OPERATION(args) \
|
||||
::custom_tracking::reactor_operation args
|
||||
|
||||
struct custom_tracking
|
||||
{
|
||||
// Base class for objects containing tracked handlers.
|
||||
struct tracked_handler
|
||||
{
|
||||
std::uintmax_t handler_id_ = 0; // To uniquely identify a handler.
|
||||
std::uintmax_t tree_id_ = 0; // To identify related handlers.
|
||||
const char* object_type_; // The object type associated with the handler.
|
||||
std::uintmax_t native_handle_; // Native handle, if any.
|
||||
};
|
||||
|
||||
// Initialise the tracking system.
|
||||
static void init()
|
||||
{
|
||||
}
|
||||
|
||||
// Record the creation of a tracked handler.
|
||||
static void creation(asio::execution_context& /*ctx*/, tracked_handler& h,
|
||||
const char* object_type, void* /*object*/,
|
||||
std::uintmax_t native_handle, const char* op_name)
|
||||
{
|
||||
// Generate a unique id for the new handler.
|
||||
static std::atomic<std::uintmax_t> next_handler_id{1};
|
||||
h.handler_id_ = next_handler_id++;
|
||||
|
||||
// Copy the tree identifier forward from the current handler.
|
||||
if (*current_completion())
|
||||
h.tree_id_ = (*current_completion())->handler_.tree_id_;
|
||||
|
||||
// Store various attributes of the operation to use in later output.
|
||||
h.object_type_ = object_type;
|
||||
h.native_handle_ = native_handle;
|
||||
|
||||
std::printf(
|
||||
"Starting operation %s.%s for native_handle = %" PRIuMAX
|
||||
", handler = %" PRIuMAX ", tree = %" PRIuMAX "\n",
|
||||
object_type, op_name, h.native_handle_, h.handler_id_, h.tree_id_);
|
||||
}
|
||||
|
||||
struct completion
|
||||
{
|
||||
explicit completion(const tracked_handler& h)
|
||||
: handler_(h),
|
||||
next_(*current_completion())
|
||||
{
|
||||
*current_completion() = this;
|
||||
}
|
||||
|
||||
completion(const completion&) = delete;
|
||||
completion& operator=(const completion&) = delete;
|
||||
|
||||
// Destructor records only when an exception is thrown from the handler, or
|
||||
// if the memory is being freed without the handler having been invoked.
|
||||
~completion()
|
||||
{
|
||||
*current_completion() = next_;
|
||||
}
|
||||
|
||||
// Records that handler is to be invoked with the specified arguments.
|
||||
template <class... Args>
|
||||
void invocation_begin(Args&&... /*args*/)
|
||||
{
|
||||
std::printf("Entering handler %" PRIuMAX " in tree %" PRIuMAX "\n",
|
||||
handler_.handler_id_, handler_.tree_id_);
|
||||
}
|
||||
|
||||
// Record that handler invocation has ended.
|
||||
void invocation_end()
|
||||
{
|
||||
std::printf("Leaving handler %" PRIuMAX " in tree %" PRIuMAX "\n",
|
||||
handler_.handler_id_, handler_.tree_id_);
|
||||
}
|
||||
|
||||
tracked_handler handler_;
|
||||
|
||||
// Completions may nest. Here we stash a pointer to the outer completion.
|
||||
completion* next_;
|
||||
};
|
||||
|
||||
static completion** current_completion()
|
||||
{
|
||||
static ASIO_THREAD_KEYWORD completion* current = nullptr;
|
||||
return ¤t;
|
||||
}
|
||||
|
||||
// Record an operation that is not directly associated with a handler.
|
||||
static void operation(asio::execution_context& /*ctx*/,
|
||||
const char* /*object_type*/, void* /*object*/,
|
||||
std::uintmax_t /*native_handle*/, const char* /*op_name*/)
|
||||
{
|
||||
}
|
||||
|
||||
// Record that a descriptor has been registered with the reactor.
|
||||
static void reactor_registration(asio::execution_context& context,
|
||||
uintmax_t native_handle, uintmax_t registration)
|
||||
{
|
||||
std::printf("Adding to reactor native_handle = %" PRIuMAX
|
||||
", registration = %" PRIuMAX "\n", native_handle, registration);
|
||||
}
|
||||
|
||||
// Record that a descriptor has been deregistered from the reactor.
|
||||
static void reactor_deregistration(asio::execution_context& context,
|
||||
uintmax_t native_handle, uintmax_t registration)
|
||||
{
|
||||
std::printf("Removing from reactor native_handle = %" PRIuMAX
|
||||
", registration = %" PRIuMAX "\n", native_handle, registration);
|
||||
}
|
||||
|
||||
// Record reactor-based readiness events associated with a descriptor.
|
||||
static void reactor_events(asio::execution_context& context,
|
||||
uintmax_t registration, unsigned events)
|
||||
{
|
||||
std::printf(
|
||||
"Reactor readiness for registration = %" PRIuMAX ", events =%s%s%s\n",
|
||||
registration,
|
||||
(events & ASIO_HANDLER_REACTOR_READ_EVENT) ? " read" : "",
|
||||
(events & ASIO_HANDLER_REACTOR_WRITE_EVENT) ? " write" : "",
|
||||
(events & ASIO_HANDLER_REACTOR_ERROR_EVENT) ? " error" : "");
|
||||
}
|
||||
|
||||
// Record a reactor-based operation that is associated with a handler.
|
||||
static void reactor_operation(const tracked_handler& h,
|
||||
const char* op_name, const asio::error_code& ec)
|
||||
{
|
||||
std::printf(
|
||||
"Performed operation %s.%s for native_handle = %" PRIuMAX
|
||||
", ec = %s:%d\n", h.object_type_, op_name, h.native_handle_,
|
||||
ec.category().name(), ec.value());
|
||||
}
|
||||
|
||||
// Record a reactor-based operation that is associated with a handler.
|
||||
static void reactor_operation(const tracked_handler& h,
|
||||
const char* op_name, const asio::error_code& ec,
|
||||
std::size_t bytes_transferred)
|
||||
{
|
||||
std::printf(
|
||||
"Performed operation %s.%s for native_handle = %" PRIuMAX
|
||||
", ec = %s:%d, n = %" PRIuMAX "\n", h.object_type_, op_name,
|
||||
h.native_handle_, ec.category().name(), ec.value(),
|
||||
static_cast<uintmax_t>(bytes_transferred));
|
||||
}
|
||||
};
|
||||
|
||||
#endif // CUSTOM_TRACKING_HPP
|
Loading…
Reference in New Issue
Block a user