Skip to content

Commit

Permalink
RunningAverage: deprecate MedianMeanRunningAverage and replace its uses
Browse files Browse the repository at this point in the history
Replace all uses of MedianMeanRunningAverage with TrivialRunningAverage
which does not eat an unbounded amount of RAM. This class was only used
for debug purposes under the logMINOR flag. Having the median value in
addition to mean value does likely not warrant the additional RAM and
CPU consumption.

Deprecate the class so that it can eventually be removed.
  • Loading branch information
bertm committed Oct 13, 2024
1 parent 8b78902 commit ea3b8e4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 deletions.
11 changes: 5 additions & 6 deletions src/freenet/io/xfer/BlockReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
import freenet.support.Ticker;
import freenet.support.TimeUtil;
import freenet.support.io.NativeThread;
import freenet.support.math.MedianMeanRunningAverage;
import freenet.support.math.RunningAverage;
import freenet.support.math.TrivialRunningAverage;

/**
* IMPORTANT: The receiver can cancel the incoming transfer. This may or may not,
Expand Down Expand Up @@ -306,10 +307,8 @@ public void onMatched(Message m1) {
long endTime = System.currentTimeMillis();
long transferTime = (endTime - startTime);
if(logMINOR) {
synchronized(avgTimeTaken) {
avgTimeTaken.report(transferTime);
Logger.minor(this, "Block transfer took "+transferTime+"ms - average is "+avgTimeTaken);
}
avgTimeTaken.report(transferTime);
Logger.minor(this, "Block transfer took "+transferTime+"ms - average is "+avgTimeTaken.currentValue());
}
complete(_prb.getBlock());
return;
Expand Down Expand Up @@ -534,7 +533,7 @@ public void receiveAborted(int reason, String description) {
}
}

private static MedianMeanRunningAverage avgTimeTaken = new MedianMeanRunningAverage();
private static final RunningAverage avgTimeTaken = new TrivialRunningAverage();

private void maybeResetDiscardFilter() {
long timeleft=discardEndTime-System.currentTimeMillis();
Expand Down
17 changes: 8 additions & 9 deletions src/freenet/io/xfer/BlockTransmitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/
package freenet.io.xfer;

import java.util.HashSet;
import java.util.Deque;
import java.util.HashSet;

import freenet.io.comm.AsyncMessageCallback;
import freenet.io.comm.AsyncMessageFilterCallback;
Expand All @@ -32,18 +32,19 @@
import freenet.io.comm.NotConnectedException;
import freenet.io.comm.PeerContext;
import freenet.io.comm.RetrievalException;
import freenet.node.MessageItem;
import freenet.io.comm.SlowAsyncMessageFilterCallback;
import freenet.node.MessageItem;
import freenet.node.PrioRunnable;
import freenet.support.BitArray;
import freenet.support.Executor;
import freenet.support.LogThresholdCallback;
import freenet.support.Logger;
import freenet.support.Logger.LogLevel;
import freenet.support.Ticker;
import freenet.support.TimeUtil;
import freenet.support.Logger.LogLevel;
import freenet.support.io.NativeThread;
import freenet.support.math.MedianMeanRunningAverage;
import freenet.support.math.RunningAverage;
import freenet.support.math.TrivialRunningAverage;

/**
* @author ian
Expand Down Expand Up @@ -471,10 +472,8 @@ public void onMatched(Message m) {
if(logMINOR) {
long endTime = System.currentTimeMillis();
long transferTime = (endTime - startTime);
synchronized(avgTimeTaken) {
avgTimeTaken.report(transferTime);
Logger.minor(this, "Block send took "+transferTime+" : "+avgTimeTaken+" on "+BlockTransmitter.this);
}
avgTimeTaken.report(transferTime);
Logger.minor(this, "Block send took "+transferTime+" : average "+avgTimeTaken.currentValue()+" on "+BlockTransmitter.this);
}
synchronized(_senderThread) {
_receivedSendCompletion = true;
Expand Down Expand Up @@ -764,7 +763,7 @@ else if(logMINOR)

private long lastSentPacket = -1;

private static MedianMeanRunningAverage avgTimeTaken = new MedianMeanRunningAverage();
private static final RunningAverage avgTimeTaken = new TrivialRunningAverage();

/** LOCKING: Must be called with _senderThread held. */
private int getNumSent() {
Expand Down
19 changes: 9 additions & 10 deletions src/freenet/node/RequestSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
import freenet.support.SimpleFieldSet;
import freenet.support.TimeUtil;
import freenet.support.io.NativeThread;
import freenet.support.math.MedianMeanRunningAverage;
import freenet.support.math.RunningAverage;
import freenet.support.math.TrivialRunningAverage;

/**
* @author amphibian
Expand Down Expand Up @@ -1542,9 +1543,9 @@ public synchronized short waitUntilStatusChange(short mask) {
}
}

private static MedianMeanRunningAverage avgTimeTaken = new MedianMeanRunningAverage();
private static final RunningAverage avgTimeTaken = new TrivialRunningAverage();

private static MedianMeanRunningAverage avgTimeTakenTransfer = new MedianMeanRunningAverage();
private static final RunningAverage avgTimeTakenTransfer = new TrivialRunningAverage();

private long transferTime;

Expand Down Expand Up @@ -1583,13 +1584,11 @@ private void finish(int code, PeerNode next, boolean fromOfferedKey) {
if(status == SUCCESS) {
if((!isSSK) && transferTime > 0 && logMINOR) {
long timeTaken = System.currentTimeMillis() - startTime;
synchronized(avgTimeTaken) {
avgTimeTaken.report(timeTaken);
avgTimeTakenTransfer.report(transferTime);
if(logMINOR) Logger.minor(this, "Successful CHK request took "+timeTaken+" average "+avgTimeTaken);
if(logMINOR) Logger.minor(this, "Successful CHK request transfer "+transferTime+" average "+avgTimeTakenTransfer);
if(logMINOR) Logger.minor(this, "Search phase: median "+(avgTimeTaken.currentValue() - avgTimeTakenTransfer.currentValue())+"ms, mean "+(avgTimeTaken.meanValue() - avgTimeTakenTransfer.meanValue())+"ms");
}
avgTimeTaken.report(timeTaken);
avgTimeTakenTransfer.report(transferTime);
Logger.minor(this, "Successful CHK request took "+timeTaken+" average "+avgTimeTaken.currentValue());
Logger.minor(this, "Successful CHK request transfer "+transferTime+" average "+avgTimeTakenTransfer.currentValue());
Logger.minor(this, "Search phase: mean "+(avgTimeTaken.currentValue() - avgTimeTakenTransfer.currentValue())+"ms");
}
if(next != null) {
next.onSuccess(false, isSSK);
Expand Down
2 changes: 2 additions & 0 deletions src/freenet/support/math/MedianMeanRunningAverage.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
* (Also uses CPU time O(N log N) with the number of reports in currentValue()).
*
* @author Matthew Toseland <[email protected]> (0xE43DA450)
* @deprecated may use excessive RAM and CPU, see warning
*/
@Deprecated
public final class MedianMeanRunningAverage implements RunningAverage, Cloneable {

private final ArrayList<Double> reports = new ArrayList<>();
Expand Down

0 comments on commit ea3b8e4

Please sign in to comment.