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