| 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 #include "net/base/socket_performance_watcher.h" | 24 #include "net/base/socket_performance_watcher.h" |
| 23 #include "net/base/socket_performance_watcher_factory.h" | 25 #include "net/base/socket_performance_watcher_factory.h" |
| 24 | 26 |
| 25 namespace net { | 27 namespace net { |
| 26 | 28 |
| 27 class URLRequest; | 29 class URLRequest; |
| 28 | 30 |
| 29 // NetworkQualityEstimator provides network quality estimates (quality of the | 31 // NetworkQualityEstimator provides network quality estimates (quality of the |
| 30 // full paths to all origins that have been connected to). | 32 // full paths to all origins that have been connected to). |
| 31 // The estimates are based on the observed organic traffic. | 33 // The estimates are based on the observed organic traffic. |
| 32 // A NetworkQualityEstimator instance is attached to URLRequestContexts and | 34 // A NetworkQualityEstimator instance is attached to URLRequestContexts and |
| 33 // observes the traffic of URLRequests spawned from the URLRequestContexts. | 35 // observes the traffic of URLRequests spawned from the URLRequestContexts. |
| 34 // A single instance of NQE can be attached to multiple URLRequestContexts, | 36 // A single instance of NQE can be attached to multiple URLRequestContexts, |
| 35 // thereby increasing the single NQE instance's accuracy by providing more | 37 // thereby increasing the single NQE instance's accuracy by providing more |
| 36 // observed traffic characteristics. | 38 // observed traffic characteristics. |
| 37 class NET_EXPORT_PRIVATE NetworkQualityEstimator | 39 class NET_EXPORT_PRIVATE NetworkQualityEstimator |
| 38 : public NetworkChangeNotifier::ConnectionTypeObserver, | 40 : public NetworkChangeNotifier::ConnectionTypeObserver, |
| 39 public ExternalEstimateProvider::UpdatedEstimateDelegate, | 41 public ExternalEstimateProvider::UpdatedEstimateDelegate, |
| 40 public SocketPerformanceWatcherFactory { | 42 public SocketPerformanceWatcherFactory { |
| 41 public: | 43 public: |
| 44 // On Android, a Java counterpart will be generated for this enum. |
| 45 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net |
| 46 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: NetworkQualityObservationSource |
| 47 // GENERATED_JAVA_PREFIX_TO_STRIP: |
| 48 enum ObservationSource { |
| 49 // The observation was taken at the request layer, e.g., a round trip time |
| 50 // is recorded as the time between the request being sent and the first byte |
| 51 // being received. |
| 52 URL_REQUEST, |
| 53 // The observation is taken from TCP statistics maintained by the kernel. |
| 54 TCP, |
| 55 // The observation is taken at the QUIC layer. |
| 56 QUIC, |
| 57 // The observation is a previously cached estimate of the metric. |
| 58 CACHED_ESTIMATE, |
| 59 // The observation is derived from network connection information provided |
| 60 // by the platform. For example, typical RTT and throughput values are used |
| 61 // for a given type of network connection. |
| 62 DEFAULT_FROM_PLATFORM, |
| 63 // The observation came from a Chromium-external source. |
| 64 EXTERNAL_ESTIMATE |
| 65 }; |
| 66 |
| 67 // Observes measurements of round trip time. |
| 68 class NET_EXPORT_PRIVATE RTTObserver { |
| 69 public: |
| 70 // Will be called when a new RTT observation is available. The round trip |
| 71 // time is specified in milliseconds. The time when the observation was |
| 72 // taken and the source of the observation are provided. |
| 73 virtual void OnRTTObservation(int32_t rtt_ms, |
| 74 const base::TimeTicks& timestamp, |
| 75 ObservationSource source) = 0; |
| 76 |
| 77 protected: |
| 78 RTTObserver() {} |
| 79 virtual ~RTTObserver() {} |
| 80 |
| 81 private: |
| 82 DISALLOW_COPY_AND_ASSIGN(RTTObserver); |
| 83 }; |
| 84 |
| 85 // Observes measurements of throughput. |
| 86 class NET_EXPORT_PRIVATE ThroughputObserver { |
| 87 public: |
| 88 // Will be called when a new throughput observation is available. |
| 89 // Throughput is specified in kilobits per second. |
| 90 virtual void OnThroughputObservation(int32_t throughput_kbps, |
| 91 const base::TimeTicks& timestamp, |
| 92 ObservationSource source) = 0; |
| 93 |
| 94 protected: |
| 95 ThroughputObserver() {} |
| 96 virtual ~ThroughputObserver() {} |
| 97 |
| 98 private: |
| 99 DISALLOW_COPY_AND_ASSIGN(ThroughputObserver); |
| 100 }; |
| 101 |
| 42 // Creates a new NetworkQualityEstimator. | 102 // Creates a new NetworkQualityEstimator. |
| 43 // |variation_params| is the map containing all field trial parameters | 103 // |variation_params| is the map containing all field trial parameters |
| 44 // related to NetworkQualityEstimator field trial. | 104 // related to NetworkQualityEstimator field trial. |
| 45 // |external_estimates_provider| may be NULL. | 105 // |external_estimates_provider| may be NULL. |
| 46 NetworkQualityEstimator( | 106 NetworkQualityEstimator( |
| 47 scoped_ptr<ExternalEstimateProvider> external_estimates_provider, | 107 scoped_ptr<ExternalEstimateProvider> external_estimates_provider, |
| 48 const std::map<std::string, std::string>& variation_params); | 108 const std::map<std::string, std::string>& variation_params); |
| 49 | 109 |
| 50 ~NetworkQualityEstimator() override; | 110 ~NetworkQualityEstimator() override; |
| 51 | 111 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 78 virtual bool GetRecentMedianDownlinkThroughputKbps( | 138 virtual bool GetRecentMedianDownlinkThroughputKbps( |
| 79 const base::TimeTicks& begin_timestamp, | 139 const base::TimeTicks& begin_timestamp, |
| 80 int32_t* kbps) const; | 140 int32_t* kbps) const; |
| 81 | 141 |
| 82 // SocketPerformanceWatcherFactory implementation: | 142 // SocketPerformanceWatcherFactory implementation: |
| 83 scoped_ptr<SocketPerformanceWatcher> CreateTCPSocketPerformanceWatcher() | 143 scoped_ptr<SocketPerformanceWatcher> CreateTCPSocketPerformanceWatcher() |
| 84 const override; | 144 const override; |
| 85 scoped_ptr<SocketPerformanceWatcher> CreateUDPSocketPerformanceWatcher() | 145 scoped_ptr<SocketPerformanceWatcher> CreateUDPSocketPerformanceWatcher() |
| 86 const override; | 146 const override; |
| 87 | 147 |
| 148 // Adds |rtt_observer| to the list of round trip time observers. Must be |
| 149 // called on the IO thread. |
| 150 void AddRTTObserver(RTTObserver* rtt_observer); |
| 151 |
| 152 // Removes |rtt_observer| from the list of round trip time observers if it |
| 153 // is on the list of observers. Must be called on the IO thread. |
| 154 void RemoveRTTObserver(RTTObserver* rtt_observer); |
| 155 |
| 156 // Adds |throughput_observer| to the list of throughput observers. Must be |
| 157 // called on the IO thread. |
| 158 void AddThroughputObserver(ThroughputObserver* throughput_observer); |
| 159 |
| 160 // Removes |throughput_observer| from the list of throughput observers if it |
| 161 // is on the list of observers. Must be called on the IO thread. |
| 162 void RemoveThroughputObserver(ThroughputObserver* throughput_observer); |
| 163 |
| 164 // Configure for testing. If true, |allow_local_host_requests| will include |
| 165 // observations from localhost, and |allow_smaller_responses| will remove |
| 166 // the requirement of throughput observations that response size be above |
| 167 // a threshold. |
| 168 void ConfigureForTests(bool allow_local_host_requests, |
| 169 bool allow_smaller_responses); |
| 170 |
| 88 protected: | 171 protected: |
| 89 // NetworkID is used to uniquely identify a network. | 172 // NetworkID is used to uniquely identify a network. |
| 90 // For the purpose of network quality estimation and caching, a network is | 173 // For the purpose of network quality estimation and caching, a network is |
| 91 // uniquely identified by a combination of |type| and | 174 // uniquely identified by a combination of |type| and |
| 92 // |id|. This approach is unable to distinguish networks with | 175 // |id|. This approach is unable to distinguish networks with |
| 93 // same name (e.g., different Wi-Fi networks with same SSID). | 176 // same name (e.g., different Wi-Fi networks with same SSID). |
| 94 // This is a protected member to expose it to tests. | 177 // This is a protected member to expose it to tests. |
| 95 struct NET_EXPORT_PRIVATE NetworkID { | 178 struct NET_EXPORT_PRIVATE NetworkID { |
| 96 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) | 179 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) |
| 97 : type(type), id(id) {} | 180 : type(type), id(id) {} |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 const base::TimeTicks last_update_time_; | 305 const base::TimeTicks last_update_time_; |
| 223 | 306 |
| 224 // Quality of this cached network. | 307 // Quality of this cached network. |
| 225 const NetworkQuality network_quality_; | 308 const NetworkQuality network_quality_; |
| 226 | 309 |
| 227 private: | 310 private: |
| 228 DISALLOW_ASSIGN(CachedNetworkQuality); | 311 DISALLOW_ASSIGN(CachedNetworkQuality); |
| 229 }; | 312 }; |
| 230 | 313 |
| 231 // Records the round trip time or throughput observation, along with the time | 314 // Records the round trip time or throughput observation, along with the time |
| 232 // the observation was made. | 315 // the observation was made. The units of value are type specific. For round |
| 316 // trip time observations, the value is in milliseconds. For throughput, |
| 317 // the value is in kilobits per second. Observations can be made at several |
| 318 // places in the network stack, thus the observation source is provided as |
| 319 // well. |
| 233 struct NET_EXPORT_PRIVATE Observation { | 320 struct NET_EXPORT_PRIVATE Observation { |
| 234 Observation(int32_t value, base::TimeTicks timestamp); | 321 Observation(int32_t value, |
| 322 base::TimeTicks timestamp, |
| 323 ObservationSource source); |
| 235 ~Observation(); | 324 ~Observation(); |
| 236 | 325 |
| 237 // Value of the observation. | 326 // Value of the observation. |
| 238 const int32_t value; | 327 const int32_t value; |
| 239 | 328 |
| 240 // Time when the observation was taken. | 329 // Time when the observation was taken. |
| 241 const base::TimeTicks timestamp; | 330 const base::TimeTicks timestamp; |
| 331 |
| 332 // The source of the observation. |
| 333 const ObservationSource source; |
| 242 }; | 334 }; |
| 243 | 335 |
| 244 // Holds an observation and its weight. | 336 // Holds an observation and its weight. |
| 245 struct NET_EXPORT_PRIVATE WeightedObservation { | 337 struct NET_EXPORT_PRIVATE WeightedObservation { |
| 246 WeightedObservation(int32_t value, double weight) | 338 WeightedObservation(int32_t value, double weight) |
| 247 : value(value), weight(weight) {} | 339 : value(value), weight(weight) {} |
| 248 WeightedObservation(const WeightedObservation& other) | 340 WeightedObservation(const WeightedObservation& other) |
| 249 : WeightedObservation(other.value, other.weight) {} | 341 : WeightedObservation(other.value, other.weight) {} |
| 250 | 342 |
| 251 WeightedObservation& operator=(const WeightedObservation& other) { | 343 WeightedObservation& operator=(const WeightedObservation& other) { |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 const base::TimeTicks& begin_timestamp, | 485 const base::TimeTicks& begin_timestamp, |
| 394 int percentile) const; | 486 int percentile) const; |
| 395 | 487 |
| 396 // Returns the current network ID checking by calling the platform APIs. | 488 // Returns the current network ID checking by calling the platform APIs. |
| 397 // Virtualized for testing. | 489 // Virtualized for testing. |
| 398 virtual NetworkID GetCurrentNetworkID() const; | 490 virtual NetworkID GetCurrentNetworkID() const; |
| 399 | 491 |
| 400 // Writes the estimated quality of the current network to the cache. | 492 // Writes the estimated quality of the current network to the cache. |
| 401 void CacheNetworkQualityEstimate(); | 493 void CacheNetworkQualityEstimate(); |
| 402 | 494 |
| 495 void NotifyObserversOfRTT(const Observation& observation); |
| 496 |
| 497 void NotifyObserversOfThroughput(const Observation& observation); |
| 498 |
| 403 // Records the UMA related to RTT. | 499 // Records the UMA related to RTT. |
| 404 void RecordRTTUMA(int32_t estimated_value_msec, | 500 void RecordRTTUMA(int32_t estimated_value_msec, |
| 405 int32_t actual_value_msec) const; | 501 int32_t actual_value_msec) const; |
| 406 | 502 |
| 407 // Returns true only if |request| can be used for network quality estimation. | 503 // Returns true only if |request| can be used for network quality estimation. |
| 408 // Only the requests that go over network are considered to provide useful | 504 // Only the requests that go over network are considered to provide useful |
| 409 // observations. | 505 // observations. |
| 410 bool RequestProvidesUsefulObservations(const URLRequest& request) const; | 506 bool RequestProvidesUsefulObservations(const URLRequest& request) const; |
| 411 | 507 |
| 412 // Values of external estimate provider status. This enum must remain | 508 // Values of external estimate provider status. This enum must remain |
| 413 // synchronized with the enum of the same name in | 509 // synchronized with the enum of the same name in |
| 414 // metrics/histograms/histograms.xml. | 510 // metrics/histograms/histograms.xml. |
| 415 enum NQEExternalEstimateProviderStatus { | 511 enum NQEExternalEstimateProviderStatus { |
| 416 EXTERNAL_ESTIMATE_PROVIDER_STATUS_NOT_AVAILABLE, | 512 EXTERNAL_ESTIMATE_PROVIDER_STATUS_NOT_AVAILABLE, |
| 417 EXTERNAL_ESTIMATE_PROVIDER_STATUS_AVAILABLE, | 513 EXTERNAL_ESTIMATE_PROVIDER_STATUS_AVAILABLE, |
| 418 EXTERNAL_ESTIMATE_PROVIDER_STATUS_QUERIED, | 514 EXTERNAL_ESTIMATE_PROVIDER_STATUS_QUERIED, |
| 419 EXTERNAL_ESTIMATE_PROVIDER_STATUS_QUERY_SUCCESSFUL, | 515 EXTERNAL_ESTIMATE_PROVIDER_STATUS_QUERY_SUCCESSFUL, |
| 420 EXTERNAL_ESTIMATE_PROVIDER_STATUS_CALLBACK, | 516 EXTERNAL_ESTIMATE_PROVIDER_STATUS_CALLBACK, |
| 421 EXTERNAL_ESTIMATE_PROVIDER_STATUS_BOUNDARY | 517 EXTERNAL_ESTIMATE_PROVIDER_STATUS_BOUNDARY |
| 422 }; | 518 }; |
| 423 | 519 |
| 424 // Records the metrics related to external estimate provider. | 520 // Records the metrics related to external estimate provider. |
| 425 void RecordExternalEstimateProviderMetrics( | 521 void RecordExternalEstimateProviderMetrics( |
| 426 NQEExternalEstimateProviderStatus status) const; | 522 NQEExternalEstimateProviderStatus status) const; |
| 427 | 523 |
| 428 // Determines if the requests to local host can be used in estimating the | 524 // Determines if the requests to local host can be used in estimating the |
| 429 // network quality. Set to true only for tests. | 525 // network quality. Set to true only for tests. |
| 430 const bool allow_localhost_requests_; | 526 bool allow_localhost_requests_; |
| 431 | 527 |
| 432 // Determines if the responses smaller than |kMinTransferSizeInBytes| | 528 // Determines if the responses smaller than |kMinTransferSizeInBytes| |
| 433 // or shorter than |kMinTransferSizeInBytes| can be used in estimating the | 529 // or shorter than |kMinTransferSizeInBytes| can be used in estimating the |
| 434 // network quality. Set to true only for tests. | 530 // network quality. Set to true only for tests. |
| 435 const bool allow_small_responses_; | 531 bool allow_small_responses_; |
| 436 | 532 |
| 437 // Time when last connection change was observed. | 533 // Time when last connection change was observed. |
| 438 base::TimeTicks last_connection_change_; | 534 base::TimeTicks last_connection_change_; |
| 439 | 535 |
| 440 // ID of the current network. | 536 // ID of the current network. |
| 441 NetworkID current_network_id_; | 537 NetworkID current_network_id_; |
| 442 | 538 |
| 443 // Peak network quality (fastest round-trip-time (RTT) and highest | 539 // Peak network quality (fastest round-trip-time (RTT) and highest |
| 444 // downstream throughput) measured since last connectivity change. RTT is | 540 // downstream throughput) measured since last connectivity change. RTT is |
| 445 // measured from time the request is sent until the first byte received. | 541 // measured from time the request is sent until the first byte received. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 463 NetworkQuality | 559 NetworkQuality |
| 464 default_observations_[NetworkChangeNotifier::CONNECTION_LAST + 1]; | 560 default_observations_[NetworkChangeNotifier::CONNECTION_LAST + 1]; |
| 465 | 561 |
| 466 // Estimated network quality. Updated on mainframe requests. | 562 // Estimated network quality. Updated on mainframe requests. |
| 467 NetworkQuality estimated_median_network_quality_; | 563 NetworkQuality estimated_median_network_quality_; |
| 468 | 564 |
| 469 // ExternalEstimateProvider that provides network quality using operating | 565 // ExternalEstimateProvider that provides network quality using operating |
| 470 // system APIs. May be NULL. | 566 // system APIs. May be NULL. |
| 471 const scoped_ptr<ExternalEstimateProvider> external_estimate_provider_; | 567 const scoped_ptr<ExternalEstimateProvider> external_estimate_provider_; |
| 472 | 568 |
| 569 // Observer lists for round trip times and throughput measurements. |
| 570 base::ObserverList<RTTObserver> rtt_observer_list_; |
| 571 base::ObserverList<ThroughputObserver> throughput_observer_list_; |
| 572 |
| 473 base::ThreadChecker thread_checker_; | 573 base::ThreadChecker thread_checker_; |
| 474 | 574 |
| 475 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); | 575 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |
| 476 }; | 576 }; |
| 477 | 577 |
| 478 } // namespace net | 578 } // namespace net |
| 479 | 579 |
| 480 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 580 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
| OLD | NEW |