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_OBSERVATION_BUFFER_H_ 6 #define NET_NQE_OBSERVATION_BUFFER_H_ 7 8 #include <stdint.h> 9 10 #include <map> 11 #include <memory> 12 #include <optional> 13 #include <set> 14 #include <vector> 15 16 #include "base/containers/circular_deque.h" 17 #include "base/memory/raw_ptr.h" 18 #include "base/time/tick_clock.h" 19 #include "net/base/net_export.h" 20 #include "net/nqe/network_quality_estimator_util.h" 21 #include "net/nqe/network_quality_observation.h" 22 #include "net/nqe/network_quality_observation_source.h" 23 24 namespace base { 25 26 class TimeTicks; 27 28 } // namespace base 29 30 namespace net { 31 32 class NetworkQualityEstimatorParams; 33 34 namespace nqe::internal { 35 36 struct WeightedObservation; 37 38 // Stores observations sorted by time and provides utility functions for 39 // computing weighted and non-weighted summary statistics. 40 class NET_EXPORT_PRIVATE ObservationBuffer { 41 public: 42 ObservationBuffer(const NetworkQualityEstimatorParams* params, 43 const base::TickClock* tick_clock, 44 double weight_multiplier_per_second, 45 double weight_multiplier_per_signal_level); 46 47 // This constructor does not copy the |observations_| from |other| to |this|. 48 // As such, this constructor should only be called before adding any 49 // observations to |other|. 50 ObservationBuffer(const ObservationBuffer& other); 51 52 ObservationBuffer& operator=(const ObservationBuffer&) = delete; 53 54 ~ObservationBuffer(); 55 56 // Adds |observation| to the buffer. The oldest observation in the buffer 57 // will be evicted to make room if the buffer is already full. 58 void AddObservation(const Observation& observation); 59 60 // Returns the number of observations in this buffer. Size()61 size_t Size() const { return static_cast<size_t>(observations_.size()); } 62 63 // Returns the capacity of this buffer. 64 size_t Capacity() const; 65 66 // Clears the observations stored in this buffer. Clear()67 void Clear() { observations_.clear(); } 68 69 // Returns true iff the |percentile| value of the observations in this 70 // buffer is available. Sets |result| to the computed |percentile| 71 // value of all observations made on or after |begin_timestamp|. If the 72 // value is unavailable, false is returned and |result| is not modified. 73 // Percentile value is unavailable if all the values in observation buffer are 74 // older than |begin_timestamp|. |current_signal_strength| is the current 75 // signal strength. |result| must not be null. If |observations_count| is not 76 // null, then it is set to the number of observations that were available 77 // in the observation buffer for computing the percentile. 78 std::optional<int32_t> GetPercentile(base::TimeTicks begin_timestamp, 79 int32_t current_signal_strength, 80 int percentile, 81 size_t* observations_count) const; 82 SetTickClockForTesting(const base::TickClock * tick_clock)83 void SetTickClockForTesting(const base::TickClock* tick_clock) { 84 tick_clock_ = tick_clock; 85 } 86 87 // Removes all observations from the buffer whose corresponding entry in 88 // |deleted_observation_sources| is set to true. For example, if index 1 and 89 // 3 in |deleted_observation_sources| are set to true, then all observations 90 // in the buffer that have source set to either 1 or 3 would be removed. 91 void RemoveObservationsWithSource( 92 bool deleted_observation_sources[NETWORK_QUALITY_OBSERVATION_SOURCE_MAX]); 93 94 private: 95 // Computes the weighted observations and stores them in 96 // |weighted_observations| sorted by ascending |WeightedObservation.value|. 97 // Only the observations with timestamp later than |begin_timestamp| are 98 // considered. |current_signal_strength| is the current signal strength 99 // when the observation was taken. This method also sets |total_weight| to 100 // the total weight of all observations. Should be called only when there is 101 // at least one observation in the buffer. 102 void ComputeWeightedObservations( 103 const base::TimeTicks& begin_timestamp, 104 int32_t current_signal_strength, 105 std::vector<WeightedObservation>* weighted_observations, 106 double* total_weight) const; 107 108 raw_ptr<const NetworkQualityEstimatorParams> params_; 109 110 // Holds observations sorted by time, with the oldest observation at the 111 // front of the queue. 112 base::circular_deque<Observation> observations_; 113 114 // The factor by which the weight of an observation reduces every second. 115 // For example, if an observation is 6 seconds old, its weight would be: 116 // weight_multiplier_per_second_ ^ 6 117 // Calculated from |kHalfLifeSeconds| by solving the following equation: 118 // weight_multiplier_per_second_ ^ kHalfLifeSeconds = 0.5 119 const double weight_multiplier_per_second_; 120 121 // The factor by which the weight of an observation reduces for every unit 122 // difference in the current signal strength, and the signal strength at 123 // which the observation was taken. 124 // For example, if the observation was taken at 1 unit, and current signal 125 // strength is 4 units, the weight of the observation would be: 126 // |weight_multiplier_per_signal_level_| ^ 3. 127 const double weight_multiplier_per_signal_level_; 128 129 raw_ptr<const base::TickClock> tick_clock_; 130 }; 131 132 } // namespace nqe::internal 133 134 } // namespace net 135 136 #endif // NET_NQE_OBSERVATION_BUFFER_H_ 137