Fixed up bugs and problems in new timer calibration code.

This commit is contained in:
Caspian Rychlik-Prince 2005-03-08 23:13:50 +00:00
parent 94cb99de64
commit 7d7ec34082
1 changed files with 27 additions and 20 deletions

View File

@ -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,23 +223,35 @@ 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;
recalibrationNow = lowresNow; if (ticksSinceRecalibration < 0) {
long totalTicksSinceRecalibrationStarted = recalibrationNow - recalibrationThen; // Bah. Wrapped timer, so forget it this time and start over.
if (totalTicksSinceRecalibrationStarted > LONG_RECALIBRATION_THRESHOLD) { hiresRecalibrationThen = hiresNow;
// Ok, we've now been operating at a different resolution for long enough. recalibrationThen = lowresNow;
// Let's choose a new default resolution based on the average we've } else {
// calculated since it first started needing recalibrating recalibrationNow = lowresNow;
long actualRecalibrationDuration = recalibrationNow - recalibrationThen; long totalMillisSinceRecalibrationStarted = recalibrationNow - recalibrationThen;
defaultResolution = (long) (1000.0 * totalTicksSinceRecalibrationStarted / actualRecalibrationDuration); if (totalMillisSinceRecalibrationStarted > LONG_RECALIBRATION_THRESHOLD) {
tempResolution = defaultResolution; // Ok, we've now been operating at a different resolution for long enough.
if (DEBUG_CALIBRATION) { // Let's choose a new default resolution based on the average we've
System.err.println("Permanent recalibration to "+defaultResolution+" ticks per second"); // calculated since it first started needing recalibrating
long totalTicksSinceRecalibrationStarted = hiresNow - hiresRecalibrationThen;
if (totalTicksSinceRecalibrationStarted > 0) {
defaultResolution = (long) (1000.0 * totalTicksSinceRecalibrationStarted / totalMillisSinceRecalibrationStarted);
tempResolution = defaultResolution;
if (DEBUG_CALIBRATION) {
System.err.println("Permanent recalibration to "+defaultResolution+" ticks per second");
}
} else {
// Bah. Once again, a wrapped timer.
hiresRecalibrationThen = hiresNow;
recalibrationThen = lowresNow;
}
} }
} }
} }
@ -247,11 +259,6 @@ public final class Sys {
// 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;
} }
} }