From b820ce292eabdb296b7af251c5ea4a11e3e4d802 Mon Sep 17 00:00:00 2001 From: Etienne Perot Date: Mon, 1 Jul 2024 17:05:35 -0700 Subject: [PATCH] Profiling metrics: Do not use `time.Now` and rely only on `runtime.nanotime`. This simplifies clock handling. `runtime.nanotime` uses `CLOCK_MONOTONIC` which works just fine. PiperOrigin-RevId: 648528699 --- pkg/metric/profiling_metric.go | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/pkg/metric/profiling_metric.go b/pkg/metric/profiling_metric.go index c66ce60d5..7fff92719 100644 --- a/pkg/metric/profiling_metric.go +++ b/pkg/metric/profiling_metric.go @@ -252,17 +252,8 @@ func collectProfilingMetrics(s *snapshots, values []func(fieldValues ...*FieldVa ) backoffFactor := initialBackoffFactor - // To keep track of time cheaply, we use `CheapNowNano`. - // However, this can drift as it has poor precision. - // To get something more precise, we periodically call `time.Now` - // and `CheapNowNano` and use these two variables to track both. - // This way, we can compute a more precise time by using - // `CheapNowNano() - cheapTime + preciseTime`. - preciseTime := s.startTime - cheapTime := cheapStartTime - stopCollecting := false - for nextCollection := s.startTime; !stopCollecting; nextCollection += profilingRate.Nanoseconds() { + for nextCollection := cheapStartTime; !stopCollecting; nextCollection += profilingRate.Nanoseconds() { // For small durations, just spin. Otherwise sleep. for { @@ -271,7 +262,7 @@ func collectProfilingMetrics(s *snapshots, values []func(fieldValues ...*FieldVa spinMaxNanos = 250 yieldMaxNanos = 1_000 ) - now := CheapNowNano() - cheapTime + preciseTime + now := CheapNowNano() nanosToNextCollection := nextCollection - now if nanosToNextCollection <= 0 { // Collect now. @@ -294,8 +285,7 @@ func collectProfilingMetrics(s *snapshots, values []func(fieldValues ...*FieldVa // Collect one last time before stopping. } - collectStart := CheapNowNano() - cheapTime + preciseTime - timestamp := time.Duration(collectStart - s.startTime) + timestamp := time.Duration(CheapNowNano() - cheapStartTime) base := curSnapshot * numEntries ringBuf := s.ringbuffer[ringbufferIdx] ringBuf[base] = uint64(timestamp) @@ -316,9 +306,6 @@ func collectProfilingMetrics(s *snapshots, values []func(fieldValues ...*FieldVa time.Sleep(backoffSleep) backoffFactor = min(backoffFactor*backoffFactorGrowth, backoffFactorMax) } - // Refresh precise time. - preciseTime = time.Now().UnixNano() - cheapTime = CheapNowNano() } } if curSnapshot != 0 {