I'm fairly new to boost::asio, but I'm working on a project that has already existed for a few years and uses asio extensively. My current assignment is to add periodic metrics about various things the system is doing. One of the metrics is to observe how deep the boost::asio::io_service work queues and timer queues become at an arbitrary period of runtime. So I need to be able to ask a boost:asio::io_service object how many things it has in its queues.
To illustrate what I'm asking, consider the following:
boost::asio::io_service asio_service;
asio_service.post( boost::bind( do_work, "eat" ) );
asio_service.post( boost::bind( do_work, "drink" ) );
asio_service.post( boost::bind( do_work, "and be merry!" ) );
std::cout << "There are " << asio_service.XXXX()
<< "things in the post() queue and "
<< asio_service.YYYY() << " timers"
Is there a way with boost asio to get equivalent functionality to what my "XXXX()" and "YYYY()" calls are expressing?
I looked at the asio timer queue code and saw that the queue is really just a vector and a list, but both are private. Since they are private, I cannot inherit to gain access, and I don't want to have to inherit or write some kind of odd-ball visitor pattern to wrap things up for this one pair of metrics: direct access to these counts would be ideal; special versions of boost that I hack-up to give me access would not be ideal: I'm looking for a way to do this that already exists in boost. Hopefully I'm not the first to ask for this.
You cannot get statistics about the io_service
queue without modifying the asio library directly. As you have noted, the container is private. The size of the queue is really not terribly important anyhow, since the performance or throughput depends on the completion handlers. What I've done in the past to solve something similar is to measure the time required to post a trivial handler to the io_service
void
Status::impl()
{
const boost::posix_time::ptime start = boost::posix_time::microsec_clock::local_time();
_io_service.post(
boost::bind(
&Status::loadHandler,
this,
start
)
);
}
void
Status::loadHandler(
const boost::posix_time::ptime& start,
)
{
// calculate duration spent in reactor queue
const boost::posix_time::ptime end = boost::posix_time::microsec_clock::local_time();
const boost::posix_time::time_duration load = end - start;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With