blob: 51d4cd02312a42d7ae7aff3ee07abd78dae1b657 [file] [log] [blame]
tbansal80a52162016-05-20 17:55:041// Copyright 2016 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_NQE_THROUGHPUT_ANALYZER_H_
6#define NET_NQE_THROUGHPUT_ANALYZER_H_
7
8#include <stdint.h>
9
Takuto Ikutafc262dcd2019-01-08 06:26:5810#include <unordered_map>
11#include <unordered_set>
12
tbansal80a52162016-05-20 17:55:0413#include "base/callback.h"
Keishi Hattori0e45c022021-11-27 09:25:5214#include "base/memory/raw_ptr.h"
tbansal80a52162016-05-20 17:55:0415#include "base/memory/ref_counted.h"
Tarun Bansalc71cf622018-10-16 16:24:4416#include "base/sequence_checker.h"
tbansal80a52162016-05-20 17:55:0417#include "base/time/time.h"
18#include "net/base/net_export.h"
tbansal82edab42017-06-19 05:55:2519#include "net/log/net_log_with_source.h"
tbansal80a52162016-05-20 17:55:0420
21namespace {
Anna Malova2592bd4a2020-03-03 15:52:1622typedef base::RepeatingCallback<void(int32_t)> ThroughputObservationCallback;
tbansal80a52162016-05-20 17:55:0423}
24
25namespace base {
26class SingleThreadTaskRunner;
Tarun Bansal00943112017-10-10 22:49:1427class TickClock;
tbansal80a52162016-05-20 17:55:0428}
29
30namespace net {
31
tbansal4a4305a2017-06-08 05:03:1932class NetworkQualityEstimatorParams;
Tarun Bansaled2b20b642018-10-15 19:51:3233class NetworkQualityEstimator;
tbansal80a52162016-05-20 17:55:0434class URLRequest;
35
36namespace nqe {
37
38namespace internal {
39
40// Makes throughput observations. Polls NetworkActivityMonitor
41// (TrafficStats on Android) to count number of bits received over throughput
42// observation windows in accordance with the following rules:
43// (1) A new window of observation begins any time a URL request header is
44// about to be sent, or a request completes or is destroyed.
45// (2) A request is "active" if its headers are sent, but it hasn't completed,
46// and "local" if destined to local host. If at any time during a
47// throughput observation window there is an active, local request, the
48// window is discarded.
49// (3) If less than 32KB is received over the network during a window of
50// observation, that window is discarded.
51class NET_EXPORT_PRIVATE ThroughputAnalyzer {
52 public:
53 // |throughput_observation_callback| is called on the |task_runner| when
54 // |this| has a new throughput observation.
55 // |use_local_host_requests_for_tests| should only be true when testing
56 // against local HTTP server and allows the requests to local host to be
57 // used for network quality estimation. |use_smaller_responses_for_tests|
58 // should only be true when testing, and allows the responses smaller than
59 // |kMinTransferSizeInBits| or shorter than
60 // |kMinRequestDurationMicroseconds| to be used for network quality
61 // estimation.
62 // Virtualized for testing.
63 ThroughputAnalyzer(
Tarun Bansaled2b20b642018-10-15 19:51:3264 const NetworkQualityEstimator* network_quality_estimator,
tbansalff83205e2017-05-23 00:09:4565 const NetworkQualityEstimatorParams* params,
tbansal80a52162016-05-20 17:55:0466 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
67 ThroughputObservationCallback throughput_observation_callback,
Greg Thompsonaa48ce8d2018-04-03 06:11:4368 const base::TickClock* tick_clock,
tbansal82edab42017-06-19 05:55:2569 const NetLogWithSource& net_log);
Peter Boström293b1342021-09-22 17:31:4370
71 ThroughputAnalyzer(const ThroughputAnalyzer&) = delete;
72 ThroughputAnalyzer& operator=(const ThroughputAnalyzer&) = delete;
73
tbansal80a52162016-05-20 17:55:0474 virtual ~ThroughputAnalyzer();
75
76 // Notifies |this| that the headers of |request| are about to be sent.
77 void NotifyStartTransaction(const URLRequest& request);
78
Tarun Bansal00943112017-10-10 22:49:1479 // Notifies |this| that unfiltered bytes have been read for |request|.
80 void NotifyBytesRead(const URLRequest& request);
81
tbansal80a52162016-05-20 17:55:0482 // Notifies |this| that |request| has completed.
83 void NotifyRequestCompleted(const URLRequest& request);
84
Jianfeng Wange829c4982019-07-03 21:17:0985 // Notifies |this| that |request| has an expected response body size in octets
86 // (8-bit bytes). |expected_content_size| is an estimate of total body length
87 // based on the Content-Length header field when available or a general size
88 // estimate when the Content-Length is not provided.
89 void NotifyExpectedResponseContentSize(const URLRequest& request,
90 int64_t expected_content_size);
91
tbansal80a52162016-05-20 17:55:0492 // Notifies |this| of a change in connection type.
93 void OnConnectionTypeChanged();
94
tbansal7018e2a2016-06-25 00:40:3995 // |use_localhost_requests| should only be true when testing against local
96 // HTTP server and allows the requests to local host to be used for network
97 // quality estimation.
98 void SetUseLocalHostRequestsForTesting(bool use_localhost_requests);
99
tbansal82edab42017-06-19 05:55:25100 // Returns true if throughput is currently tracked by a throughput
101 // observation window.
102 bool IsCurrentlyTrackingThroughput() const;
103
Tarun Bansal00943112017-10-10 22:49:14104 // Overrides the tick clock used by |this| for testing.
Greg Thompsonaa48ce8d2018-04-03 06:11:43105 void SetTickClockForTesting(const base::TickClock* tick_clock);
Tarun Bansal00943112017-10-10 22:49:14106
tbansal80a52162016-05-20 17:55:04107 // Returns the number of bits received by Chromium so far. The count may not
108 // start from zero, so the caller should only look at difference from a prior
109 // call. The count is obtained by polling TrafficStats on Android, and
110 // net::NetworkActivityMonitor on all other platforms. Virtualized for
111 // testing.
112 virtual int64_t GetBitsReceived() const;
113
Tarun Bansal00943112017-10-10 22:49:14114 // Returns the number of in-flight requests that can be used for computing
115 // throughput.
Jianfeng Wang25d28452019-07-17 00:35:21116 size_t CountActiveInFlightRequests() const;
117
118 // Returns the total number of in-flight requests. This also includes hanging
119 // requests.
120 size_t CountTotalInFlightRequests() const;
Tarun Bansal00943112017-10-10 22:49:14121
Jianfeng Wange829c4982019-07-03 21:17:09122 // Returns the sum of expected response content size in bytes for all inflight
123 // requests. Request with an unknown response content size have the default
124 // response content size.
125 int64_t CountTotalContentSizeBytes() const;
126
127 protected:
128 // Exposed for testing.
129 bool disable_throughput_measurements_for_testing() const {
130 return disable_throughput_measurements_;
131 }
132
Tarun Bansal00943112017-10-10 22:49:14133 // Removes hanging requests from |requests_|. If any hanging requests are
134 // detected to be in-flight, the observation window is ended. Protected for
135 // testing.
136 void EraseHangingRequests(const URLRequest& request);
137
Tarun Bansal8914dbba2017-12-12 21:03:59138 // Returns true if the current throughput observation window is heuristically
139 // determined to contain hanging requests.
140 bool IsHangingWindow(int64_t bits_received,
141 base::TimeDelta duration,
142 double downstream_kbps_double) const;
143
tbansal80a52162016-05-20 17:55:04144 private:
145 friend class TestThroughputAnalyzer;
146
Jianfeng Wange829c4982019-07-03 21:17:09147 // Mapping from URL request to the expected content size of the response body
148 // for that request. The map tracks all inflight requests. If the expected
149 // content size is not available, the value is set to the default value.
150 typedef std::unordered_map<const URLRequest*, int64_t> ResponseContentSizes;
151
Tarun Bansal00943112017-10-10 22:49:14152 // Mapping from URL request to the last time data was received for that
153 // request.
Takuto Ikutaadf31eb2019-01-05 00:32:48154 typedef std::unordered_map<const URLRequest*, base::TimeTicks> Requests;
Tarun Bansal00943112017-10-10 22:49:14155
156 // Set of URL requests to hold the requests that reduce the accuracy of
157 // throughput computation. These requests are not used in throughput
158 // computation.
Takuto Ikuta8332bf9d2019-01-05 03:58:00159 typedef std::unordered_set<const URLRequest*> AccuracyDegradingRequests;
tbansal80a52162016-05-20 17:55:04160
Jianfeng Wange829c4982019-07-03 21:17:09161 // Updates the response content size map for |request|. Also keeps the total
162 // response content size counter updated. Adds an new entry if there is no
163 // matching record in the map.
164 void UpdateResponseContentSize(const URLRequest* request,
165 int64_t response_size);
166
tbansal80a52162016-05-20 17:55:04167 // Returns true if downstream throughput can be recorded. In that case,
168 // |downstream_kbps| is set to the computed downstream throughput (in
169 // kilobits per second). If a downstream throughput observation is taken,
170 // then the throughput observation window is reset so as to continue
171 // tracking throughput. A throughput observation can be taken only if the
172 // time-window is currently active, and enough bytes have accumulated in
173 // that window. |downstream_kbps| should not be null.
tbansalff83205e2017-05-23 00:09:45174 bool MaybeGetThroughputObservation(int32_t* downstream_kbps);
tbansal80a52162016-05-20 17:55:04175
176 // Starts the throughput observation window that keeps track of network
177 // bytes if the following conditions are true:
178 // (i) All active requests are non-local;
179 // (ii) There is at least one active, non-local request; and,
180 // (iii) The throughput observation window is not already tracking
181 // throughput. The window is started by setting the |start_| and
182 // |bits_received_|.
183 void MaybeStartThroughputObservationWindow();
184
185 // EndThroughputObservationWindow ends the throughput observation window.
186 void EndThroughputObservationWindow();
187
tbansal80a52162016-05-20 17:55:04188 // Returns true if the |request| degrades the accuracy of the throughput
189 // observation window. A local request or a request that spans a connection
190 // change degrades the accuracy of the throughput computation.
191 bool DegradesAccuracy(const URLRequest& request) const;
192
193 // Bounds |accuracy_degrading_requests_| and |requests_| to ensure their sizes
194 // do not exceed their capacities.
195 void BoundRequestsSize();
196
Tarun Bansal00943112017-10-10 22:49:14197 // Guaranteed to be non-null during the duration of |this|.
Keishi Hattori0e45c022021-11-27 09:25:52198 const raw_ptr<const NetworkQualityEstimator> network_quality_estimator_;
Tarun Bansal00943112017-10-10 22:49:14199
200 // Guaranteed to be non-null during the duration of |this|.
Keishi Hattori0e45c022021-11-27 09:25:52201 const raw_ptr<const NetworkQualityEstimatorParams> params_;
Tarun Bansal00943112017-10-10 22:49:14202
tbansal80a52162016-05-20 17:55:04203 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
204
205 // Called every time a new throughput observation is available.
206 ThroughputObservationCallback throughput_observation_callback_;
207
Tarun Bansal00943112017-10-10 22:49:14208 // Guaranteed to be non-null during the lifetime of |this|.
Kenichi Ishibashi78d751862021-11-11 09:32:38209 // This isn't a const pointer since SetTickClockForTesting() modifies it.
Keishi Hattori0e45c022021-11-27 09:25:52210 raw_ptr<const base::TickClock> tick_clock_;
Tarun Bansal00943112017-10-10 22:49:14211
tbansal80a52162016-05-20 17:55:04212 // Time when last connection change was observed.
213 base::TimeTicks last_connection_change_;
214
215 // Start time of the current throughput observation window. Set to null if
216 // the window is not currently active.
217 base::TimeTicks window_start_time_;
218
219 // Number of bits received prior to |start_| as reported by
220 // NetworkActivityMonitor.
Tsuyoshi Horo2fa6b8de2022-06-09 01:40:20221 int64_t bits_received_at_window_start_ = 0;
tbansal80a52162016-05-20 17:55:04222
223 // Container that holds active requests that reduce the accuracy of
224 // throughput computation. These requests are not used in throughput
225 // computation.
Tarun Bansal00943112017-10-10 22:49:14226 AccuracyDegradingRequests accuracy_degrading_requests_;
tbansal80a52162016-05-20 17:55:04227
228 // Container that holds active requests that do not reduce the accuracy of
229 // throughput computation. These requests are used in throughput computation.
230 Requests requests_;
231
Jianfeng Wange829c4982019-07-03 21:17:09232 // Container that holds inflight request sizes. These requests are used in
233 // computing the total of response content size for all inflight requests.
234 ResponseContentSizes response_content_sizes_;
235
236 // The running total of response content size for all inflight requests.
Tsuyoshi Horo2fa6b8de2022-06-09 01:40:20237 int64_t total_response_content_size_ = 0;
Jianfeng Wange829c4982019-07-03 21:17:09238
Tarun Bansal00943112017-10-10 22:49:14239 // Last time when the check for hanging requests was run.
240 base::TimeTicks last_hanging_request_check_;
241
tbansal80a52162016-05-20 17:55:04242 // If true, then |this| throughput analyzer stops tracking the throughput
243 // observations until Chromium is restarted. This may happen if the throughput
244 // analyzer has lost track of the requests that degrade throughput computation
245 // accuracy.
Tsuyoshi Horo2fa6b8de2022-06-09 01:40:20246 bool disable_throughput_measurements_ = false;
tbansal80a52162016-05-20 17:55:04247
248 // Determines if the requests to local host can be used in estimating the
249 // network quality. Set to true only for tests.
Tsuyoshi Horo2fa6b8de2022-06-09 01:40:20250 bool use_localhost_requests_for_tests_ = false;
tbansal80a52162016-05-20 17:55:04251
Tarun Bansalc71cf622018-10-16 16:24:44252 SEQUENCE_CHECKER(sequence_checker_);
tbansal80a52162016-05-20 17:55:04253
tbansal82edab42017-06-19 05:55:25254 NetLogWithSource net_log_;
tbansal80a52162016-05-20 17:55:04255};
256
257} // namespace internal
258
259} // namespace nqe
260
261} // namespace net
262
bnc3698b0a02016-12-09 23:36:50263#endif // NET_NQE_THROUGHPUT_ANALYZER_H_