blob: 5ff9bd7354027850bc243ddd1c4351cbc40cb11c [file] [log] [blame]
tbansalea2fb8c2015-05-22 22:23:001// 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
Avi Drissman13fc8932015-12-20 04:40:468#include <stddef.h>
tbansalea2fb8c2015-05-22 22:23:009#include <stdint.h>
10
tbansal6f840fc52015-06-13 03:30:3411#include <deque>
tbansalb177b5392015-06-25 11:13:0212#include <map>
tbansal75540a52015-07-15 22:18:2113#include <string>
jsbellcea42a52015-11-30 23:50:2514#include <tuple>
tbansal6f840fc52015-06-13 03:30:3415
tbansalea2fb8c2015-05-22 22:23:0016#include "base/gtest_prod_util.h"
17#include "base/macros.h"
bengrf1b6738e2015-10-07 01:21:3018#include "base/memory/ref_counted.h"
tbansal75540a52015-07-15 22:18:2119#include "base/memory/scoped_ptr.h"
tbansal0f56a39a2016-04-07 22:03:3820#include "base/memory/weak_ptr.h"
bengrf1b6738e2015-10-07 01:21:3021#include "base/observer_list.h"
tbansalea2fb8c2015-05-22 22:23:0022#include "base/threading/thread_checker.h"
23#include "base/time/time.h"
tbansal1c92d5b2015-08-14 20:14:4324#include "net/base/external_estimate_provider.h"
tbansal6f840fc52015-06-13 03:30:3425#include "net/base/net_export.h"
tbansalea2fb8c2015-05-22 22:23:0026#include "net/base/network_change_notifier.h"
tbansalba8f4112015-09-03 21:57:1927#include "net/base/socket_performance_watcher_factory.h"
tbansalea2fb8c2015-05-22 22:23:0028
tbansal0f56a39a2016-04-07 22:03:3829namespace base {
30class SingleThreadTaskRunner;
31} // namespace base
32
tbansalea2fb8c2015-05-22 22:23:0033namespace net {
34
tbansal79ed5cd2015-08-10 18:53:5635class URLRequest;
36
tbansalea2fb8c2015-05-22 22:23:0037// NetworkQualityEstimator provides network quality estimates (quality of the
38// full paths to all origins that have been connected to).
39// The estimates are based on the observed organic traffic.
40// A NetworkQualityEstimator instance is attached to URLRequestContexts and
41// observes the traffic of URLRequests spawned from the URLRequestContexts.
42// A single instance of NQE can be attached to multiple URLRequestContexts,
43// thereby increasing the single NQE instance's accuracy by providing more
44// observed traffic characteristics.
45class NET_EXPORT_PRIVATE NetworkQualityEstimator
tbansal1c92d5b2015-08-14 20:14:4346 : public NetworkChangeNotifier::ConnectionTypeObserver,
tbansal0f56a39a2016-04-07 22:03:3847 public ExternalEstimateProvider::UpdatedEstimateDelegate {
tbansalea2fb8c2015-05-22 22:23:0048 public:
bengrf1b6738e2015-10-07 01:21:3049 // On Android, a Java counterpart will be generated for this enum.
50 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net
51 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: NetworkQualityObservationSource
52 // GENERATED_JAVA_PREFIX_TO_STRIP:
53 enum ObservationSource {
54 // The observation was taken at the request layer, e.g., a round trip time
55 // is recorded as the time between the request being sent and the first byte
56 // being received.
57 URL_REQUEST,
58 // The observation is taken from TCP statistics maintained by the kernel.
59 TCP,
60 // The observation is taken at the QUIC layer.
61 QUIC,
62 // The observation is a previously cached estimate of the metric.
63 CACHED_ESTIMATE,
64 // The observation is derived from network connection information provided
65 // by the platform. For example, typical RTT and throughput values are used
66 // for a given type of network connection.
67 DEFAULT_FROM_PLATFORM,
68 // The observation came from a Chromium-external source.
69 EXTERNAL_ESTIMATE
70 };
71
72 // Observes measurements of round trip time.
73 class NET_EXPORT_PRIVATE RTTObserver {
74 public:
75 // Will be called when a new RTT observation is available. The round trip
76 // time is specified in milliseconds. The time when the observation was
77 // taken and the source of the observation are provided.
78 virtual void OnRTTObservation(int32_t rtt_ms,
79 const base::TimeTicks& timestamp,
80 ObservationSource source) = 0;
81
82 protected:
83 RTTObserver() {}
84 virtual ~RTTObserver() {}
85
86 private:
87 DISALLOW_COPY_AND_ASSIGN(RTTObserver);
88 };
89
90 // Observes measurements of throughput.
91 class NET_EXPORT_PRIVATE ThroughputObserver {
92 public:
93 // Will be called when a new throughput observation is available.
94 // Throughput is specified in kilobits per second.
95 virtual void OnThroughputObservation(int32_t throughput_kbps,
96 const base::TimeTicks& timestamp,
97 ObservationSource source) = 0;
98
99 protected:
100 ThroughputObserver() {}
101 virtual ~ThroughputObserver() {}
102
103 private:
104 DISALLOW_COPY_AND_ASSIGN(ThroughputObserver);
105 };
106
tbansalea2fb8c2015-05-22 22:23:00107 // Creates a new NetworkQualityEstimator.
tbansalb177b5392015-06-25 11:13:02108 // |variation_params| is the map containing all field trial parameters
109 // related to NetworkQualityEstimator field trial.
tbansal1c92d5b2015-08-14 20:14:43110 // |external_estimates_provider| may be NULL.
111 NetworkQualityEstimator(
112 scoped_ptr<ExternalEstimateProvider> external_estimates_provider,
tbansalb177b5392015-06-25 11:13:02113 const std::map<std::string, std::string>& variation_params);
tbansalea2fb8c2015-05-22 22:23:00114
bengrf1b6738e2015-10-07 01:21:30115 // Construct a NetworkQualityEstimator instance allowing for test
116 // configuration. Registers for network type change notifications so estimates
117 // can be kept network specific.
118 // |external_estimates_provider| may be NULL.
119 // |variation_params| is the map containing all field trial parameters for the
120 // network quality estimator field trial.
121 // |allow_local_host_requests_for_tests| should only be true when testing
122 // against local HTTP server and allows the requests to local host to be
123 // used for network quality estimation.
124 // |allow_smaller_responses_for_tests| should only be true when testing.
125 // Allows the responses smaller than |kMinTransferSizeInBytes| or shorter than
126 // |kMinRequestDurationMicroseconds| to be used for network quality
127 // estimation.
128 NetworkQualityEstimator(
129 scoped_ptr<ExternalEstimateProvider> external_estimates_provider,
130 const std::map<std::string, std::string>& variation_params,
131 bool allow_local_host_requests_for_tests,
132 bool allow_smaller_responses_for_tests);
133
tbansalea2fb8c2015-05-22 22:23:00134 ~NetworkQualityEstimator() override;
135
tbansal28fd8a42016-03-25 18:37:38136 // Returns true if RTT is available and sets |rtt| to estimated RTT at the
137 // HTTP layer. Virtualized for testing. |rtt| should not be null. The RTT at
138 // the HTTP layer measures the time from when the request was sent (this
139 // happens after the connection is established) to the time when the response
140 // headers were received.
141 virtual bool GetURLRequestRTTEstimate(base::TimeDelta* rtt) const;
tbansal91fb5882015-08-05 04:24:56142
143 // Returns true if downlink throughput is available and sets |kbps| to
tbansal31ea1bbf82016-02-03 00:13:31144 // estimated downlink throughput (in kilobits per second).
tbansalb658f3882015-08-19 21:27:49145 // Virtualized for testing. |kbps| should not be null.
tbansal9533b6f2015-08-11 22:06:08146 virtual bool GetDownlinkThroughputKbpsEstimate(int32_t* kbps) const;
tbansal91fb5882015-08-05 04:24:56147
tbansal79ed5cd2015-08-10 18:53:56148 // Notifies NetworkQualityEstimator that the response header of |request| has
149 // been received.
150 void NotifyHeadersReceived(const URLRequest& request);
151
152 // Notifies NetworkQualityEstimator that the response body of |request| has
153 // been received.
154 void NotifyRequestCompleted(const URLRequest& request);
tbansalea2fb8c2015-05-22 22:23:00155
tbansal28fd8a42016-03-25 18:37:38156 // Returns true if median RTT at the HTTP layer is available and sets |rtt|
157 // to the median of RTT observations since |begin_timestamp|.
158 // Virtualized for testing. |rtt| should not be null. The RTT at the HTTP
159 // layer measures the time from when the request was sent (this happens after
160 // the connection is established) to the time when the response headers were
161 // received.
162 virtual bool GetRecentURLRequestRTTMedian(
163 const base::TimeTicks& begin_timestamp,
164 base::TimeDelta* rtt) const;
tbansalb658f3882015-08-19 21:27:49165
166 // Returns true if median downstream throughput is available and sets |kbps|
tbansal31ea1bbf82016-02-03 00:13:31167 // to the median of downstream throughput (in kilobits per second)
168 // observations since |begin_timestamp|. Virtualized for testing. |kbps|
169 // should not be null.
tbansalb658f3882015-08-19 21:27:49170 virtual bool GetRecentMedianDownlinkThroughputKbps(
171 const base::TimeTicks& begin_timestamp,
172 int32_t* kbps) const;
tbansalf7c6d522015-08-05 21:59:36173
bengrf1b6738e2015-10-07 01:21:30174 // Adds |rtt_observer| to the list of round trip time observers. Must be
175 // called on the IO thread.
176 void AddRTTObserver(RTTObserver* rtt_observer);
177
178 // Removes |rtt_observer| from the list of round trip time observers if it
179 // is on the list of observers. Must be called on the IO thread.
180 void RemoveRTTObserver(RTTObserver* rtt_observer);
181
182 // Adds |throughput_observer| to the list of throughput observers. Must be
183 // called on the IO thread.
184 void AddThroughputObserver(ThroughputObserver* throughput_observer);
185
186 // Removes |throughput_observer| from the list of throughput observers if it
187 // is on the list of observers. Must be called on the IO thread.
188 void RemoveThroughputObserver(ThroughputObserver* throughput_observer);
189
tbansal0f56a39a2016-04-07 22:03:38190 SocketPerformanceWatcherFactory* GetSocketPerformanceWatcherFactory();
191
tbansal75540a52015-07-15 22:18:21192 protected:
193 // NetworkID is used to uniquely identify a network.
194 // For the purpose of network quality estimation and caching, a network is
195 // uniquely identified by a combination of |type| and
196 // |id|. This approach is unable to distinguish networks with
197 // same name (e.g., different Wi-Fi networks with same SSID).
198 // This is a protected member to expose it to tests.
199 struct NET_EXPORT_PRIVATE NetworkID {
200 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id)
201 : type(type), id(id) {}
202 NetworkID(const NetworkID& other) : type(other.type), id(other.id) {}
203 ~NetworkID() {}
204
205 NetworkID& operator=(const NetworkID& other) {
206 type = other.type;
207 id = other.id;
208 return *this;
209 }
210
211 // Overloaded because NetworkID is used as key in a map.
212 bool operator<(const NetworkID& other) const {
jsbellcea42a52015-11-30 23:50:25213 return std::tie(type, id) < std::tie(other.type, other.id);
tbansal75540a52015-07-15 22:18:21214 }
215
216 // Connection type of the network.
217 NetworkChangeNotifier::ConnectionType type;
218
219 // Name of this network. This is set to:
220 // - Wi-Fi SSID if the device is connected to a Wi-Fi access point and the
221 // SSID name is available, or
222 // - MCC/MNC code of the cellular carrier if the device is connected to a
223 // cellular network, or
224 // - "Ethernet" in case the device is connected to ethernet.
225 // - An empty string in all other cases or if the network name is not
226 // exposed by platform APIs.
227 std::string id;
228 };
229
tbansal75540a52015-07-15 22:18:21230 // Returns true if the cached network quality estimate was successfully read.
231 bool ReadCachedNetworkQualityEstimate();
232
tbansalba8f4112015-09-03 21:57:19233 // NetworkChangeNotifier::ConnectionTypeObserver implementation:
tbansal75540a52015-07-15 22:18:21234 void OnConnectionTypeChanged(
235 NetworkChangeNotifier::ConnectionType type) override;
236
tbansal2c6b0d32015-09-17 03:49:02237 // ExternalEstimateProvider::UpdatedEstimateObserver implementation.
238 void OnUpdatedEstimateAvailable() override;
239
tbansalea2fb8c2015-05-22 22:23:00240 private:
tbansal6f840fc52015-06-13 03:30:34241 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations);
tbansal2e590c62015-07-09 00:22:02242 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestKbpsRTTUpdates);
tbansal6f840fc52015-06-13 03:30:34243 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestAddObservation);
tbansalb177b5392015-06-25 11:13:02244 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, ObtainOperatingParams);
tbansal4418ad122015-06-26 19:06:33245 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, HalfLifeParam);
tbansalea2fb8c2015-05-22 22:23:00246 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator);
tbansal11af83132015-06-24 20:34:22247 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
248 PercentileSameTimestamps);
249 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
250 PercentileDifferentTimestamps);
251 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, ComputedPercentiles);
tbansal75540a52015-07-15 22:18:21252 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestCaching);
253 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
254 TestLRUCacheMaximumSize);
tbansalf7c6d522015-08-05 21:59:36255 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestGetMedianRTTSince);
tbansal2c6b0d32015-09-17 03:49:02256 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
257 TestExternalEstimateProvider);
258 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
259 TestExternalEstimateProviderMergeEstimates);
tbansal0f56a39a2016-04-07 22:03:38260
261 class SocketWatcher;
262 class SocketWatcherFactory;
tbansal75540a52015-07-15 22:18:21263
tbansal9533b6f2015-08-11 22:06:08264 // NetworkQuality is used to cache the quality of a network connection.
265 class NET_EXPORT_PRIVATE NetworkQuality {
266 public:
267 NetworkQuality();
268 // |rtt| is the estimate of the round trip time.
269 // |downstream_throughput_kbps| is the estimate of the downstream
tbansal31ea1bbf82016-02-03 00:13:31270 // throughput in kilobits per second.
tbansal9533b6f2015-08-11 22:06:08271 NetworkQuality(const base::TimeDelta& rtt,
272 int32_t downstream_throughput_kbps);
273 NetworkQuality(const NetworkQuality& other);
274 ~NetworkQuality();
275
276 NetworkQuality& operator=(const NetworkQuality& other);
277
278 // Returns the estimate of the round trip time.
279 const base::TimeDelta& rtt() const { return rtt_; }
280
tbansal31ea1bbf82016-02-03 00:13:31281 // Returns the estimate of the downstream throughput in Kbps (Kilobits per
tbansal9533b6f2015-08-11 22:06:08282 // second).
283 int32_t downstream_throughput_kbps() const {
284 return downstream_throughput_kbps_;
285 }
286
287 private:
288 // Estimated round trip time.
289 base::TimeDelta rtt_;
290
tbansal31ea1bbf82016-02-03 00:13:31291 // Estimated downstream throughput in kilobits per second.
tbansal9533b6f2015-08-11 22:06:08292 int32_t downstream_throughput_kbps_;
293 };
294
tbansal75540a52015-07-15 22:18:21295 // CachedNetworkQuality stores the quality of a previously seen network.
296 class NET_EXPORT_PRIVATE CachedNetworkQuality {
297 public:
298 explicit CachedNetworkQuality(const NetworkQuality& network_quality);
299 CachedNetworkQuality(const CachedNetworkQuality& other);
300 ~CachedNetworkQuality();
301
302 // Returns the network quality associated with this cached entry.
shiva.jmad81bb8932015-08-31 05:25:43303 const NetworkQuality& network_quality() const { return network_quality_; }
tbansal75540a52015-07-15 22:18:21304
305 // Returns true if this cache entry was updated before
306 // |cached_network_quality|.
307 bool OlderThan(const CachedNetworkQuality& cached_network_quality) const;
308
309 // Time when this cache entry was last updated.
310 const base::TimeTicks last_update_time_;
311
312 // Quality of this cached network.
313 const NetworkQuality network_quality_;
314
315 private:
316 DISALLOW_ASSIGN(CachedNetworkQuality);
317 };
tbansalea2fb8c2015-05-22 22:23:00318
tbansal31ea1bbf82016-02-03 00:13:31319 // Records observations of network quality metrics (such as round trip time
320 // or throughput), along with the time the observation was made. Observations
321 // can be made at several places in the network stack, thus the observation
322 // source is provided as well. ValueType must be numerical so that statistics
323 // such as median, average can be computed.
324 template <typename ValueType>
tbansal5659c35e2015-06-25 16:49:36325 struct NET_EXPORT_PRIVATE Observation {
tbansal31ea1bbf82016-02-03 00:13:31326 Observation(const ValueType& value,
bengrf1b6738e2015-10-07 01:21:30327 base::TimeTicks timestamp,
tbansal31ea1bbf82016-02-03 00:13:31328 ObservationSource source)
329 : value(value), timestamp(timestamp), source(source) {
330 DCHECK(!timestamp.is_null());
331 }
332 ~Observation() {}
tbansal6f840fc52015-06-13 03:30:34333
334 // Value of the observation.
tbansal31ea1bbf82016-02-03 00:13:31335 const ValueType value;
tbansal6f840fc52015-06-13 03:30:34336
337 // Time when the observation was taken.
338 const base::TimeTicks timestamp;
bengrf1b6738e2015-10-07 01:21:30339
340 // The source of the observation.
341 const ObservationSource source;
tbansal6f840fc52015-06-13 03:30:34342 };
343
tbansal11af83132015-06-24 20:34:22344 // Holds an observation and its weight.
tbansal31ea1bbf82016-02-03 00:13:31345 template <typename ValueType>
tbansal75540a52015-07-15 22:18:21346 struct NET_EXPORT_PRIVATE WeightedObservation {
tbansal31ea1bbf82016-02-03 00:13:31347 WeightedObservation(ValueType value, double weight)
tbansal11af83132015-06-24 20:34:22348 : value(value), weight(weight) {}
349 WeightedObservation(const WeightedObservation& other)
350 : WeightedObservation(other.value, other.weight) {}
351
352 WeightedObservation& operator=(const WeightedObservation& other) {
353 value = other.value;
354 weight = other.weight;
355 return *this;
356 }
357
358 // Required for sorting the samples in the ascending order of values.
359 bool operator<(const WeightedObservation& other) const {
360 return (value < other.value);
361 }
362
363 // Value of the sample.
tbansal31ea1bbf82016-02-03 00:13:31364 ValueType value;
tbansal11af83132015-06-24 20:34:22365
366 // Weight of the sample. This is computed based on how much time has passed
367 // since the sample was taken.
368 double weight;
369 };
370
tbansal6f840fc52015-06-13 03:30:34371 // Stores observations sorted by time.
tbansal31ea1bbf82016-02-03 00:13:31372 template <typename ValueType>
tbansal5659c35e2015-06-25 16:49:36373 class NET_EXPORT_PRIVATE ObservationBuffer {
tbansal6f840fc52015-06-13 03:30:34374 public:
tbansal4418ad122015-06-26 19:06:33375 explicit ObservationBuffer(double weight_multiplier_per_second);
tbansal6f840fc52015-06-13 03:30:34376 ~ObservationBuffer();
377
378 // Adds |observation| to the buffer. The oldest observation in the buffer
379 // will be evicted to make room if the buffer is already full.
tbansal31ea1bbf82016-02-03 00:13:31380 void AddObservation(const Observation<ValueType>& observation) {
381 DCHECK_LE(observations_.size(),
382 static_cast<size_t>(kMaximumObservationsBufferSize));
383 // Evict the oldest element if the buffer is already full.
384 if (observations_.size() == kMaximumObservationsBufferSize)
385 observations_.pop_front();
386
387 observations_.push_back(observation);
388 DCHECK_LE(observations_.size(),
389 static_cast<size_t>(kMaximumObservationsBufferSize));
390 }
tbansal6f840fc52015-06-13 03:30:34391
392 // Returns the number of observations in this buffer.
tbansal31ea1bbf82016-02-03 00:13:31393 size_t Size() const { return observations_.size(); }
tbansal6f840fc52015-06-13 03:30:34394
395 // Clears the observations stored in this buffer.
tbansal31ea1bbf82016-02-03 00:13:31396 void Clear() { observations_.clear(); }
tbansal6f840fc52015-06-13 03:30:34397
tbansalf7c6d522015-08-05 21:59:36398 // Returns true iff the |percentile| value of the observations in this
399 // buffer is available. Sets |result| to the computed |percentile|
400 // value among all observations since |begin_timestamp|. If the value is
401 // unavailable, false is returned and |result| is not modified. Percentile
402 // value is unavailable if all the values in observation buffer are older
tbansal28fd8a42016-03-25 18:37:38403 // than |begin_timestamp|. |result| must not be null.
404 // |disallowed_observation_sources| is the list of observation sources that
405 // should be excluded when computing the percentile.
tbansalf7c6d522015-08-05 21:59:36406 bool GetPercentile(const base::TimeTicks& begin_timestamp,
tbansal31ea1bbf82016-02-03 00:13:31407 ValueType* result,
tbansal28fd8a42016-03-25 18:37:38408 int percentile,
409 const std::vector<ObservationSource>&
410 disallowed_observation_sources) const;
tbansal11af83132015-06-24 20:34:22411
tbansal6f840fc52015-06-13 03:30:34412 private:
413 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations);
tbansalb177b5392015-06-25 11:13:02414 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
415 ObtainOperatingParams);
tbansal4418ad122015-06-26 19:06:33416 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, HalfLifeParam);
tbansal6f840fc52015-06-13 03:30:34417
tbansal11af83132015-06-24 20:34:22418 // Computes the weighted observations and stores them in
419 // |weighted_observations| sorted by ascending |WeightedObservation.value|.
tbansalf7c6d522015-08-05 21:59:36420 // Only the observations with timestamp later than |begin_timestamp| are
421 // considered. Also, sets |total_weight| to the total weight of all
422 // observations. Should be called only when there is at least one
tbansal28fd8a42016-03-25 18:37:38423 // observation in the buffer. |disallowed_observation_sources| is the list
424 // of observation sources that should be excluded when computing the
425 // weighted observations.
tbansal11af83132015-06-24 20:34:22426 void ComputeWeightedObservations(
tbansalf7c6d522015-08-05 21:59:36427 const base::TimeTicks& begin_timestamp,
tbansal31ea1bbf82016-02-03 00:13:31428 std::vector<WeightedObservation<ValueType>>& weighted_observations,
tbansal28fd8a42016-03-25 18:37:38429 double* total_weight,
430 const std::vector<ObservationSource>& disallowed_observation_sources)
431 const;
tbansal11af83132015-06-24 20:34:22432
tbansal6f840fc52015-06-13 03:30:34433 // Holds observations sorted by time, with the oldest observation at the
434 // front of the queue.
tbansal31ea1bbf82016-02-03 00:13:31435 std::deque<Observation<ValueType>> observations_;
tbansal6f840fc52015-06-13 03:30:34436
tbansal11af83132015-06-24 20:34:22437 // The factor by which the weight of an observation reduces every second.
438 // For example, if an observation is 6 seconds old, its weight would be:
439 // weight_multiplier_per_second_ ^ 6
440 // Calculated from |kHalfLifeSeconds| by solving the following equation:
441 // weight_multiplier_per_second_ ^ kHalfLifeSeconds = 0.5
442 const double weight_multiplier_per_second_;
443
tbansal6f840fc52015-06-13 03:30:34444 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer);
445 };
446
tbansal31ea1bbf82016-02-03 00:13:31447 // Value of round trip time observations is in base::TimeDelta.
448 typedef net::NetworkQualityEstimator::Observation<base::TimeDelta>
449 RttObservation;
450 typedef net::NetworkQualityEstimator::ObservationBuffer<base::TimeDelta>
451 RttObservationBuffer;
452
453 // Value of throughput observations is in kilobits per second.
454 typedef net::NetworkQualityEstimator::Observation<int32_t>
455 ThroughputObservation;
456 typedef net::NetworkQualityEstimator::ObservationBuffer<int32_t>
457 ThroughputObservationBuffer;
458
tbansal75540a52015-07-15 22:18:21459 // This does not use a unordered_map or hash_map for code simplicity (key just
460 // implements operator<, rather than hash and equality) and because the map is
461 // tiny.
462 typedef std::map<NetworkID, CachedNetworkQuality> CachedNetworkQualities;
463
tbansal9533b6f2015-08-11 22:06:08464 // Throughput is set to |kInvalidThroughput| if a valid value is
465 // unavailable. Readers should discard throughput value if it is set to
466 // |kInvalidThroughput|.
467 static const int32_t kInvalidThroughput;
468
tbansalea2fb8c2015-05-22 22:23:00469 // Tiny transfer sizes may give inaccurate throughput results.
470 // Minimum size of the transfer over which the throughput is computed.
471 static const int kMinTransferSizeInBytes = 10000;
472
473 // Minimum duration (in microseconds) of the transfer over which the
474 // throughput is computed.
475 static const int kMinRequestDurationMicroseconds = 1000;
476
tbansalb177b5392015-06-25 11:13:02477 // Minimum valid value of the variation parameter that holds RTT (in
478 // milliseconds) values.
479 static const int kMinimumRTTVariationParameterMsec = 1;
480
481 // Minimum valid value of the variation parameter that holds throughput (in
tbansal31ea1bbf82016-02-03 00:13:31482 // kilobits per second) values.
tbansalb177b5392015-06-25 11:13:02483 static const int kMinimumThroughputVariationParameterKbps = 1;
484
tbansal75540a52015-07-15 22:18:21485 // Maximum size of the cache that holds network quality estimates.
486 // Smaller size may reduce the cache hit rate due to frequent evictions.
487 // Larger size may affect performance.
488 static const size_t kMaximumNetworkQualityCacheSize = 10;
489
490 // Maximum number of observations that can be held in the ObservationBuffer.
491 static const size_t kMaximumObservationsBufferSize = 300;
tbansalb177b5392015-06-25 11:13:02492
tbansal2c6b0d32015-09-17 03:49:02493 // Time duration (in milliseconds) after which the estimate provided by
494 // external estimate provider is considered stale.
495 static const int kExternalEstimateProviderFreshnessDurationMsec =
496 5 * 60 * 1000;
497
tbansal9533b6f2015-08-11 22:06:08498 // Returns the RTT value to be used when the valid RTT is unavailable. Readers
499 // should discard RTT if it is set to the value returned by |InvalidRTT()|.
500 static const base::TimeDelta InvalidRTT();
501
tbansal0f56a39a2016-04-07 22:03:38502 // Notifies |this| of a new transport layer RTT.
503 void OnUpdatedRTTAvailable(SocketPerformanceWatcherFactory::Protocol protocol,
504 const base::TimeDelta& rtt);
505
tbansal2c6b0d32015-09-17 03:49:02506 // Queries the external estimate provider for the latest network quality
507 // estimates, and adds those estimates to the current observation buffer.
508 void QueryExternalEstimateProvider();
tbansal1c92d5b2015-08-14 20:14:43509
tbansalb177b5392015-06-25 11:13:02510 // Obtains operating parameters from the field trial parameters.
511 void ObtainOperatingParams(
512 const std::map<std::string, std::string>& variation_params);
513
514 // Adds the default median RTT and downstream throughput estimate for the
515 // current connection type to the observation buffer.
516 void AddDefaultEstimates();
tbansalea2fb8c2015-05-22 22:23:00517
tbansal11af83132015-06-24 20:34:22518 // Returns an estimate of network quality at the specified |percentile|.
tbansalf7c6d522015-08-05 21:59:36519 // Only the observations later than |begin_timestamp| are taken into account.
tbansal11af83132015-06-24 20:34:22520 // |percentile| must be between 0 and 100 (both inclusive) with higher
521 // percentiles indicating less performant networks. For example, if
522 // |percentile| is 90, then the network is expected to be faster than the
523 // returned estimate with 0.9 probability. Similarly, network is expected to
524 // be slower than the returned estimate with 0.1 probability.
tbansal28fd8a42016-03-25 18:37:38525 base::TimeDelta GetURLRequestRTTEstimateInternal(
526 const base::TimeTicks& begin_timestamp,
527 int percentile) const;
tbansalf7c6d522015-08-05 21:59:36528 int32_t GetDownlinkThroughputKbpsEstimateInternal(
529 const base::TimeTicks& begin_timestamp,
530 int percentile) const;
tbansal11af83132015-06-24 20:34:22531
tbansal75540a52015-07-15 22:18:21532 // Returns the current network ID checking by calling the platform APIs.
533 // Virtualized for testing.
534 virtual NetworkID GetCurrentNetworkID() const;
535
536 // Writes the estimated quality of the current network to the cache.
537 void CacheNetworkQualityEstimate();
538
tbansal31ea1bbf82016-02-03 00:13:31539 void NotifyObserversOfRTT(const RttObservation& observation);
bengrf1b6738e2015-10-07 01:21:30540
tbansal31ea1bbf82016-02-03 00:13:31541 void NotifyObserversOfThroughput(const ThroughputObservation& observation);
bengrf1b6738e2015-10-07 01:21:30542
tbansal2e590c62015-07-09 00:22:02543 // Records the UMA related to RTT.
544 void RecordRTTUMA(int32_t estimated_value_msec,
545 int32_t actual_value_msec) const;
546
tbansal79ed5cd2015-08-10 18:53:56547 // Returns true only if |request| can be used for network quality estimation.
548 // Only the requests that go over network are considered to provide useful
549 // observations.
550 bool RequestProvidesUsefulObservations(const URLRequest& request) const;
551
tbansal2c6b0d32015-09-17 03:49:02552 // Values of external estimate provider status. This enum must remain
553 // synchronized with the enum of the same name in
554 // metrics/histograms/histograms.xml.
555 enum NQEExternalEstimateProviderStatus {
556 EXTERNAL_ESTIMATE_PROVIDER_STATUS_NOT_AVAILABLE,
557 EXTERNAL_ESTIMATE_PROVIDER_STATUS_AVAILABLE,
558 EXTERNAL_ESTIMATE_PROVIDER_STATUS_QUERIED,
559 EXTERNAL_ESTIMATE_PROVIDER_STATUS_QUERY_SUCCESSFUL,
560 EXTERNAL_ESTIMATE_PROVIDER_STATUS_CALLBACK,
561 EXTERNAL_ESTIMATE_PROVIDER_STATUS_BOUNDARY
562 };
563
564 // Records the metrics related to external estimate provider.
565 void RecordExternalEstimateProviderMetrics(
566 NQEExternalEstimateProviderStatus status) const;
567
tbansalea2fb8c2015-05-22 22:23:00568 // Determines if the requests to local host can be used in estimating the
569 // network quality. Set to true only for tests.
570 const bool allow_localhost_requests_;
571
tbansalafc2c1cb2015-06-15 23:58:59572 // Determines if the responses smaller than |kMinTransferSizeInBytes|
573 // or shorter than |kMinTransferSizeInBytes| can be used in estimating the
574 // network quality. Set to true only for tests.
575 const bool allow_small_responses_;
576
tbansalea2fb8c2015-05-22 22:23:00577 // Time when last connection change was observed.
578 base::TimeTicks last_connection_change_;
579
tbansal75540a52015-07-15 22:18:21580 // ID of the current network.
581 NetworkID current_network_id_;
tbansalea2fb8c2015-05-22 22:23:00582
tbansal75540a52015-07-15 22:18:21583 // Peak network quality (fastest round-trip-time (RTT) and highest
584 // downstream throughput) measured since last connectivity change. RTT is
585 // measured from time the request is sent until the first byte received.
586 // The accuracy is decreased by ignoring these factors:
tbansalea2fb8c2015-05-22 22:23:00587 // 1) Multiple URLRequests can occur concurrently.
tbansal75540a52015-07-15 22:18:21588 // 2) Includes server processing time.
589 NetworkQuality peak_network_quality_;
590
591 // Cache that stores quality of previously seen networks.
592 CachedNetworkQualities cached_network_qualities_;
tbansal6f840fc52015-06-13 03:30:34593
tbansal31ea1bbf82016-02-03 00:13:31594 // Buffer that holds throughput observations (in kilobits per second) sorted
595 // by timestamp.
596 ThroughputObservationBuffer downstream_throughput_kbps_observations_;
tbansal6f840fc52015-06-13 03:30:34597
tbansal31ea1bbf82016-02-03 00:13:31598 // Buffer that holds RTT observations sorted by timestamp.
tbansal28fd8a42016-03-25 18:37:38599 RttObservationBuffer rtt_observations_;
tbansalea2fb8c2015-05-22 22:23:00600
tbansalb177b5392015-06-25 11:13:02601 // Default network quality observations obtained from the network quality
602 // estimator field trial parameters. The observations are indexed by
603 // ConnectionType.
604 NetworkQuality
605 default_observations_[NetworkChangeNotifier::CONNECTION_LAST + 1];
606
tbansal2e590c62015-07-09 00:22:02607 // Estimated network quality. Updated on mainframe requests.
608 NetworkQuality estimated_median_network_quality_;
609
tbansal1c92d5b2015-08-14 20:14:43610 // ExternalEstimateProvider that provides network quality using operating
611 // system APIs. May be NULL.
tbansal2c6b0d32015-09-17 03:49:02612 const scoped_ptr<ExternalEstimateProvider> external_estimate_provider_;
tbansal1c92d5b2015-08-14 20:14:43613
bengrf1b6738e2015-10-07 01:21:30614 // Observer lists for round trip times and throughput measurements.
615 base::ObserverList<RTTObserver> rtt_observer_list_;
616 base::ObserverList<ThroughputObserver> throughput_observer_list_;
617
tbansal0f56a39a2016-04-07 22:03:38618 scoped_ptr<SocketPerformanceWatcherFactory> watcher_factory_;
619
tbansalea2fb8c2015-05-22 22:23:00620 base::ThreadChecker thread_checker_;
621
tbansal0f56a39a2016-04-07 22:03:38622 base::WeakPtrFactory<NetworkQualityEstimator> weak_ptr_factory_;
623
tbansalea2fb8c2015-05-22 22:23:00624 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator);
625};
626
627} // namespace net
628
629#endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_