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: OBSERVATION_SOURCE_ |
| 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 OBSERVATION_SOURCE_URL_REQUEST, |
| 53 // The observation is taken from TCP statistics maintained by the kernel. |
| 54 OBSERVATION_SOURCE_TCP, |
| 55 // The observation is taken at the QUIC layer. |
| 56 OBSERVATION_SOURCE_QUIC, |
| 57 // The observation is a previously cached estimate of the metric. |
| 58 OBSERVATION_SOURCE_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 OBSERVATION_SOURCE_DEFAULT_FROM_PLATFORM, |
| 63 // The observation came from a Chromium-external source. |
| 64 OBSERVATION_SOURCE_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 throughtput 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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 const base::TimeTicks last_update_time_; | 298 const base::TimeTicks last_update_time_; |
216 | 299 |
217 // Quality of this cached network. | 300 // Quality of this cached network. |
218 const NetworkQuality network_quality_; | 301 const NetworkQuality network_quality_; |
219 | 302 |
220 private: | 303 private: |
221 DISALLOW_ASSIGN(CachedNetworkQuality); | 304 DISALLOW_ASSIGN(CachedNetworkQuality); |
222 }; | 305 }; |
223 | 306 |
224 // Records the round trip time or throughput observation, along with the time | 307 // Records the round trip time or throughput observation, along with the time |
225 // the observation was made. | 308 // the observation was made. The units of value are type specific. For round |
| 309 // trip time observations, the value is in milliseconds. For throughput, |
| 310 // the value is in kilobits per second. Observations can be made at several |
| 311 // places in the network stack, thus the observation source is provided as |
| 312 // well. |
226 struct NET_EXPORT_PRIVATE Observation { | 313 struct NET_EXPORT_PRIVATE Observation { |
227 Observation(int32_t value, base::TimeTicks timestamp); | 314 Observation(int32_t value, |
| 315 base::TimeTicks timestamp, |
| 316 ObservationSource source); |
228 ~Observation(); | 317 ~Observation(); |
229 | 318 |
230 // Value of the observation. | 319 // Value of the observation. |
231 const int32_t value; | 320 const int32_t value; |
232 | 321 |
233 // Time when the observation was taken. | 322 // Time when the observation was taken. |
234 const base::TimeTicks timestamp; | 323 const base::TimeTicks timestamp; |
| 324 |
| 325 // The source of the observation. |
| 326 const ObservationSource source; |
235 }; | 327 }; |
236 | 328 |
237 // Holds an observation and its weight. | 329 // Holds an observation and its weight. |
238 struct NET_EXPORT_PRIVATE WeightedObservation { | 330 struct NET_EXPORT_PRIVATE WeightedObservation { |
239 WeightedObservation(int32_t value, double weight) | 331 WeightedObservation(int32_t value, double weight) |
240 : value(value), weight(weight) {} | 332 : value(value), weight(weight) {} |
241 WeightedObservation(const WeightedObservation& other) | 333 WeightedObservation(const WeightedObservation& other) |
242 : WeightedObservation(other.value, other.weight) {} | 334 : WeightedObservation(other.value, other.weight) {} |
243 | 335 |
244 WeightedObservation& operator=(const WeightedObservation& other) { | 336 WeightedObservation& operator=(const WeightedObservation& other) { |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 const base::TimeTicks& begin_timestamp, | 472 const base::TimeTicks& begin_timestamp, |
381 int percentile) const; | 473 int percentile) const; |
382 | 474 |
383 // Returns the current network ID checking by calling the platform APIs. | 475 // Returns the current network ID checking by calling the platform APIs. |
384 // Virtualized for testing. | 476 // Virtualized for testing. |
385 virtual NetworkID GetCurrentNetworkID() const; | 477 virtual NetworkID GetCurrentNetworkID() const; |
386 | 478 |
387 // Writes the estimated quality of the current network to the cache. | 479 // Writes the estimated quality of the current network to the cache. |
388 void CacheNetworkQualityEstimate(); | 480 void CacheNetworkQualityEstimate(); |
389 | 481 |
| 482 void NotifyObserversOfRTT(const Observation& observation); |
| 483 |
| 484 void NotifyObserversOfThroughput(const Observation& observation); |
| 485 |
390 // Records the UMA related to RTT. | 486 // Records the UMA related to RTT. |
391 void RecordRTTUMA(int32_t estimated_value_msec, | 487 void RecordRTTUMA(int32_t estimated_value_msec, |
392 int32_t actual_value_msec) const; | 488 int32_t actual_value_msec) const; |
393 | 489 |
394 // Returns true only if |request| can be used for network quality estimation. | 490 // Returns true only if |request| can be used for network quality estimation. |
395 // Only the requests that go over network are considered to provide useful | 491 // Only the requests that go over network are considered to provide useful |
396 // observations. | 492 // observations. |
397 bool RequestProvidesUsefulObservations(const URLRequest& request) const; | 493 bool RequestProvidesUsefulObservations(const URLRequest& request) const; |
398 | 494 |
399 // Determines if the requests to local host can be used in estimating the | 495 // Determines if the requests to local host can be used in estimating the |
400 // network quality. Set to true only for tests. | 496 // network quality. Set to true only for tests. |
401 const bool allow_localhost_requests_; | 497 bool allow_localhost_requests_; |
402 | 498 |
403 // Determines if the responses smaller than |kMinTransferSizeInBytes| | 499 // Determines if the responses smaller than |kMinTransferSizeInBytes| |
404 // or shorter than |kMinTransferSizeInBytes| can be used in estimating the | 500 // or shorter than |kMinTransferSizeInBytes| can be used in estimating the |
405 // network quality. Set to true only for tests. | 501 // network quality. Set to true only for tests. |
406 const bool allow_small_responses_; | 502 bool allow_small_responses_; |
407 | 503 |
408 // Time when last connection change was observed. | 504 // Time when last connection change was observed. |
409 base::TimeTicks last_connection_change_; | 505 base::TimeTicks last_connection_change_; |
410 | 506 |
411 // ID of the current network. | 507 // ID of the current network. |
412 NetworkID current_network_id_; | 508 NetworkID current_network_id_; |
413 | 509 |
414 // Peak network quality (fastest round-trip-time (RTT) and highest | 510 // Peak network quality (fastest round-trip-time (RTT) and highest |
415 // downstream throughput) measured since last connectivity change. RTT is | 511 // downstream throughput) measured since last connectivity change. RTT is |
416 // measured from time the request is sent until the first byte received. | 512 // measured from time the request is sent until the first byte received. |
(...skipping 17 matching lines...) Expand all Loading... |
434 NetworkQuality | 530 NetworkQuality |
435 default_observations_[NetworkChangeNotifier::CONNECTION_LAST + 1]; | 531 default_observations_[NetworkChangeNotifier::CONNECTION_LAST + 1]; |
436 | 532 |
437 // Estimated network quality. Updated on mainframe requests. | 533 // Estimated network quality. Updated on mainframe requests. |
438 NetworkQuality estimated_median_network_quality_; | 534 NetworkQuality estimated_median_network_quality_; |
439 | 535 |
440 // ExternalEstimateProvider that provides network quality using operating | 536 // ExternalEstimateProvider that provides network quality using operating |
441 // system APIs. May be NULL. | 537 // system APIs. May be NULL. |
442 const scoped_ptr<ExternalEstimateProvider> external_estimates_provider_; | 538 const scoped_ptr<ExternalEstimateProvider> external_estimates_provider_; |
443 | 539 |
| 540 // Observer lists for round trip times and throughput measurements. |
| 541 base::ObserverList<RTTObserver> rtt_observer_list_; |
| 542 base::ObserverList<ThroughputObserver> throughput_observer_list_; |
| 543 |
444 base::ThreadChecker thread_checker_; | 544 base::ThreadChecker thread_checker_; |
445 | 545 |
446 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); | 546 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |
447 }; | 547 }; |
448 | 548 |
449 } // namespace net | 549 } // namespace net |
450 | 550 |
451 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 551 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
OLD | NEW |