Skip to content

Commit 95ca976

Browse files
author
huangjun
committed
[bvar] fix sampler interval after switch to cpuwide_time_ns
Commit 12fb539 ("Use monotonic time instead of wall time", #3268) switched the three time-source calls in SamplerCollector::run() from gettimeofday_us() to cpuwide_time_ns(), but the surrounding code still treats the timestamps as microseconds: - abstime += 1000000L now represents 1 ms (not 1 s), causing the sampler to spin at ~1 kHz instead of 1 Hz; - usleep(abstime - now) receives a nanosecond delta, which usleep() interprets as microseconds. Use cpuwide_time_us() instead, which preserves the monotonic behavior from #3268 while keeping the existing microsecond-based arithmetic correct. Fixes #3277.
1 parent 35682ff commit 95ca976

1 file changed

Lines changed: 3 additions & 3 deletions

File tree

src/bvar/detail/sampler.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ void SamplerCollector::run() {
155155
butil::LinkNode<Sampler> root;
156156
int consecutive_nosleep = 0;
157157
while (!_stop) {
158-
int64_t abstime = butil::cpuwide_time_ns();
158+
int64_t abstime = butil::cpuwide_time_us();
159159
Sampler* s = this->reset();
160160
if (s) {
161161
s->InsertBeforeAsList(&root);
@@ -176,13 +176,13 @@ void SamplerCollector::run() {
176176
p = saved_next;
177177
}
178178
bool slept = false;
179-
int64_t now = butil::cpuwide_time_ns();
179+
int64_t now = butil::cpuwide_time_us();
180180
_cumulated_time_us += now - abstime;
181181
abstime += 1000000L;
182182
while (abstime > now) {
183183
::usleep(abstime - now);
184184
slept = true;
185-
now = butil::cpuwide_time_ns();
185+
now = butil::cpuwide_time_us();
186186
}
187187
if (slept) {
188188
consecutive_nosleep = 0;

0 commit comments

Comments
 (0)