All Implemented Interfaces:
Runnable

public class BandwidthHistoryextends SimpleTimer2.TimedEvent
Records live bandwidth samples every second for the minigraph. Maintains a 1200-entry ring buffer (20 min at 1s resolution) and flushes to /tmp/i2p-bandwidth.dat every 60 seconds so data survives page refreshes and router restarts.

Started automatically during router startup. Accessed by net.i2p.router.web.helpers.SidebarRenderer#getDataAttributes to populate the canvas minigraph with real data instead of interpolated RRD points.

  • Field Details

    • CAPACITY

      public static final int CAPACITY
      Buffer: 1200 entries = 20 min at 1s.
      See Also:
    • SAMPLE_INTERVAL

      public static final long SAMPLE_INTERVAL
      Sample once per second.
      See Also:
    • FLUSH_INTERVAL

      private static final long FLUSH_INTERVAL
      Flush to disk every 60s.
      See Also:
    • MAX_FILE_AGE

      private static final long MAX_FILE_AGE
      Discard file data older than 30 min.
      See Also:
    • COMMA_SPLIT

      private static final Pattern COMMA_SPLIT
      Splits a saved history line on the commas.
    • FILE_NAME

      private static final String FILE_NAME
      File holding the bandwidth history.
      See Also:
    • instance

      private static BandwidthHistory instance
      The one history in use.
    • _ctx

      private final RouterContext _ctx
      The router context.
    • _timestamps

      private final long[] _timestamps
      The saved times of the samples.
    • _rx

      private final long[] _rx
      The saved received byte counts.
    • _tx

      private final long[] _tx
      The saved sent byte counts.
    • _capacity

      private final int _capacity
      Most samples held at once.
    • _file

      private final File _file
      File the history is saved to.
    • _head

      private int _head
      Index of the oldest sample.
    • _count

      private int _count
      Number of samples stored.
    • _nextFlush

      private long _nextFlush
      When the history is saved next.
  • Constructor Details

    • BandwidthHistory

      public BandwidthHistory(RouterContext ctx)
      Create a bandwidth history.
      Parameters:
      ctx - may be null in unit tests
  • Method Details

    • getInstance

      public static BandwidthHistory getInstance()
      The singleton instance.
      Returns:
      the singleton, or null if not yet created
    • getCapacity

      public int getCapacity()
      Report how many samples fit before the oldest is overwritten.
      Returns:
      the capacity in samples, currently always 1200
    • timeReached

      public void timeReached()
      Sample bandwidth limiter and record. Reschedules itself at 1s.
      Specified by:
      timeReached in class SimpleTimer2.TimedEvent
    • record

      public void record(long rx, long tx)
      Add a sample to the ring buffer.
      Parameters:
      rx - the receive bandwidth in Bps
      tx - the transmit bandwidth in Bps
    • getCount

      public int getCount()
      Report how many samples the buffer currently holds.
      Returns:
      a count from 0 to the capacity, inclusive
    • getLastRx

      public String getLastRx(int n)
      Return the last n receive values as a comma-separated string. Oldest value first, most recent last. Empty string if no data.
      Parameters:
      n - the number of values to return
      Returns:
      comma-separated string of the last n values
    • getLastTx

      public String getLastTx(int n)
      Return the last n send values as a comma-separated string. Oldest value first, most recent last. Empty string if no data.
      Parameters:
      n - the number of values to return
      Returns:
      comma-separated string of the last n values
    • save

      void save()
      Write the ring buffer to disk.
    • load

      private void load()
      Load the ring buffer from disk. Discards data older than MAX_FILE_AGE.
    • formatLast

      private String formatLast(int n, long[] values)
      Build a comma-separated string of the last n values from the given array. Must be called under synchronized(this).
      Parameters:
      n - how many of the most recent samples to include; 0 or less returns an empty string
      values - the ring buffer array to read (size _capacity), indexed modulo capacity
      Returns:
      the samples joined with commas, oldest first, empty if there is nothing to show