Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: components/network_time/network_time_tracker.cc

Issue 2254433003: When network time is unavailable, record the reason in UMA (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix maximums to not overflow, and fix unit tests Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/network_time/network_time_tracker.h" 5 #include "components/network_time/network_time_tracker.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <string> 8 #include <string>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 void NetworkTimeTracker::WaitForFetchForTesting(uint32_t nonce) { 299 void NetworkTimeTracker::WaitForFetchForTesting(uint32_t nonce) {
300 query_signer_->OverrideNonceForTesting(kKeyVersion, nonce); 300 query_signer_->OverrideNonceForTesting(kKeyVersion, nonce);
301 base::RunLoop().Run(); 301 base::RunLoop().Run();
302 } 302 }
303 303
304 base::TimeDelta NetworkTimeTracker::GetTimerDelayForTesting() const { 304 base::TimeDelta NetworkTimeTracker::GetTimerDelayForTesting() const {
305 DCHECK(timer_.IsRunning()); 305 DCHECK(timer_.IsRunning());
306 return timer_.GetCurrentDelay(); 306 return timer_.GetCurrentDelay();
307 } 307 }
308 308
309 bool NetworkTimeTracker::GetNetworkTime(base::Time* network_time, 309 NetworkTimeTracker::NetworkTimeResult NetworkTimeTracker::GetNetworkTime(
310 base::TimeDelta* uncertainty) const { 310 base::Time* network_time,
311 base::TimeDelta* uncertainty) const {
311 DCHECK(thread_checker_.CalledOnValidThread()); 312 DCHECK(thread_checker_.CalledOnValidThread());
312 DCHECK(network_time); 313 DCHECK(network_time);
313 if (network_time_at_last_measurement_.is_null()) { 314 if (network_time_at_last_measurement_.is_null()) {
314 return false; 315 return NETWORK_TIME_NO_SYNC;
315 } 316 }
316 DCHECK(!ticks_at_last_measurement_.is_null()); 317 DCHECK(!ticks_at_last_measurement_.is_null());
317 DCHECK(!time_at_last_measurement_.is_null()); 318 DCHECK(!time_at_last_measurement_.is_null());
318 base::TimeDelta tick_delta = 319 base::TimeDelta tick_delta =
319 tick_clock_->NowTicks() - ticks_at_last_measurement_; 320 tick_clock_->NowTicks() - ticks_at_last_measurement_;
320 base::TimeDelta time_delta = clock_->Now() - time_at_last_measurement_; 321 base::TimeDelta time_delta = clock_->Now() - time_at_last_measurement_;
321 if (time_delta.InMilliseconds() < 0) { // Has wall clock run backward? 322 if (time_delta.InMilliseconds() < 0) { // Has wall clock run backward?
322 DVLOG(1) << "Discarding network time due to wall clock running backward"; 323 DVLOG(1) << "Discarding network time due to wall clock running backward";
324 UMA_HISTOGRAM_CUSTOM_TIMES(
325 "NetworkTimeTracker.WallClockRanBackwards", time_delta.magnitude(),
326 base::TimeDelta::FromSeconds(1), base::TimeDelta::FromDays(7), 50);
323 network_time_at_last_measurement_ = base::Time(); 327 network_time_at_last_measurement_ = base::Time();
324 return false; 328 return NETWORK_TIME_SYNC_LOST;
325 } 329 }
326 // Now we know that both |tick_delta| and |time_delta| are positive. 330 // Now we know that both |tick_delta| and |time_delta| are positive.
327 base::TimeDelta divergence = (tick_delta - time_delta).magnitude(); 331 base::TimeDelta divergence = tick_delta - time_delta;
328 if (divergence > base::TimeDelta::FromSeconds(kClockDivergenceSeconds)) { 332 if (divergence.magnitude() >
333 base::TimeDelta::FromSeconds(kClockDivergenceSeconds)) {
329 // Most likely either the machine has suspended, or the wall clock has been 334 // Most likely either the machine has suspended, or the wall clock has been
330 // reset. 335 // reset.
331 DVLOG(1) << "Discarding network time due to clocks diverging"; 336 DVLOG(1) << "Discarding network time due to clocks diverging";
337
338 // The below histograms do not use |kClockDivergenceSeconds| as the
339 // lower-bound, so that |kClockDivergenceSeconds| can be changed
340 // without causing the buckets to change and making data from
341 // old/new clients incompatible.
342 if (divergence.InMilliseconds() < 0) {
343 UMA_HISTOGRAM_CUSTOM_TIMES(
344 "NetworkTimeTracker.ClockDivergence.Negative", divergence.magnitude(),
345 base::TimeDelta::FromSeconds(60), base::TimeDelta::FromDays(7), 50);
346 } else {
347 UMA_HISTOGRAM_CUSTOM_TIMES(
348 "NetworkTimeTracker.ClockDivergence.Positive", divergence.magnitude(),
349 base::TimeDelta::FromSeconds(60), base::TimeDelta::FromDays(7), 50);
350 }
332 network_time_at_last_measurement_ = base::Time(); 351 network_time_at_last_measurement_ = base::Time();
333 return false; 352 return NETWORK_TIME_SYNC_LOST;
334 } 353 }
335 *network_time = network_time_at_last_measurement_ + tick_delta; 354 *network_time = network_time_at_last_measurement_ + tick_delta;
336 if (uncertainty) { 355 if (uncertainty) {
337 *uncertainty = network_time_uncertainty_ + divergence; 356 *uncertainty = network_time_uncertainty_ + divergence;
338 } 357 }
339 return true; 358 return NETWORK_TIME_AVAILABLE;
340 } 359 }
341 360
342 void NetworkTimeTracker::CheckTime() { 361 void NetworkTimeTracker::CheckTime() {
343 DCHECK(thread_checker_.CalledOnValidThread()); 362 DCHECK(thread_checker_.CalledOnValidThread());
344 363
345 // If NetworkTimeTracker is waking up after a backoff, this will reset the 364 // If NetworkTimeTracker is waking up after a backoff, this will reset the
346 // timer to its default faster frequency. 365 // timer to its default faster frequency.
347 QueueCheckTime(CheckTimeInterval()); 366 QueueCheckTime(CheckTimeInterval());
348 367
349 if (!ShouldIssueTimeQuery()) { 368 if (!ShouldIssueTimeQuery()) {
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
462 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) { 481 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) {
463 timer_.Start(FROM_HERE, delay, this, &NetworkTimeTracker::CheckTime); 482 timer_.Start(FROM_HERE, delay, this, &NetworkTimeTracker::CheckTime);
464 } 483 }
465 484
466 bool NetworkTimeTracker::ShouldIssueTimeQuery() { 485 bool NetworkTimeTracker::ShouldIssueTimeQuery() {
467 // Do not query the time service if not enabled via Variations Service. 486 // Do not query the time service if not enabled via Variations Service.
468 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying)) { 487 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying)) {
469 return false; 488 return false;
470 } 489 }
471 490
472 // If GetNetworkTime() returns false, synchronization has been lost 491 // If GetNetworkTime() does not return NETWORK_TIME_AVAILABLE,
473 // and a query is needed. 492 // synchronization has been lost and a query is needed.
474 base::Time network_time; 493 base::Time network_time;
475 if (!GetNetworkTime(&network_time, nullptr)) { 494 if (GetNetworkTime(&network_time, nullptr) != NETWORK_TIME_AVAILABLE) {
476 return true; 495 return true;
477 } 496 }
478 497
479 // Otherwise, make the decision at random. 498 // Otherwise, make the decision at random.
480 return base::RandDouble() < RandomQueryProbability(); 499 return base::RandDouble() < RandomQueryProbability();
481 } 500 }
482 501
483 } // namespace network_time 502 } // namespace network_time
OLDNEW
« no previous file with comments | « components/network_time/network_time_tracker.h ('k') | components/network_time/network_time_tracker_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698