tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 1 | // 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 Ikuta | fc262dcd | 2019-01-08 06:26:58 | [diff] [blame] | 10 | #include <unordered_map> |
| 11 | #include <unordered_set> |
| 12 | |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 13 | #include "base/callback.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 14 | #include "base/memory/raw_ptr.h" |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 15 | #include "base/memory/ref_counted.h" |
Tarun Bansal | c71cf62 | 2018-10-16 16:24:44 | [diff] [blame] | 16 | #include "base/sequence_checker.h" |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 17 | #include "base/time/time.h" |
| 18 | #include "net/base/net_export.h" |
tbansal | 82edab4 | 2017-06-19 05:55:25 | [diff] [blame] | 19 | #include "net/log/net_log_with_source.h" |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 20 | |
| 21 | namespace { |
Anna Malova | 2592bd4a | 2020-03-03 15:52:16 | [diff] [blame] | 22 | typedef base::RepeatingCallback<void(int32_t)> ThroughputObservationCallback; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | namespace base { |
| 26 | class SingleThreadTaskRunner; |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 27 | class TickClock; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | namespace net { |
| 31 | |
tbansal | 4a4305a | 2017-06-08 05:03:19 | [diff] [blame] | 32 | class NetworkQualityEstimatorParams; |
Tarun Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 33 | class NetworkQualityEstimator; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 34 | class URLRequest; |
| 35 | |
| 36 | namespace nqe { |
| 37 | |
| 38 | namespace 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. |
| 51 | class 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 Bansal | ed2b20b64 | 2018-10-15 19:51:32 | [diff] [blame] | 64 | const NetworkQualityEstimator* network_quality_estimator, |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 65 | const NetworkQualityEstimatorParams* params, |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 66 | scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 67 | ThroughputObservationCallback throughput_observation_callback, |
Greg Thompson | aa48ce8d | 2018-04-03 06:11:43 | [diff] [blame] | 68 | const base::TickClock* tick_clock, |
tbansal | 82edab4 | 2017-06-19 05:55:25 | [diff] [blame] | 69 | const NetLogWithSource& net_log); |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 70 | |
| 71 | ThroughputAnalyzer(const ThroughputAnalyzer&) = delete; |
| 72 | ThroughputAnalyzer& operator=(const ThroughputAnalyzer&) = delete; |
| 73 | |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 74 | virtual ~ThroughputAnalyzer(); |
| 75 | |
| 76 | // Notifies |this| that the headers of |request| are about to be sent. |
| 77 | void NotifyStartTransaction(const URLRequest& request); |
| 78 | |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 79 | // Notifies |this| that unfiltered bytes have been read for |request|. |
| 80 | void NotifyBytesRead(const URLRequest& request); |
| 81 | |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 82 | // Notifies |this| that |request| has completed. |
| 83 | void NotifyRequestCompleted(const URLRequest& request); |
| 84 | |
Jianfeng Wang | e829c498 | 2019-07-03 21:17:09 | [diff] [blame] | 85 | // 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 | |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 92 | // Notifies |this| of a change in connection type. |
| 93 | void OnConnectionTypeChanged(); |
| 94 | |
tbansal | 7018e2a | 2016-06-25 00:40:39 | [diff] [blame] | 95 | // |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 | |
tbansal | 82edab4 | 2017-06-19 05:55:25 | [diff] [blame] | 100 | // Returns true if throughput is currently tracked by a throughput |
| 101 | // observation window. |
| 102 | bool IsCurrentlyTrackingThroughput() const; |
| 103 | |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 104 | // Overrides the tick clock used by |this| for testing. |
Greg Thompson | aa48ce8d | 2018-04-03 06:11:43 | [diff] [blame] | 105 | void SetTickClockForTesting(const base::TickClock* tick_clock); |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 106 | |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 107 | // 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 Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 114 | // Returns the number of in-flight requests that can be used for computing |
| 115 | // throughput. |
Jianfeng Wang | 25d2845 | 2019-07-17 00:35:21 | [diff] [blame] | 116 | 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 Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 121 | |
Jianfeng Wang | e829c498 | 2019-07-03 21:17:09 | [diff] [blame] | 122 | // 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 Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 133 | // 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 Bansal | 8914dbba | 2017-12-12 21:03:59 | [diff] [blame] | 138 | // 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 | |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 144 | private: |
| 145 | friend class TestThroughputAnalyzer; |
| 146 | |
Jianfeng Wang | e829c498 | 2019-07-03 21:17:09 | [diff] [blame] | 147 | // 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 Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 152 | // Mapping from URL request to the last time data was received for that |
| 153 | // request. |
Takuto Ikuta | adf31eb | 2019-01-05 00:32:48 | [diff] [blame] | 154 | typedef std::unordered_map<const URLRequest*, base::TimeTicks> Requests; |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 155 | |
| 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 Ikuta | 8332bf9d | 2019-01-05 03:58:00 | [diff] [blame] | 159 | typedef std::unordered_set<const URLRequest*> AccuracyDegradingRequests; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 160 | |
Jianfeng Wang | e829c498 | 2019-07-03 21:17:09 | [diff] [blame] | 161 | // 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 | |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 167 | // 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. |
tbansal | ff83205e | 2017-05-23 00:09:45 | [diff] [blame] | 174 | bool MaybeGetThroughputObservation(int32_t* downstream_kbps); |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 175 | |
| 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 | |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 188 | // 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 Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 197 | // Guaranteed to be non-null during the duration of |this|. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 198 | const raw_ptr<const NetworkQualityEstimator> network_quality_estimator_; |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 199 | |
| 200 | // Guaranteed to be non-null during the duration of |this|. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 201 | const raw_ptr<const NetworkQualityEstimatorParams> params_; |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 202 | |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 203 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 204 | |
| 205 | // Called every time a new throughput observation is available. |
| 206 | ThroughputObservationCallback throughput_observation_callback_; |
| 207 | |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 208 | // Guaranteed to be non-null during the lifetime of |this|. |
Kenichi Ishibashi | 78d75186 | 2021-11-11 09:32:38 | [diff] [blame] | 209 | // This isn't a const pointer since SetTickClockForTesting() modifies it. |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 210 | raw_ptr<const base::TickClock> tick_clock_; |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 211 | |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 212 | // 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 Horo | 2fa6b8de | 2022-06-09 01:40:20 | [diff] [blame^] | 221 | int64_t bits_received_at_window_start_ = 0; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 222 | |
| 223 | // Container that holds active requests that reduce the accuracy of |
| 224 | // throughput computation. These requests are not used in throughput |
| 225 | // computation. |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 226 | AccuracyDegradingRequests accuracy_degrading_requests_; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 227 | |
| 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 Wang | e829c498 | 2019-07-03 21:17:09 | [diff] [blame] | 232 | // 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 Horo | 2fa6b8de | 2022-06-09 01:40:20 | [diff] [blame^] | 237 | int64_t total_response_content_size_ = 0; |
Jianfeng Wang | e829c498 | 2019-07-03 21:17:09 | [diff] [blame] | 238 | |
Tarun Bansal | 0094311 | 2017-10-10 22:49:14 | [diff] [blame] | 239 | // Last time when the check for hanging requests was run. |
| 240 | base::TimeTicks last_hanging_request_check_; |
| 241 | |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 242 | // 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 Horo | 2fa6b8de | 2022-06-09 01:40:20 | [diff] [blame^] | 246 | bool disable_throughput_measurements_ = false; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 247 | |
| 248 | // Determines if the requests to local host can be used in estimating the |
| 249 | // network quality. Set to true only for tests. |
Tsuyoshi Horo | 2fa6b8de | 2022-06-09 01:40:20 | [diff] [blame^] | 250 | bool use_localhost_requests_for_tests_ = false; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 251 | |
Tarun Bansal | c71cf62 | 2018-10-16 16:24:44 | [diff] [blame] | 252 | SEQUENCE_CHECKER(sequence_checker_); |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 253 | |
tbansal | 82edab4 | 2017-06-19 05:55:25 | [diff] [blame] | 254 | NetLogWithSource net_log_; |
tbansal | 80a5216 | 2016-05-20 17:55:04 | [diff] [blame] | 255 | }; |
| 256 | |
| 257 | } // namespace internal |
| 258 | |
| 259 | } // namespace nqe |
| 260 | |
| 261 | } // namespace net |
| 262 | |
bnc | 3698b0a0 | 2016-12-09 23:36:50 | [diff] [blame] | 263 | #endif // NET_NQE_THROUGHPUT_ANALYZER_H_ |