tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

probe_controller.h (9432B)


      1 /*
      2 *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
      3 *
      4 *  Use of this source code is governed by a BSD-style license
      5 *  that can be found in the LICENSE file in the root of the source
      6 *  tree. An additional intellectual property rights grant can be found
      7 *  in the file PATENTS.  All contributing project authors may
      8 *  be found in the AUTHORS file in the root of the source tree.
      9 */
     10 
     11 #ifndef MODULES_CONGESTION_CONTROLLER_GOOG_CC_PROBE_CONTROLLER_H_
     12 #define MODULES_CONGESTION_CONTROLLER_GOOG_CC_PROBE_CONTROLLER_H_
     13 
     14 #include <stdint.h>
     15 
     16 #include <optional>
     17 #include <vector>
     18 
     19 #include "absl/base/attributes.h"
     20 #include "api/field_trials_view.h"
     21 #include "api/rtc_event_log/rtc_event_log.h"
     22 #include "api/transport/network_types.h"
     23 #include "api/units/data_rate.h"
     24 #include "api/units/time_delta.h"
     25 #include "api/units/timestamp.h"
     26 #include "rtc_base/experiments/field_trial_parser.h"
     27 
     28 namespace webrtc {
     29 
     30 struct ProbeControllerConfig {
     31  explicit ProbeControllerConfig(const FieldTrialsView* key_value_config);
     32  ProbeControllerConfig(const ProbeControllerConfig&);
     33  ProbeControllerConfig& operator=(const ProbeControllerConfig&) = default;
     34  ~ProbeControllerConfig();
     35 
     36  // These parameters configure the initial probes. First we send one or two
     37  // probes of sizes p1 * start_bitrate_ and p2 * start_bitrate_.
     38  // Then whenever we get a bitrate estimate of at least further_probe_threshold
     39  // times the size of the last sent probe we'll send another one of size
     40  // step_size times the new estimate.
     41  FieldTrialParameter<double> first_exponential_probe_scale;
     42  FieldTrialOptional<double> second_exponential_probe_scale;
     43  FieldTrialParameter<double> further_exponential_probe_scale;
     44  FieldTrialParameter<double> further_probe_threshold;
     45  FieldTrialParameter<bool> abort_further_probe_if_max_lower_than_current;
     46  // Duration of time from the first initial probe where repeated initial probes
     47  // are sent if repeated initial probing is enabled.
     48  FieldTrialParameter<TimeDelta> repeated_initial_probing_time_period;
     49  // The minimum probing duration of an individual probe during
     50  // the repeated_initial_probing_time_period.
     51  FieldTrialParameter<TimeDelta> initial_probe_duration;
     52  // Delta time between sent bursts of packets in a probe during
     53  // the repeated_initial_probing_time_period.
     54  FieldTrialParameter<TimeDelta> initial_min_probe_delta;
     55  // Configures how often we send ALR probes and how big they are.
     56  FieldTrialParameter<TimeDelta> alr_probing_interval;
     57  FieldTrialParameter<double> alr_probe_scale;
     58  // Configures how often we send probes if NetworkStateEstimate is available.
     59  FieldTrialParameter<TimeDelta> network_state_estimate_probing_interval;
     60  // Periodically probe as long as the ratio between current estimate and
     61  // NetworkStateEstimate is lower then this.
     62  FieldTrialParameter<double>
     63      probe_if_estimate_lower_than_network_state_estimate_ratio;
     64  FieldTrialParameter<TimeDelta>
     65      estimate_lower_than_network_state_estimate_probing_interval;
     66  FieldTrialParameter<double> network_state_probe_scale;
     67  // Overrides min_probe_duration if network_state_estimate_probing_interval
     68  // is set and a network state estimate is known and equal or higher than the
     69  // probe target.
     70  FieldTrialParameter<TimeDelta> network_state_probe_duration;
     71  // Overrides min_probe_delta if network_state_estimate_probing_interval
     72  // is set and a network state estimate is known and equal or higher than the
     73  // probe target.
     74  FieldTrialParameter<TimeDelta> network_state_min_probe_delta;
     75 
     76  // Configures the probes emitted by changed to the allocated bitrate.
     77  FieldTrialParameter<bool> probe_on_max_allocated_bitrate_change;
     78  FieldTrialOptional<double> first_allocation_probe_scale;
     79  FieldTrialOptional<double> second_allocation_probe_scale;
     80  FieldTrialParameter<double> allocation_probe_limit_by_current_scale;
     81 
     82  // The minimum number probing packets used.
     83  FieldTrialParameter<int> min_probe_packets_sent;
     84  // The minimum probing duration.
     85  FieldTrialParameter<TimeDelta> min_probe_duration;
     86  // Delta time between sent bursts of packets in a probe.
     87  FieldTrialParameter<TimeDelta> min_probe_delta;
     88  FieldTrialParameter<double> loss_limited_probe_scale;
     89  // Don't send a probe if min(estimate, network state estimate) is larger than
     90  // this fraction of the set max or max allocated bitrate.
     91  FieldTrialParameter<double> skip_if_estimate_larger_than_fraction_of_max;
     92  // Scale factor of the max allocated bitrate. Used when deciding if a probe
     93  // can be skiped due to that the estimate is already high enough.
     94  FieldTrialParameter<double> skip_probe_max_allocated_scale;
     95 };
     96 
     97 // Reason that bandwidth estimate is limited. Bandwidth estimate can be limited
     98 // by either delay based bwe, or loss based bwe when it increases/decreases the
     99 // estimate.
    100 enum class BandwidthLimitedCause : int {
    101  kLossLimitedBweIncreasing = 0,
    102  kLossLimitedBwe = 1,
    103  kDelayBasedLimited = 2,
    104  kDelayBasedLimitedDelayIncreased = 3,
    105  kRttBasedBackOffHighRtt = 4
    106 };
    107 
    108 // This class controls initiation of probing to estimate initial channel
    109 // capacity. There is also support for probing during a session when max
    110 // bitrate is adjusted by an application.
    111 class ProbeController {
    112 public:
    113  explicit ProbeController(const FieldTrialsView* key_value_config,
    114                           RtcEventLog* event_log);
    115  ~ProbeController();
    116 
    117  ProbeController(const ProbeController&) = delete;
    118  ProbeController& operator=(const ProbeController&) = delete;
    119 
    120  ABSL_MUST_USE_RESULT std::vector<ProbeClusterConfig> SetBitrates(
    121      DataRate min_bitrate,
    122      DataRate start_bitrate,
    123      DataRate max_bitrate,
    124      Timestamp at_time);
    125 
    126  // The total bitrate, as opposed to the max bitrate, is the sum of the
    127  // configured bitrates for all active streams.
    128  ABSL_MUST_USE_RESULT std::vector<ProbeClusterConfig>
    129  OnMaxTotalAllocatedBitrate(DataRate max_total_allocated_bitrate,
    130                             Timestamp at_time);
    131 
    132  ABSL_MUST_USE_RESULT std::vector<ProbeClusterConfig> OnNetworkAvailability(
    133      NetworkAvailability msg);
    134 
    135  ABSL_MUST_USE_RESULT std::vector<ProbeClusterConfig> SetEstimatedBitrate(
    136      DataRate bitrate,
    137      BandwidthLimitedCause bandwidth_limited_cause,
    138      Timestamp at_time);
    139 
    140  void EnablePeriodicAlrProbing(bool enable);
    141 
    142  // Probes are sent periodically every 1s during the first 5s after the network
    143  // becomes available or until OnMaxTotalAllocatedBitrate is invoked with a
    144  // none zero max_total_allocated_bitrate (there are active streams being
    145  // sent.) Probe rate is up to max configured bitrate configured via
    146  // SetBitrates.
    147  void EnableRepeatedInitialProbing(bool enable);
    148 
    149  void SetAlrStartTime(std::optional<Timestamp> alr_start_time);
    150  void SetAlrEndedTime(Timestamp alr_end_time);
    151 
    152  ABSL_MUST_USE_RESULT std::vector<ProbeClusterConfig> RequestProbe(
    153      Timestamp at_time);
    154 
    155  void SetNetworkStateEstimate(NetworkStateEstimate estimate);
    156 
    157  // Resets the ProbeController to a state equivalent to as if it was just
    158  // created EXCEPT for configuration settings like
    159  // `enable_periodic_alr_probing_` `network_available_` and
    160  // `max_total_allocated_bitrate_`.
    161  void Reset(Timestamp at_time);
    162 
    163  ABSL_MUST_USE_RESULT std::vector<ProbeClusterConfig> Process(
    164      Timestamp at_time);
    165 
    166 private:
    167  enum class State {
    168    // Initial state where no probing has been triggered yet.
    169    kInit,
    170    // Waiting for probing results to continue further probing.
    171    kWaitingForProbingResult,
    172    // Probing is complete.
    173    kProbingComplete,
    174  };
    175 
    176  void UpdateState(State new_state);
    177  ABSL_MUST_USE_RESULT std::vector<ProbeClusterConfig>
    178  InitiateExponentialProbing(Timestamp at_time);
    179  ABSL_MUST_USE_RESULT std::vector<ProbeClusterConfig> InitiateProbing(
    180      Timestamp now,
    181      std::vector<DataRate> bitrates_to_probe,
    182      bool probe_further);
    183  bool TimeForAlrProbe(Timestamp at_time) const;
    184  bool TimeForNetworkStateProbe(Timestamp at_time) const;
    185  bool TimeForNextRepeatedInitialProbe(Timestamp at_time) const;
    186  ProbeClusterConfig CreateProbeClusterConfig(Timestamp at_time,
    187                                              DataRate bitrate);
    188 
    189  bool network_available_;
    190  bool repeated_initial_probing_enabled_ = false;
    191  Timestamp last_allowed_repeated_initial_probe_ = Timestamp::MinusInfinity();
    192  BandwidthLimitedCause bandwidth_limited_cause_ =
    193      BandwidthLimitedCause::kDelayBasedLimited;
    194  State state_;
    195  DataRate min_bitrate_to_probe_further_ = DataRate::PlusInfinity();
    196  Timestamp time_last_probing_initiated_ = Timestamp::MinusInfinity();
    197  DataRate estimated_bitrate_ = DataRate::Zero();
    198  std::optional<NetworkStateEstimate> network_estimate_;
    199  DataRate start_bitrate_ = DataRate::Zero();
    200  DataRate max_bitrate_ = DataRate::PlusInfinity();
    201  Timestamp last_bwe_drop_probing_time_ = Timestamp::Zero();
    202  std::optional<Timestamp> alr_start_time_;
    203  std::optional<Timestamp> alr_end_time_;
    204  bool enable_periodic_alr_probing_;
    205  Timestamp time_of_last_large_drop_ = Timestamp::MinusInfinity();
    206  DataRate bitrate_before_last_large_drop_ = DataRate::Zero();
    207  DataRate max_total_allocated_bitrate_ = DataRate::Zero();
    208 
    209  const bool in_rapid_recovery_experiment_;
    210  RtcEventLog* event_log_;
    211 
    212  int32_t next_probe_cluster_id_ = 1;
    213 
    214  ProbeControllerConfig config_;
    215 };
    216 
    217 }  // namespace webrtc
    218 
    219 #endif  // MODULES_CONGESTION_CONTROLLER_GOOG_CC_PROBE_CONTROLLER_H_