throughput_analyzer.h 11 KB

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