boost :: asio :: io_service占用计时器和帖子的队列长度 [英] boost::asio::io_service occupied queue lengths for timers and posts

查看:196
本文介绍了boost :: asio :: io_service占用计时器和帖子的队列长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对boost :: asio相当陌生,但是我正在从事一个已经存在了几年并且广泛使用asio的项目.我当前的任务是添加有关系统正在执行的各种操作的定期度量.指标之一是观察boost :: asio :: io_service工作队列和计时器队列在任意运行时间段内的深度.因此,我需要能够询问boost:asio :: io_service对象其队列中有多少东西.

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"

boost asio是否有办法获得与我的"XXXX()"和"YYYY()"调用所表达的功能相同的功能?

Is there a way with boost asio to get equivalent functionality to what my "XXXX()" and "YYYY()" calls are expressing?

我查看了asio计时器队列代码,发现该队列实际上只是一个向量和一个列表,但是两者都是私有的.由于它们是私有的,因此我无法继承来获取访问权限,也不想继承或编写某种奇怪的访客模式来为这对度量标准包装内容:直接访问这些计数将是理想的;我试图让我访问的Boost的特殊版本并不理想:我正在寻找一种方法来实现boost中已经存在的功能.希望我不是第一个要求这样做的人.

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.

推荐答案

如果不直接修改asio库,就无法获取有关io_service队列的统计信息.您已经注意到,该容器是私有的.无论如何,队列的大小实际上并不是非常重要,因为性能或吞吐量取决于完成处理程序.我过去为解决类似问题所做的工作是测量将琐碎的处理程序发布到io_service

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;
}

这篇关于boost :: asio :: io_service占用计时器和帖子的队列长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆