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 #ifndef NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 5 #ifndef NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
| 6 #define NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 6 #define NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <deque> | 10 #include <deque> |
| 11 #include <map> | 11 #include <map> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/ref_counted.h" | |
| 16 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/observer_list.h" | |
| 17 #include "base/threading/thread_checker.h" | 19 #include "base/threading/thread_checker.h" |
| 18 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 19 #include "net/base/external_estimate_provider.h" | 21 #include "net/base/external_estimate_provider.h" |
| 20 #include "net/base/net_export.h" | 22 #include "net/base/net_export.h" |
| 21 #include "net/base/network_change_notifier.h" | 23 #include "net/base/network_change_notifier.h" |
| 22 | 24 |
| 23 namespace net { | 25 namespace net { |
| 24 | 26 |
| 25 class URLRequest; | 27 class URLRequest; |
| 26 | 28 |
| 27 // NetworkQualityEstimator provides network quality estimates (quality of the | 29 // NetworkQualityEstimator provides network quality estimates (quality of the |
| 28 // full paths to all origins that have been connected to). | 30 // full paths to all origins that have been connected to). |
| 29 // The estimates are based on the observed organic traffic. | 31 // The estimates are based on the observed organic traffic. |
| 30 // A NetworkQualityEstimator instance is attached to URLRequestContexts and | 32 // A NetworkQualityEstimator instance is attached to URLRequestContexts and |
| 31 // observes the traffic of URLRequests spawned from the URLRequestContexts. | 33 // observes the traffic of URLRequests spawned from the URLRequestContexts. |
| 32 // A single instance of NQE can be attached to multiple URLRequestContexts, | 34 // A single instance of NQE can be attached to multiple URLRequestContexts, |
| 33 // thereby increasing the single NQE instance's accuracy by providing more | 35 // thereby increasing the single NQE instance's accuracy by providing more |
| 34 // observed traffic characteristics. | 36 // observed traffic characteristics. |
| 35 class NET_EXPORT_PRIVATE NetworkQualityEstimator | 37 class NET_EXPORT_PRIVATE NetworkQualityEstimator |
| 36 : public NetworkChangeNotifier::ConnectionTypeObserver, | 38 : public NetworkChangeNotifier::ConnectionTypeObserver, |
| 37 public ExternalEstimateProvider::UpdatedEstimateDelegate { | 39 public ExternalEstimateProvider::UpdatedEstimateDelegate { |
| 38 public: | 40 public: |
| 41 // On Android, a Java counterpart will be generated for this enum. | |
| 42 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net | |
| 43 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: NetworkQualityObservationSource | |
| 44 // GENERATED_JAVA_PREFIX_TO_STRIP: OBSERVATION_SOURCE_ | |
| 45 enum ObservationSource { | |
| 46 OBSERVATION_SOURCE_URL_REQUEST, | |
| 47 OBSERVATION_SOURCE_TCP, | |
| 48 OBSERVATION_SOURCE_QUIC, | |
| 49 OBSERVATION_SOURCE_CACHED_ESTIMATE, | |
| 50 OBSERVATION_SOURCE_DEFAULT_FROM_PLATFORM, | |
|
pauljensen
2015/09/02 16:54:48
Each of these needs a comment explaining it.
bengr
2015/09/14 21:04:35
Done.
| |
| 51 OBSERVATION_SOURCE_EXTERNAL_ESTIMATE | |
| 52 }; | |
| 53 | |
| 54 // Observes measurements of round trip time. | |
| 55 class NET_EXPORT_PRIVATE RTTObserver { | |
| 56 public: | |
| 57 // Will be called when a new RTT observation is available. The round trip | |
| 58 // times is specified in milliseconds. | |
|
pauljensen
2015/09/02 16:54:48
times->time
bengr
2015/09/14 21:04:35
Done.
| |
| 59 virtual void OnRTTObservation(int32_t rtt_ms, | |
| 60 const base::TimeTicks& timestamp, | |
|
pauljensen
2015/09/02 16:54:48
You might want to explain what |timestamp| is a ti
bengr
2015/09/14 21:04:35
Right. I wanted to decouple this so that there wou
| |
| 61 ObservationSource source) = 0; | |
| 62 | |
| 63 protected: | |
| 64 RTTObserver() {} | |
| 65 virtual ~RTTObserver() {} | |
| 66 | |
| 67 private: | |
| 68 DISALLOW_COPY_AND_ASSIGN(RTTObserver); | |
| 69 }; | |
| 70 | |
| 71 // Observes measurements of throughput. | |
| 72 class NET_EXPORT_PRIVATE ThroughputObserver { | |
| 73 public: | |
| 74 // Will be called when a new throughput observation is available. | |
| 75 // Throughput is specified in kilobits per second. | |
| 76 virtual void OnThroughputObservation(int32_t throughput_kbps, | |
| 77 const base::TimeTicks& timestamp, | |
| 78 ObservationSource source) = 0; | |
| 79 | |
| 80 protected: | |
| 81 ThroughputObserver() {} | |
| 82 virtual ~ThroughputObserver() {} | |
| 83 | |
| 84 private: | |
| 85 DISALLOW_COPY_AND_ASSIGN(ThroughputObserver); | |
| 86 }; | |
| 87 | |
| 39 // Creates a new NetworkQualityEstimator. | 88 // Creates a new NetworkQualityEstimator. |
| 40 // |variation_params| is the map containing all field trial parameters | 89 // |variation_params| is the map containing all field trial parameters |
| 41 // related to NetworkQualityEstimator field trial. | 90 // related to NetworkQualityEstimator field trial. |
| 42 // |external_estimates_provider| may be NULL. | 91 // |external_estimates_provider| may be NULL. |
| 43 NetworkQualityEstimator( | 92 NetworkQualityEstimator( |
| 44 scoped_ptr<ExternalEstimateProvider> external_estimates_provider, | 93 scoped_ptr<ExternalEstimateProvider> external_estimates_provider, |
| 45 const std::map<std::string, std::string>& variation_params); | 94 const std::map<std::string, std::string>& variation_params); |
| 46 | 95 |
| 47 ~NetworkQualityEstimator() override; | 96 ~NetworkQualityEstimator() override; |
| 48 | 97 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 69 virtual bool GetRecentMedianRTT(const base::TimeTicks& begin_timestamp, | 118 virtual bool GetRecentMedianRTT(const base::TimeTicks& begin_timestamp, |
| 70 base::TimeDelta* rtt) const; | 119 base::TimeDelta* rtt) const; |
| 71 | 120 |
| 72 // Returns true if median downstream throughput is available and sets |kbps| | 121 // Returns true if median downstream throughput is available and sets |kbps| |
| 73 // to the median of downstream Kbps observations since |begin_timestamp|. | 122 // to the median of downstream Kbps observations since |begin_timestamp|. |
| 74 // Virtualized for testing. |kbps| should not be null. | 123 // Virtualized for testing. |kbps| should not be null. |
| 75 virtual bool GetRecentMedianDownlinkThroughputKbps( | 124 virtual bool GetRecentMedianDownlinkThroughputKbps( |
| 76 const base::TimeTicks& begin_timestamp, | 125 const base::TimeTicks& begin_timestamp, |
| 77 int32_t* kbps) const; | 126 int32_t* kbps) const; |
| 78 | 127 |
| 128 // Adds |rtt_observer| to the list of round trip time observers. Must be | |
| 129 // called on the IO thread. | |
| 130 void AddRTTObserver(RTTObserver* rtt_observer); | |
| 131 | |
| 132 // Removes |rtt_observer| from the list of round trip time observers if it | |
| 133 // is on the list of observers. Must be called on the IO thread. | |
| 134 void RemoveRTTObserver(RTTObserver* rtt_observer); | |
| 135 | |
| 136 // Adds |throughput_observer| to the list of throughput observers. Must be | |
| 137 // called on the IO thread. | |
| 138 void AddThroughputObserver(ThroughputObserver* throughput_observer); | |
| 139 | |
| 140 // Removes |throughput_observer| from the list of throughput observers if it | |
| 141 // is on the list of observers. Must be called on the IO thread. | |
| 142 void RemoveThroughputObserver(ThroughputObserver* throughput_observer); | |
| 143 | |
| 144 // Configure for testing. If true, |allow_local_host_requests| will include | |
| 145 // observations from localhost, and |allow_smaller_responses| will remove | |
| 146 // the requirement of throughtput observations that response size be above | |
| 147 // a threshold. | |
| 148 void ConfigureForTests(bool allow_local_host_requests, | |
| 149 bool allow_smaller_responses); | |
| 150 | |
| 79 protected: | 151 protected: |
| 80 // NetworkID is used to uniquely identify a network. | 152 // NetworkID is used to uniquely identify a network. |
| 81 // For the purpose of network quality estimation and caching, a network is | 153 // For the purpose of network quality estimation and caching, a network is |
| 82 // uniquely identified by a combination of |type| and | 154 // uniquely identified by a combination of |type| and |
| 83 // |id|. This approach is unable to distinguish networks with | 155 // |id|. This approach is unable to distinguish networks with |
| 84 // same name (e.g., different Wi-Fi networks with same SSID). | 156 // same name (e.g., different Wi-Fi networks with same SSID). |
| 85 // This is a protected member to expose it to tests. | 157 // This is a protected member to expose it to tests. |
| 86 struct NET_EXPORT_PRIVATE NetworkID { | 158 struct NET_EXPORT_PRIVATE NetworkID { |
| 87 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) | 159 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) |
| 88 : type(type), id(id) {} | 160 : type(type), id(id) {} |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 206 const base::TimeTicks last_update_time_; | 278 const base::TimeTicks last_update_time_; |
| 207 | 279 |
| 208 // Quality of this cached network. | 280 // Quality of this cached network. |
| 209 const NetworkQuality network_quality_; | 281 const NetworkQuality network_quality_; |
| 210 | 282 |
| 211 private: | 283 private: |
| 212 DISALLOW_ASSIGN(CachedNetworkQuality); | 284 DISALLOW_ASSIGN(CachedNetworkQuality); |
| 213 }; | 285 }; |
| 214 | 286 |
| 215 // Records the round trip time or throughput observation, along with the time | 287 // Records the round trip time or throughput observation, along with the time |
| 216 // the observation was made. | 288 // the observation was made. The units of value are type specific. For round |
| 289 // trip time observations, the value is in milliseconds. For throughput, | |
| 290 // the value is in kilobits per second. Observations can be made at several | |
| 291 // places in the network stack, thus the observation source is provided as | |
| 292 // well. | |
| 217 struct NET_EXPORT_PRIVATE Observation { | 293 struct NET_EXPORT_PRIVATE Observation { |
| 218 Observation(int32_t value, base::TimeTicks timestamp); | 294 Observation(int32_t value, |
| 295 base::TimeTicks timestamp, | |
| 296 ObservationSource source); | |
| 219 ~Observation(); | 297 ~Observation(); |
| 220 | 298 |
| 221 // Value of the observation. | 299 // Value of the observation. |
| 222 const int32_t value; | 300 const int32_t value; |
| 223 | 301 |
| 224 // Time when the observation was taken. | 302 // Time when the observation was taken. |
| 225 const base::TimeTicks timestamp; | 303 const base::TimeTicks timestamp; |
| 304 | |
| 305 // The source of the observation. | |
| 306 const ObservationSource source; | |
| 226 }; | 307 }; |
| 227 | 308 |
| 228 // Holds an observation and its weight. | 309 // Holds an observation and its weight. |
| 229 struct NET_EXPORT_PRIVATE WeightedObservation { | 310 struct NET_EXPORT_PRIVATE WeightedObservation { |
| 230 WeightedObservation(int32_t value, double weight) | 311 WeightedObservation(int32_t value, double weight) |
| 231 : value(value), weight(weight) {} | 312 : value(value), weight(weight) {} |
| 232 WeightedObservation(const WeightedObservation& other) | 313 WeightedObservation(const WeightedObservation& other) |
| 233 : WeightedObservation(other.value, other.weight) {} | 314 : WeightedObservation(other.value, other.weight) {} |
| 234 | 315 |
| 235 WeightedObservation& operator=(const WeightedObservation& other) { | 316 WeightedObservation& operator=(const WeightedObservation& other) { |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 371 const base::TimeTicks& begin_timestamp, | 452 const base::TimeTicks& begin_timestamp, |
| 372 int percentile) const; | 453 int percentile) const; |
| 373 | 454 |
| 374 // Returns the current network ID checking by calling the platform APIs. | 455 // Returns the current network ID checking by calling the platform APIs. |
| 375 // Virtualized for testing. | 456 // Virtualized for testing. |
| 376 virtual NetworkID GetCurrentNetworkID() const; | 457 virtual NetworkID GetCurrentNetworkID() const; |
| 377 | 458 |
| 378 // Writes the estimated quality of the current network to the cache. | 459 // Writes the estimated quality of the current network to the cache. |
| 379 void CacheNetworkQualityEstimate(); | 460 void CacheNetworkQualityEstimate(); |
| 380 | 461 |
| 462 void NotifyObserversOfRTT(const Observation& observation); | |
| 463 | |
| 464 void NotifyObserversOfThroughput(const Observation& observation); | |
| 465 | |
| 381 // Records the UMA related to RTT. | 466 // Records the UMA related to RTT. |
| 382 void RecordRTTUMA(int32_t estimated_value_msec, | 467 void RecordRTTUMA(int32_t estimated_value_msec, |
| 383 int32_t actual_value_msec) const; | 468 int32_t actual_value_msec) const; |
| 384 | 469 |
| 385 // Returns true only if |request| can be used for network quality estimation. | 470 // Returns true only if |request| can be used for network quality estimation. |
| 386 // Only the requests that go over network are considered to provide useful | 471 // Only the requests that go over network are considered to provide useful |
| 387 // observations. | 472 // observations. |
| 388 bool RequestProvidesUsefulObservations(const URLRequest& request) const; | 473 bool RequestProvidesUsefulObservations(const URLRequest& request) const; |
| 389 | 474 |
| 390 // Determines if the requests to local host can be used in estimating the | 475 // Determines if the requests to local host can be used in estimating the |
| 391 // network quality. Set to true only for tests. | 476 // network quality. Set to true only for tests. |
| 392 const bool allow_localhost_requests_; | 477 bool allow_localhost_requests_; |
| 393 | 478 |
| 394 // Determines if the responses smaller than |kMinTransferSizeInBytes| | 479 // Determines if the responses smaller than |kMinTransferSizeInBytes| |
| 395 // or shorter than |kMinTransferSizeInBytes| can be used in estimating the | 480 // or shorter than |kMinTransferSizeInBytes| can be used in estimating the |
| 396 // network quality. Set to true only for tests. | 481 // network quality. Set to true only for tests. |
| 397 const bool allow_small_responses_; | 482 bool allow_small_responses_; |
| 398 | 483 |
| 399 // Time when last connection change was observed. | 484 // Time when last connection change was observed. |
| 400 base::TimeTicks last_connection_change_; | 485 base::TimeTicks last_connection_change_; |
| 401 | 486 |
| 402 // ID of the current network. | 487 // ID of the current network. |
| 403 NetworkID current_network_id_; | 488 NetworkID current_network_id_; |
| 404 | 489 |
| 405 // Peak network quality (fastest round-trip-time (RTT) and highest | 490 // Peak network quality (fastest round-trip-time (RTT) and highest |
| 406 // downstream throughput) measured since last connectivity change. RTT is | 491 // downstream throughput) measured since last connectivity change. RTT is |
| 407 // measured from time the request is sent until the first byte received. | 492 // measured from time the request is sent until the first byte received. |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 425 NetworkQuality | 510 NetworkQuality |
| 426 default_observations_[NetworkChangeNotifier::CONNECTION_LAST + 1]; | 511 default_observations_[NetworkChangeNotifier::CONNECTION_LAST + 1]; |
| 427 | 512 |
| 428 // Estimated network quality. Updated on mainframe requests. | 513 // Estimated network quality. Updated on mainframe requests. |
| 429 NetworkQuality estimated_median_network_quality_; | 514 NetworkQuality estimated_median_network_quality_; |
| 430 | 515 |
| 431 // ExternalEstimateProvider that provides network quality using operating | 516 // ExternalEstimateProvider that provides network quality using operating |
| 432 // system APIs. May be NULL. | 517 // system APIs. May be NULL. |
| 433 const scoped_ptr<ExternalEstimateProvider> external_estimates_provider_; | 518 const scoped_ptr<ExternalEstimateProvider> external_estimates_provider_; |
| 434 | 519 |
| 520 // Observer lists for round trip times and throughput measurements. | |
| 521 base::ObserverList<RTTObserver> rtt_observer_list_; | |
| 522 base::ObserverList<ThroughputObserver> throughput_observer_list_; | |
| 523 | |
| 435 base::ThreadChecker thread_checker_; | 524 base::ThreadChecker thread_checker_; |
| 436 | 525 |
| 437 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); | 526 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |
| 438 }; | 527 }; |
| 439 | 528 |
| 440 } // namespace net | 529 } // namespace net |
| 441 | 530 |
| 442 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 531 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
| OLD | NEW |