Chromium Code Reviews| 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/nqe/network_quality_estimator.h" | 5 #include "net/nqe/network_quality_estimator.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cmath> | 8 #include <cmath> |
| 9 #include <limits> | 9 #include <limits> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 133 | 133 |
| 134 bool GetValueForVariationParam( | 134 bool GetValueForVariationParam( |
| 135 const std::map<std::string, std::string>& variation_params, | 135 const std::map<std::string, std::string>& variation_params, |
| 136 const std::string& parameter_name, | 136 const std::string& parameter_name, |
| 137 int32_t* variations_value) { | 137 int32_t* variations_value) { |
| 138 auto it = variation_params.find(parameter_name); | 138 auto it = variation_params.find(parameter_name); |
| 139 return it != variation_params.end() && | 139 return it != variation_params.end() && |
| 140 base::StringToInt(it->second, variations_value); | 140 base::StringToInt(it->second, variations_value); |
| 141 } | 141 } |
| 142 | 142 |
| 143 // Returns the value of the |parameter_name| in |variation_params|. If | |
| 144 // |parameter_name| is not set in |variation_params|, then |default_value| is | |
| 145 // returned. | |
| 146 int32_t GetValueForVariationParamWithDefaultValue( | |
| 147 const std::map<std::string, std::string>& variation_params, | |
| 148 const std::string& parameter_name, | |
| 149 int32_t default_value) { | |
| 150 int32_t variations_value; | |
| 151 return GetValueForVariationParam(variation_params, parameter_name, | |
| 152 &variations_value) | |
| 153 ? variations_value | |
| 154 : default_value; | |
| 155 } | |
| 156 | |
| 143 net::NetworkQualityObservationSource ProtocolSourceToObservationSource( | 157 net::NetworkQualityObservationSource ProtocolSourceToObservationSource( |
| 144 net::SocketPerformanceWatcherFactory::Protocol protocol) { | 158 net::SocketPerformanceWatcherFactory::Protocol protocol) { |
| 145 switch (protocol) { | 159 switch (protocol) { |
| 146 case net::SocketPerformanceWatcherFactory::PROTOCOL_TCP: | 160 case net::SocketPerformanceWatcherFactory::PROTOCOL_TCP: |
| 147 return net::NETWORK_QUALITY_OBSERVATION_SOURCE_TCP; | 161 return net::NETWORK_QUALITY_OBSERVATION_SOURCE_TCP; |
| 148 case net::SocketPerformanceWatcherFactory::PROTOCOL_QUIC: | 162 case net::SocketPerformanceWatcherFactory::PROTOCOL_QUIC: |
| 149 return net::NETWORK_QUALITY_OBSERVATION_SOURCE_QUIC; | 163 return net::NETWORK_QUALITY_OBSERVATION_SOURCE_QUIC; |
| 150 } | 164 } |
| 151 NOTREACHED(); | 165 NOTREACHED(); |
| 152 return net::NETWORK_QUALITY_OBSERVATION_SOURCE_TCP; | 166 return net::NETWORK_QUALITY_OBSERVATION_SOURCE_TCP; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 246 | 260 |
| 247 NetworkQualityEstimator::NetworkQualityEstimator( | 261 NetworkQualityEstimator::NetworkQualityEstimator( |
| 248 std::unique_ptr<ExternalEstimateProvider> external_estimates_provider, | 262 std::unique_ptr<ExternalEstimateProvider> external_estimates_provider, |
| 249 const std::map<std::string, std::string>& variation_params, | 263 const std::map<std::string, std::string>& variation_params, |
| 250 bool use_local_host_requests_for_tests, | 264 bool use_local_host_requests_for_tests, |
| 251 bool use_smaller_responses_for_tests) | 265 bool use_smaller_responses_for_tests) |
| 252 : use_localhost_requests_(use_local_host_requests_for_tests), | 266 : use_localhost_requests_(use_local_host_requests_for_tests), |
| 253 use_small_responses_(use_smaller_responses_for_tests), | 267 use_small_responses_(use_smaller_responses_for_tests), |
| 254 weight_multiplier_per_second_( | 268 weight_multiplier_per_second_( |
| 255 GetWeightMultiplierPerSecond(variation_params)), | 269 GetWeightMultiplierPerSecond(variation_params)), |
| 270 algorithm_(GetValueForVariationParamWithDefaultValue(variation_params, | |
|
bengr
2016/06/02 22:59:43
You should add a comment somewhere that you defaul
tbansal1
2016/06/03 00:50:10
Done.
| |
| 271 "algorithm", | |
| 272 0)), | |
| 256 tick_clock_(new base::DefaultTickClock()), | 273 tick_clock_(new base::DefaultTickClock()), |
| 257 effective_connection_type_recomputation_interval_( | 274 effective_connection_type_recomputation_interval_( |
| 258 base::TimeDelta::FromSeconds(15)), | 275 base::TimeDelta::FromSeconds(15)), |
| 259 last_connection_change_(tick_clock_->NowTicks()), | 276 last_connection_change_(tick_clock_->NowTicks()), |
| 260 current_network_id_( | 277 current_network_id_( |
| 261 NetworkID(NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN, | 278 NetworkID(NetworkChangeNotifier::ConnectionType::CONNECTION_UNKNOWN, |
| 262 std::string())), | 279 std::string())), |
| 263 downstream_throughput_kbps_observations_(weight_multiplier_per_second_), | 280 downstream_throughput_kbps_observations_(weight_multiplier_per_second_), |
| 264 rtt_observations_(weight_multiplier_per_second_), | 281 rtt_observations_(weight_multiplier_per_second_), |
| 265 external_estimate_provider_(std::move(external_estimates_provider)), | 282 external_estimate_provider_(std::move(external_estimates_provider)), |
| 266 effective_connection_type_(EFFECTIVE_CONNECTION_TYPE_UNKNOWN), | 283 effective_connection_type_(EFFECTIVE_CONNECTION_TYPE_UNKNOWN), |
| 267 weak_ptr_factory_(this) { | 284 weak_ptr_factory_(this) { |
| 268 static_assert(kDefaultHalfLifeSeconds > 0, | 285 static_assert(kDefaultHalfLifeSeconds > 0, |
| 269 "Default half life duration must be > 0"); | 286 "Default half life duration must be > 0"); |
| 270 static_assert(kMaximumNetworkQualityCacheSize > 0, | 287 static_assert(kMaximumNetworkQualityCacheSize > 0, |
| 271 "Size of the network quality cache must be > 0"); | 288 "Size of the network quality cache must be > 0"); |
| 272 // This limit should not be increased unless the logic for removing the | 289 // This limit should not be increased unless the logic for removing the |
| 273 // oldest cache entry is rewritten to use a doubly-linked-list LRU queue. | 290 // oldest cache entry is rewritten to use a doubly-linked-list LRU queue. |
| 274 static_assert(kMaximumNetworkQualityCacheSize <= 10, | 291 static_assert(kMaximumNetworkQualityCacheSize <= 10, |
| 275 "Size of the network quality cache must <= 10"); | 292 "Size of the network quality cache must <= 10"); |
| 293 DCHECK_GE(algorithm_, 0); | |
|
bengr
2016/06/02 22:59:43
Shouldn't this be DCHECK_EQ? algorithm_ should be
tbansal1
2016/06/03 00:50:10
I removed it since with strings it does not really
| |
| 276 | 294 |
| 277 ObtainOperatingParams(variation_params); | 295 ObtainOperatingParams(variation_params); |
| 278 ObtainEffectiveConnectionTypeModelParams(variation_params); | 296 ObtainEffectiveConnectionTypeModelParams(variation_params); |
| 279 NetworkChangeNotifier::AddConnectionTypeObserver(this); | 297 NetworkChangeNotifier::AddConnectionTypeObserver(this); |
| 280 if (external_estimate_provider_) { | 298 if (external_estimate_provider_) { |
| 281 RecordExternalEstimateProviderMetrics( | 299 RecordExternalEstimateProviderMetrics( |
| 282 EXTERNAL_ESTIMATE_PROVIDER_STATUS_AVAILABLE); | 300 EXTERNAL_ESTIMATE_PROVIDER_STATUS_AVAILABLE); |
| 283 external_estimate_provider_->SetUpdatedEstimateDelegate(this); | 301 external_estimate_provider_->SetUpdatedEstimateDelegate(this); |
| 284 } else { | 302 } else { |
| 285 RecordExternalEstimateProviderMetrics( | 303 RecordExternalEstimateProviderMetrics( |
| (...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 748 NetworkQualityEstimator::GetEffectiveConnectionType() const { | 766 NetworkQualityEstimator::GetEffectiveConnectionType() const { |
| 749 DCHECK(thread_checker_.CalledOnValidThread()); | 767 DCHECK(thread_checker_.CalledOnValidThread()); |
| 750 return GetRecentEffectiveConnectionType(base::TimeTicks()); | 768 return GetRecentEffectiveConnectionType(base::TimeTicks()); |
| 751 } | 769 } |
| 752 | 770 |
| 753 NetworkQualityEstimator::EffectiveConnectionType | 771 NetworkQualityEstimator::EffectiveConnectionType |
| 754 NetworkQualityEstimator::GetRecentEffectiveConnectionType( | 772 NetworkQualityEstimator::GetRecentEffectiveConnectionType( |
| 755 const base::TimeTicks& start_time) const { | 773 const base::TimeTicks& start_time) const { |
| 756 DCHECK(thread_checker_.CalledOnValidThread()); | 774 DCHECK(thread_checker_.CalledOnValidThread()); |
| 757 | 775 |
| 776 switch (algorithm_) { | |
| 777 case 0: | |
| 778 return GetRecentEffectiveConnectionTypeAlgorithm0(start_time); | |
| 779 default: | |
| 780 NOTREACHED(); | |
| 781 } | |
| 782 return GetRecentEffectiveConnectionTypeAlgorithm0(start_time); | |
| 783 } | |
| 784 | |
| 785 NetworkQualityEstimator::EffectiveConnectionType | |
| 786 NetworkQualityEstimator::GetRecentEffectiveConnectionTypeAlgorithm0( | |
| 787 const base::TimeTicks& start_time) const { | |
| 788 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 789 DCHECK_EQ(0, algorithm_); | |
| 790 | |
| 758 // If the device is currently offline, then return | 791 // If the device is currently offline, then return |
| 759 // EFFECTIVE_CONNECTION_TYPE_OFFLINE. | 792 // EFFECTIVE_CONNECTION_TYPE_OFFLINE. |
| 760 if (GetCurrentNetworkID().type == NetworkChangeNotifier::CONNECTION_NONE) | 793 if (GetCurrentNetworkID().type == NetworkChangeNotifier::CONNECTION_NONE) |
| 761 return EFFECTIVE_CONNECTION_TYPE_OFFLINE; | 794 return EFFECTIVE_CONNECTION_TYPE_OFFLINE; |
| 762 | 795 |
| 763 base::TimeDelta http_rtt = nqe::internal::InvalidRTT(); | 796 base::TimeDelta http_rtt = nqe::internal::InvalidRTT(); |
| 764 if (!GetRecentHttpRTTMedian(start_time, &http_rtt)) | 797 if (!GetRecentHttpRTTMedian(start_time, &http_rtt)) |
| 765 http_rtt = nqe::internal::InvalidRTT(); | 798 http_rtt = nqe::internal::InvalidRTT(); |
| 766 | 799 |
| 767 int32_t kbps = nqe::internal::kInvalidThroughput; | 800 int32_t kbps = nqe::internal::kInvalidThroughput; |
| 768 if (!GetRecentMedianDownlinkThroughputKbps(start_time, &kbps)) | 801 if (!GetRecentMedianDownlinkThroughputKbps(start_time, &kbps)) |
| 769 kbps = nqe::internal::kInvalidThroughput; | 802 kbps = nqe::internal::kInvalidThroughput; |
| 770 | 803 |
| 771 if (http_rtt == nqe::internal::InvalidRTT() && | 804 if (http_rtt == nqe::internal::InvalidRTT() || |
| 772 kbps == nqe::internal::kInvalidThroughput) { | 805 kbps == nqe::internal::kInvalidThroughput) { |
| 773 // Quality of the current network is unknown. | 806 // Quality of the current network is unknown. |
| 774 return EFFECTIVE_CONNECTION_TYPE_UNKNOWN; | 807 return EFFECTIVE_CONNECTION_TYPE_UNKNOWN; |
| 775 } | 808 } |
| 776 | 809 |
| 777 // Search from the slowest connection type to the fastest to find the | 810 // Search from the slowest connection type to the fastest to find the |
| 778 // EffectiveConnectionType that best matches the current connection's | 811 // EffectiveConnectionType that best matches the current connection's |
| 779 // performance. The match is done by comparing RTT and throughput. | 812 // performance. The match is done by comparing RTT and throughput. |
| 780 for (size_t i = 0; i < EFFECTIVE_CONNECTION_TYPE_LAST; ++i) { | 813 for (size_t i = 0; i < EFFECTIVE_CONNECTION_TYPE_LAST; ++i) { |
| 781 EffectiveConnectionType type = static_cast<EffectiveConnectionType>(i); | 814 EffectiveConnectionType type = static_cast<EffectiveConnectionType>(i); |
| (...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1190 NotifyObserversOfEffectiveConnectionTypeChanged() { | 1223 NotifyObserversOfEffectiveConnectionTypeChanged() { |
| 1191 DCHECK(thread_checker_.CalledOnValidThread()); | 1224 DCHECK(thread_checker_.CalledOnValidThread()); |
| 1192 | 1225 |
| 1193 // TODO(tbansal): Add hysteresis in the notification. | 1226 // TODO(tbansal): Add hysteresis in the notification. |
| 1194 FOR_EACH_OBSERVER( | 1227 FOR_EACH_OBSERVER( |
| 1195 EffectiveConnectionTypeObserver, effective_connection_type_observer_list_, | 1228 EffectiveConnectionTypeObserver, effective_connection_type_observer_list_, |
| 1196 OnEffectiveConnectionTypeChanged(effective_connection_type_)); | 1229 OnEffectiveConnectionTypeChanged(effective_connection_type_)); |
| 1197 } | 1230 } |
| 1198 | 1231 |
| 1199 } // namespace net | 1232 } // namespace net |
| OLD | NEW |