boost::asio::io_service 占用计时器和帖子的队列长度

发布于 2024-11-25 21:32:05 字数 878 浏览 0 评论 0原文

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

为了说明我的要求,请考虑以下内容:

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()”调用所表达的功能相同的功能?

我查看了asio计时器队列代码,发现队列实际上只是一个向量和一个列表,但两者都是私有的。由于它们是私有的,我无法继承来获得访问权限,并且我不想必须继承或编写某种奇怪的访问者模式来包装这一对指标:直接访问这些计数将是理想的;我破解的特殊版本的 boost 来给我访问并不理想:我正在寻找一种已经存在于 boost 中的方法来做到这一点。希望我不是第一个提出这个要求的人。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熟人话多 2024-12-02 21:32:05

如果不直接修改 asio 库,则无法获取有关 io_service 队列的统计信息。正如您所指出的,该容器是私有的。无论如何,队列的大小实际上并不是非常重要,因为性能或吞吐量取决于完成处理程序。我过去为解决类似问题所做的就是测量将一个简单的处理程序发布到 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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文