From efc202d2ee720a843b5832ee41e4babfeb2db733 Mon Sep 17 00:00:00 2001
From: zab <zab@mail.i2p>
Date: Sat, 17 Nov 2012 19:22:23 +0000
Subject: [PATCH] more use of the new methods

---
 .../router/web/ProfileOrganizerRenderer.java  |  9 +++---
 core/java/src/net/i2p/stat/Rate.java          | 29 +++++++++++++------
 core/java/src/net/i2p/stat/RateAverages.java  | 28 ++++++++++++++++--
 .../net/i2p/router/RouterThrottleImpl.java    | 19 ++----------
 .../peermanager/CapacityCalculator.java       |  8 +++--
 .../tunnel/pool/ExploratoryPeerSelector.java  |  2 +-
 .../i2p/router/tunnel/pool/TunnelPool.java    | 15 ++++++----
 7 files changed, 68 insertions(+), 42 deletions(-)

diff --git a/apps/routerconsole/java/src/net/i2p/router/web/ProfileOrganizerRenderer.java b/apps/routerconsole/java/src/net/i2p/router/web/ProfileOrganizerRenderer.java
index 1c7077e274..edd7b31dc9 100644
--- a/apps/routerconsole/java/src/net/i2p/router/web/ProfileOrganizerRenderer.java
+++ b/apps/routerconsole/java/src/net/i2p/router/web/ProfileOrganizerRenderer.java
@@ -172,11 +172,12 @@ class ProfileOrganizerRenderer {
             if (_context.banlist().isBanlisted(peer)) buf.append(_("Banned"));
             if (prof.getIsFailing()) buf.append(' ').append(_("Failing"));
             if (_context.commSystem().wasUnreachable(peer)) buf.append(' ').append(_("Unreachable"));
+            RateAverages ra = RateAverages.getTemp();
             Rate failed = prof.getTunnelHistory().getFailedRate().getRate(30*60*1000);
-            long fails = failed.getCurrentEventCount() + failed.getLastEventCount();
+            long fails = failed.computeAverages(ra, false).getTotalEventCount();
             if (fails > 0) {
                 Rate accepted = prof.getTunnelCreateResponseTime().getRate(30*60*1000);
-                long total = fails + accepted.getCurrentEventCount() + accepted.getLastEventCount();
+                long total = fails + accepted.computeAverages(ra, false).getTotalEventCount();
                 if (total / fails <= 10)   // hide if < 10%
                     buf.append(' ').append(fails).append('/').append(total).append(' ').append(_("Test Fails"));
             }
@@ -219,7 +220,7 @@ class ProfileOrganizerRenderer {
         buf.append("<th class=\"smallhead\">").append(_("1h Fail Rate")).append("</th>");
         buf.append("<th class=\"smallhead\">").append(_("1d Fail Rate")).append("</th>");
         buf.append("</tr>");
-        RateAverages ra = new RateAverages();
+        RateAverages ra = RateAverages.getTemp();
         for (Iterator<PeerProfile> iter = integratedPeers.iterator(); iter.hasNext();) {
             PeerProfile prof = iter.next();
             Hash peer = prof.getPeer();
@@ -349,7 +350,6 @@ class ProfileOrganizerRenderer {
             Rate r = rs.getRate(rate);
             if (r == null)
                 return _(NA);
-            ra.reset();
             r.computeAverages(ra, false);
             if (ra.getTotalEventCount() == 0)
                 return _(NA);
@@ -363,7 +363,6 @@ class ProfileOrganizerRenderer {
             Rate r = rs.getRate(rate);
             if (r == null)
                 return "0%";
-            ra.reset();
             r.computeAverages(ra, false);
             if (ra.getTotalEventCount() <= 0)
                 return "0%";
diff --git a/core/java/src/net/i2p/stat/Rate.java b/core/java/src/net/i2p/stat/Rate.java
index e08fd95fd6..5f5ad7f178 100644
--- a/core/java/src/net/i2p/stat/Rate.java
+++ b/core/java/src/net/i2p/stat/Rate.java
@@ -414,12 +414,21 @@ public class Rate {
         return 0.0D;
     }
     
+    /**
+     * @return a thread-local temp object containing computed averages.
+     */
+    public RateAverages computeAverages() {
+        return computeAverages(RateAverages.getTemp(),false);
+    }
+    
     /**
      * @param out where to store the computed averages.  
      * @param useLifetime whether the lifetime average should be used if
      * there are no events.
+     * @return the same RateAverages object for chaining
      */
-    public synchronized void computeAverages(RateAverages out, boolean useLifetime) {
+    public synchronized RateAverages computeAverages(RateAverages out, boolean useLifetime) {
+        out.reset();
         
         final long total = _currentEventCount + _lastEventCount;
         out.setTotalEventCount(total);
@@ -427,15 +436,17 @@ public class Rate {
         if (total <= 0) {
             final double avg = useLifetime ? getAvgOrLifetimeAvg() : getAverageValue();
             out.setAverage(avg);
-            return;
+        } else {
+
+            if (_currentEventCount > 0)
+                out.setCurrent( getCurrentTotalValue() / _currentEventCount );
+            if (_lastEventCount > 0)
+                out.setLast( getLastTotalValue() / _lastEventCount );
+
+            out.setTotalValues(getCurrentTotalValue() + getLastTotalValue());
+            out.setAverage( out.getTotalValues()  / total );
         }
-        
-        if (_currentEventCount > 0)
-            out.setCurrent( getCurrentTotalValue() / _currentEventCount );
-        if (_lastEventCount > 0)
-            out.setLast( getLastTotalValue() / _lastEventCount );
-        
-        out.setAverage( ( getCurrentTotalValue() + getLastTotalValue() ) / total );
+        return out;
     }
 
     public synchronized void store(String prefix, StringBuilder buf) throws IOException {
diff --git a/core/java/src/net/i2p/stat/RateAverages.java b/core/java/src/net/i2p/stat/RateAverages.java
index 560c11263f..af114ab25f 100644
--- a/core/java/src/net/i2p/stat/RateAverages.java
+++ b/core/java/src/net/i2p/stat/RateAverages.java
@@ -7,14 +7,30 @@ package net.i2p.stat;
  */
 public class RateAverages {
     
-    private double average, current, last;
+    /** thread-local temp instance */
+    private static final ThreadLocal<RateAverages> TEMP =
+            new ThreadLocal<RateAverages>() {
+        public RateAverages initialValue() {
+            return new RateAverages();
+        }
+    };
+    
+    /**
+     * @return thread-local temp instance.
+     */
+    public static RateAverages getTemp() {
+        return TEMP.get();
+    }
+    
+    private double average, current, last, totalValues;
     private long totalEventCount;
     
-    public void reset() {
+    void reset() {
         average = 0;
         current = 0;
         last = 0;
         totalEventCount = 0;
+        totalValues = 0;
     }
 
     public double getAverage() {
@@ -49,4 +65,12 @@ public class RateAverages {
         this.totalEventCount = totalEventCount;
     }
     
+    public double getTotalValues() {
+        return totalValues;
+    }
+    
+    public void setTotalValues(double totalValues) {
+        this.totalValues = totalValues;
+    }
+    
 }
diff --git a/router/java/src/net/i2p/router/RouterThrottleImpl.java b/router/java/src/net/i2p/router/RouterThrottleImpl.java
index 7c9c37306b..b7237c559f 100644
--- a/router/java/src/net/i2p/router/RouterThrottleImpl.java
+++ b/router/java/src/net/i2p/router/RouterThrottleImpl.java
@@ -41,15 +41,6 @@ class RouterThrottleImpl implements RouterThrottle {
 
     private static final long REJECT_STARTUP_TIME = 20*60*1000;
     
-    /** scratch space for calculations of rate averages */
-    private static final ThreadLocal<RateAverages> RATE_AVERAGES =
-            new ThreadLocal<RateAverages>() {
-        @Override
-        public RateAverages initialValue() {
-            return new RateAverages();
-        }
-    };
-
     public RouterThrottleImpl(RouterContext context) {
         _context = context;
         _log = context.logManager().getLog(RouterThrottleImpl.class);
@@ -128,8 +119,7 @@ class RouterThrottleImpl implements RouterThrottle {
         //long lag = _context.jobQueue().getMaxLag();
         // reject here if lag too high???
         
-        RateAverages ra = RATE_AVERAGES.get();
-        ra.reset();
+        RateAverages ra = RateAverages.getTemp();
         
         // TODO
         // This stat is highly dependent on transport mix.
@@ -260,11 +250,8 @@ class RouterThrottleImpl implements RouterThrottle {
         double messagesPerTunnel = DEFAULT_MESSAGES_PER_TUNNEL_ESTIMATE;
         if (rs != null) {
             r = rs.getRate(60*1000);
-            if (r != null) {
-                ra.reset();
-                r.computeAverages(ra, true);
-                messagesPerTunnel = ra.getAverage();
-            }
+            if (r != null) 
+                messagesPerTunnel = r.computeAverages(ra, true).getAverage();
         }
         if (messagesPerTunnel < DEFAULT_MESSAGES_PER_TUNNEL_ESTIMATE)
             messagesPerTunnel = DEFAULT_MESSAGES_PER_TUNNEL_ESTIMATE;
diff --git a/router/java/src/net/i2p/router/peermanager/CapacityCalculator.java b/router/java/src/net/i2p/router/peermanager/CapacityCalculator.java
index 5c3894e1a6..6750c025de 100644
--- a/router/java/src/net/i2p/router/peermanager/CapacityCalculator.java
+++ b/router/java/src/net/i2p/router/peermanager/CapacityCalculator.java
@@ -2,6 +2,7 @@ package net.i2p.router.peermanager;
 
 import net.i2p.I2PAppContext;
 import net.i2p.stat.Rate;
+import net.i2p.stat.RateAverages;
 import net.i2p.stat.RateStat;
 
 /**
@@ -122,15 +123,16 @@ class CapacityCalculator {
         Rate curAccepted = acceptStat.getRate(period);
         Rate curRejected = rejectStat.getRate(period);
         Rate curFailed = failedStat.getRate(period);
+        RateAverages ra = RateAverages.getTemp();
 
         double eventCount = 0;
         if (curAccepted != null) {
-            eventCount = curAccepted.getCurrentEventCount() + curAccepted.getLastEventCount();
+            eventCount = curAccepted.computeAverages(ra, false).getTotalEventCount();
             // Punish for rejections.
             // We don't want to simply do eventCount -= rejected or we get to zero with 50% rejection,
             // and we don't want everybody to be at zero during times of congestion.
             if (eventCount > 0 && curRejected != null) {
-                long rejected = curRejected.getCurrentEventCount() + curRejected.getLastEventCount();
+                long rejected = curRejected.computeAverages(ra,false).getTotalEventCount();
                 if (rejected > 0)
                     eventCount *= eventCount / (eventCount + (2 * rejected));
             }
@@ -144,7 +146,7 @@ class CapacityCalculator {
         // fast pool, for example, you have a 1/7 chance of being falsely blamed.
         // We also don't want to drive everybody's capacity to zero, that isn't helpful.
         if (curFailed != null) {
-            double failed = curFailed.getCurrentTotalValue() + curFailed.getLastTotalValue();
+            double failed = curFailed.computeAverages(ra, false).getTotalValues();
             if (failed > 0) {
                 //if ( (period <= 10*60*1000) && (curFailed.getCurrentEventCount() > 0) )
                 //    return 0.0d; // their tunnels have failed in the last 0-10 minutes
diff --git a/router/java/src/net/i2p/router/tunnel/pool/ExploratoryPeerSelector.java b/router/java/src/net/i2p/router/tunnel/pool/ExploratoryPeerSelector.java
index ed3837ec44..d5a7ecd62e 100644
--- a/router/java/src/net/i2p/router/tunnel/pool/ExploratoryPeerSelector.java
+++ b/router/java/src/net/i2p/router/tunnel/pool/ExploratoryPeerSelector.java
@@ -178,6 +178,6 @@ class ExploratoryPeerSelector extends TunnelPeerSelector {
         Rate r = rs.getRate(period);
         if (r == null)
             return 0;
-        return (int) (r.getLastEventCount() + r.getCurrentEventCount());
+        return (int) (r.computeAverages().getTotalEventCount());
     }
 }
diff --git a/router/java/src/net/i2p/router/tunnel/pool/TunnelPool.java b/router/java/src/net/i2p/router/tunnel/pool/TunnelPool.java
index d14c32420d..4a5e346e85 100644
--- a/router/java/src/net/i2p/router/tunnel/pool/TunnelPool.java
+++ b/router/java/src/net/i2p/router/tunnel/pool/TunnelPool.java
@@ -19,6 +19,7 @@ import net.i2p.router.TunnelInfo;
 import net.i2p.router.TunnelPoolSettings;
 import net.i2p.router.tunnel.HopConfig;
 import net.i2p.stat.Rate;
+import net.i2p.stat.RateAverages;
 import net.i2p.stat.RateStat;
 import net.i2p.util.Log;
 
@@ -331,9 +332,10 @@ public class TunnelPool {
                 Rate rr = r.getRate(10*60*1000);
                 Rate sr = s.getRate(10*60*1000);
                 if (er != null && rr != null && sr != null) {
-                    long ec = er.getCurrentEventCount() + er.getLastEventCount();
-                    long rc = rr.getCurrentEventCount() + rr.getLastEventCount();
-                    long sc = sr.getCurrentEventCount() + sr.getLastEventCount();
+                    RateAverages ra = RateAverages.getTemp();
+                    long ec = er.computeAverages(ra, false).getTotalEventCount();
+                    long rc = rr.computeAverages(ra, false).getTotalEventCount();
+                    long sc = sr.computeAverages(ra, false).getTotalEventCount();
                     long tot = ec + rc + sc;
                     if (tot >= BUILD_TRIES_QUANTITY_OVERRIDE) {
                         if (1000 * sc / tot <=  1000 / BUILD_TRIES_QUANTITY_OVERRIDE)
@@ -366,9 +368,10 @@ public class TunnelPool {
                 Rate rr = r.getRate(10*60*1000);
                 Rate sr = s.getRate(10*60*1000);
                 if (er != null && rr != null && sr != null) {
-                    long ec = er.getCurrentEventCount() + er.getLastEventCount();
-                    long rc = rr.getCurrentEventCount() + rr.getLastEventCount();
-                    long sc = sr.getCurrentEventCount() + sr.getLastEventCount();
+                    RateAverages ra = RateAverages.getTemp();
+                    long ec = er.computeAverages(ra, false).getTotalEventCount();
+                    long rc = rr.computeAverages(ra, false).getTotalEventCount();
+                    long sc = sr.computeAverages(ra, false).getTotalEventCount();
                     long tot = ec + rc + sc;
                     if (tot >= BUILD_TRIES_LENGTH_OVERRIDE) {
                         if (1000 * sc / tot <=  1000 / BUILD_TRIES_LENGTH_OVERRIDE)
-- 
GitLab