All Implemented Interfaces:
Runnable

public class BandwidthGraphextends SimpleTimer2.TimedEvent
Self-contained bandwidth sampler for the snark screen log graph. Every SAMPLE_INTERVAL_MS, snapshots the cumulative traffic counters (peer wire rates summed over torrents, datagram counters fed by DatagramSender and the receive paths, HTTP tracker counters fed by I2PSnarkUtil) and stores per-interval byte totals in a fixed ring. Runs for the webapp's lifetime, independent of any browser being open, so the graph stays continuous. Flushes to the tmp dir every FLUSH_INTERVAL_MS so history survives webapp or router restarts; entries older than WINDOW_MS are dropped on load.
  • Field Summary

    Modifier and Type
    Field
    Description
    private int
    Samples held in the ring.
    private static final AtomicLong
    Bytes received over the datagram sockets.
    private static final AtomicLong
    Bytes sent over the datagram sockets.
    private int
    Position of the newest sample in the ring.
    private static final AtomicLong
    Bytes received over the HTTP proxies.
    private static final AtomicLong
    Bytes sent over the HTTP proxies.
    private static BandwidthGraph
    The single instance of the graph.
    private long
    Counter snapshots taken at the previous tick, for delta computation.
    private long
    Counter snapshots taken at the previous tick, for delta computation.
    private long
    When the samples were last saved to disk.
    private long
    Counter snapshots taken at the previous tick, for delta computation.
    private long
    Counter snapshots taken at the previous tick, for delta computation.
    private long
    When the last sample was taken.
    private final Log
    Logger for the bandwidth graph.
    private final SnarkManager
    The manager whose torrents are counted.
    private final long[]
    The received bytes per sample.
    private final long[]
    The sample times, in a ring.
    private final long[]
    The sent bytes per sample.
    private long
    Increments whenever a sample is added.
    static final int
    Ring capacity: 240 samples = 4 hours at 60 s.
    private static final String
    File holding the saved samples.
    private static final long
    Flush to disk every five minutes.
    static final long
    Sample once per minute.
    static final long
    Window covered by a full ring, in ms; older persisted samples are dropped.

    Fields inherited from class SimpleTimer2.TimedEvent

    _state, DEFAULT_FUZZ
    Modifier and Type
    Field
    Description
    State of the current event.
    protected static final int
    Default fuzz threshold in milliseconds.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    Builds the graph for one manager.
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    datagramReceived(int bytes)
    Record datagram bytes received (DHT + UDP tracker listeners).
    static void
    datagramSent(int bytes)
    Record datagram bytes sent (DHT + UDP tracker funnel).
    private static long
    delta(long current, long previous)
    Counter delta with reset detection: a cumulative source that shrank was reset, so only the post-reset amount is attributable to this interval.
    private static File
    Persistence location: the JVM tmp dir (defaulting to /tmp), matching the BandwidthHistory pattern.
    private void
    Write the serialized ring to the tmp-dir file atomically-ish (write temp, rename), ignoring all errors — persistence is best-effort by design.
    static String
    Serialize the ring oldest-first as "timestampSec,rxKB,txKB;...".
    static long
    The current sample version: increments once per stored sample.
    static void
    httpTransferred(long txBytes, long rxBytes)
    Record an HTTP tracker/torrent fetch: approximate request size plus response size.
    private void
    Load persisted samples younger than the window, seeding the ring so a fresh webapp start still shows recent history.
    private void
    Snapshot counters, compute deltas, and append one ring entry.
    private String
    Encode the graph data for saving.
    static void
    Start the sampler for this snark manager, loading prior history from the tmp-dir file when present.
    static void
    Stop the sampler and flush pending history.
    void
    One sampling pass: derive interval bytes from counter deltas plus current peer-wire rates, append to the ring, and flush periodically.

    Methods inherited from class SimpleTimer2.TimedEvent

    cancel, forceReschedule, reschedule, reschedule, run, schedule, setFuzz, setPool, toString
    Modifier and Type
    Method
    Description
    boolean
    Cancel the timed event.
    void
    forceReschedule(long timeoutMs)
    Always use the new time - ignores fuzz.
    void
    reschedule(long timeoutMs)
    Use the earliest of the new time and the old time May be called from within timeReached(), but schedule() is better there.
    void
    reschedule(long timeoutMs, boolean useEarliestTime)
    May be called from within timeReached(), but schedule() is better there.
    void
    run()
    void
    schedule(long timeoutMs)
    Schedule this event.
    void
    setFuzz(int fuzz)
    Reschedule threshold in ms.
    void
    Timer pool for this event.
    Timer summary for log messages.

    Methods inherited from class Object

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

    • SAMPLE_INTERVAL_MS

      public static final long SAMPLE_INTERVAL_MS
      Sample once per minute.
      See Also:
    • CAPACITY

      public static final int CAPACITY
      Ring capacity: 240 samples = 4 hours at 60 s.
      See Also:
    • WINDOW_MS

      public static final long WINDOW_MS
      Window covered by a full ring, in ms; older persisted samples are dropped.
      See Also:
    • FLUSH_INTERVAL_MS

      private static final long FLUSH_INTERVAL_MS
      Flush to disk every five minutes.
      See Also:
    • FILE_NAME

      private static final String FILE_NAME
      File holding the saved samples.
      See Also:
    • _datagramTx

      private static final AtomicLong _datagramTx
      Bytes sent over the datagram sockets.
    • _datagramRx

      private static final AtomicLong _datagramRx
      Bytes received over the datagram sockets.
    • _httpTx

      private static final AtomicLong _httpTx
      Bytes sent over the HTTP proxies.
    • _httpRx

      private static final AtomicLong _httpRx
      Bytes received over the HTTP proxies.
    • _instance

      private static volatile BandwidthGraph _instance
      The single instance of the graph.
    • _manager

      private final SnarkManager _manager
      The manager whose torrents are counted.
    • _log

      private final Log _log
      Logger for the bandwidth graph.
    • _times

      private final long[] _times
      The sample times, in a ring.
    • _rx

      private final long[] _rx
      The received bytes per sample.
    • _tx

      private final long[] _tx
      The sent bytes per sample.
    • _head

      private int _head
      Position of the newest sample in the ring.
    • _count

      private int _count
      Samples held in the ring.
    • _version

      private long _version
      Increments whenever a sample is added.
    • _lastFlush

      private long _lastFlush
      When the samples were last saved to disk.
    • _lastDatagramTx

      private long _lastDatagramTx
      Counter snapshots taken at the previous tick, for delta computation.
    • _lastDatagramRx

      private long _lastDatagramRx
      Counter snapshots taken at the previous tick, for delta computation.
    • _lastHttpTx

      private long _lastHttpTx
      Counter snapshots taken at the previous tick, for delta computation.
    • _lastHttpRx

      private long _lastHttpRx
      Counter snapshots taken at the previous tick, for delta computation.
    • _lastSampleTime

      private long _lastSampleTime
      When the last sample was taken.
  • Constructor Details

    • BandwidthGraph

      private BandwidthGraph(SnarkManager manager)
      Builds the graph for one manager.
      Parameters:
      manager - the manager whose torrents are counted
  • Method Details

    • datagramSent

      public static void datagramSent(int bytes)
      Record datagram bytes sent (DHT + UDP tracker funnel).
      Parameters:
      bytes - the datagram byte count to add to the cumulative sent total
    • datagramReceived

      public static void datagramReceived(int bytes)
      Record datagram bytes received (DHT + UDP tracker listeners).
      Parameters:
      bytes - the datagram byte count to add to the cumulative received total
    • httpTransferred

      public static void httpTransferred(long txBytes, long rxBytes)
      Record an HTTP tracker/torrent fetch: approximate request size plus response size.
      Parameters:
      txBytes - the bytes sent, added only when greater than zero
      rxBytes - the bytes received, added only when greater than zero
    • start

      public static void start(SnarkManager manager)
      Start the sampler for this snark manager, loading prior history from the tmp-dir file when present. Subsequent calls are no-ops.
      Parameters:
      manager - the manager whose torrent transfers the sampler records
    • stop

      public static void stop()
      Stop the sampler and flush pending history. Safe to call repeatedly.
    • getVersion

      public static long getVersion()
      The current sample version: increments once per stored sample. Clients use it to detect new data without parsing the samples themselves.
      Returns:
      monotonically increasing sample counter
    • getSamples

      public static String getSamples()
      Serialize the ring oldest-first as "timestampSec,rxKB,txKB;...". Values are KB (bytes/1024 rounded) to keep the wire format compact.
      Returns:
      compact CSV of every stored sample, empty string when none
    • timeReached

      public void timeReached()
      One sampling pass: derive interval bytes from counter deltas plus current peer-wire rates, append to the ring, and flush periodically.
      Specified by:
      timeReached in class SimpleTimer2.TimedEvent
    • sample

      private void sample()
      Snapshot counters, compute deltas, and append one ring entry.
    • delta

      private static long delta(long current, long previous)
      Counter delta with reset detection: a cumulative source that shrank was reset, so only the post-reset amount is attributable to this interval.
      Parameters:
      current - the cumulative counter at the end of the interval
      previous - the cumulative counter at the start of the interval
      Returns:
      the amount added during the interval, or current itself if the source was reset
    • serialize

      private String serialize()
      Encode the graph data for saving.
      Returns:
      CSV "ts,rxKB,txKB;..." oldest-first — KB keeps the data URL small
    • flush

      private void flush()
      Write the serialized ring to the tmp-dir file atomically-ish (write temp, rename), ignoring all errors — persistence is best-effort by design.
    • load

      private void load()
      Load persisted samples younger than the window, seeding the ring so a fresh webapp start still shows recent history.
    • file

      private static File file()
      Persistence location: the JVM tmp dir (defaulting to /tmp), matching the BandwidthHistory pattern.
      Returns:
      the file i2p-snark-bandwidth.dat in the JVM tmp dir