Fixed up bugs and problems in new timer calibration code.
This commit is contained in:
parent
94cb99de64
commit
7d7ec34082
|
@ -97,8 +97,8 @@ public final class Sys {
|
||||||
/** Current recalibration timer value, in milliseconds */
|
/** Current recalibration timer value, in milliseconds */
|
||||||
private static long recalibrationNow;
|
private static long recalibrationNow;
|
||||||
|
|
||||||
/** Total hires ticks that have passed since we started using recalibrated time */
|
/** Hires time when we started counting recalibration time */
|
||||||
private static long ticksSinceRecalibration;
|
private static long hiresRecalibrationThen;
|
||||||
|
|
||||||
/** Last lowres timer time, in milliseconds */
|
/** Last lowres timer time, in milliseconds */
|
||||||
private static long lowresThen;
|
private static long lowresThen;
|
||||||
|
@ -223,35 +223,42 @@ public final class Sys {
|
||||||
|
|
||||||
if (currentResolution == defaultResolution) {
|
if (currentResolution == defaultResolution) {
|
||||||
// Start timing
|
// Start timing
|
||||||
ticksSinceRecalibration = 0L;
|
hiresRecalibrationThen = hiresNow;
|
||||||
recalibrationThen = lowresNow;
|
recalibrationThen = lowresNow;
|
||||||
recalibrationNow = lowresNow;
|
recalibrationNow = lowresNow;
|
||||||
} else {
|
} else {
|
||||||
// Continue counting ticks
|
// Continue counting ticks
|
||||||
ticksSinceRecalibration += getTime();
|
long ticksSinceRecalibration = hiresNow - hiresRecalibrationThen;
|
||||||
|
if (ticksSinceRecalibration < 0) {
|
||||||
|
// Bah. Wrapped timer, so forget it this time and start over.
|
||||||
|
hiresRecalibrationThen = hiresNow;
|
||||||
|
recalibrationThen = lowresNow;
|
||||||
|
} else {
|
||||||
recalibrationNow = lowresNow;
|
recalibrationNow = lowresNow;
|
||||||
long totalTicksSinceRecalibrationStarted = recalibrationNow - recalibrationThen;
|
long totalMillisSinceRecalibrationStarted = recalibrationNow - recalibrationThen;
|
||||||
if (totalTicksSinceRecalibrationStarted > LONG_RECALIBRATION_THRESHOLD) {
|
if (totalMillisSinceRecalibrationStarted > LONG_RECALIBRATION_THRESHOLD) {
|
||||||
// Ok, we've now been operating at a different resolution for long enough.
|
// Ok, we've now been operating at a different resolution for long enough.
|
||||||
// Let's choose a new default resolution based on the average we've
|
// Let's choose a new default resolution based on the average we've
|
||||||
// calculated since it first started needing recalibrating
|
// calculated since it first started needing recalibrating
|
||||||
long actualRecalibrationDuration = recalibrationNow - recalibrationThen;
|
long totalTicksSinceRecalibrationStarted = hiresNow - hiresRecalibrationThen;
|
||||||
defaultResolution = (long) (1000.0 * totalTicksSinceRecalibrationStarted / actualRecalibrationDuration);
|
if (totalTicksSinceRecalibrationStarted > 0) {
|
||||||
|
defaultResolution = (long) (1000.0 * totalTicksSinceRecalibrationStarted / totalMillisSinceRecalibrationStarted);
|
||||||
tempResolution = defaultResolution;
|
tempResolution = defaultResolution;
|
||||||
if (DEBUG_CALIBRATION) {
|
if (DEBUG_CALIBRATION) {
|
||||||
System.err.println("Permanent recalibration to "+defaultResolution+" ticks per second");
|
System.err.println("Permanent recalibration to "+defaultResolution+" ticks per second");
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Bah. Once again, a wrapped timer.
|
||||||
|
hiresRecalibrationThen = hiresNow;
|
||||||
|
recalibrationThen = lowresNow;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Temporarily change current resolution to the recently calculated resolution
|
// Temporarily change current resolution to the recently calculated resolution
|
||||||
currentResolution = tempResolution;
|
currentResolution = tempResolution;
|
||||||
} else {
|
} else {
|
||||||
// Recalibrate the baseline using the operating system
|
|
||||||
defaultResolution = implementation.getTimerResolution();
|
|
||||||
if (DEBUG_CALIBRATION && currentResolution != defaultResolution) {
|
|
||||||
System.err.println("Resetting to system calibration "+defaultResolution+" ticks per second");
|
|
||||||
}
|
|
||||||
currentResolution = defaultResolution;
|
currentResolution = defaultResolution;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue