Author: toad
Date: 2008-09-01 22:56:09 +0000 (Mon, 01 Sep 2008)
New Revision: 22330

Modified:
   trunk/freenet/src/freenet/clients/http/ConnectionsToadlet.java
   trunk/freenet/src/freenet/node/PeerManager.java
   trunk/freenet/src/freenet/node/PeerNode.java
   trunk/freenet/src/freenet/node/PeerNodeStatus.java
Log:
Keep a simple count of selections since connected, rather than a big treeset 
containing a long for every hit.


Modified: trunk/freenet/src/freenet/clients/http/ConnectionsToadlet.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/ConnectionsToadlet.java      
2008-09-01 22:50:42 UTC (rev 22329)
+++ trunk/freenet/src/freenet/clients/http/ConnectionsToadlet.java      
2008-09-01 22:56:09 UTC (rev 22330)
@@ -121,7 +121,7 @@
                                return compareLongs(total1, total2);
                        }else if(sortBy.equals("selection_percentage")){
                                long sinceWhen = System.currentTimeMillis() - 
PeerNode.SELECTION_SAMPLING_PERIOD;
-                               return 
compareInts(firstNode.getNumberOfSelections().headSet(sinceWhen).size(), 
secondNode.getNumberOfSelections().headSet(sinceWhen).size());
+                               return 
Double.compare(firstNode.getSelectionRate(), secondNode.getSelectionRate());
                        }else if(sortBy.equals("time_delta")){
                                return compareLongs(firstNode.getClockDelta(), 
secondNode.getClockDelta());
                        }else if(sortBy.equals(("uptime"))){
@@ -693,9 +693,11 @@

        abstract protected SimpleFieldSet getNoderef();

-       private void drawRow(HTMLNode peerTable, PeerNodeStatus peerNodeStatus, 
boolean advancedModeEnabled, boolean fProxyJavascriptEnabled, long now, String 
path, boolean enablePeerActions, SimpleColumn[] endCols, boolean 
drawMessageTypes, int numberOfSelectionSamples) {
-               int peerSelectionCount = 
peerNodeStatus.getNumberOfSelections().tailSet(now - 
PeerNode.SELECTION_SAMPLING_PERIOD).size();
-               int peerSelectionPercentage = (numberOfSelectionSamples > 0 ? 
(peerSelectionCount*100/numberOfSelectionSamples) : 0);
+       private void drawRow(HTMLNode peerTable, PeerNodeStatus peerNodeStatus, 
boolean advancedModeEnabled, boolean fProxyJavascriptEnabled, long now, String 
path, boolean enablePeerActions, SimpleColumn[] endCols, boolean 
drawMessageTypes, double totalSelectionRate) {
+               double selectionRate = peerNodeStatus.getSelectionRate();
+               int peerSelectionPercentage = 0;
+               if(totalSelectionRate > 0)
+                       peerSelectionPercentage = (int) (selectionRate * 100 / 
selectionRate);
                HTMLNode peerRow = peerTable.addChild("tr", "class", 
"darknet_connections_"+(peerSelectionPercentage > 
PeerNode.SELECTION_PERCENTAGE_WARNING ? "warning" : "normal"));

                if(enablePeerActions) {
@@ -777,7 +779,7 @@
                        // percent of time connected column
                        peerRow.addChild("td", "class", "peer-idle" /* FIXME 
*/).addChild("#", 
fix1.format(peerNodeStatus.getPercentTimeRoutableConnection()));
                        // selection stats
-                       peerRow.addChild("td", "class", "peer-idle" /* FIXME 
*/).addChild("#", (numberOfSelectionSamples > 0 ? (peerSelectionPercentage+"%") 
: "N/A"));
+                       peerRow.addChild("td", "class", "peer-idle" /* FIXME 
*/).addChild("#", (totalSelectionRate > 0 ? (peerSelectionPercentage+"%") : 
"N/A"));
                        // total traffic column
                        peerRow.addChild("td", "class", "peer-idle" /* FIXME 
*/).addChild("#", SizeUtil.formatSize(peerNodeStatus.getTotalInputBytes())+" / 
"+SizeUtil.formatSize(peerNodeStatus.getTotalOutputBytes())+"/"+SizeUtil.formatSize(peerNodeStatus.getResendBytesSent()));
                        // congestion control

Modified: trunk/freenet/src/freenet/node/PeerManager.java
===================================================================
--- trunk/freenet/src/freenet/node/PeerManager.java     2008-09-01 22:50:42 UTC 
(rev 22329)
+++ trunk/freenet/src/freenet/node/PeerManager.java     2008-09-01 22:56:09 UTC 
(rev 22330)
@@ -883,8 +883,13 @@
                int count = 0;

                Long selectionSamplesTimestamp = now - 
PeerNode.SELECTION_SAMPLING_PERIOD;
-               int numberOfSelectionsSamples = 
getNumberOfSelectionSamples().tailSet(selectionSamplesTimestamp).size();
-               boolean enableFOAFMitigationHack = (peers.length >= 
PeerNode.SELECTION_MIN_PEERS) && (numberOfSelectionsSamples > 0);
+               double[] selectionRates = new double[peers.length];
+               double totalSelectionRate = 0.0;
+               for(int i=0;i<peers.length;i++) {
+                       selectionRates[i] = peers[i].selectionRate();
+                       totalSelectionRate += selectionRates[i];
+               }
+               boolean enableFOAFMitigationHack = (peers.length >= 
PeerNode.SELECTION_MIN_PEERS) && (totalSelectionRate > 0.0);
                for(int i = 0; i < peers.length; i++) {
                        PeerNode p = peers[i];
                        if(routedTo.contains(p)) {
@@ -908,8 +913,8 @@
                                continue;
                        }
                        if(enableFOAFMitigationHack) {
-                               int selectionSamples = 
p.getNumberOfSelections().tailSet(selectionSamplesTimestamp).size();
-                               int selectionSamplesPercentage = 
selectionSamples*100/numberOfSelectionsSamples;
+                               double selectionRate = selectionRates[i];
+                               double selectionSamplesPercentage = 
selectionRate / totalSelectionRate;
                                if(PeerNode.SELECTION_PERCENTAGE_WARNING < 
selectionSamplesPercentage) {
                                        if(logMINOR)
                                                Logger.minor(this, "Skipping 
over-selectionned peer(" + selectionSamplesPercentage + "%): " + p.getPeer());

Modified: trunk/freenet/src/freenet/node/PeerNode.java
===================================================================
--- trunk/freenet/src/freenet/node/PeerNode.java        2008-09-01 22:50:42 UTC 
(rev 22329)
+++ trunk/freenet/src/freenet/node/PeerNode.java        2008-09-01 22:56:09 UTC 
(rev 22330)
@@ -153,12 +153,7 @@
        /** Time added or restarted (reset on startup unlike peerAddedTime) */
        private long timeAddedOrRestarted;

-       /**
-        * Track the number of times this PeerNode has been selected by
-        * the routing algorithm over a given period of time
-        */
-       private SortedSet<Long> numberOfSelections = new TreeSet<Long>();
-       private final Object numberOfSelectionsSync = new Object();
+       private long countSelectionsSinceConnected = 0;
        // 5mins; yes it's alchemy!
        public static final int SELECTION_SAMPLING_PERIOD = 5 * 60 * 1000;
        // 30%; yes it's alchemy too! and probably *way* too high to serve any 
purpose
@@ -1897,6 +1892,7 @@
                        // Don't reset the uptime if we rekey
                        if(!isConnected) {
                                connectedTime = now;
+                               countSelectionsSinceConnected = 0;
                                sentInitialMessages = false;
                        } else
                                wasARekey = true;
@@ -3995,22 +3991,23 @@
                return (short)(((int)uptime) & 0xFF);
        }

-       public SortedSet<Long> getNumberOfSelections() {
-               // FIXME: returning a copy is not an option: find a smarter way 
of dealing with the synchronization
-               synchronized(numberOfSelectionsSync) {
-                       return numberOfSelections;
-               }
-       }
-       
        public void incrementNumberOfSelections(long time) {
                // TODO: reimplement with a bit field to spare memory
-               synchronized(numberOfSelectionsSync) {
-                       if(numberOfSelections.size() > SELECTION_MAX_SAMPLES)
-                               numberOfSelections = 
numberOfSelections.tailSet(time - SELECTION_SAMPLING_PERIOD);
-                       numberOfSelections.add(time);
+               synchronized(this) {
+                       countSelectionsSinceConnected++;
                }
        }

+       /**
+        * @return The rate at which this peer has been selected since it 
connected.
+        */
+       public synchronized double selectionRate() {
+               long uptime = System.currentTimeMillis() - this.connectedTime;
+               // Avoid bias due to short uptime.
+               if(uptime < 10*1000) return 0.0;
+               return countSelectionsSinceConnected / (double) uptime;
+       }
+
        private long offeredMainJarVersion;

        public void setMainJarOfferedVersion(long mainJarVersion) {

Modified: trunk/freenet/src/freenet/node/PeerNodeStatus.java
===================================================================
--- trunk/freenet/src/freenet/node/PeerNodeStatus.java  2008-09-01 22:50:42 UTC 
(rev 22329)
+++ trunk/freenet/src/freenet/node/PeerNodeStatus.java  2008-09-01 22:56:09 UTC 
(rev 22330)
@@ -95,7 +95,7 @@

        private final int reportedUptimePercentage;

-       private final SortedSet<Long> numberOfSelections;
+       private final double selectionRate;

        PeerNodeStatus(PeerNode peerNode, boolean noHeavy) {
                if(Logger.shouldLog(Logger.MINOR, this)) {
@@ -109,7 +109,7 @@
                        peerAddress = p.getFreenetAddress().toString();
                        peerPort = p.getPort();
                }
-               this.numberOfSelections = peerNode.getNumberOfSelections();
+               this.selectionRate = peerNode.selectionRate();
                this.statusValue = peerNode.getPeerNodeStatus();
                this.statusName = peerNode.getPeerNodeStatusString();
                this.statusCSSName = peerNode.getPeerNodeStatusCSSClassName();
@@ -398,7 +398,7 @@
                return reportedUptimePercentage;
        }

-       public SortedSet<Long> getNumberOfSelections() {
-               return numberOfSelections;
+       public double getSelectionRate() {
+               return selectionRate;
        }
 }


Reply via email to