From 95ca976f2a3622c1f27e69f7adbca2031d8c2f26 Mon Sep 17 00:00:00 2001 From: huangjun Date: Thu, 23 Apr 2026 00:56:57 +0800 Subject: [PATCH] [bvar] fix sampler interval after switch to cpuwide_time_ns Commit 12fb539a ("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. --- src/bvar/detail/sampler.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/bvar/detail/sampler.cpp b/src/bvar/detail/sampler.cpp index f3462558c1..4632938bb2 100644 --- a/src/bvar/detail/sampler.cpp +++ b/src/bvar/detail/sampler.cpp @@ -155,7 +155,7 @@ void SamplerCollector::run() { butil::LinkNode root; int consecutive_nosleep = 0; while (!_stop) { - int64_t abstime = butil::cpuwide_time_ns(); + int64_t abstime = butil::cpuwide_time_us(); Sampler* s = this->reset(); if (s) { s->InsertBeforeAsList(&root); @@ -176,13 +176,13 @@ void SamplerCollector::run() { p = saved_next; } bool slept = false; - int64_t now = butil::cpuwide_time_ns(); + int64_t now = butil::cpuwide_time_us(); _cumulated_time_us += now - abstime; abstime += 1000000L; while (abstime > now) { ::usleep(abstime - now); slept = true; - now = butil::cpuwide_time_ns(); + now = butil::cpuwide_time_us(); } if (slept) { consecutive_nosleep = 0;