Update of /cvsroot/freenet/freenet/src/freenet/node/rt
In directory sc8-pr-cvs1:/tmp/cvs-serv26136/src/freenet/node/rt
Modified Files:
ResponseTimeEstimator.java
Log Message:
Intermediate commit. Prepare for moving the actual storage of estimation keys into a
separate inner class.
Index: ResponseTimeEstimator.java
===================================================================
RCS file: /cvsroot/freenet/freenet/src/freenet/node/rt/ResponseTimeEstimator.java,v
retrieving revision 1.22
retrieving revision 1.23
diff -u -w -r1.22 -r1.23
--- ResponseTimeEstimator.java 31 Oct 2003 13:53:41 -0000 1.22
+++ ResponseTimeEstimator.java 1 Nov 2003 11:50:55 -0000 1.23
@@ -277,10 +277,50 @@
dumpLog();
}
+ class keyTimeSens{
+ BigInteger key;
+ int time;
+ double sensitivity;
+ keyTimeSens(BigInteger key, int time, double sensitivity){
+ this.key = key;
+ this.time = time;
+ this.sensitivity = sensitivity;
+ }
+ }
+ keyTimeSens get(int index){
+ return new keyTimeSens(key[index],time[index],sensitivity[index]);
+ }
+ void set(int index,keyTimeSens value){
+ key[index] = value.key;
+ time[index] = value.time;
+ sensitivity[index] = value.sensitivity;
+ }
+
static final BigDecimal DEC_ONE = new BigDecimal(1.0);
static final BigInteger THREE = BigInteger.valueOf(3);
static final BigInteger FOUR = BigInteger.valueOf(4);
+ //Reversively finds the position of the first key in the store, after the
position 'startAt', that is smaller than n
+ //Returns -1 if no such key is present in the store
+ protected int findFirstSmaller_reverse(BigInteger n, int startAt){
+ while (key[startAt].compareTo(n) == 1) {
+ startAt--;
+ if (startAt < 0)
+ return -1;
+ }
+ return startAt;
+ }
+ //Finds the position of the first key in the store, after the position 'startAt',
that is larger than n
+ //Returns -1 if no such key is present in the store
+ protected int findFirstLarger(BigInteger n, int startAt){
+ while (key[startAt].compareTo(n) == -1) {
+ startAt++;
+ if (startAt >= key.length)
+ return -1;
+ }
+ return startAt;
+ }
+
protected synchronized int reportDecreasing(BigInteger lowerBound, BigInteger
upperBound, BigInteger center, int usec, int initialSteps, int lowerBoundPos, int
upperBoundPos) {
if (logDEBUG)
Core.logger.log(this, "reportDecreasing(" +
lowerBound.toString(16) + "," + upperBound.toString(16) + "," + center.toString(16) +
"," + initialSteps + "," + lowerBoundPos + "," + upperBoundPos + ")", new
Exception("debug"), Logger.DEBUG);
@@ -295,26 +335,24 @@
// We are increasing
// inc shiftAmount each time we move a point
int shiftAmount = initialSteps;
- while (key[upperBoundPos].compareTo(upperBound) == 1) {
- upperBoundPos--;
- if (upperBoundPos < 0)
- return initialSteps;
- }
- while (key[lowerBoundPos].compareTo(lowerBound) == -1) {
- lowerBoundPos++;
- if (lowerBoundPos >= key.length)
- return initialSteps;
- }
+
+ upperBoundPos = findFirstSmaller_reverse(upperBound,upperBoundPos);
+ if (upperBoundPos < 0) return initialSteps;
+
+ lowerBoundPos = findFirstLarger(lowerBound,lowerBoundPos);
+ if (lowerBoundPos < 0) return initialSteps;
+
if (lowerBoundPos > upperBoundPos)
return initialSteps;
for (int i = lowerBoundPos; i <= upperBoundPos; i++) {
- if (time[i] < 0)
- Core.logger.log(this, "time[" + i + "] = " + time[i] +
" - NEGATIVE TIME!", Logger.ERROR);
+ keyTimeSens k = get(i);
+ if (k.time < 0)
+ Core.logger.log(this, "time[" + i + "] = " + k.time +
" - NEGATIVE TIME!", Logger.ERROR);
if (logDEBUG)
- Core.logger.log(this, "Trying key[" + i + "]: " +
key[i].toString(16) + "," + time[i], Logger.DEBUG);
+ Core.logger.log(this, "Trying key[" + i + "]: " +
k.key.toString(16) + "," + k.time, Logger.DEBUG);
ensureSensitivityBelowMax(i);
- double sens = sensitivity[i];
- BigInteger diff = handleWrap(key[i].subtract(center));
+ //double sens = sensitivity[i];
+ BigInteger diff = handleKeyspaceWrap(k.key.subtract(center));
// Result = old position + ((old position * sensitivity) /
(sensitivity + 1)
// >> shiftAmount
@@ -327,20 +365,20 @@
*/
// Multiply by 75% to prevent points from collapsing on each
other
BigDecimal fracDiff = new BigDecimal(diff);
- BigDecimal fracSens = new BigDecimal(sens);
+ BigDecimal fracSens = new BigDecimal(k.sensitivity);
BigDecimal fracOrig = new BigDecimal(center);
BigDecimal resultDiff = fracDiff.divide(fracSens.add(DEC_ONE),
BigDecimal.ROUND_DOWN);
diff = resultDiff.toBigInteger().shiftRight(shiftAmount);
diff = diff.multiply(THREE).divide(FOUR);
- key[i] = handleWrap(key[i].subtract(diff));
+ k.key = handleKeyspaceWrap(k.key.subtract(diff));
- int timediff = usec - time[i];
+ int timediff = usec - k.time;
if (logDEBUG)
Core.logger.log(this, "usec=" + usec + ", time[" + i +
"]=" + time[i] + ", timediff=" + timediff, Logger.DEBUG);
double fracTimediff = (double) timediff;
if (logDEBUG)
Core.logger.log(this, "fracTimediff=" + fracTimediff,
Logger.DEBUG);
- double resultTimediff = fracTimediff / (sens + 1.0);
+ double resultTimediff = fracTimediff / (k.sensitivity + 1.0);
if (logDEBUG)
Core.logger.log(this, "resultTimediff=" +
resultTimediff, Logger.DEBUG);
timediff = ((int) resultTimediff) >> shiftAmount;
@@ -349,12 +387,13 @@
timediff = (int) ((((long) timediff) * 3) / 4);
if (logDEBUG)
Core.logger.log(this, "time[" + i + "] = " + time[i] +
", timediff = " + timediff, Logger.DEBUG);
- time[i] += timediff;
+ k.time += timediff;
if (logDEBUG)
Core.logger.log(this, "key[" + i + "] now: " +
key[i].toString(16) + "," + time[i], Logger.DEBUG);
- if (time[i] < 0)
- Core.logger.log(this, "time[" + i + "] = " + time[i] +
" - NEGATIVE TIME!", Logger.ERROR);
- sensitivity[i] += 1.0 / (1 << shiftAmount);
+ if (k.time < 0)
+ Core.logger.log(this, "time[" + i + "] = " + k.time +
" - NEGATIVE TIME!", Logger.ERROR);
+ k.sensitivity += 1.0 / (1 << shiftAmount);
+ set(i,k);
shiftAmount++;
}
dumpLog();
@@ -362,14 +401,14 @@
}
//Handles keyspace wrapping, always returns a properly wrapped value
- private BigInteger handleWrap(BigInteger key) {
+ private BigInteger handleKeyspaceWrap(BigInteger key) {
//TODO: Can we really jump two whole keyspaces in
//one calculation anywhere? If not then the recursion
//is unnessecary
if (key.signum() == -1)
//If the value has fallen below 0
- return handleWrap(key.add(keyspace)); //Then move
upwards (at least) one keyspace
+ return handleKeyspaceWrap(key.add(keyspace)); //Then
move upwards (at least) one keyspace
else if (key.compareTo(keyspace) == 1) //else, if the
value has become larger than the keyspace max
- return handleWrap(key.subtract(keyspace)); //then move
downwards (at least) one keyspace
+ return handleKeyspaceWrap(key.subtract(keyspace)); //then
move downwards (at least) one keyspace
else
return key;
//else do nothing
}
@@ -396,48 +435,49 @@
// We are increasing
// inc shiftAmount each time we move a point
int shiftAmount = initialSteps;
- while(key[upperBoundPos].compareTo(upperBound) == 1) {
- upperBoundPos--;
+
+ upperBoundPos = findFirstSmaller_reverse(upperBound,upperBoundPos);
if(upperBoundPos < 0) return initialSteps;
- }
- while(key[lowerBoundPos].compareTo(lowerBound) == -1) {
- lowerBoundPos++;
- if(lowerBoundPos >= key.length) return initialSteps;
- }
+
+ lowerBoundPos = findFirstLarger(lowerBound,lowerBoundPos);
+ if (lowerBoundPos < 0)return initialSteps;
+
if(lowerBoundPos > upperBoundPos) return initialSteps;
for(int i=upperBoundPos;i>=lowerBoundPos;i--) {
+ keyTimeSens k = get(i);
if(logDEBUG)
Core.logger.log(this, "Trying key["+i+"]: "+
- key[i].toString(16)+","+time[i],
+ k.key.toString(16)+","+k.time,
Logger.DEBUG);
- if(time[i] < 0)
- Core.logger.log(this, "time["+i+"] = "+time[i]+
+ if(k.time < 0)
+ Core.logger.log(this, "time["+i+"] = "+k.time+
" - NEGATIVE TIME!", Logger.ERROR);
ensureSensitivityBelowMax(i);
- double sens = sensitivity[i];
- BigInteger diff = handleWrap(center.subtract(key[i]));
+ //double sens = sensitivity[i];
+ BigInteger diff = handleKeyspaceWrap(center.subtract(k.key));
BigDecimal fracDiff = new BigDecimal(diff);
- BigDecimal fracSens = new BigDecimal(sens);
+ BigDecimal fracSens = new BigDecimal(k.sensitivity);
BigDecimal fracOrig = new BigDecimal(center);
BigDecimal resultDiff = fracDiff.divide(fracSens.add(DEC_ONE),
BigDecimal.ROUND_DOWN);
diff = resultDiff.toBigInteger().shiftRight(shiftAmount);
diff = diff.multiply(THREE).divide(FOUR);
- key[i] = handleWrap(key[i].add(diff.shiftRight(shiftAmount)));
- int timediff = usec - time[i];
+ k.key = handleKeyspaceWrap(k.key.add(diff.shiftRight(shiftAmount)));
+ int timediff = usec - k.time;
double fracTimediff = (double)timediff;
- double resultTimediff = fracTimediff/(sens+1.0);
+ double resultTimediff = fracTimediff/(k.sensitivity+1.0);
timediff = ((int)resultTimediff) >> shiftAmount;
timediff = (int)((((long)timediff)*3)/4);
- time[i] += timediff;
+ k.time += timediff;
if(logDEBUG)
- Core.logger.log(this, "key["+i+"] now: "+key[i].toString(16)+
- ","+time[i], Logger.DEBUG);
- sensitivity[i] += 1.0/(1<<shiftAmount);
+ Core.logger.log(this, "key["+i+"] now: "+k.key.toString(16)+
+ ","+k.time, Logger.DEBUG);
+ k.sensitivity += 1.0/(1<<shiftAmount);
shiftAmount++;
- if(time[i] < 0)
- Core.logger.log(this, "time["+i+"] = "+time[i]+
+ if(k.time < 0)
+ Core.logger.log(this, "time["+i+"] = "+k.time+
" - NEGATIVE TIME!", Logger.ERROR);
+ set(i,k);
}
dumpLog();
return shiftAmount;
_______________________________________________
cvs mailing list
[EMAIL PROTECTED]
http://dodo.freenetproject.org/cgi-bin/mailman/listinfo/cvs