1 // Copyright 2016 The Chromium Authors 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 10 #include <unordered_map> 11 #include <unordered_set> 12 13 #include "base/functional/callback.h" 14 #include "base/memory/raw_ptr.h" 15 #include "base/memory/scoped_refptr.h" 16 #include "base/sequence_checker.h" 17 #include "base/time/time.h" 18 #include "net/base/net_export.h" 19 #include "net/log/net_log_with_source.h" 20 21 namespace { 22 typedef base::RepeatingCallback<void(int32_t)> ThroughputObservationCallback; 23 } 24 25 namespace base { 26 class SingleThreadTaskRunner; 27 class TickClock; 28 } 29 30 namespace net { 31 32 class NetworkQualityEstimatorParams; 33 class NetworkQualityEstimator; 34 class URLRequest; 35 36 namespace nqe::internal { 37 38 // Makes throughput observations. Polls NetworkActivityMonitor 39 // (TrafficStats on Android) to count number of bits received over throughput 40 // observation windows in accordance with the following rules: 41 // (1) A new window of observation begins any time a URL request header is 42 // about to be sent, or a request completes or is destroyed. 43 // (2) A request is "active" if its headers are sent, but it hasn't completed, 44 // and "local" if destined to local host. If at any time during a 45 // throughput observation window there is an active, local request, the 46 // window is discarded. 47 // (3) If less than 32KB is received over the network during a window of 48 // observation, that window is discarded. 49 class NET_EXPORT_PRIVATE ThroughputAnalyzer { 50 public: 51 // |throughput_observation_callback| is called on the |task_runner| when 52 // |this| has a new throughput observation. 53 // |use_local_host_requests_for_tests| should only be true when testing 54 // against local HTTP server and allows the requests to local host to be 55 // used for network quality estimation. |use_smaller_responses_for_tests| 56 // should only be true when testing, and allows the responses smaller than 57 // |kMinTransferSizeInBits| or shorter than 58 // |kMinRequestDurationMicroseconds| to be used for network quality 59 // estimation. 60 // Virtualized for testing. 61 ThroughputAnalyzer( 62 const NetworkQualityEstimator* network_quality_estimator, 63 const NetworkQualityEstimatorParams* params, 64 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 65 ThroughputObservationCallback throughput_observation_callback, 66 const base::TickClock* tick_clock, 67 const NetLogWithSource& net_log); 68 69 ThroughputAnalyzer(const ThroughputAnalyzer&) = delete; 70 ThroughputAnalyzer& operator=(const ThroughputAnalyzer&) = delete; 71 72 virtual ~ThroughputAnalyzer(); 73 74 // Notifies |this| that the headers of |request| are about to be sent. 75 void NotifyStartTransaction(const URLRequest& request); 76 77 // Notifies |this| that unfiltered bytes have been read for |request|. 78 void NotifyBytesRead(const URLRequest& request); 79 80 // Notifies |this| that |request| has completed. 81 void NotifyRequestCompleted(const URLRequest& request); 82 83 // Notifies |this| that |request| has an expected response body size in octets 84 // (8-bit bytes). |expected_content_size| is an estimate of total body length 85 // based on the Content-Length header field when available or a general size 86 // estimate when the Content-Length is not provided. 87 void NotifyExpectedResponseContentSize(const URLRequest& request, 88 int64_t expected_content_size); 89 90 // Notifies |this| of a change in connection type. 91 void OnConnectionTypeChanged(); 92 93 // |use_localhost_requests| should only be true when testing against local 94 // HTTP server and allows the requests to local host to be used for network 95 // quality estimation. 96 void SetUseLocalHostRequestsForTesting(bool use_localhost_requests); 97 98 // Returns true if throughput is currently tracked by a throughput 99 // observation window. 100 bool IsCurrentlyTrackingThroughput() const; 101 102 // Overrides the tick clock used by |this| for testing. 103 void SetTickClockForTesting(const base::TickClock* tick_clock); 104 105 // Returns the number of bits received by Chromium so far. The count may not 106 // start from zero, so the caller should only look at difference from a prior 107 // call. The count is obtained by polling TrafficStats on Android, and 108 // net::NetworkActivityMonitor on all other platforms. Virtualized for 109 // testing. 110 virtual int64_t GetBitsReceived() const; 111 112 // Returns the number of in-flight requests that can be used for computing 113 // throughput. 114 size_t CountActiveInFlightRequests() const; 115 116 // Returns the total number of in-flight requests. This also includes hanging 117 // requests. 118 size_t CountTotalInFlightRequests() const; 119 120 // Returns the sum of expected response content size in bytes for all inflight 121 // requests. Request with an unknown response content size have the default 122 // response content size. 123 int64_t CountTotalContentSizeBytes() const; 124 125 protected: 126 // Exposed for testing. disable_throughput_measurements_for_testing()127 bool disable_throughput_measurements_for_testing() const { 128 return disable_throughput_measurements_; 129 } 130 131 // Removes hanging requests from |requests_|. If any hanging requests are 132 // detected to be in-flight, the observation window is ended. Protected for 133 // testing. 134 void EraseHangingRequests(const URLRequest& request); 135 136 // Returns true if the current throughput observation window is heuristically 137 // determined to contain hanging requests. 138 bool IsHangingWindow(int64_t bits_received, base::TimeDelta duration) const; 139 140 private: 141 friend class TestThroughputAnalyzer; 142 143 // Mapping from URL request to the expected content size of the response body 144 // for that request. The map tracks all inflight requests. If the expected 145 // content size is not available, the value is set to the default value. 146 typedef std::unordered_map<const URLRequest*, int64_t> ResponseContentSizes; 147 148 // Mapping from URL request to the last time data was received for that 149 // request. 150 typedef std::unordered_map<const URLRequest*, base::TimeTicks> Requests; 151 152 // Set of URL requests to hold the requests that reduce the accuracy of 153 // throughput computation. These requests are not used in throughput 154 // computation. 155 typedef std::unordered_set<raw_ptr<const URLRequest, CtnExperimental>> 156 AccuracyDegradingRequests; 157 158 // Updates the response content size map for |request|. Also keeps the total 159 // response content size counter updated. Adds an new entry if there is no 160 // matching record in the map. 161 void UpdateResponseContentSize(const URLRequest* request, 162 int64_t response_size); 163 164 // Returns true if downstream throughput can be recorded. In that case, 165 // |downstream_kbps| is set to the computed downstream throughput (in 166 // kilobits per second). If a downstream throughput observation is taken, 167 // then the throughput observation window is reset so as to continue 168 // tracking throughput. A throughput observation can be taken only if the 169 // time-window is currently active, and enough bytes have accumulated in 170 // that window. |downstream_kbps| should not be null. 171 bool MaybeGetThroughputObservation(int32_t* downstream_kbps); 172 173 // Starts the throughput observation window that keeps track of network 174 // bytes if the following conditions are true: 175 // (i) All active requests are non-local; 176 // (ii) There is at least one active, non-local request; and, 177 // (iii) The throughput observation window is not already tracking 178 // throughput. The window is started by setting the |start_| and 179 // |bits_received_|. 180 void MaybeStartThroughputObservationWindow(); 181 182 // EndThroughputObservationWindow ends the throughput observation window. 183 void EndThroughputObservationWindow(); 184 185 // Returns true if the |request| degrades the accuracy of the throughput 186 // observation window. A local request or a request that spans a connection 187 // change degrades the accuracy of the throughput computation. 188 bool DegradesAccuracy(const URLRequest& request) const; 189 190 // Bounds |accuracy_degrading_requests_| and |requests_| to ensure their sizes 191 // do not exceed their capacities. 192 void BoundRequestsSize(); 193 194 // Guaranteed to be non-null during the duration of |this|. 195 const raw_ptr<const NetworkQualityEstimator> network_quality_estimator_; 196 197 // Guaranteed to be non-null during the duration of |this|. 198 const raw_ptr<const NetworkQualityEstimatorParams> params_; 199 200 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 201 202 // Called every time a new throughput observation is available. 203 ThroughputObservationCallback throughput_observation_callback_; 204 205 // Guaranteed to be non-null during the lifetime of |this|. 206 // This isn't a const pointer since SetTickClockForTesting() modifies it. 207 raw_ptr<const base::TickClock> tick_clock_; 208 209 // Time when last connection change was observed. 210 base::TimeTicks last_connection_change_; 211 212 // Start time of the current throughput observation window. Set to null if 213 // the window is not currently active. 214 base::TimeTicks window_start_time_; 215 216 // Number of bits received prior to |start_| as reported by 217 // NetworkActivityMonitor. 218 int64_t bits_received_at_window_start_ = 0; 219 220 // Container that holds active requests that reduce the accuracy of 221 // throughput computation. These requests are not used in throughput 222 // computation. 223 AccuracyDegradingRequests accuracy_degrading_requests_; 224 225 // Container that holds active requests that do not reduce the accuracy of 226 // throughput computation. These requests are used in throughput computation. 227 Requests requests_; 228 229 // Container that holds inflight request sizes. These requests are used in 230 // computing the total of response content size for all inflight requests. 231 ResponseContentSizes response_content_sizes_; 232 233 // The running total of response content size for all inflight requests. 234 int64_t total_response_content_size_ = 0; 235 236 // Last time when the check for hanging requests was run. 237 base::TimeTicks last_hanging_request_check_; 238 239 // If true, then |this| throughput analyzer stops tracking the throughput 240 // observations until Chromium is restarted. This may happen if the throughput 241 // analyzer has lost track of the requests that degrade throughput computation 242 // accuracy. 243 bool disable_throughput_measurements_ = false; 244 245 // Determines if the requests to local host can be used in estimating the 246 // network quality. Set to true only for tests. 247 bool use_localhost_requests_for_tests_ = false; 248 249 SEQUENCE_CHECKER(sequence_checker_); 250 251 NetLogWithSource net_log_; 252 }; 253 254 } // namespace nqe::internal 255 256 } // namespace net 257 258 #endif // NET_NQE_THROUGHPUT_ANALYZER_H_ 259