You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@brpc.apache.org by "hjwsm1989 (via GitHub)" <gi...@apache.org> on 2026/04/22 16:59:58 UTC
[PR] [bvar] fix sampler interval after switch to cpuwide_time_ns (brpc)
hjwsm1989 opened a new pull request, #3278:
URL: https://github.com/apache/brpc/pull/3278
### What problem does this PR solve?
Fixes the unit-mismatch regression introduced by #3268 (commit 12fb539a, "Use monotonic time instead of wall time").
Before this PR, on master / 1.17.0-rc1, the bvar sampler thread effectively runs at ~1 kHz instead of 1 Hz and spams
sampler.cpp:192 bvar is busy at sampling for 2 seconds!
PR #3268 switched the three time-source calls in `SamplerCollector::run()` from `gettimeofday_us()` (microseconds) to `cpuwide_time_ns()` (nanoseconds), but left the 1-second offset constant (`abstime += 1000000L`) and the `usleep(abstime - now)` call untouched. As a result:
- `abstime += 1000000L` now adds 1 ms instead of 1 s, so the sampler spins at ~1 kHz instead of 1 Hz;
- `usleep(abstime - now)` is passed a nanosecond delta that `usleep()` interprets as microseconds, which further distorts the sleep duration.
See #3277 for the full analysis and a reproducer.
### What is changed and how does it work?
`src/bvar/detail/sampler.cpp::SamplerCollector::run()`:
- `abstime += 1000000L` (1 ms in nanoseconds) → `abstime += 1000000000L` (1 s in nanoseconds), restoring the intended 1-second sample period.
- `::usleep(abstime - now)` — `usleep()` takes microseconds, but the difference is now in nanoseconds; divide by 1000.
### Checklist
- [x] Fix compiles and links (verified against an application tree linking brpc master).
- [x] Manually verified on a live cluster: "bvar is busy at sampling" warning count dropped from ~17k/minute per process to 0; sampler thread CPU dropped accordingly.
- [ ] No dedicated unit test added — the existing `SamplerCollector::run()` loop is not currently covered by a direct unit test. Happy to add one if reviewers can suggest a good shape (the challenge is driving the loop deterministically without sleeping wall-clock time).
Fixes #3277.
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] [bvar] fix sampler interval after switch to cpuwide_time_ns (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on PR #3278:
URL: https://github.com/apache/brpc/pull/3278#issuecomment-4301444181
cc @ivanallen
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] [bvar] fix sampler interval after switch to cpuwide_time_ns (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright commented on PR #3278:
URL: https://github.com/apache/brpc/pull/3278#issuecomment-4301283032
I made a mistake before.
Please use `cpuwide_time_us` in sampler.cpp to fix this issue.
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] [bvar] fix sampler interval after switch to cpuwide_time_ns (brpc)
Posted by "hjwsm1989 (via GitHub)" <gi...@apache.org>.
hjwsm1989 commented on PR #3278:
URL: https://github.com/apache/brpc/pull/3278#issuecomment-4301322738
> I made a mistake before.
>
> Please use `cpuwide_time_us` in sampler.cpp to fix this issue.
ok, doing
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] [bvar] fix sampler interval after switch to cpuwide_time_ns (brpc)
Posted by "hjwsm1989 (via GitHub)" <gi...@apache.org>.
hjwsm1989 commented on PR #3278:
URL: https://github.com/apache/brpc/pull/3278#issuecomment-4298283683
@chenBright could you please take a look when you have time? This fixes a unit-mismatch regression introduced by your #3268. Full analysis in #3277. Thanks!
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org
Re: [PR] [bvar] fix sampler interval after switch to cpuwide_time_ns (brpc)
Posted by "chenBright (via GitHub)" <gi...@apache.org>.
chenBright merged PR #3278:
URL: https://github.com/apache/brpc/pull/3278
--
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: dev-unsubscribe@brpc.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@brpc.apache.org
For additional commands, e-mail: dev-help@brpc.apache.org