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 |
| 110 // Construct a NetworkQualityEstimator instance allowing for test |
| 111 // configuration. Registers for network type change notifications so estimates |
| 112 // can be kept network specific. |
| 113 // |external_estimates_provider| may be NULL. |
| 114 // |variation_params| is the map containing all field trial parameters for the |
| 115 // network quality estimator field trial. |
| 116 // |allow_local_host_requests_for_tests| should only be true when testing |
| 117 // against local HTTP server and allows the requests to local host to be |
| 118 // used for network quality estimation. |
| 119 // |allow_smaller_responses_for_tests| should only be true when testing. |
| 120 // Allows the responses smaller than |kMinTransferSizeInBytes| or shorter than |
| 121 // |kMinRequestDurationMicroseconds| to be used for network quality |
| 122 // estimation. |
| 123 NetworkQualityEstimator( |
| 124 scoped_ptr<ExternalEstimateProvider> external_estimates_provider, |
| 125 const std::map<std::string, std::string>& variation_params, |
| 126 bool allow_local_host_requests_for_tests, |
| 127 bool allow_smaller_responses_for_tests); |
| 128 |
50 ~NetworkQualityEstimator() override; | 129 ~NetworkQualityEstimator() override; |
51 | 130 |
52 // Returns true if RTT is available and sets |rtt| to estimated RTT. | 131 // Returns true if RTT is available and sets |rtt| to estimated RTT. |
53 // Virtualized for testing. |rtt| should not be null. | 132 // Virtualized for testing. |rtt| should not be null. |
54 virtual bool GetRTTEstimate(base::TimeDelta* rtt) const; | 133 virtual bool GetRTTEstimate(base::TimeDelta* rtt) const; |
55 | 134 |
56 // Returns true if downlink throughput is available and sets |kbps| to | 135 // Returns true if downlink throughput is available and sets |kbps| to |
57 // estimated downlink throughput (in Kilobits per second). | 136 // estimated downlink throughput (in Kilobits per second). |
58 // Virtualized for testing. |kbps| should not be null. | 137 // Virtualized for testing. |kbps| should not be null. |
59 virtual bool GetDownlinkThroughputKbpsEstimate(int32_t* kbps) const; | 138 virtual bool GetDownlinkThroughputKbpsEstimate(int32_t* kbps) const; |
(...skipping 18 matching lines...) Expand all Loading... |
78 virtual bool GetRecentMedianDownlinkThroughputKbps( | 157 virtual bool GetRecentMedianDownlinkThroughputKbps( |
79 const base::TimeTicks& begin_timestamp, | 158 const base::TimeTicks& begin_timestamp, |
80 int32_t* kbps) const; | 159 int32_t* kbps) const; |
81 | 160 |
82 // SocketPerformanceWatcherFactory implementation: | 161 // SocketPerformanceWatcherFactory implementation: |
83 scoped_ptr<SocketPerformanceWatcher> CreateTCPSocketPerformanceWatcher() | 162 scoped_ptr<SocketPerformanceWatcher> CreateTCPSocketPerformanceWatcher() |
84 const override; | 163 const override; |
85 scoped_ptr<SocketPerformanceWatcher> CreateUDPSocketPerformanceWatcher() | 164 scoped_ptr<SocketPerformanceWatcher> CreateUDPSocketPerformanceWatcher() |
86 const override; | 165 const override; |
87 | 166 |
| 167 // Adds |rtt_observer| to the list of round trip time observers. Must be |
| 168 // called on the IO thread. |
| 169 void AddRTTObserver(RTTObserver* rtt_observer); |
| 170 |
| 171 // Removes |rtt_observer| from the list of round trip time observers if it |
| 172 // is on the list of observers. Must be called on the IO thread. |
| 173 void RemoveRTTObserver(RTTObserver* rtt_observer); |
| 174 |
| 175 // Adds |throughput_observer| to the list of throughput observers. Must be |
| 176 // called on the IO thread. |
| 177 void AddThroughputObserver(ThroughputObserver* throughput_observer); |
| 178 |
| 179 // Removes |throughput_observer| from the list of throughput observers if it |
| 180 // is on the list of observers. Must be called on the IO thread. |
| 181 void RemoveThroughputObserver(ThroughputObserver* throughput_observer); |
| 182 |
88 protected: | 183 protected: |
89 // NetworkID is used to uniquely identify a network. | 184 // NetworkID is used to uniquely identify a network. |
90 // For the purpose of network quality estimation and caching, a network is | 185 // For the purpose of network quality estimation and caching, a network is |
91 // uniquely identified by a combination of |type| and | 186 // uniquely identified by a combination of |type| and |
92 // |id|. This approach is unable to distinguish networks with | 187 // |id|. This approach is unable to distinguish networks with |
93 // same name (e.g., different Wi-Fi networks with same SSID). | 188 // same name (e.g., different Wi-Fi networks with same SSID). |
94 // This is a protected member to expose it to tests. | 189 // This is a protected member to expose it to tests. |
95 struct NET_EXPORT_PRIVATE NetworkID { | 190 struct NET_EXPORT_PRIVATE NetworkID { |
96 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) | 191 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) |
97 : type(type), id(id) {} | 192 : type(type), id(id) {} |
(...skipping 18 matching lines...) Expand all Loading... |
116 // - Wi-Fi SSID if the device is connected to a Wi-Fi access point and the | 211 // - Wi-Fi SSID if the device is connected to a Wi-Fi access point and the |
117 // SSID name is available, or | 212 // SSID name is available, or |
118 // - MCC/MNC code of the cellular carrier if the device is connected to a | 213 // - MCC/MNC code of the cellular carrier if the device is connected to a |
119 // cellular network, or | 214 // cellular network, or |
120 // - "Ethernet" in case the device is connected to ethernet. | 215 // - "Ethernet" in case the device is connected to ethernet. |
121 // - An empty string in all other cases or if the network name is not | 216 // - An empty string in all other cases or if the network name is not |
122 // exposed by platform APIs. | 217 // exposed by platform APIs. |
123 std::string id; | 218 std::string id; |
124 }; | 219 }; |
125 | 220 |
126 // Construct a NetworkQualityEstimator instance allowing for test | |
127 // configuration. Registers for network type change notifications so estimates | |
128 // can be kept network specific. | |
129 // |external_estimates_provider| may be NULL. | |
130 // |variation_params| is the map containing all field trial parameters for the | |
131 // network quality estimator field trial. | |
132 // |allow_local_host_requests_for_tests| should only be true when testing | |
133 // against local HTTP server and allows the requests to local host to be | |
134 // used for network quality estimation. | |
135 // |allow_smaller_responses_for_tests| should only be true when testing. | |
136 // Allows the responses smaller than |kMinTransferSizeInBytes| or shorter than | |
137 // |kMinRequestDurationMicroseconds| to be used for network quality | |
138 // estimation. | |
139 NetworkQualityEstimator( | |
140 scoped_ptr<ExternalEstimateProvider> external_estimates_provider, | |
141 const std::map<std::string, std::string>& variation_params, | |
142 bool allow_local_host_requests_for_tests, | |
143 bool allow_smaller_responses_for_tests); | |
144 | |
145 // Returns true if the cached network quality estimate was successfully read. | 221 // Returns true if the cached network quality estimate was successfully read. |
146 bool ReadCachedNetworkQualityEstimate(); | 222 bool ReadCachedNetworkQualityEstimate(); |
147 | 223 |
148 // NetworkChangeNotifier::ConnectionTypeObserver implementation: | 224 // NetworkChangeNotifier::ConnectionTypeObserver implementation: |
149 void OnConnectionTypeChanged( | 225 void OnConnectionTypeChanged( |
150 NetworkChangeNotifier::ConnectionType type) override; | 226 NetworkChangeNotifier::ConnectionType type) override; |
151 | 227 |
152 // ExternalEstimateProvider::UpdatedEstimateObserver implementation. | 228 // ExternalEstimateProvider::UpdatedEstimateObserver implementation. |
153 void OnUpdatedEstimateAvailable() override; | 229 void OnUpdatedEstimateAvailable() override; |
154 | 230 |
(...skipping 10 matching lines...) Expand all Loading... |
165 PercentileDifferentTimestamps); | 241 PercentileDifferentTimestamps); |
166 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, ComputedPercentiles); | 242 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, ComputedPercentiles); |
167 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestCaching); | 243 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestCaching); |
168 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | 244 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
169 TestLRUCacheMaximumSize); | 245 TestLRUCacheMaximumSize); |
170 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestGetMedianRTTSince); | 246 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestGetMedianRTTSince); |
171 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | 247 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
172 TestExternalEstimateProvider); | 248 TestExternalEstimateProvider); |
173 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, | 249 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
174 TestExternalEstimateProviderMergeEstimates); | 250 TestExternalEstimateProviderMergeEstimates); |
| 251 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestObservers); |
175 | 252 |
176 // NetworkQuality is used to cache the quality of a network connection. | 253 // NetworkQuality is used to cache the quality of a network connection. |
177 class NET_EXPORT_PRIVATE NetworkQuality { | 254 class NET_EXPORT_PRIVATE NetworkQuality { |
178 public: | 255 public: |
179 NetworkQuality(); | 256 NetworkQuality(); |
180 // |rtt| is the estimate of the round trip time. | 257 // |rtt| is the estimate of the round trip time. |
181 // |downstream_throughput_kbps| is the estimate of the downstream | 258 // |downstream_throughput_kbps| is the estimate of the downstream |
182 // throughput. | 259 // throughput. |
183 NetworkQuality(const base::TimeDelta& rtt, | 260 NetworkQuality(const base::TimeDelta& rtt, |
184 int32_t downstream_throughput_kbps); | 261 int32_t downstream_throughput_kbps); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 const base::TimeTicks last_update_time_; | 299 const base::TimeTicks last_update_time_; |
223 | 300 |
224 // Quality of this cached network. | 301 // Quality of this cached network. |
225 const NetworkQuality network_quality_; | 302 const NetworkQuality network_quality_; |
226 | 303 |
227 private: | 304 private: |
228 DISALLOW_ASSIGN(CachedNetworkQuality); | 305 DISALLOW_ASSIGN(CachedNetworkQuality); |
229 }; | 306 }; |
230 | 307 |
231 // Records the round trip time or throughput observation, along with the time | 308 // Records the round trip time or throughput observation, along with the time |
232 // the observation was made. | 309 // the observation was made. The units of value are type specific. For round |
| 310 // trip time observations, the value is in milliseconds. For throughput, |
| 311 // the value is in kilobits per second. Observations can be made at several |
| 312 // places in the network stack, thus the observation source is provided as |
| 313 // well. |
233 struct NET_EXPORT_PRIVATE Observation { | 314 struct NET_EXPORT_PRIVATE Observation { |
234 Observation(int32_t value, base::TimeTicks timestamp); | 315 Observation(int32_t value, |
| 316 base::TimeTicks timestamp, |
| 317 ObservationSource source); |
235 ~Observation(); | 318 ~Observation(); |
236 | 319 |
237 // Value of the observation. | 320 // Value of the observation. |
238 const int32_t value; | 321 const int32_t value; |
239 | 322 |
240 // Time when the observation was taken. | 323 // Time when the observation was taken. |
241 const base::TimeTicks timestamp; | 324 const base::TimeTicks timestamp; |
| 325 |
| 326 // The source of the observation. |
| 327 const ObservationSource source; |
242 }; | 328 }; |
243 | 329 |
244 // Holds an observation and its weight. | 330 // Holds an observation and its weight. |
245 struct NET_EXPORT_PRIVATE WeightedObservation { | 331 struct NET_EXPORT_PRIVATE WeightedObservation { |
246 WeightedObservation(int32_t value, double weight) | 332 WeightedObservation(int32_t value, double weight) |
247 : value(value), weight(weight) {} | 333 : value(value), weight(weight) {} |
248 WeightedObservation(const WeightedObservation& other) | 334 WeightedObservation(const WeightedObservation& other) |
249 : WeightedObservation(other.value, other.weight) {} | 335 : WeightedObservation(other.value, other.weight) {} |
250 | 336 |
251 WeightedObservation& operator=(const WeightedObservation& other) { | 337 WeightedObservation& operator=(const WeightedObservation& other) { |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 const base::TimeTicks& begin_timestamp, | 479 const base::TimeTicks& begin_timestamp, |
394 int percentile) const; | 480 int percentile) const; |
395 | 481 |
396 // Returns the current network ID checking by calling the platform APIs. | 482 // Returns the current network ID checking by calling the platform APIs. |
397 // Virtualized for testing. | 483 // Virtualized for testing. |
398 virtual NetworkID GetCurrentNetworkID() const; | 484 virtual NetworkID GetCurrentNetworkID() const; |
399 | 485 |
400 // Writes the estimated quality of the current network to the cache. | 486 // Writes the estimated quality of the current network to the cache. |
401 void CacheNetworkQualityEstimate(); | 487 void CacheNetworkQualityEstimate(); |
402 | 488 |
| 489 void NotifyObserversOfRTT(const Observation& observation); |
| 490 |
| 491 void NotifyObserversOfThroughput(const Observation& observation); |
| 492 |
403 // Records the UMA related to RTT. | 493 // Records the UMA related to RTT. |
404 void RecordRTTUMA(int32_t estimated_value_msec, | 494 void RecordRTTUMA(int32_t estimated_value_msec, |
405 int32_t actual_value_msec) const; | 495 int32_t actual_value_msec) const; |
406 | 496 |
407 // Returns true only if |request| can be used for network quality estimation. | 497 // 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 | 498 // Only the requests that go over network are considered to provide useful |
409 // observations. | 499 // observations. |
410 bool RequestProvidesUsefulObservations(const URLRequest& request) const; | 500 bool RequestProvidesUsefulObservations(const URLRequest& request) const; |
411 | 501 |
412 // Values of external estimate provider status. This enum must remain | 502 // Values of external estimate provider status. This enum must remain |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
463 NetworkQuality | 553 NetworkQuality |
464 default_observations_[NetworkChangeNotifier::CONNECTION_LAST + 1]; | 554 default_observations_[NetworkChangeNotifier::CONNECTION_LAST + 1]; |
465 | 555 |
466 // Estimated network quality. Updated on mainframe requests. | 556 // Estimated network quality. Updated on mainframe requests. |
467 NetworkQuality estimated_median_network_quality_; | 557 NetworkQuality estimated_median_network_quality_; |
468 | 558 |
469 // ExternalEstimateProvider that provides network quality using operating | 559 // ExternalEstimateProvider that provides network quality using operating |
470 // system APIs. May be NULL. | 560 // system APIs. May be NULL. |
471 const scoped_ptr<ExternalEstimateProvider> external_estimate_provider_; | 561 const scoped_ptr<ExternalEstimateProvider> external_estimate_provider_; |
472 | 562 |
| 563 // Observer lists for round trip times and throughput measurements. |
| 564 base::ObserverList<RTTObserver> rtt_observer_list_; |
| 565 base::ObserverList<ThroughputObserver> throughput_observer_list_; |
| 566 |
473 base::ThreadChecker thread_checker_; | 567 base::ThreadChecker thread_checker_; |
474 | 568 |
475 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); | 569 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |
476 }; | 570 }; |
477 | 571 |
478 } // namespace net | 572 } // namespace net |
479 | 573 |
480 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 574 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
OLD | NEW |