java.lang.Object
net.i2p.util.SimpleTimer2.TimedEvent
org.klomp.snark.BandwidthGraph
- All Implemented Interfaces:
Runnable
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 TypeFieldDescriptionprivate intSamples held in the ring.private static final AtomicLongBytes received over the datagram sockets.private static final AtomicLongBytes sent over the datagram sockets.private intPosition of the newest sample in the ring.private static final AtomicLongBytes received over the HTTP proxies.private static final AtomicLongBytes sent over the HTTP proxies.private static BandwidthGraphThe single instance of the graph.private longCounter snapshots taken at the previous tick, for delta computation.private longCounter snapshots taken at the previous tick, for delta computation.private longWhen the samples were last saved to disk.private longCounter snapshots taken at the previous tick, for delta computation.private longCounter snapshots taken at the previous tick, for delta computation.private longWhen the last sample was taken.private final LogLogger for the bandwidth graph.private final SnarkManagerThe 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 longIncrements whenever a sample is added.static final intRing capacity: 240 samples = 4 hours at 60 s.private static final StringFile holding the saved samples.private static final longFlush to disk every five minutes.static final longSample once per minute.static final longWindow covered by a full ring, in ms; older persisted samples are dropped.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
ConstructorsModifierConstructorDescriptionprivateBandwidthGraph(SnarkManager manager) Builds the graph for one manager.Method Summary
Modifier and TypeMethodDescriptionstatic voiddatagramReceived(int bytes) Record datagram bytes received (DHT + UDP tracker listeners).static voiddatagramSent(int bytes) Record datagram bytes sent (DHT + UDP tracker funnel).private static longdelta(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 Filefile()Persistence location: the JVM tmp dir (defaulting to /tmp), matching the BandwidthHistory pattern.private voidflush()Write the serialized ring to the tmp-dir file atomically-ish (write temp, rename), ignoring all errors — persistence is best-effort by design.static StringSerialize the ring oldest-first as "timestampSec,rxKB,txKB;...".static longThe current sample version: increments once per stored sample.static voidhttpTransferred(long txBytes, long rxBytes) Record an HTTP tracker/torrent fetch: approximate request size plus response size.private voidload()Load persisted samples younger than the window, seeding the ring so a fresh webapp start still shows recent history.private voidsample()Snapshot counters, compute deltas, and append one ring entry.private StringEncode the graph data for saving.static voidstart(SnarkManager manager) Start the sampler for this snark manager, loading prior history from the tmp-dir file when present.static voidstop()Stop the sampler and flush pending history.voidOne 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, 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
SAMPLE_INTERVAL_MS
CAPACITY
public static final int CAPACITYRing capacity: 240 samples = 4 hours at 60 s.- See Also:
WINDOW_MS
public static final long WINDOW_MSWindow covered by a full ring, in ms; older persisted samples are dropped.- See Also:
FLUSH_INTERVAL_MS
private static final long FLUSH_INTERVAL_MSFlush to disk every five minutes.- See Also:
FILE_NAME
_datagramTx
Bytes sent over the datagram sockets._datagramRx
Bytes received over the datagram sockets._httpTx
Bytes sent over the HTTP proxies._httpRx
Bytes received over the HTTP proxies._instance
The single instance of the graph._manager
The manager whose torrents are counted._log
Logger for the bandwidth graph._times
private final long[] _timesThe sample times, in a ring._rx
private final long[] _rxThe received bytes per sample._tx
private final long[] _txThe sent bytes per sample._head
private int _headPosition of the newest sample in the ring._count
private int _countSamples held in the ring._version
private long _versionIncrements whenever a sample is added._lastFlush
private long _lastFlushWhen the samples were last saved to disk._lastDatagramTx
private long _lastDatagramTxCounter snapshots taken at the previous tick, for delta computation._lastDatagramRx
private long _lastDatagramRxCounter snapshots taken at the previous tick, for delta computation._lastHttpTx
private long _lastHttpTxCounter snapshots taken at the previous tick, for delta computation._lastHttpRx
private long _lastHttpRxCounter snapshots taken at the previous tick, for delta computation._lastSampleTime
private long _lastSampleTimeWhen the last sample was taken.
Constructor Details
BandwidthGraph
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 zerorxBytes- the bytes received, added only when greater than zero
start
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
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:
timeReachedin classSimpleTimer2.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 intervalprevious- 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
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
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