java.lang.Object
net.i2p.router.transport.udp.SimpleBandwidthEstimator
All Implemented Interfaces:
BandwidthEstimator

public class SimpleBandwidthEstimatorextends Object implements BandwidthEstimator
A Westwood+ bandwidth estimator with: <ul> <li>A first-stage anti-aliasing low-pass filter based on RTT</li> <li>A time-varying EWMA filter based on inter-arrival time</li> </ul>

<p>This estimator is adapted from the Linux kernel's <code>tcp_westwood.c</code> implementation and the Westwood+ research paper.</p>

<p>Ref: <em>TCP Westwood: End-to-End Congestion Control for Wired/Wireless Networks</em> - Casetti et al.</p> <p>Ref: <em>End-to-End Bandwidth Estimation for Congestion Control in Packet Networks</em> - Grieco and Mascolo.</p>

<p>Adapted for I2P streaming transport in 0.9.49.</p>

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private int
    Messages acked so far.
    private float
    Estimate excluding the zero-hop peers.
    private float
    Filtered bandwidth estimate from the acks.
    private final I2PAppContext
    The app context.
    private static int
    Current decay factor from the config.
    private final int
    Decay factor used for the zero-hop peers, snapshotted at construction so runtime tuning never perturbs a live stream.
    private final Log
    Logger for the bandwidth estimator.
    private final PeerState
    The peer state being estimated.
    private long
    When the last ack arrived.
    private static final int
    Default decay factor of the estimate.
    private static final int
    Smallest RTT used in the estimate, in ms.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a new bandwidth estimator for the given peer.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addSample(int acked)
    Records a new bandwidth sample based on the number of bytes acknowledged.
    private void
    addSample(int acked, long now, int rtt)
    Internal synchronized version of addSample that also takes the current time and RTT.
    private float
    computeBWE(long now, int rtt)
    Synchronized version of the bandwidth computation method.
    private void
    Applies exponential decay to the bandwidth estimate when no new data is received.
    private String
    formatRate(double value)
    Formats a byte rate for the debug log.
    float
    Returns the current bandwidth estimate in bytes per millisecond.
    float
    Returns the current bandwidth estimate at the specified time.
    static int
    Returns the current EWMA decay factor.
    static void
    setDecayFactor(int factor)
    The EWMA decay factor for new estimators.
    Returns a string representation of this estimator's current state.
    private void
    updateBK(long time, int packets, int rtt)
    Updates the bandwidth estimate using the latest sample.
    private float
    westwood_do_filter(float a, float b)
    Applies an exponential weighted moving average (EWMA) filter to the bandwidth estimate.

    Methods inherited from class Object

    clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

    • _context

      private final I2PAppContext _context
      The app context.
    • _log

      private final Log _log
      Logger for the bandwidth estimator.
    • _state

      private final PeerState _state
      The peer state being estimated.
    • _tAck

      private long _tAck
      When the last ack arrived.
    • _bKFiltered

      private float _bKFiltered
      Filtered bandwidth estimate from the acks.
    • _bK_ns_est

      private float _bK_ns_est
      Estimate excluding the zero-hop peers.
    • _acked

      private int _acked
      Messages acked so far.
    • DEFAULT_DECAY_FACTOR

      private static final int DEFAULT_DECAY_FACTOR
      Default decay factor of the estimate.
      See Also:
    • _decayFactor

      private static volatile int _decayFactor
      Current decay factor from the config.
    • WESTWOOD_RTT_MIN

      private static final int WESTWOOD_RTT_MIN
      Smallest RTT used in the estimate, in ms.
      See Also:
    • _localDecayFactor

      private final int _localDecayFactor
      Decay factor used for the zero-hop peers, snapshotted at construction so runtime tuning never perturbs a live stream.
  • Constructor Details

    • SimpleBandwidthEstimator

      SimpleBandwidthEstimator(I2PAppContext ctx, PeerState state)
      Creates a new bandwidth estimator for the given peer.
      Parameters:
      ctx - I2P application context
      state - peer state for RTT and other transport metrics
  • Method Details

    • getDecayFactor

      public static int getDecayFactor()
      Returns the current EWMA decay factor.
      Returns:
      the decay factor
    • setDecayFactor

      public static void setDecayFactor(int factor)
      The EWMA decay factor for new estimators. Higher = more smoothing, lower = faster adaptation.
      Parameters:
      factor - EWMA decay factor to apply to new estimators, 2-16; values
    • addSample

      public void addSample(int acked)
      Records a new bandwidth sample based on the number of bytes acknowledged.
      Specified by:
      addSample in interface BandwidthEstimator
      Parameters:
      acked - number of bytes newly acknowledged
    • addSample

      private void addSample(int acked, long now, int rtt)
      Internal synchronized version of addSample that also takes the current time and RTT.
      Parameters:
      acked - number of bytes newly acknowledged
      now - current time in milliseconds
      rtt - current round-trip time estimate
    • getBandwidthEstimate

      public float getBandwidthEstimate()
      Returns the current bandwidth estimate in bytes per millisecond.
      Specified by:
      getBandwidthEstimate in interface BandwidthEstimator
      Returns:
      bandwidth estimate in bytes per millisecond
    • getBandwidthEstimate

      public float getBandwidthEstimate(long now)
      Returns the current bandwidth estimate at the specified time.
      Parameters:
      now - current time in milliseconds
      Returns:
      bandwidth estimate in bytes per millisecond
    • computeBWE

      private float computeBWE(long now, int rtt)
      Synchronized version of the bandwidth computation method.
      Parameters:
      now - current time in milliseconds
      rtt - current round-trip time
      Returns:
      updated bandwidth estimate
    • decay

      private void decay()
      Applies exponential decay to the bandwidth estimate when no new data is received.
    • updateBK

      private void updateBK(long time, int packets, int rtt)
      Updates the bandwidth estimate using the latest sample. If no recent acknowledgments were received, virtual null samples are inserted.
      Parameters:
      time - time of the measurement
      packets - number of bytes acknowledged
      rtt - current round-trip time
    • formatRate

      private String formatRate(double value)
      Formats a byte rate for the debug log.
      Parameters:
      value - the rate in bytes per second
      Returns:
      the formatted rate
    • westwood_do_filter

      private float westwood_do_filter(float a, float b)
      Applies an exponential weighted moving average (EWMA) filter to the bandwidth estimate.
      Parameters:
      a - the previous estimate in bytes per millisecond, weighted by decayFactor - 1
      b - the new measurement in bytes per millisecond, weight 1
      Returns:
      the filtered estimate, with the weight of a set by the decay factor snapshotted at construction
    • toString

      public String toString()
      Returns a string representation of this estimator's current state.
      Overrides:
      toString in class Object
      Returns:
      string representation