java.lang.Object
net.i2p.util.SimpleTimer2.TimedEvent
net.i2p.router.BandwidthHistory
- All Implemented Interfaces:
Runnable
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 Summary
Modifier and TypeFieldDescriptionprivate final intMost samples held at once.private intNumber of samples stored.private final RouterContextThe router context.private final FileFile the history is saved to.private intIndex of the oldest sample.private longWhen the history is saved next.private final long[]The saved received byte counts.private final long[]The saved times of the samples.private final long[]The saved sent byte counts.static final intBuffer: 1200 entries = 20 min at 1s.private static final PatternSplits a saved history line on the commas.private static final StringFile holding the bandwidth history.private static final longFlush to disk every 60s.private static BandwidthHistoryThe one history in use.private static final longDiscard file data older than 30 min.static final longSample once per second.Fields inherited from class SimpleTimer2.TimedEvent
_state, DEFAULT_FUZZModifier and TypeFieldDescriptionprotected SimpleTimer2.TimedEventStateState of the current event.protected static final intDefault fuzz threshold in milliseconds.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionprivate StringformatLast(int n, long[] values) Build a comma-separated string of the last n values from the given array.intReport how many samples fit before the oldest is overwritten.intgetCount()Report how many samples the buffer currently holds.static BandwidthHistoryThe singleton instance.getLastRx(int n) Return the lastnreceive values as a comma-separated string.getLastTx(int n) Return the lastnsend values as a comma-separated string.private voidload()Load the ring buffer from disk.voidrecord(long rx, long tx) Add a sample to the ring buffer.(package private) voidsave()Write the ring buffer to disk.voidSample bandwidth limiter and record.Methods inherited from class SimpleTimer2.TimedEvent
cancel, forceReschedule, reschedule, reschedule, run, schedule, setFuzz, setPool, toStringModifier and TypeMethodDescriptionbooleancancel()Cancel the timed event.voidforceReschedule(long timeoutMs) Always use the new time - ignores fuzz.voidreschedule(long timeoutMs) Use the earliest of the new time and the old time May be called from within timeReached(), but schedule() is better there.voidreschedule(long timeoutMs, boolean useEarliestTime) May be called from within timeReached(), but schedule() is better there.voidrun()voidschedule(long timeoutMs) Schedule this event.voidsetFuzz(int fuzz) Reschedule threshold in ms.voidsetPool(SimpleTimer2 pool) Timer pool for this event.toString()Timer summary for log messages.
Field Details
CAPACITY
SAMPLE_INTERVAL
FLUSH_INTERVAL
MAX_FILE_AGE
private static final long MAX_FILE_AGEDiscard file data older than 30 min.- See Also:
COMMA_SPLIT
Splits a saved history line on the commas.FILE_NAME
File holding the bandwidth history.- See Also:
instance
The one history in use._ctx
The router context._timestamps
private final long[] _timestampsThe saved times of the samples._rx
private final long[] _rxThe saved received byte counts._tx
private final long[] _txThe saved sent byte counts._capacity
private final int _capacityMost samples held at once._file
File the history is saved to._head
private int _headIndex of the oldest sample._count
private int _countNumber of samples stored._nextFlush
private long _nextFlushWhen the history is saved next.
Constructor Details
BandwidthHistory
Create a bandwidth history.- Parameters:
ctx- may be null in unit tests
Method Details
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:
timeReachedin classSimpleTimer2.TimedEvent
record
public void record(long rx, long tx) Add a sample to the ring buffer.- Parameters:
rx- the receive bandwidth in Bpstx- 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
Return the lastnreceive 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
Return the lastnsend 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
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 stringvalues- 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