• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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<const URLRequest*> AccuracyDegradingRequests;
156 
157   // Updates the response content size map for |request|. Also keeps the total
158   // response content size counter updated. Adds an new entry if there is no
159   // matching record in the map.
160   void UpdateResponseContentSize(const URLRequest* request,
161                                  int64_t response_size);
162 
163   // Returns true if downstream throughput can be recorded. In that case,
164   // |downstream_kbps| is set to the computed downstream throughput (in
165   // kilobits per second). If a downstream throughput observation is taken,
166   // then the throughput observation window is reset so as to continue
167   // tracking throughput. A throughput observation can be taken only if the
168   // time-window is currently active, and enough bytes have accumulated in
169   // that window. |downstream_kbps| should not be null.
170   bool MaybeGetThroughputObservation(int32_t* downstream_kbps);
171 
172   // Starts the throughput observation window that keeps track of network
173   // bytes if the following conditions are true:
174   // (i) All active requests are non-local;
175   // (ii) There is at least one active, non-local request; and,
176   // (iii) The throughput observation window is not already tracking
177   // throughput. The window is started by setting the |start_| and
178   // |bits_received_|.
179   void MaybeStartThroughputObservationWindow();
180 
181   // EndThroughputObservationWindow ends the throughput observation window.
182   void EndThroughputObservationWindow();
183 
184   // Returns true if the |request| degrades the accuracy of the throughput
185   // observation window. A local request or a request that spans a connection
186   // change degrades the accuracy of the throughput computation.
187   bool DegradesAccuracy(const URLRequest& request) const;
188 
189   // Bounds |accuracy_degrading_requests_| and |requests_| to ensure their sizes
190   // do not exceed their capacities.
191   void BoundRequestsSize();
192 
193   // Guaranteed to be non-null during the duration of |this|.
194   const raw_ptr<const NetworkQualityEstimator> network_quality_estimator_;
195 
196   // Guaranteed to be non-null during the duration of |this|.
197   const raw_ptr<const NetworkQualityEstimatorParams> params_;
198 
199   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
200 
201   // Called every time a new throughput observation is available.
202   ThroughputObservationCallback throughput_observation_callback_;
203 
204   // Guaranteed to be non-null during the lifetime of |this|.
205   // This isn't a const pointer since SetTickClockForTesting() modifies it.
206   raw_ptr<const base::TickClock> tick_clock_;
207 
208   // Time when last connection change was observed.
209   base::TimeTicks last_connection_change_;
210 
211   // Start time of the current throughput observation window. Set to null if
212   // the window is not currently active.
213   base::TimeTicks window_start_time_;
214 
215   // Number of bits received prior to |start_| as reported by
216   // NetworkActivityMonitor.
217   int64_t bits_received_at_window_start_ = 0;
218 
219   // Container that holds active requests that reduce the accuracy of
220   // throughput computation. These requests are not used in throughput
221   // computation.
222   AccuracyDegradingRequests accuracy_degrading_requests_;
223 
224   // Container that holds active requests that do not reduce the accuracy of
225   // throughput computation. These requests are used in throughput computation.
226   Requests requests_;
227 
228   // Container that holds inflight request sizes. These requests are used in
229   // computing the total of response content size for all inflight requests.
230   ResponseContentSizes response_content_sizes_;
231 
232   // The running total of response content size for all inflight requests.
233   int64_t total_response_content_size_ = 0;
234 
235   // Last time when the check for hanging requests was run.
236   base::TimeTicks last_hanging_request_check_;
237 
238   // If true, then |this| throughput analyzer stops tracking the throughput
239   // observations until Chromium is restarted. This may happen if the throughput
240   // analyzer has lost track of the requests that degrade throughput computation
241   // accuracy.
242   bool disable_throughput_measurements_ = false;
243 
244   // Determines if the requests to local host can be used in estimating the
245   // network quality. Set to true only for tests.
246   bool use_localhost_requests_for_tests_ = false;
247 
248   SEQUENCE_CHECKER(sequence_checker_);
249 
250   NetLogWithSource net_log_;
251 };
252 
253 }  // namespace nqe::internal
254 
255 }  // namespace net
256 
257 #endif  // NET_NQE_THROUGHPUT_ANALYZER_H_
258