From 80d661cb7a3f53f7a08ed4fd7ff56908c292c91b Mon Sep 17 00:00:00 2001 From: Taylor Brandstetter Date: Wed, 10 Feb 2021 14:11:57 -0800 Subject: [PATCH] Use monotonic clock for legacy stats caching. Fixes a bug where, if the wall clock goes backwards (from changing time zone, daylight savings, manual change by user, etc.), the stats collector stops updating stats. Bug: webrtc:12439 Change-Id: Ie45f484110d43444aea6d6b1a218b05d0f8b6a0c Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/206622 Reviewed-by: Harald Alvestrand Commit-Queue: Taylor Cr-Commit-Position: refs/heads/master@{#33240} --- pc/stats_collector.cc | 18 ++++++++++-------- pc/stats_collector.h | 1 + 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/pc/stats_collector.cc b/pc/stats_collector.cc index 64ce2e0154..5641061240 100644 --- a/pc/stats_collector.cc +++ b/pc/stats_collector.cc @@ -649,15 +649,17 @@ void StatsCollector::GetStats(MediaStreamTrackInterface* track, void StatsCollector::UpdateStats( PeerConnectionInterface::StatsOutputLevel level) { RTC_DCHECK(pc_->signaling_thread()->IsCurrent()); - double time_now = GetTimeNow(); - // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of - // ms apart will be ignored. - const double kMinGatherStatsPeriod = 50; - if (stats_gathering_started_ != 0 && - stats_gathering_started_ + kMinGatherStatsPeriod > time_now) { + // Calls to UpdateStats() that occur less than kMinGatherStatsPeriodMs apart + // will be ignored. Using a monotonic clock specifically for this, while using + // a UTC clock for the reports themselves. + const int64_t kMinGatherStatsPeriodMs = 50; + int64_t cache_now_ms = rtc::TimeMillis(); + if (cache_timestamp_ms_ != 0 && + cache_timestamp_ms_ + kMinGatherStatsPeriodMs > cache_now_ms) { return; } - stats_gathering_started_ = time_now; + cache_timestamp_ms_ = cache_now_ms; + stats_gathering_started_ = GetTimeNow(); // TODO(tommi): All of these hop over to the worker thread to fetch // information. We could use an AsyncInvoker to run all of these and post @@ -1267,7 +1269,7 @@ void StatsCollector::UpdateTrackReports() { } void StatsCollector::ClearUpdateStatsCacheForTest() { - stats_gathering_started_ = 0; + cache_timestamp_ms_ = 0; } } // namespace webrtc diff --git a/pc/stats_collector.h b/pc/stats_collector.h index a600a6efae..02a1ced116 100644 --- a/pc/stats_collector.h +++ b/pc/stats_collector.h @@ -151,6 +151,7 @@ class StatsCollector : public StatsCollectorInterface { TrackIdMap track_ids_; // Raw pointer to the peer connection the statistics are gathered from. PeerConnectionInternal* const pc_; + int64_t cache_timestamp_ms_ = 0; double stats_gathering_started_; const bool use_standard_bytes_stats_;