gitccl commented on issue #2640:
URL: https://github.com/apache/brpc/issues/2640#issuecomment-2980336773

   I agree that accurately computing `bthread_worker_usage` within the bthread 
framework is quite challenging, especially since some tasks may run for 
extended periods without yielding. Rather than striving for exact precision, we 
could use a bounded estimation to ensure that `bthread_worker_usage` not 
exceeds `bthread_worker_count`.
   
   Here’s a possible solution:
   
   * For each `TaskGroup`, add:
   
     * `_cur_cumulated_worker_time_ns`: stores the cumulative worker time as 
observed by bvar.
     * `_last_cumulated_cputime_ns`: stores the value of 
`TaskGroup::_cumulated_cputime_ns` at the time of the last observation.
   
   * For `TaskControl`, add:
   
     * `_last_observation_time`: records the timestamp of the last observation. 
   
   Updated `get_cumulated_worker_time()` Implementation:
   
   ```cpp
   double TaskControl::get_cumulated_worker_time() {
       int64_t cputime_ns = 0;
       BAIDU_SCOPED_LOCK(_modify_group_mutex);
       int64_t now = butil::cpuwide_time_ns();
       for_each_task_group([&](TaskGroup* g) {
           if (g) {
               int64_t cur_cumulated_cputime_ns = g->_cumulated_cputime_ns;
               g->_cur_cumulated_worker_time_ns += std::min(now - 
_last_observation_time,
                                                         
cur_cumulated_cputime_ns - g->_last_cumulated_cputime_ns);
               g->_last_cumulated_cputime_ns = cur_cumulated_cputime_ns;
               cputime_ns += g->_cur_cumulated_worker_time_ns;
           }
       });
       _last_observation_time = now;
       return cputime_ns / 1000000000.0;
   }
   ```
   
   This approach ensures that the reported `bthread_worker_usage` stays within 
a reasonable and bounded range by constraining its growth based on both elapsed 
wall clock time and actual CPU time consumed. It should effectively prevent the 
metric from exceeding `bthread_worker_count`, even in cases of long-running, 
non-yielding tasks.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to