apache / brpc

brpc is an Industrial-grade RPC framework using C++ Language, which is often used in high performance system such as Search, Storage, Machine learning, Advertisement, Recommendation etc. "brpc" means "better RPC".
https://brpc.apache.org
Apache License 2.0
16.41k stars 3.96k forks source link

bthread_worker_usage这个统计值为何有时候会超时bthread #2640

Open zc08291161 opened 4 months ago

zc08291161 commented 4 months ago

Describe the bug (描述bug) 在brpc的统计页面上,很容易就发现bthread_worker_usage的值 超过bthread_worker_count,理论上根据代码,bthread_worker_usage 的值不会超过TaskGroup的个数,因为代码 double TaskControl::get_cumulated_worker_time() { int64_t cputime_ns = 0; BAIDU_SCOPED_LOCK(_modify_group_mutex); const size_t ngroup = _ngroup.load(butil::memory_order_relaxed); for (size_t i = 0; i < ngroup; ++i) { if (_groups[i]) { cputime_ns += _groups[i]->_cumulated_cputime_ns; } } return cputime_ns / 1000000000.0; }

这个函数是一秒钟统计一次,如果定时器完全准的话,一定不会超过ngroup的个数 因此这个值是否强依赖定时器,因而不一定完全准确,是否应该将 cputime_ns / 1000000000.0; 更改为 cputime_ns / (real_elapse_time);其中real_elapse_time是这个函数两次 调用的时间差。

To Reproduce (复现方法)

Expected behavior (期望行为)

Versions (各种版本) OS: Compiler: brpc: 1.0.0 protobuf:

Additional context/screenshots (更多上下文/截图) image

yanglimingcn commented 4 months ago

听上去这么修改更合理些,你能否简单改一下验证一下呢?

zc08291161 commented 4 months ago

ok,I will try

yanglimingcn commented 2 months ago

ok,I will try

这个有PR了吗?

lintianzhi commented 2 months ago

不仅仅是时间的误差 重计算的线程在计算的时候不会增加cputime_ns,但是结束的时候跨秒会把前面用掉的时间都加到当前秒的cputime_ns上,会导致超过很多