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

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: Created 4 years, 4 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 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 const base::DictionaryValue* time_mapping = 196 const base::DictionaryValue* time_mapping =
197 pref_service_->GetDictionary(prefs::kNetworkTimeMapping); 197 pref_service_->GetDictionary(prefs::kNetworkTimeMapping);
198 double time_js = 0; 198 double time_js = 0;
199 double ticks_js = 0; 199 double ticks_js = 0;
200 double network_time_js = 0; 200 double network_time_js = 0;
201 double uncertainty_js = 0; 201 double uncertainty_js = 0;
202 if (time_mapping->GetDouble(kPrefTime, &time_js) && 202 if (time_mapping->GetDouble(kPrefTime, &time_js) &&
203 time_mapping->GetDouble(kPrefTicks, &ticks_js) && 203 time_mapping->GetDouble(kPrefTicks, &ticks_js) &&
204 time_mapping->GetDouble(kPrefUncertainty, &uncertainty_js) && 204 time_mapping->GetDouble(kPrefUncertainty, &uncertainty_js) &&
205 time_mapping->GetDouble(kPrefNetworkTime, &network_time_js)) { 205 time_mapping->GetDouble(kPrefNetworkTime, &network_time_js)) {
206 time_at_last_measurement_ = base::Time::FromJsTime(time_js); 206 time_at_last_measurement_ = base::Time::FromJsTime(time_js);
mab 2016/08/16 19:04:52 Totally optional, but I've thought for a while may
estark 2016/08/18 12:15:11 +1, the prefs stuff hurts my brain. Will save for
207 ticks_at_last_measurement_ = base::TimeTicks::FromInternalValue( 207 ticks_at_last_measurement_ = base::TimeTicks::FromInternalValue(
208 static_cast<int64_t>(ticks_js)); 208 static_cast<int64_t>(ticks_js));
209 network_time_uncertainty_ = base::TimeDelta::FromInternalValue( 209 network_time_uncertainty_ = base::TimeDelta::FromInternalValue(
210 static_cast<int64_t>(uncertainty_js)); 210 static_cast<int64_t>(uncertainty_js));
211 network_time_at_last_measurement_ = base::Time::FromJsTime(network_time_js); 211 network_time_at_last_measurement_ = base::Time::FromJsTime(network_time_js);
212 } 212 }
213 base::Time now = clock_->Now(); 213 base::Time now = clock_->Now();
214 if (ticks_at_last_measurement_ > tick_clock_->NowTicks() || 214 if (ticks_at_last_measurement_ > tick_clock_->NowTicks() ||
215 time_at_last_measurement_ > now || 215 time_at_last_measurement_ > now ||
216 now - time_at_last_measurement_ > 216 now - time_at_last_measurement_ >
(...skipping 82 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";
323 network_time_at_last_measurement_ = base::Time(); 324 network_time_at_last_measurement_ = base::Time();
324 return false; 325 return NETWORK_TIME_SYNC_LOST;
325 } 326 }
326 // Now we know that both |tick_delta| and |time_delta| are positive. 327 // Now we know that both |tick_delta| and |time_delta| are positive.
327 base::TimeDelta divergence = (tick_delta - time_delta).magnitude(); 328 base::TimeDelta divergence = (tick_delta - time_delta).magnitude();
328 if (divergence > base::TimeDelta::FromSeconds(kClockDivergenceSeconds)) { 329 if (divergence > base::TimeDelta::FromSeconds(kClockDivergenceSeconds)) {
329 // Most likely either the machine has suspended, or the wall clock has been 330 // Most likely either the machine has suspended, or the wall clock has been
330 // reset. 331 // reset.
331 DVLOG(1) << "Discarding network time due to clocks diverging"; 332 DVLOG(1) << "Discarding network time due to clocks diverging";
332 network_time_at_last_measurement_ = base::Time(); 333 network_time_at_last_measurement_ = base::Time();
333 return false; 334 return NETWORK_TIME_SYNC_LOST;
334 } 335 }
335 *network_time = network_time_at_last_measurement_ + tick_delta; 336 *network_time = network_time_at_last_measurement_ + tick_delta;
336 if (uncertainty) { 337 if (uncertainty) {
337 *uncertainty = network_time_uncertainty_ + divergence; 338 *uncertainty = network_time_uncertainty_ + divergence;
338 } 339 }
339 return true; 340 return NETWORK_TIME_AVAILABLE;
340 } 341 }
341 342
342 void NetworkTimeTracker::CheckTime() { 343 void NetworkTimeTracker::CheckTime() {
343 DCHECK(thread_checker_.CalledOnValidThread()); 344 DCHECK(thread_checker_.CalledOnValidThread());
344 345
345 // If NetworkTimeTracker is waking up after a backoff, this will reset the 346 // If NetworkTimeTracker is waking up after a backoff, this will reset the
346 // timer to its default faster frequency. 347 // timer to its default faster frequency.
347 QueueCheckTime(CheckTimeInterval()); 348 QueueCheckTime(CheckTimeInterval());
348 349
349 if (!ShouldIssueTimeQuery()) { 350 if (!ShouldIssueTimeQuery()) {
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) { 465 void NetworkTimeTracker::QueueCheckTime(base::TimeDelta delay) {
465 timer_.Start(FROM_HERE, delay, this, &NetworkTimeTracker::CheckTime); 466 timer_.Start(FROM_HERE, delay, this, &NetworkTimeTracker::CheckTime);
466 } 467 }
467 468
468 bool NetworkTimeTracker::ShouldIssueTimeQuery() { 469 bool NetworkTimeTracker::ShouldIssueTimeQuery() {
469 // Do not query the time service if not enabled via Variations Service. 470 // Do not query the time service if not enabled via Variations Service.
470 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying)) { 471 if (!base::FeatureList::IsEnabled(kNetworkTimeServiceQuerying)) {
471 return false; 472 return false;
472 } 473 }
473 474
474 // If GetNetworkTime() returns false, synchronization has been lost 475 // If GetNetworkTime() does not return NETWORK_TIME_AVAILABLE,
475 // and a query is needed. 476 // synchronization has been lost and a query is needed.
476 base::Time network_time; 477 base::Time network_time;
477 if (!GetNetworkTime(&network_time, nullptr)) { 478 if (GetNetworkTime(&network_time, nullptr) != NETWORK_TIME_AVAILABLE) {
478 return true; 479 return true;
479 } 480 }
480 481
481 // Otherwise, make the decision at random. 482 // Otherwise, make the decision at random.
482 return base::RandDouble() < RandomQueryProbability(); 483 return base::RandDouble() < RandomQueryProbability();
483 } 484 }
484 485
485 } // namespace network_time 486 } // namespace network_time
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698