| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "net/base/network_quality_estimator.h" | 5 #include "net/base/network_quality_estimator.h" |
| 6 | 6 |
| 7 #include <float.h> | 7 #include <float.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <cmath> | 9 #include <cmath> |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 variations_value >= kMinimumThroughputVariationParameterKbps) { | 236 variations_value >= kMinimumThroughputVariationParameterKbps) { |
| 237 default_observations_[i] = | 237 default_observations_[i] = |
| 238 NetworkQuality(default_observations_[i].rtt(), variations_value); | 238 NetworkQuality(default_observations_[i].rtt(), variations_value); |
| 239 } | 239 } |
| 240 } | 240 } |
| 241 } | 241 } |
| 242 | 242 |
| 243 void NetworkQualityEstimator::AddDefaultEstimates() { | 243 void NetworkQualityEstimator::AddDefaultEstimates() { |
| 244 DCHECK(thread_checker_.CalledOnValidThread()); | 244 DCHECK(thread_checker_.CalledOnValidThread()); |
| 245 if (default_observations_[current_network_id_.type].rtt() != InvalidRTT()) { | 245 if (default_observations_[current_network_id_.type].rtt() != InvalidRTT()) { |
| 246 rtt_msec_observations_.AddObservation(Observation( | 246 Observation rtt_observation( |
| 247 default_observations_[current_network_id_.type].rtt().InMilliseconds(), | 247 default_observations_[current_network_id_.type].rtt().InMilliseconds(), |
| 248 base::TimeTicks::Now())); | 248 base::TimeTicks::Now(), OBSERVATION_SOURCE_DEFAULT_FROM_PLATFORM); |
| 249 rtt_msec_observations_.AddObservation(rtt_observation); |
| 250 NotifyObserversOfRTT(rtt_observation); |
| 249 } | 251 } |
| 250 if (default_observations_[current_network_id_.type] | 252 if (default_observations_[current_network_id_.type] |
| 251 .downstream_throughput_kbps() != kInvalidThroughput) { | 253 .downstream_throughput_kbps() != kInvalidThroughput) { |
| 254 Observation throughput_observation( |
| 255 default_observations_[current_network_id_.type] |
| 256 .downstream_throughput_kbps(), |
| 257 base::TimeTicks::Now(), OBSERVATION_SOURCE_DEFAULT_FROM_PLATFORM); |
| 252 downstream_throughput_kbps_observations_.AddObservation( | 258 downstream_throughput_kbps_observations_.AddObservation( |
| 253 Observation(default_observations_[current_network_id_.type] | 259 throughput_observation); |
| 254 .downstream_throughput_kbps(), | 260 NotifyObserversOfThroughput(throughput_observation); |
| 255 base::TimeTicks::Now())); | |
| 256 } | 261 } |
| 257 } | 262 } |
| 258 | 263 |
| 259 NetworkQualityEstimator::~NetworkQualityEstimator() { | 264 NetworkQualityEstimator::~NetworkQualityEstimator() { |
| 260 DCHECK(thread_checker_.CalledOnValidThread()); | 265 DCHECK(thread_checker_.CalledOnValidThread()); |
| 261 NetworkChangeNotifier::RemoveConnectionTypeObserver(this); | 266 NetworkChangeNotifier::RemoveConnectionTypeObserver(this); |
| 262 } | 267 } |
| 263 | 268 |
| 264 void NetworkQualityEstimator::NotifyHeadersReceived(const URLRequest& request) { | 269 void NetworkQualityEstimator::NotifyHeadersReceived(const URLRequest& request) { |
| 265 DCHECK(thread_checker_.CalledOnValidThread()); | 270 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 293 | 298 |
| 294 // Duration between when the resource was requested and when response | 299 // Duration between when the resource was requested and when response |
| 295 // headers were received. | 300 // headers were received. |
| 296 base::TimeDelta observed_rtt = headers_received_time - request_start_time; | 301 base::TimeDelta observed_rtt = headers_received_time - request_start_time; |
| 297 DCHECK_GE(observed_rtt, base::TimeDelta()); | 302 DCHECK_GE(observed_rtt, base::TimeDelta()); |
| 298 if (observed_rtt < peak_network_quality_.rtt()) { | 303 if (observed_rtt < peak_network_quality_.rtt()) { |
| 299 peak_network_quality_ = NetworkQuality( | 304 peak_network_quality_ = NetworkQuality( |
| 300 observed_rtt, peak_network_quality_.downstream_throughput_kbps()); | 305 observed_rtt, peak_network_quality_.downstream_throughput_kbps()); |
| 301 } | 306 } |
| 302 | 307 |
| 303 rtt_msec_observations_.AddObservation( | 308 Observation rtt_observation(observed_rtt.InMilliseconds(), now, |
| 304 Observation(observed_rtt.InMilliseconds(), now)); | 309 OBSERVATION_SOURCE_URL_REQUEST); |
| 310 rtt_msec_observations_.AddObservation(rtt_observation); |
| 311 NotifyObserversOfRTT(rtt_observation); |
| 305 | 312 |
| 306 // Compare the RTT observation with the estimated value and record it. | 313 // Compare the RTT observation with the estimated value and record it. |
| 307 if (estimated_median_network_quality_.rtt() != InvalidRTT()) { | 314 if (estimated_median_network_quality_.rtt() != InvalidRTT()) { |
| 308 RecordRTTUMA(estimated_median_network_quality_.rtt().InMilliseconds(), | 315 RecordRTTUMA(estimated_median_network_quality_.rtt().InMilliseconds(), |
| 309 observed_rtt.InMilliseconds()); | 316 observed_rtt.InMilliseconds()); |
| 310 } | 317 } |
| 311 } | 318 } |
| 312 | 319 |
| 313 void NetworkQualityEstimator::NotifyRequestCompleted( | 320 void NetworkQualityEstimator::NotifyRequestCompleted( |
| 314 const URLRequest& request) { | 321 const URLRequest& request) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 361 // connection. | 368 // connection. |
| 362 if (downstream_kbps - downstream_kbps_as_integer > 0) | 369 if (downstream_kbps - downstream_kbps_as_integer > 0) |
| 363 downstream_kbps_as_integer++; | 370 downstream_kbps_as_integer++; |
| 364 | 371 |
| 365 DCHECK_GT(downstream_kbps_as_integer, 0.0); | 372 DCHECK_GT(downstream_kbps_as_integer, 0.0); |
| 366 if (downstream_kbps_as_integer > | 373 if (downstream_kbps_as_integer > |
| 367 peak_network_quality_.downstream_throughput_kbps()) | 374 peak_network_quality_.downstream_throughput_kbps()) |
| 368 peak_network_quality_ = | 375 peak_network_quality_ = |
| 369 NetworkQuality(peak_network_quality_.rtt(), downstream_kbps_as_integer); | 376 NetworkQuality(peak_network_quality_.rtt(), downstream_kbps_as_integer); |
| 370 | 377 |
| 378 Observation throughput_observation(downstream_kbps_as_integer, now, |
| 379 OBSERVATION_SOURCE_URL_REQUEST); |
| 371 downstream_throughput_kbps_observations_.AddObservation( | 380 downstream_throughput_kbps_observations_.AddObservation( |
| 372 Observation(downstream_kbps_as_integer, now)); | 381 throughput_observation); |
| 382 NotifyObserversOfThroughput(throughput_observation); |
| 383 } |
| 384 |
| 385 void NetworkQualityEstimator::AddRTTObserver(RTTObserver* rtt_observer) { |
| 386 DCHECK(thread_checker_.CalledOnValidThread()); |
| 387 rtt_observer_list_.AddObserver(rtt_observer); |
| 388 } |
| 389 |
| 390 void NetworkQualityEstimator::RemoveRTTObserver(RTTObserver* rtt_observer) { |
| 391 DCHECK(thread_checker_.CalledOnValidThread()); |
| 392 rtt_observer_list_.RemoveObserver(rtt_observer); |
| 393 } |
| 394 |
| 395 void NetworkQualityEstimator::AddThroughputObserver( |
| 396 ThroughputObserver* throughput_observer) { |
| 397 DCHECK(thread_checker_.CalledOnValidThread()); |
| 398 throughput_observer_list_.AddObserver(throughput_observer); |
| 399 } |
| 400 |
| 401 void NetworkQualityEstimator::RemoveThroughputObserver( |
| 402 ThroughputObserver* throughput_observer) { |
| 403 DCHECK(thread_checker_.CalledOnValidThread()); |
| 404 throughput_observer_list_.RemoveObserver(throughput_observer); |
| 405 } |
| 406 |
| 407 void NetworkQualityEstimator::ConfigureForTests(bool allow_local_host_requests, |
| 408 bool allow_smaller_responses) { |
| 409 DCHECK(thread_checker_.CalledOnValidThread()); |
| 410 allow_localhost_requests_ = allow_local_host_requests, |
| 411 allow_small_responses_ = allow_smaller_responses; |
| 373 } | 412 } |
| 374 | 413 |
| 375 void NetworkQualityEstimator::RecordRTTUMA(int32_t estimated_value_msec, | 414 void NetworkQualityEstimator::RecordRTTUMA(int32_t estimated_value_msec, |
| 376 int32_t actual_value_msec) const { | 415 int32_t actual_value_msec) const { |
| 377 DCHECK(thread_checker_.CalledOnValidThread()); | 416 DCHECK(thread_checker_.CalledOnValidThread()); |
| 378 | 417 |
| 379 // Record the difference between the actual and the estimated value. | 418 // Record the difference between the actual and the estimated value. |
| 380 if (estimated_value_msec >= actual_value_msec) { | 419 if (estimated_value_msec >= actual_value_msec) { |
| 381 base::HistogramBase* difference_rtt = | 420 base::HistogramBase* difference_rtt = |
| 382 GetHistogram("DifferenceRTTEstimatedAndActual.", | 421 GetHistogram("DifferenceRTTEstimatedAndActual.", |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 580 bool NetworkQualityEstimator::GetRecentMedianDownlinkThroughputKbps( | 619 bool NetworkQualityEstimator::GetRecentMedianDownlinkThroughputKbps( |
| 581 const base::TimeTicks& begin_timestamp, | 620 const base::TimeTicks& begin_timestamp, |
| 582 int32_t* kbps) const { | 621 int32_t* kbps) const { |
| 583 DCHECK(thread_checker_.CalledOnValidThread()); | 622 DCHECK(thread_checker_.CalledOnValidThread()); |
| 584 DCHECK(kbps); | 623 DCHECK(kbps); |
| 585 *kbps = GetDownlinkThroughputKbpsEstimateInternal(begin_timestamp, 50); | 624 *kbps = GetDownlinkThroughputKbpsEstimateInternal(begin_timestamp, 50); |
| 586 return (*kbps != kInvalidThroughput); | 625 return (*kbps != kInvalidThroughput); |
| 587 } | 626 } |
| 588 | 627 |
| 589 NetworkQualityEstimator::Observation::Observation(int32_t value, | 628 NetworkQualityEstimator::Observation::Observation(int32_t value, |
| 590 base::TimeTicks timestamp) | 629 base::TimeTicks timestamp, |
| 591 : value(value), timestamp(timestamp) { | 630 ObservationSource source) |
| 631 : value(value), timestamp(timestamp), source(source) { |
| 592 DCHECK_GE(value, 0); | 632 DCHECK_GE(value, 0); |
| 593 DCHECK(!timestamp.is_null()); | 633 DCHECK(!timestamp.is_null()); |
| 594 } | 634 } |
| 595 | 635 |
| 596 NetworkQualityEstimator::Observation::~Observation() { | 636 NetworkQualityEstimator::Observation::~Observation() { |
| 597 } | 637 } |
| 598 | 638 |
| 599 NetworkQualityEstimator::ObservationBuffer::ObservationBuffer( | 639 NetworkQualityEstimator::ObservationBuffer::ObservationBuffer( |
| 600 double weight_multiplier_per_second) | 640 double weight_multiplier_per_second) |
| 601 : weight_multiplier_per_second_(weight_multiplier_per_second) { | 641 : weight_multiplier_per_second_(weight_multiplier_per_second) { |
| (...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 796 cached_network_qualities_.find(current_network_id_); | 836 cached_network_qualities_.find(current_network_id_); |
| 797 | 837 |
| 798 if (it == cached_network_qualities_.end()) | 838 if (it == cached_network_qualities_.end()) |
| 799 return false; | 839 return false; |
| 800 | 840 |
| 801 NetworkQuality network_quality(it->second.network_quality()); | 841 NetworkQuality network_quality(it->second.network_quality()); |
| 802 | 842 |
| 803 DCHECK_NE(InvalidRTT(), network_quality.rtt()); | 843 DCHECK_NE(InvalidRTT(), network_quality.rtt()); |
| 804 DCHECK_NE(kInvalidThroughput, network_quality.downstream_throughput_kbps()); | 844 DCHECK_NE(kInvalidThroughput, network_quality.downstream_throughput_kbps()); |
| 805 | 845 |
| 806 downstream_throughput_kbps_observations_.AddObservation(Observation( | 846 Observation througphput_observation( |
| 807 network_quality.downstream_throughput_kbps(), base::TimeTicks::Now())); | 847 network_quality.downstream_throughput_kbps(), base::TimeTicks::Now(), |
| 808 rtt_msec_observations_.AddObservation(Observation( | 848 OBSERVATION_SOURCE_CACHED_ESTIMATE); |
| 809 network_quality.rtt().InMilliseconds(), base::TimeTicks::Now())); | 849 downstream_throughput_kbps_observations_.AddObservation( |
| 850 througphput_observation); |
| 851 NotifyObserversOfThroughput(througphput_observation); |
| 852 |
| 853 Observation rtt_observation(network_quality.rtt().InMilliseconds(), |
| 854 base::TimeTicks::Now(), |
| 855 OBSERVATION_SOURCE_CACHED_ESTIMATE); |
| 856 rtt_msec_observations_.AddObservation(rtt_observation); |
| 857 NotifyObserversOfRTT(rtt_observation); |
| 858 |
| 810 return true; | 859 return true; |
| 811 } | 860 } |
| 812 | 861 |
| 813 void NetworkQualityEstimator::OnUpdatedEstimateAvailable() { | 862 void NetworkQualityEstimator::OnUpdatedEstimateAvailable() { |
| 814 DCHECK(thread_checker_.CalledOnValidThread()); | 863 DCHECK(thread_checker_.CalledOnValidThread()); |
| 815 DCHECK(external_estimates_provider_); | 864 DCHECK(external_estimates_provider_); |
| 816 // TODO(tbansal): Query provider for the recent value. | 865 // TODO(tbansal): Query provider for the recent value. |
| 817 } | 866 } |
| 818 | 867 |
| 819 void NetworkQualityEstimator::CacheNetworkQualityEstimate() { | 868 void NetworkQualityEstimator::CacheNetworkQualityEstimate() { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 862 new SocketPerformanceWatcherTCP()); | 911 new SocketPerformanceWatcherTCP()); |
| 863 } | 912 } |
| 864 | 913 |
| 865 scoped_ptr<SocketPerformanceWatcher> | 914 scoped_ptr<SocketPerformanceWatcher> |
| 866 NetworkQualityEstimator::CreateUDPSocketPerformanceWatcher() const { | 915 NetworkQualityEstimator::CreateUDPSocketPerformanceWatcher() const { |
| 867 DCHECK(thread_checker_.CalledOnValidThread()); | 916 DCHECK(thread_checker_.CalledOnValidThread()); |
| 868 return scoped_ptr<SocketPerformanceWatcher>( | 917 return scoped_ptr<SocketPerformanceWatcher>( |
| 869 new SocketPerformanceWatcherUDP()); | 918 new SocketPerformanceWatcherUDP()); |
| 870 } | 919 } |
| 871 | 920 |
| 921 void NetworkQualityEstimator::NotifyObserversOfRTT( |
| 922 const Observation& observation) { |
| 923 FOR_EACH_OBSERVER(RTTObserver, rtt_observer_list_, |
| 924 OnRTTObservation(observation.value, observation.timestamp, |
| 925 observation.source)); |
| 926 } |
| 927 |
| 928 void NetworkQualityEstimator::NotifyObserversOfThroughput( |
| 929 const Observation& observation) { |
| 930 FOR_EACH_OBSERVER( |
| 931 ThroughputObserver, throughput_observer_list_, |
| 932 OnThroughputObservation(observation.value, observation.timestamp, |
| 933 observation.source)); |
| 934 } |
| 935 |
| 872 NetworkQualityEstimator::CachedNetworkQuality::CachedNetworkQuality( | 936 NetworkQualityEstimator::CachedNetworkQuality::CachedNetworkQuality( |
| 873 const NetworkQuality& network_quality) | 937 const NetworkQuality& network_quality) |
| 874 : last_update_time_(base::TimeTicks::Now()), | 938 : last_update_time_(base::TimeTicks::Now()), |
| 875 network_quality_(network_quality) { | 939 network_quality_(network_quality) { |
| 876 } | 940 } |
| 877 | 941 |
| 878 NetworkQualityEstimator::CachedNetworkQuality::CachedNetworkQuality( | 942 NetworkQualityEstimator::CachedNetworkQuality::CachedNetworkQuality( |
| 879 const CachedNetworkQuality& other) | 943 const CachedNetworkQuality& other) |
| 880 : last_update_time_(other.last_update_time_), | 944 : last_update_time_(other.last_update_time_), |
| 881 network_quality_(other.network_quality_) { | 945 network_quality_(other.network_quality_) { |
| (...skipping 27 matching lines...) Expand all Loading... |
| 909 | 973 |
| 910 NetworkQualityEstimator::NetworkQuality& | 974 NetworkQualityEstimator::NetworkQuality& |
| 911 NetworkQualityEstimator::NetworkQuality:: | 975 NetworkQualityEstimator::NetworkQuality:: |
| 912 operator=(const NetworkQuality& other) { | 976 operator=(const NetworkQuality& other) { |
| 913 rtt_ = other.rtt_; | 977 rtt_ = other.rtt_; |
| 914 downstream_throughput_kbps_ = other.downstream_throughput_kbps_; | 978 downstream_throughput_kbps_ = other.downstream_throughput_kbps_; |
| 915 return *this; | 979 return *this; |
| 916 } | 980 } |
| 917 | 981 |
| 918 } // namespace net | 982 } // namespace net |
| OLD | NEW |