From ea3b8e49c33407e382f75418605f1ffdb84fbc98 Mon Sep 17 00:00:00 2001 From: Bert Massop Date: Sun, 13 Oct 2024 20:48:59 +0200 Subject: [PATCH] RunningAverage: deprecate MedianMeanRunningAverage and replace its uses 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. --- src/freenet/io/xfer/BlockReceiver.java | 11 +++++------ src/freenet/io/xfer/BlockTransmitter.java | 17 ++++++++--------- src/freenet/node/RequestSender.java | 19 +++++++++---------- .../math/MedianMeanRunningAverage.java | 2 ++ 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/freenet/io/xfer/BlockReceiver.java b/src/freenet/io/xfer/BlockReceiver.java index 176edc0002..3ebfb66e1f 100644 --- a/src/freenet/io/xfer/BlockReceiver.java +++ b/src/freenet/io/xfer/BlockReceiver.java @@ -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, @@ -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; @@ -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(); diff --git a/src/freenet/io/xfer/BlockTransmitter.java b/src/freenet/io/xfer/BlockTransmitter.java index e363102312..2ff0d2344b 100644 --- a/src/freenet/io/xfer/BlockTransmitter.java +++ b/src/freenet/io/xfer/BlockTransmitter.java @@ -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; @@ -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 @@ -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; @@ -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() { diff --git a/src/freenet/node/RequestSender.java b/src/freenet/node/RequestSender.java index 2a52a0a80e..48a3fd88bc 100644 --- a/src/freenet/node/RequestSender.java +++ b/src/freenet/node/RequestSender.java @@ -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 @@ -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; @@ -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); diff --git a/src/freenet/support/math/MedianMeanRunningAverage.java b/src/freenet/support/math/MedianMeanRunningAverage.java index 7b38553762..0c440a36eb 100644 --- a/src/freenet/support/math/MedianMeanRunningAverage.java +++ b/src/freenet/support/math/MedianMeanRunningAverage.java @@ -9,7 +9,9 @@ * (Also uses CPU time O(N log N) with the number of reports in currentValue()). * * @author Matthew Toseland (0xE43DA450) + * @deprecated may use excessive RAM and CPU, see warning */ +@Deprecated public final class MedianMeanRunningAverage implements RunningAverage, Cloneable { private final ArrayList reports = new ArrayList<>();