Skip to content

Commit 9e6fdd0

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() (microseconds) to cpuwide_time_ns() (nanoseconds), but left the 1-second offset constant and the usleep() 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. Fix by using a 1-second offset expressed in nanoseconds and by converting the nanosecond delta to microseconds before handing it to usleep(). Fixes #3277.
1 parent 35682ff commit 9e6fdd0

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

src/bvar/detail/sampler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ void SamplerCollector::run() {
178178
bool slept = false;
179179
int64_t now = butil::cpuwide_time_ns();
180180
_cumulated_time_us += now - abstime;
181-
abstime += 1000000L;
181+
abstime += 1000000000L; // 1 second, in nanoseconds
182182
while (abstime > now) {
183-
::usleep(abstime - now);
183+
::usleep((abstime - now) / 1000); // usleep() takes microseconds
184184
slept = true;
185185
now = butil::cpuwide_time_ns();
186186
}

0 commit comments

Comments
 (0)