blob: ea4eaa1dfd96167084a45217ac0297e80cb407e9 [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
8#include <stdint.h>
9
tbansal6f840fc52015-06-13 03:30:3410#include <deque>
tbansalb177b5392015-06-25 11:13:0211#include <map>
tbansal75540a52015-07-15 22:18:2112#include <string>
jsbellcea42a52015-11-30 23:50:2513#include <tuple>
tbansal6f840fc52015-06-13 03:30:3414
tbansalea2fb8c2015-05-22 22:23:0015#include "base/gtest_prod_util.h"
16#include "base/macros.h"
bengrf1b6738e2015-10-07 01:21:3017#include "base/memory/ref_counted.h"
tbansal75540a52015-07-15 22:18:2118#include "base/memory/scoped_ptr.h"
bengrf1b6738e2015-10-07 01:21:3019#include "base/observer_list.h"
tbansalea2fb8c2015-05-22 22:23:0020#include "base/threading/thread_checker.h"
21#include "base/time/time.h"
tbansal1c92d5b2015-08-14 20:14:4322#include "net/base/external_estimate_provider.h"
tbansal6f840fc52015-06-13 03:30:3423#include "net/base/net_export.h"
tbansalea2fb8c2015-05-22 22:23:0024#include "net/base/network_change_notifier.h"
tbansalba8f4112015-09-03 21:57:1925#include "net/base/socket_performance_watcher.h"
26#include "net/base/socket_performance_watcher_factory.h"
tbansalea2fb8c2015-05-22 22:23:0027
28namespace net {
29
tbansal79ed5cd2015-08-10 18:53:5630class URLRequest;
31
tbansalea2fb8c2015-05-22 22:23:0032// 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.
40class NET_EXPORT_PRIVATE NetworkQualityEstimator
tbansal1c92d5b2015-08-14 20:14:4341 : public NetworkChangeNotifier::ConnectionTypeObserver,
tbansalba8f4112015-09-03 21:57:1942 public ExternalEstimateProvider::UpdatedEstimateDelegate,
43 public SocketPerformanceWatcherFactory {
tbansalea2fb8c2015-05-22 22:23:0044 public:
bengrf1b6738e2015-10-07 01:21:3045 // 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
tbansalea2fb8c2015-05-22 22:23:00103 // Creates a new NetworkQualityEstimator.
tbansalb177b5392015-06-25 11:13:02104 // |variation_params| is the map containing all field trial parameters
105 // related to NetworkQualityEstimator field trial.
tbansal1c92d5b2015-08-14 20:14:43106 // |external_estimates_provider| may be NULL.
107 NetworkQualityEstimator(
108 scoped_ptr<ExternalEstimateProvider> external_estimates_provider,
tbansalb177b5392015-06-25 11:13:02109 const std::map<std::string, std::string>& variation_params);
tbansalea2fb8c2015-05-22 22:23:00110
bengrf1b6738e2015-10-07 01:21:30111 // 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
tbansalea2fb8c2015-05-22 22:23:00130 ~NetworkQualityEstimator() override;
131
rockotf5eb5752015-08-11 19:45:31132 // Returns true if RTT is available and sets |rtt| to estimated RTT.
tbansalb658f3882015-08-19 21:27:49133 // Virtualized for testing. |rtt| should not be null.
tbansal9533b6f2015-08-11 22:06:08134 virtual bool GetRTTEstimate(base::TimeDelta* rtt) const;
tbansal91fb5882015-08-05 04:24:56135
136 // Returns true if downlink throughput is available and sets |kbps| to
137 // estimated downlink throughput (in Kilobits per second).
tbansalb658f3882015-08-19 21:27:49138 // Virtualized for testing. |kbps| should not be null.
tbansal9533b6f2015-08-11 22:06:08139 virtual bool GetDownlinkThroughputKbpsEstimate(int32_t* kbps) const;
tbansal91fb5882015-08-05 04:24:56140
tbansal79ed5cd2015-08-10 18:53:56141 // 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);
tbansalea2fb8c2015-05-22 22:23:00148
tbansal9533b6f2015-08-11 22:06:08149 // Returns true if median RTT is available and sets |rtt| to the median of
150 // RTT observations since |begin_timestamp|.
tbansalb658f3882015-08-19 21:27:49151 // 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;
tbansalf7c6d522015-08-05 21:59:36161
tbansalba8f4112015-09-03 21:57:19162 // SocketPerformanceWatcherFactory implementation:
tbansalc8a94ea2015-11-02 23:58:51163 scoped_ptr<SocketPerformanceWatcher> CreateSocketPerformanceWatcher(
164 const Protocol protocol) override;
165 void OnUpdatedRTTAvailable(const Protocol protocol,
166 const base::TimeDelta& rtt) override;
tbansalba8f4112015-09-03 21:57:19167
bengrf1b6738e2015-10-07 01:21:30168 // 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
tbansal75540a52015-07-15 22:18:21184 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 {
jsbellcea42a52015-11-30 23:50:25205 return std::tie(type, id) < std::tie(other.type, other.id);
tbansal75540a52015-07-15 22:18:21206 }
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
tbansal75540a52015-07-15 22:18:21222 // Returns true if the cached network quality estimate was successfully read.
223 bool ReadCachedNetworkQualityEstimate();
224
tbansalba8f4112015-09-03 21:57:19225 // NetworkChangeNotifier::ConnectionTypeObserver implementation:
tbansal75540a52015-07-15 22:18:21226 void OnConnectionTypeChanged(
227 NetworkChangeNotifier::ConnectionType type) override;
228
tbansal2c6b0d32015-09-17 03:49:02229 // ExternalEstimateProvider::UpdatedEstimateObserver implementation.
230 void OnUpdatedEstimateAvailable() override;
231
tbansalea2fb8c2015-05-22 22:23:00232 private:
tbansal6f840fc52015-06-13 03:30:34233 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations);
tbansal2e590c62015-07-09 00:22:02234 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestKbpsRTTUpdates);
tbansal6f840fc52015-06-13 03:30:34235 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestAddObservation);
tbansalb177b5392015-06-25 11:13:02236 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, ObtainOperatingParams);
tbansal4418ad122015-06-26 19:06:33237 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, HalfLifeParam);
tbansalea2fb8c2015-05-22 22:23:00238 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator);
tbansal11af83132015-06-24 20:34:22239 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
240 PercentileSameTimestamps);
241 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
242 PercentileDifferentTimestamps);
243 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, ComputedPercentiles);
tbansal75540a52015-07-15 22:18:21244 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestCaching);
245 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
246 TestLRUCacheMaximumSize);
tbansalf7c6d522015-08-05 21:59:36247 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestGetMedianRTTSince);
tbansal2c6b0d32015-09-17 03:49:02248 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
249 TestExternalEstimateProvider);
250 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
251 TestExternalEstimateProviderMergeEstimates);
bengrf1b6738e2015-10-07 01:21:30252 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestObservers);
tbansal75540a52015-07-15 22:18:21253
tbansal9533b6f2015-08-11 22:06:08254 // 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
tbansal75540a52015-07-15 22:18:21285 // 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.jmad81bb8932015-08-31 05:25:43293 const NetworkQuality& network_quality() const { return network_quality_; }
tbansal75540a52015-07-15 22:18:21294
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 };
tbansalea2fb8c2015-05-22 22:23:00308
tbansal6f840fc52015-06-13 03:30:34309 // Records the round trip time or throughput observation, along with the time
bengrf1b6738e2015-10-07 01:21:30310 // 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.
tbansal5659c35e2015-06-25 16:49:36315 struct NET_EXPORT_PRIVATE Observation {
bengrf1b6738e2015-10-07 01:21:30316 Observation(int32_t value,
317 base::TimeTicks timestamp,
318 ObservationSource source);
tbansal6f840fc52015-06-13 03:30:34319 ~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;
bengrf1b6738e2015-10-07 01:21:30326
327 // The source of the observation.
328 const ObservationSource source;
tbansal6f840fc52015-06-13 03:30:34329 };
330
tbansal11af83132015-06-24 20:34:22331 // Holds an observation and its weight.
tbansal75540a52015-07-15 22:18:21332 struct NET_EXPORT_PRIVATE WeightedObservation {
tbansal11af83132015-06-24 20:34:22333 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
tbansal6f840fc52015-06-13 03:30:34357 // Stores observations sorted by time.
tbansal5659c35e2015-06-25 16:49:36358 class NET_EXPORT_PRIVATE ObservationBuffer {
tbansal6f840fc52015-06-13 03:30:34359 public:
tbansal4418ad122015-06-26 19:06:33360 explicit ObservationBuffer(double weight_multiplier_per_second);
tbansal6f840fc52015-06-13 03:30:34361 ~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
tbansalf7c6d522015-08-05 21:59:36373 // 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;
tbansal11af83132015-06-24 20:34:22383
tbansal6f840fc52015-06-13 03:30:34384 private:
385 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations);
tbansalb177b5392015-06-25 11:13:02386 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
387 ObtainOperatingParams);
tbansal4418ad122015-06-26 19:06:33388 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, HalfLifeParam);
tbansal6f840fc52015-06-13 03:30:34389
tbansal11af83132015-06-24 20:34:22390 // Computes the weighted observations and stores them in
391 // |weighted_observations| sorted by ascending |WeightedObservation.value|.
tbansalf7c6d522015-08-05 21:59:36392 // 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.
tbansal11af83132015-06-24 20:34:22396 void ComputeWeightedObservations(
tbansalf7c6d522015-08-05 21:59:36397 const base::TimeTicks& begin_timestamp,
tbansal11af83132015-06-24 20:34:22398 std::vector<WeightedObservation>& weighted_observations,
399 double* total_weight) const;
400
tbansal6f840fc52015-06-13 03:30:34401 // Holds observations sorted by time, with the oldest observation at the
402 // front of the queue.
403 std::deque<Observation> observations_;
404
tbansal11af83132015-06-24 20:34:22405 // 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
tbansal6f840fc52015-06-13 03:30:34412 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer);
413 };
414
tbansal75540a52015-07-15 22:18:21415 // 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
tbansal9533b6f2015-08-11 22:06:08420 // 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
tbansalea2fb8c2015-05-22 22:23:00425 // 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
tbansalb177b5392015-06-25 11:13:02433 // 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
tbansal75540a52015-07-15 22:18:21441 // 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;
tbansalb177b5392015-06-25 11:13:02448
tbansal2c6b0d32015-09-17 03:49:02449 // 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
tbansal9533b6f2015-08-11 22:06:08454 // 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
tbansal2c6b0d32015-09-17 03:49:02458 // Queries the external estimate provider for the latest network quality
459 // estimates, and adds those estimates to the current observation buffer.
460 void QueryExternalEstimateProvider();
tbansal1c92d5b2015-08-14 20:14:43461
tbansalb177b5392015-06-25 11:13:02462 // 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();
tbansalea2fb8c2015-05-22 22:23:00469
tbansal11af83132015-06-24 20:34:22470 // Returns an estimate of network quality at the specified |percentile|.
tbansalf7c6d522015-08-05 21:59:36471 // Only the observations later than |begin_timestamp| are taken into account.
tbansal11af83132015-06-24 20:34:22472 // |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.
tbansalf7c6d522015-08-05 21:59:36477 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;
tbansal11af83132015-06-24 20:34:22482
tbansal75540a52015-07-15 22:18:21483 // 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
bengrf1b6738e2015-10-07 01:21:30490 void NotifyObserversOfRTT(const Observation& observation);
491
492 void NotifyObserversOfThroughput(const Observation& observation);
493
tbansal2e590c62015-07-09 00:22:02494 // Records the UMA related to RTT.
495 void RecordRTTUMA(int32_t estimated_value_msec,
496 int32_t actual_value_msec) const;
497
tbansal79ed5cd2015-08-10 18:53:56498 // 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
tbansal2c6b0d32015-09-17 03:49:02503 // 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
tbansalea2fb8c2015-05-22 22:23:00519 // 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
tbansalafc2c1cb2015-06-15 23:58:59523 // 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
tbansalea2fb8c2015-05-22 22:23:00528 // Time when last connection change was observed.
529 base::TimeTicks last_connection_change_;
530
tbansal75540a52015-07-15 22:18:21531 // ID of the current network.
532 NetworkID current_network_id_;
tbansalea2fb8c2015-05-22 22:23:00533
tbansal75540a52015-07-15 22:18:21534 // 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:
tbansalea2fb8c2015-05-22 22:23:00538 // 1) Multiple URLRequests can occur concurrently.
tbansal75540a52015-07-15 22:18:21539 // 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_;
tbansal6f840fc52015-06-13 03:30:34544
tbansal11af83132015-06-24 20:34:22545 // Buffer that holds Kbps observations sorted by timestamp.
tbansal79ed5cd2015-08-10 18:53:56546 ObservationBuffer downstream_throughput_kbps_observations_;
tbansal6f840fc52015-06-13 03:30:34547
tbansal11af83132015-06-24 20:34:22548 // Buffer that holds RTT (in milliseconds) observations sorted by timestamp.
tbansal6f840fc52015-06-13 03:30:34549 ObservationBuffer rtt_msec_observations_;
tbansalea2fb8c2015-05-22 22:23:00550
tbansalb177b5392015-06-25 11:13:02551 // 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
tbansal2e590c62015-07-09 00:22:02557 // Estimated network quality. Updated on mainframe requests.
558 NetworkQuality estimated_median_network_quality_;
559
tbansal1c92d5b2015-08-14 20:14:43560 // ExternalEstimateProvider that provides network quality using operating
561 // system APIs. May be NULL.
tbansal2c6b0d32015-09-17 03:49:02562 const scoped_ptr<ExternalEstimateProvider> external_estimate_provider_;
tbansal1c92d5b2015-08-14 20:14:43563
bengrf1b6738e2015-10-07 01:21:30564 // Observer lists for round trip times and throughput measurements.
565 base::ObserverList<RTTObserver> rtt_observer_list_;
566 base::ObserverList<ThroughputObserver> throughput_observer_list_;
567
tbansalea2fb8c2015-05-22 22:23:00568 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_