Skip to content

Commit

Permalink
Extend handler tracking to cover reactor-related events.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskohlhoff committed Mar 31, 2015
1 parent b2e85ca commit 5bb7a74
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 0 deletions.
41 changes: 41 additions & 0 deletions asio/include/asio/detail/handler_tracking.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -122,6 +132,18 @@ class handler_tracking
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,
Expand Down Expand Up @@ -164,6 +186,19 @@ class handler_tracking
# 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

Expand All @@ -177,6 +212,12 @@ class handler_tracking
# 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)
Expand Down
45 changes: 45 additions & 0 deletions asio/include/asio/detail/impl/epoll_reactor.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -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_);

Expand All @@ -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_);

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions asio/include/asio/detail/impl/handler_tracking.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 41 additions & 0 deletions asio/include/asio/detail/impl/kqueue_reactor.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand All @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions asio/src/examples/cpp11/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
Loading

0 comments on commit 5bb7a74

Please sign in to comment.