tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
| 6 | #define NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | |
tbansal | 6f840fc5 | 2015-06-13 03:30:34 | [diff] [blame] | 10 | #include <deque> |
tbansal | b177b539 | 2015-06-25 11:13:02 | [diff] [blame] | 11 | #include <map> |
tbansal | 75540a5 | 2015-07-15 22:18:21 | [diff] [blame] | 12 | #include <string> |
jsbell | cea42a5 | 2015-11-30 23:50:25 | [diff] [blame^] | 13 | #include <tuple> |
tbansal | 6f840fc5 | 2015-06-13 03:30:34 | [diff] [blame] | 14 | |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 15 | #include "base/gtest_prod_util.h" |
| 16 | #include "base/macros.h" |
bengr | f1b6738e | 2015-10-07 01:21:30 | [diff] [blame] | 17 | #include "base/memory/ref_counted.h" |
tbansal | 75540a5 | 2015-07-15 22:18:21 | [diff] [blame] | 18 | #include "base/memory/scoped_ptr.h" |
bengr | f1b6738e | 2015-10-07 01:21:30 | [diff] [blame] | 19 | #include "base/observer_list.h" |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 20 | #include "base/threading/thread_checker.h" |
| 21 | #include "base/time/time.h" |
tbansal | 1c92d5b | 2015-08-14 20:14:43 | [diff] [blame] | 22 | #include "net/base/external_estimate_provider.h" |
tbansal | 6f840fc5 | 2015-06-13 03:30:34 | [diff] [blame] | 23 | #include "net/base/net_export.h" |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 24 | #include "net/base/network_change_notifier.h" |
tbansal | ba8f411 | 2015-09-03 21:57:19 | [diff] [blame] | 25 | #include "net/base/socket_performance_watcher.h" |
| 26 | #include "net/base/socket_performance_watcher_factory.h" |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 27 | |
| 28 | namespace net { |
| 29 | |
tbansal | 79ed5cd | 2015-08-10 18:53:56 | [diff] [blame] | 30 | class URLRequest; |
| 31 | |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 32 | // NetworkQualityEstimator provides network quality estimates (quality of the |
| 33 | // full paths to all origins that have been connected to). |
| 34 | // The estimates are based on the observed organic traffic. |
| 35 | // A NetworkQualityEstimator instance is attached to URLRequestContexts and |
| 36 | // observes the traffic of URLRequests spawned from the URLRequestContexts. |
| 37 | // A single instance of NQE can be attached to multiple URLRequestContexts, |
| 38 | // thereby increasing the single NQE instance's accuracy by providing more |
| 39 | // observed traffic characteristics. |
| 40 | class NET_EXPORT_PRIVATE NetworkQualityEstimator |
tbansal | 1c92d5b | 2015-08-14 20:14:43 | [diff] [blame] | 41 | : public NetworkChangeNotifier::ConnectionTypeObserver, |
tbansal | ba8f411 | 2015-09-03 21:57:19 | [diff] [blame] | 42 | public ExternalEstimateProvider::UpdatedEstimateDelegate, |
| 43 | public SocketPerformanceWatcherFactory { |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 44 | public: |
bengr | f1b6738e | 2015-10-07 01:21:30 | [diff] [blame] | 45 | // On Android, a Java counterpart will be generated for this enum. |
| 46 | // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net |
| 47 | // GENERATED_JAVA_CLASS_NAME_OVERRIDE: NetworkQualityObservationSource |
| 48 | // GENERATED_JAVA_PREFIX_TO_STRIP: |
| 49 | enum ObservationSource { |
| 50 | // The observation was taken at the request layer, e.g., a round trip time |
| 51 | // is recorded as the time between the request being sent and the first byte |
| 52 | // being received. |
| 53 | URL_REQUEST, |
| 54 | // The observation is taken from TCP statistics maintained by the kernel. |
| 55 | TCP, |
| 56 | // The observation is taken at the QUIC layer. |
| 57 | QUIC, |
| 58 | // The observation is a previously cached estimate of the metric. |
| 59 | CACHED_ESTIMATE, |
| 60 | // The observation is derived from network connection information provided |
| 61 | // by the platform. For example, typical RTT and throughput values are used |
| 62 | // for a given type of network connection. |
| 63 | DEFAULT_FROM_PLATFORM, |
| 64 | // The observation came from a Chromium-external source. |
| 65 | EXTERNAL_ESTIMATE |
| 66 | }; |
| 67 | |
| 68 | // Observes measurements of round trip time. |
| 69 | class NET_EXPORT_PRIVATE RTTObserver { |
| 70 | public: |
| 71 | // Will be called when a new RTT observation is available. The round trip |
| 72 | // time is specified in milliseconds. The time when the observation was |
| 73 | // taken and the source of the observation are provided. |
| 74 | virtual void OnRTTObservation(int32_t rtt_ms, |
| 75 | const base::TimeTicks& timestamp, |
| 76 | ObservationSource source) = 0; |
| 77 | |
| 78 | protected: |
| 79 | RTTObserver() {} |
| 80 | virtual ~RTTObserver() {} |
| 81 | |
| 82 | private: |
| 83 | DISALLOW_COPY_AND_ASSIGN(RTTObserver); |
| 84 | }; |
| 85 | |
| 86 | // Observes measurements of throughput. |
| 87 | class NET_EXPORT_PRIVATE ThroughputObserver { |
| 88 | public: |
| 89 | // Will be called when a new throughput observation is available. |
| 90 | // Throughput is specified in kilobits per second. |
| 91 | virtual void OnThroughputObservation(int32_t throughput_kbps, |
| 92 | const base::TimeTicks& timestamp, |
| 93 | ObservationSource source) = 0; |
| 94 | |
| 95 | protected: |
| 96 | ThroughputObserver() {} |
| 97 | virtual ~ThroughputObserver() {} |
| 98 | |
| 99 | private: |
| 100 | DISALLOW_COPY_AND_ASSIGN(ThroughputObserver); |
| 101 | }; |
| 102 | |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 103 | // Creates a new NetworkQualityEstimator. |
tbansal | b177b539 | 2015-06-25 11:13:02 | [diff] [blame] | 104 | // |variation_params| is the map containing all field trial parameters |
| 105 | // related to NetworkQualityEstimator field trial. |
tbansal | 1c92d5b | 2015-08-14 20:14:43 | [diff] [blame] | 106 | // |external_estimates_provider| may be NULL. |
| 107 | NetworkQualityEstimator( |
| 108 | scoped_ptr<ExternalEstimateProvider> external_estimates_provider, |
tbansal | b177b539 | 2015-06-25 11:13:02 | [diff] [blame] | 109 | const std::map<std::string, std::string>& variation_params); |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 110 | |
bengr | f1b6738e | 2015-10-07 01:21:30 | [diff] [blame] | 111 | // Construct a NetworkQualityEstimator instance allowing for test |
| 112 | // configuration. Registers for network type change notifications so estimates |
| 113 | // can be kept network specific. |
| 114 | // |external_estimates_provider| may be NULL. |
| 115 | // |variation_params| is the map containing all field trial parameters for the |
| 116 | // network quality estimator field trial. |
| 117 | // |allow_local_host_requests_for_tests| should only be true when testing |
| 118 | // against local HTTP server and allows the requests to local host to be |
| 119 | // used for network quality estimation. |
| 120 | // |allow_smaller_responses_for_tests| should only be true when testing. |
| 121 | // Allows the responses smaller than |kMinTransferSizeInBytes| or shorter than |
| 122 | // |kMinRequestDurationMicroseconds| to be used for network quality |
| 123 | // estimation. |
| 124 | NetworkQualityEstimator( |
| 125 | scoped_ptr<ExternalEstimateProvider> external_estimates_provider, |
| 126 | const std::map<std::string, std::string>& variation_params, |
| 127 | bool allow_local_host_requests_for_tests, |
| 128 | bool allow_smaller_responses_for_tests); |
| 129 | |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 130 | ~NetworkQualityEstimator() override; |
| 131 | |
rockot | f5eb575 | 2015-08-11 19:45:31 | [diff] [blame] | 132 | // Returns true if RTT is available and sets |rtt| to estimated RTT. |
tbansal | b658f388 | 2015-08-19 21:27:49 | [diff] [blame] | 133 | // Virtualized for testing. |rtt| should not be null. |
tbansal | 9533b6f | 2015-08-11 22:06:08 | [diff] [blame] | 134 | virtual bool GetRTTEstimate(base::TimeDelta* rtt) const; |
tbansal | 91fb588 | 2015-08-05 04:24:56 | [diff] [blame] | 135 | |
| 136 | // Returns true if downlink throughput is available and sets |kbps| to |
| 137 | // estimated downlink throughput (in Kilobits per second). |
tbansal | b658f388 | 2015-08-19 21:27:49 | [diff] [blame] | 138 | // Virtualized for testing. |kbps| should not be null. |
tbansal | 9533b6f | 2015-08-11 22:06:08 | [diff] [blame] | 139 | virtual bool GetDownlinkThroughputKbpsEstimate(int32_t* kbps) const; |
tbansal | 91fb588 | 2015-08-05 04:24:56 | [diff] [blame] | 140 | |
tbansal | 79ed5cd | 2015-08-10 18:53:56 | [diff] [blame] | 141 | // Notifies NetworkQualityEstimator that the response header of |request| has |
| 142 | // been received. |
| 143 | void NotifyHeadersReceived(const URLRequest& request); |
| 144 | |
| 145 | // Notifies NetworkQualityEstimator that the response body of |request| has |
| 146 | // been received. |
| 147 | void NotifyRequestCompleted(const URLRequest& request); |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 148 | |
tbansal | 9533b6f | 2015-08-11 22:06:08 | [diff] [blame] | 149 | // Returns true if median RTT is available and sets |rtt| to the median of |
| 150 | // RTT observations since |begin_timestamp|. |
tbansal | b658f388 | 2015-08-19 21:27:49 | [diff] [blame] | 151 | // Virtualized for testing. |rtt| should not be null. |
| 152 | virtual bool GetRecentMedianRTT(const base::TimeTicks& begin_timestamp, |
| 153 | base::TimeDelta* rtt) const; |
| 154 | |
| 155 | // Returns true if median downstream throughput is available and sets |kbps| |
| 156 | // to the median of downstream Kbps observations since |begin_timestamp|. |
| 157 | // Virtualized for testing. |kbps| should not be null. |
| 158 | virtual bool GetRecentMedianDownlinkThroughputKbps( |
| 159 | const base::TimeTicks& begin_timestamp, |
| 160 | int32_t* kbps) const; |
tbansal | f7c6d52 | 2015-08-05 21:59:36 | [diff] [blame] | 161 | |
tbansal | ba8f411 | 2015-09-03 21:57:19 | [diff] [blame] | 162 | // SocketPerformanceWatcherFactory implementation: |
tbansal | c8a94ea | 2015-11-02 23:58:51 | [diff] [blame] | 163 | scoped_ptr<SocketPerformanceWatcher> CreateSocketPerformanceWatcher( |
| 164 | const Protocol protocol) override; |
| 165 | void OnUpdatedRTTAvailable(const Protocol protocol, |
| 166 | const base::TimeDelta& rtt) override; |
tbansal | ba8f411 | 2015-09-03 21:57:19 | [diff] [blame] | 167 | |
bengr | f1b6738e | 2015-10-07 01:21:30 | [diff] [blame] | 168 | // Adds |rtt_observer| to the list of round trip time observers. Must be |
| 169 | // called on the IO thread. |
| 170 | void AddRTTObserver(RTTObserver* rtt_observer); |
| 171 | |
| 172 | // Removes |rtt_observer| from the list of round trip time observers if it |
| 173 | // is on the list of observers. Must be called on the IO thread. |
| 174 | void RemoveRTTObserver(RTTObserver* rtt_observer); |
| 175 | |
| 176 | // Adds |throughput_observer| to the list of throughput observers. Must be |
| 177 | // called on the IO thread. |
| 178 | void AddThroughputObserver(ThroughputObserver* throughput_observer); |
| 179 | |
| 180 | // Removes |throughput_observer| from the list of throughput observers if it |
| 181 | // is on the list of observers. Must be called on the IO thread. |
| 182 | void RemoveThroughputObserver(ThroughputObserver* throughput_observer); |
| 183 | |
tbansal | 75540a5 | 2015-07-15 22:18:21 | [diff] [blame] | 184 | protected: |
| 185 | // NetworkID is used to uniquely identify a network. |
| 186 | // For the purpose of network quality estimation and caching, a network is |
| 187 | // uniquely identified by a combination of |type| and |
| 188 | // |id|. This approach is unable to distinguish networks with |
| 189 | // same name (e.g., different Wi-Fi networks with same SSID). |
| 190 | // This is a protected member to expose it to tests. |
| 191 | struct NET_EXPORT_PRIVATE NetworkID { |
| 192 | NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) |
| 193 | : type(type), id(id) {} |
| 194 | NetworkID(const NetworkID& other) : type(other.type), id(other.id) {} |
| 195 | ~NetworkID() {} |
| 196 | |
| 197 | NetworkID& operator=(const NetworkID& other) { |
| 198 | type = other.type; |
| 199 | id = other.id; |
| 200 | return *this; |
| 201 | } |
| 202 | |
| 203 | // Overloaded because NetworkID is used as key in a map. |
| 204 | bool operator<(const NetworkID& other) const { |
jsbell | cea42a5 | 2015-11-30 23:50:25 | [diff] [blame^] | 205 | return std::tie(type, id) < std::tie(other.type, other.id); |
tbansal | 75540a5 | 2015-07-15 22:18:21 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // Connection type of the network. |
| 209 | NetworkChangeNotifier::ConnectionType type; |
| 210 | |
| 211 | // Name of this network. This is set to: |
| 212 | // - Wi-Fi SSID if the device is connected to a Wi-Fi access point and the |
| 213 | // SSID name is available, or |
| 214 | // - MCC/MNC code of the cellular carrier if the device is connected to a |
| 215 | // cellular network, or |
| 216 | // - "Ethernet" in case the device is connected to ethernet. |
| 217 | // - An empty string in all other cases or if the network name is not |
| 218 | // exposed by platform APIs. |
| 219 | std::string id; |
| 220 | }; |
| 221 | |
tbansal | 75540a5 | 2015-07-15 22:18:21 | [diff] [blame] | 222 | // Returns true if the cached network quality estimate was successfully read. |
| 223 | bool ReadCachedNetworkQualityEstimate(); |
| 224 | |
tbansal | ba8f411 | 2015-09-03 21:57:19 | [diff] [blame] | 225 | // NetworkChangeNotifier::ConnectionTypeObserver implementation: |
tbansal | 75540a5 | 2015-07-15 22:18:21 | [diff] [blame] | 226 | void OnConnectionTypeChanged( |
| 227 | NetworkChangeNotifier::ConnectionType type) override; |
| 228 | |
tbansal | 2c6b0d3 | 2015-09-17 03:49:02 | [diff] [blame] | 229 | // ExternalEstimateProvider::UpdatedEstimateObserver implementation. |
| 230 | void OnUpdatedEstimateAvailable() override; |
| 231 | |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 232 | private: |
tbansal | 6f840fc5 | 2015-06-13 03:30:34 | [diff] [blame] | 233 | FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); |
tbansal | 2e590c6 | 2015-07-09 00:22:02 | [diff] [blame] | 234 | FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestKbpsRTTUpdates); |
tbansal | 6f840fc5 | 2015-06-13 03:30:34 | [diff] [blame] | 235 | FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestAddObservation); |
tbansal | b177b539 | 2015-06-25 11:13:02 | [diff] [blame] | 236 | FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, ObtainOperatingParams); |
tbansal | 4418ad12 | 2015-06-26 19:06:33 | [diff] [blame] | 237 | FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, HalfLifeParam); |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 238 | FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator); |
tbansal | 11af8313 | 2015-06-24 20:34:22 | [diff] [blame] | 239 | FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
| 240 | PercentileSameTimestamps); |
| 241 | FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
| 242 | PercentileDifferentTimestamps); |
| 243 | FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, ComputedPercentiles); |
tbansal | 75540a5 | 2015-07-15 22:18:21 | [diff] [blame] | 244 | FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestCaching); |
| 245 | FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
| 246 | TestLRUCacheMaximumSize); |
tbansal | f7c6d52 | 2015-08-05 21:59:36 | [diff] [blame] | 247 | FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestGetMedianRTTSince); |
tbansal | 2c6b0d3 | 2015-09-17 03:49:02 | [diff] [blame] | 248 | FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
| 249 | TestExternalEstimateProvider); |
| 250 | FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
| 251 | TestExternalEstimateProviderMergeEstimates); |
bengr | f1b6738e | 2015-10-07 01:21:30 | [diff] [blame] | 252 | FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestObservers); |
tbansal | 75540a5 | 2015-07-15 22:18:21 | [diff] [blame] | 253 | |
tbansal | 9533b6f | 2015-08-11 22:06:08 | [diff] [blame] | 254 | // NetworkQuality is used to cache the quality of a network connection. |
| 255 | class NET_EXPORT_PRIVATE NetworkQuality { |
| 256 | public: |
| 257 | NetworkQuality(); |
| 258 | // |rtt| is the estimate of the round trip time. |
| 259 | // |downstream_throughput_kbps| is the estimate of the downstream |
| 260 | // throughput. |
| 261 | NetworkQuality(const base::TimeDelta& rtt, |
| 262 | int32_t downstream_throughput_kbps); |
| 263 | NetworkQuality(const NetworkQuality& other); |
| 264 | ~NetworkQuality(); |
| 265 | |
| 266 | NetworkQuality& operator=(const NetworkQuality& other); |
| 267 | |
| 268 | // Returns the estimate of the round trip time. |
| 269 | const base::TimeDelta& rtt() const { return rtt_; } |
| 270 | |
| 271 | // Returns the estimate of the downstream throughput in Kbps (Kilo bits per |
| 272 | // second). |
| 273 | int32_t downstream_throughput_kbps() const { |
| 274 | return downstream_throughput_kbps_; |
| 275 | } |
| 276 | |
| 277 | private: |
| 278 | // Estimated round trip time. |
| 279 | base::TimeDelta rtt_; |
| 280 | |
| 281 | // Estimated downstream throughput in Kbps. |
| 282 | int32_t downstream_throughput_kbps_; |
| 283 | }; |
| 284 | |
tbansal | 75540a5 | 2015-07-15 22:18:21 | [diff] [blame] | 285 | // CachedNetworkQuality stores the quality of a previously seen network. |
| 286 | class NET_EXPORT_PRIVATE CachedNetworkQuality { |
| 287 | public: |
| 288 | explicit CachedNetworkQuality(const NetworkQuality& network_quality); |
| 289 | CachedNetworkQuality(const CachedNetworkQuality& other); |
| 290 | ~CachedNetworkQuality(); |
| 291 | |
| 292 | // Returns the network quality associated with this cached entry. |
shiva.jm | ad81bb893 | 2015-08-31 05:25:43 | [diff] [blame] | 293 | const NetworkQuality& network_quality() const { return network_quality_; } |
tbansal | 75540a5 | 2015-07-15 22:18:21 | [diff] [blame] | 294 | |
| 295 | // Returns true if this cache entry was updated before |
| 296 | // |cached_network_quality|. |
| 297 | bool OlderThan(const CachedNetworkQuality& cached_network_quality) const; |
| 298 | |
| 299 | // Time when this cache entry was last updated. |
| 300 | const base::TimeTicks last_update_time_; |
| 301 | |
| 302 | // Quality of this cached network. |
| 303 | const NetworkQuality network_quality_; |
| 304 | |
| 305 | private: |
| 306 | DISALLOW_ASSIGN(CachedNetworkQuality); |
| 307 | }; |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 308 | |
tbansal | 6f840fc5 | 2015-06-13 03:30:34 | [diff] [blame] | 309 | // Records the round trip time or throughput observation, along with the time |
bengr | f1b6738e | 2015-10-07 01:21:30 | [diff] [blame] | 310 | // the observation was made. The units of value are type specific. For round |
| 311 | // trip time observations, the value is in milliseconds. For throughput, |
| 312 | // the value is in kilobits per second. Observations can be made at several |
| 313 | // places in the network stack, thus the observation source is provided as |
| 314 | // well. |
tbansal | 5659c35e | 2015-06-25 16:49:36 | [diff] [blame] | 315 | struct NET_EXPORT_PRIVATE Observation { |
bengr | f1b6738e | 2015-10-07 01:21:30 | [diff] [blame] | 316 | Observation(int32_t value, |
| 317 | base::TimeTicks timestamp, |
| 318 | ObservationSource source); |
tbansal | 6f840fc5 | 2015-06-13 03:30:34 | [diff] [blame] | 319 | ~Observation(); |
| 320 | |
| 321 | // Value of the observation. |
| 322 | const int32_t value; |
| 323 | |
| 324 | // Time when the observation was taken. |
| 325 | const base::TimeTicks timestamp; |
bengr | f1b6738e | 2015-10-07 01:21:30 | [diff] [blame] | 326 | |
| 327 | // The source of the observation. |
| 328 | const ObservationSource source; |
tbansal | 6f840fc5 | 2015-06-13 03:30:34 | [diff] [blame] | 329 | }; |
| 330 | |
tbansal | 11af8313 | 2015-06-24 20:34:22 | [diff] [blame] | 331 | // Holds an observation and its weight. |
tbansal | 75540a5 | 2015-07-15 22:18:21 | [diff] [blame] | 332 | struct NET_EXPORT_PRIVATE WeightedObservation { |
tbansal | 11af8313 | 2015-06-24 20:34:22 | [diff] [blame] | 333 | WeightedObservation(int32_t value, double weight) |
| 334 | : value(value), weight(weight) {} |
| 335 | WeightedObservation(const WeightedObservation& other) |
| 336 | : WeightedObservation(other.value, other.weight) {} |
| 337 | |
| 338 | WeightedObservation& operator=(const WeightedObservation& other) { |
| 339 | value = other.value; |
| 340 | weight = other.weight; |
| 341 | return *this; |
| 342 | } |
| 343 | |
| 344 | // Required for sorting the samples in the ascending order of values. |
| 345 | bool operator<(const WeightedObservation& other) const { |
| 346 | return (value < other.value); |
| 347 | } |
| 348 | |
| 349 | // Value of the sample. |
| 350 | int32_t value; |
| 351 | |
| 352 | // Weight of the sample. This is computed based on how much time has passed |
| 353 | // since the sample was taken. |
| 354 | double weight; |
| 355 | }; |
| 356 | |
tbansal | 6f840fc5 | 2015-06-13 03:30:34 | [diff] [blame] | 357 | // Stores observations sorted by time. |
tbansal | 5659c35e | 2015-06-25 16:49:36 | [diff] [blame] | 358 | class NET_EXPORT_PRIVATE ObservationBuffer { |
tbansal | 6f840fc5 | 2015-06-13 03:30:34 | [diff] [blame] | 359 | public: |
tbansal | 4418ad12 | 2015-06-26 19:06:33 | [diff] [blame] | 360 | explicit ObservationBuffer(double weight_multiplier_per_second); |
tbansal | 6f840fc5 | 2015-06-13 03:30:34 | [diff] [blame] | 361 | ~ObservationBuffer(); |
| 362 | |
| 363 | // Adds |observation| to the buffer. The oldest observation in the buffer |
| 364 | // will be evicted to make room if the buffer is already full. |
| 365 | void AddObservation(const Observation& observation); |
| 366 | |
| 367 | // Returns the number of observations in this buffer. |
| 368 | size_t Size() const; |
| 369 | |
| 370 | // Clears the observations stored in this buffer. |
| 371 | void Clear(); |
| 372 | |
tbansal | f7c6d52 | 2015-08-05 21:59:36 | [diff] [blame] | 373 | // Returns true iff the |percentile| value of the observations in this |
| 374 | // buffer is available. Sets |result| to the computed |percentile| |
| 375 | // value among all observations since |begin_timestamp|. If the value is |
| 376 | // unavailable, false is returned and |result| is not modified. Percentile |
| 377 | // value is unavailable if all the values in observation buffer are older |
| 378 | // than |begin_timestamp|. |
| 379 | // |result| must not be null. |
| 380 | bool GetPercentile(const base::TimeTicks& begin_timestamp, |
| 381 | int32_t* result, |
| 382 | int percentile) const; |
tbansal | 11af8313 | 2015-06-24 20:34:22 | [diff] [blame] | 383 | |
tbansal | 6f840fc5 | 2015-06-13 03:30:34 | [diff] [blame] | 384 | private: |
| 385 | FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations); |
tbansal | b177b539 | 2015-06-25 11:13:02 | [diff] [blame] | 386 | FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, |
| 387 | ObtainOperatingParams); |
tbansal | 4418ad12 | 2015-06-26 19:06:33 | [diff] [blame] | 388 | FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, HalfLifeParam); |
tbansal | 6f840fc5 | 2015-06-13 03:30:34 | [diff] [blame] | 389 | |
tbansal | 11af8313 | 2015-06-24 20:34:22 | [diff] [blame] | 390 | // Computes the weighted observations and stores them in |
| 391 | // |weighted_observations| sorted by ascending |WeightedObservation.value|. |
tbansal | f7c6d52 | 2015-08-05 21:59:36 | [diff] [blame] | 392 | // Only the observations with timestamp later than |begin_timestamp| are |
| 393 | // considered. Also, sets |total_weight| to the total weight of all |
| 394 | // observations. Should be called only when there is at least one |
| 395 | // observation in the buffer. |
tbansal | 11af8313 | 2015-06-24 20:34:22 | [diff] [blame] | 396 | void ComputeWeightedObservations( |
tbansal | f7c6d52 | 2015-08-05 21:59:36 | [diff] [blame] | 397 | const base::TimeTicks& begin_timestamp, |
tbansal | 11af8313 | 2015-06-24 20:34:22 | [diff] [blame] | 398 | std::vector<WeightedObservation>& weighted_observations, |
| 399 | double* total_weight) const; |
| 400 | |
tbansal | 6f840fc5 | 2015-06-13 03:30:34 | [diff] [blame] | 401 | // Holds observations sorted by time, with the oldest observation at the |
| 402 | // front of the queue. |
| 403 | std::deque<Observation> observations_; |
| 404 | |
tbansal | 11af8313 | 2015-06-24 20:34:22 | [diff] [blame] | 405 | // The factor by which the weight of an observation reduces every second. |
| 406 | // For example, if an observation is 6 seconds old, its weight would be: |
| 407 | // weight_multiplier_per_second_ ^ 6 |
| 408 | // Calculated from |kHalfLifeSeconds| by solving the following equation: |
| 409 | // weight_multiplier_per_second_ ^ kHalfLifeSeconds = 0.5 |
| 410 | const double weight_multiplier_per_second_; |
| 411 | |
tbansal | 6f840fc5 | 2015-06-13 03:30:34 | [diff] [blame] | 412 | DISALLOW_COPY_AND_ASSIGN(ObservationBuffer); |
| 413 | }; |
| 414 | |
tbansal | 75540a5 | 2015-07-15 22:18:21 | [diff] [blame] | 415 | // This does not use a unordered_map or hash_map for code simplicity (key just |
| 416 | // implements operator<, rather than hash and equality) and because the map is |
| 417 | // tiny. |
| 418 | typedef std::map<NetworkID, CachedNetworkQuality> CachedNetworkQualities; |
| 419 | |
tbansal | 9533b6f | 2015-08-11 22:06:08 | [diff] [blame] | 420 | // Throughput is set to |kInvalidThroughput| if a valid value is |
| 421 | // unavailable. Readers should discard throughput value if it is set to |
| 422 | // |kInvalidThroughput|. |
| 423 | static const int32_t kInvalidThroughput; |
| 424 | |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 425 | // Tiny transfer sizes may give inaccurate throughput results. |
| 426 | // Minimum size of the transfer over which the throughput is computed. |
| 427 | static const int kMinTransferSizeInBytes = 10000; |
| 428 | |
| 429 | // Minimum duration (in microseconds) of the transfer over which the |
| 430 | // throughput is computed. |
| 431 | static const int kMinRequestDurationMicroseconds = 1000; |
| 432 | |
tbansal | b177b539 | 2015-06-25 11:13:02 | [diff] [blame] | 433 | // Minimum valid value of the variation parameter that holds RTT (in |
| 434 | // milliseconds) values. |
| 435 | static const int kMinimumRTTVariationParameterMsec = 1; |
| 436 | |
| 437 | // Minimum valid value of the variation parameter that holds throughput (in |
| 438 | // kbps) values. |
| 439 | static const int kMinimumThroughputVariationParameterKbps = 1; |
| 440 | |
tbansal | 75540a5 | 2015-07-15 22:18:21 | [diff] [blame] | 441 | // Maximum size of the cache that holds network quality estimates. |
| 442 | // Smaller size may reduce the cache hit rate due to frequent evictions. |
| 443 | // Larger size may affect performance. |
| 444 | static const size_t kMaximumNetworkQualityCacheSize = 10; |
| 445 | |
| 446 | // Maximum number of observations that can be held in the ObservationBuffer. |
| 447 | static const size_t kMaximumObservationsBufferSize = 300; |
tbansal | b177b539 | 2015-06-25 11:13:02 | [diff] [blame] | 448 | |
tbansal | 2c6b0d3 | 2015-09-17 03:49:02 | [diff] [blame] | 449 | // Time duration (in milliseconds) after which the estimate provided by |
| 450 | // external estimate provider is considered stale. |
| 451 | static const int kExternalEstimateProviderFreshnessDurationMsec = |
| 452 | 5 * 60 * 1000; |
| 453 | |
tbansal | 9533b6f | 2015-08-11 22:06:08 | [diff] [blame] | 454 | // Returns the RTT value to be used when the valid RTT is unavailable. Readers |
| 455 | // should discard RTT if it is set to the value returned by |InvalidRTT()|. |
| 456 | static const base::TimeDelta InvalidRTT(); |
| 457 | |
tbansal | 2c6b0d3 | 2015-09-17 03:49:02 | [diff] [blame] | 458 | // Queries the external estimate provider for the latest network quality |
| 459 | // estimates, and adds those estimates to the current observation buffer. |
| 460 | void QueryExternalEstimateProvider(); |
tbansal | 1c92d5b | 2015-08-14 20:14:43 | [diff] [blame] | 461 | |
tbansal | b177b539 | 2015-06-25 11:13:02 | [diff] [blame] | 462 | // Obtains operating parameters from the field trial parameters. |
| 463 | void ObtainOperatingParams( |
| 464 | const std::map<std::string, std::string>& variation_params); |
| 465 | |
| 466 | // Adds the default median RTT and downstream throughput estimate for the |
| 467 | // current connection type to the observation buffer. |
| 468 | void AddDefaultEstimates(); |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 469 | |
tbansal | 11af8313 | 2015-06-24 20:34:22 | [diff] [blame] | 470 | // Returns an estimate of network quality at the specified |percentile|. |
tbansal | f7c6d52 | 2015-08-05 21:59:36 | [diff] [blame] | 471 | // Only the observations later than |begin_timestamp| are taken into account. |
tbansal | 11af8313 | 2015-06-24 20:34:22 | [diff] [blame] | 472 | // |percentile| must be between 0 and 100 (both inclusive) with higher |
| 473 | // percentiles indicating less performant networks. For example, if |
| 474 | // |percentile| is 90, then the network is expected to be faster than the |
| 475 | // returned estimate with 0.9 probability. Similarly, network is expected to |
| 476 | // be slower than the returned estimate with 0.1 probability. |
tbansal | f7c6d52 | 2015-08-05 21:59:36 | [diff] [blame] | 477 | base::TimeDelta GetRTTEstimateInternal(const base::TimeTicks& begin_timestamp, |
| 478 | int percentile) const; |
| 479 | int32_t GetDownlinkThroughputKbpsEstimateInternal( |
| 480 | const base::TimeTicks& begin_timestamp, |
| 481 | int percentile) const; |
tbansal | 11af8313 | 2015-06-24 20:34:22 | [diff] [blame] | 482 | |
tbansal | 75540a5 | 2015-07-15 22:18:21 | [diff] [blame] | 483 | // Returns the current network ID checking by calling the platform APIs. |
| 484 | // Virtualized for testing. |
| 485 | virtual NetworkID GetCurrentNetworkID() const; |
| 486 | |
| 487 | // Writes the estimated quality of the current network to the cache. |
| 488 | void CacheNetworkQualityEstimate(); |
| 489 | |
bengr | f1b6738e | 2015-10-07 01:21:30 | [diff] [blame] | 490 | void NotifyObserversOfRTT(const Observation& observation); |
| 491 | |
| 492 | void NotifyObserversOfThroughput(const Observation& observation); |
| 493 | |
tbansal | 2e590c6 | 2015-07-09 00:22:02 | [diff] [blame] | 494 | // Records the UMA related to RTT. |
| 495 | void RecordRTTUMA(int32_t estimated_value_msec, |
| 496 | int32_t actual_value_msec) const; |
| 497 | |
tbansal | 79ed5cd | 2015-08-10 18:53:56 | [diff] [blame] | 498 | // Returns true only if |request| can be used for network quality estimation. |
| 499 | // Only the requests that go over network are considered to provide useful |
| 500 | // observations. |
| 501 | bool RequestProvidesUsefulObservations(const URLRequest& request) const; |
| 502 | |
tbansal | 2c6b0d3 | 2015-09-17 03:49:02 | [diff] [blame] | 503 | // Values of external estimate provider status. This enum must remain |
| 504 | // synchronized with the enum of the same name in |
| 505 | // metrics/histograms/histograms.xml. |
| 506 | enum NQEExternalEstimateProviderStatus { |
| 507 | EXTERNAL_ESTIMATE_PROVIDER_STATUS_NOT_AVAILABLE, |
| 508 | EXTERNAL_ESTIMATE_PROVIDER_STATUS_AVAILABLE, |
| 509 | EXTERNAL_ESTIMATE_PROVIDER_STATUS_QUERIED, |
| 510 | EXTERNAL_ESTIMATE_PROVIDER_STATUS_QUERY_SUCCESSFUL, |
| 511 | EXTERNAL_ESTIMATE_PROVIDER_STATUS_CALLBACK, |
| 512 | EXTERNAL_ESTIMATE_PROVIDER_STATUS_BOUNDARY |
| 513 | }; |
| 514 | |
| 515 | // Records the metrics related to external estimate provider. |
| 516 | void RecordExternalEstimateProviderMetrics( |
| 517 | NQEExternalEstimateProviderStatus status) const; |
| 518 | |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 519 | // Determines if the requests to local host can be used in estimating the |
| 520 | // network quality. Set to true only for tests. |
| 521 | const bool allow_localhost_requests_; |
| 522 | |
tbansal | afc2c1cb | 2015-06-15 23:58:59 | [diff] [blame] | 523 | // Determines if the responses smaller than |kMinTransferSizeInBytes| |
| 524 | // or shorter than |kMinTransferSizeInBytes| can be used in estimating the |
| 525 | // network quality. Set to true only for tests. |
| 526 | const bool allow_small_responses_; |
| 527 | |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 528 | // Time when last connection change was observed. |
| 529 | base::TimeTicks last_connection_change_; |
| 530 | |
tbansal | 75540a5 | 2015-07-15 22:18:21 | [diff] [blame] | 531 | // ID of the current network. |
| 532 | NetworkID current_network_id_; |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 533 | |
tbansal | 75540a5 | 2015-07-15 22:18:21 | [diff] [blame] | 534 | // Peak network quality (fastest round-trip-time (RTT) and highest |
| 535 | // downstream throughput) measured since last connectivity change. RTT is |
| 536 | // measured from time the request is sent until the first byte received. |
| 537 | // The accuracy is decreased by ignoring these factors: |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 538 | // 1) Multiple URLRequests can occur concurrently. |
tbansal | 75540a5 | 2015-07-15 22:18:21 | [diff] [blame] | 539 | // 2) Includes server processing time. |
| 540 | NetworkQuality peak_network_quality_; |
| 541 | |
| 542 | // Cache that stores quality of previously seen networks. |
| 543 | CachedNetworkQualities cached_network_qualities_; |
tbansal | 6f840fc5 | 2015-06-13 03:30:34 | [diff] [blame] | 544 | |
tbansal | 11af8313 | 2015-06-24 20:34:22 | [diff] [blame] | 545 | // Buffer that holds Kbps observations sorted by timestamp. |
tbansal | 79ed5cd | 2015-08-10 18:53:56 | [diff] [blame] | 546 | ObservationBuffer downstream_throughput_kbps_observations_; |
tbansal | 6f840fc5 | 2015-06-13 03:30:34 | [diff] [blame] | 547 | |
tbansal | 11af8313 | 2015-06-24 20:34:22 | [diff] [blame] | 548 | // Buffer that holds RTT (in milliseconds) observations sorted by timestamp. |
tbansal | 6f840fc5 | 2015-06-13 03:30:34 | [diff] [blame] | 549 | ObservationBuffer rtt_msec_observations_; |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 550 | |
tbansal | b177b539 | 2015-06-25 11:13:02 | [diff] [blame] | 551 | // Default network quality observations obtained from the network quality |
| 552 | // estimator field trial parameters. The observations are indexed by |
| 553 | // ConnectionType. |
| 554 | NetworkQuality |
| 555 | default_observations_[NetworkChangeNotifier::CONNECTION_LAST + 1]; |
| 556 | |
tbansal | 2e590c6 | 2015-07-09 00:22:02 | [diff] [blame] | 557 | // Estimated network quality. Updated on mainframe requests. |
| 558 | NetworkQuality estimated_median_network_quality_; |
| 559 | |
tbansal | 1c92d5b | 2015-08-14 20:14:43 | [diff] [blame] | 560 | // ExternalEstimateProvider that provides network quality using operating |
| 561 | // system APIs. May be NULL. |
tbansal | 2c6b0d3 | 2015-09-17 03:49:02 | [diff] [blame] | 562 | const scoped_ptr<ExternalEstimateProvider> external_estimate_provider_; |
tbansal | 1c92d5b | 2015-08-14 20:14:43 | [diff] [blame] | 563 | |
bengr | f1b6738e | 2015-10-07 01:21:30 | [diff] [blame] | 564 | // Observer lists for round trip times and throughput measurements. |
| 565 | base::ObserverList<RTTObserver> rtt_observer_list_; |
| 566 | base::ObserverList<ThroughputObserver> throughput_observer_list_; |
| 567 | |
tbansal | ea2fb8c | 2015-05-22 22:23:00 | [diff] [blame] | 568 | base::ThreadChecker thread_checker_; |
| 569 | |
| 570 | DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |
| 571 | }; |
| 572 | |
| 573 | } // namespace net |
| 574 | |
| 575 | #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |