| 1 | package com.rox.clock; | |
| 2 | ||
| 3 | import com.rox.time.Sleeper; | |
| 4 | import com.rox.time.TimeSource; | |
| 5 | ||
| 6 | import java.util.ArrayList; | |
| 7 | import java.util.List; | |
| 8 | ||
| 9 | /** | |
| 10 | * A clock which generates {@link Clock} ticket to {@link ClockWatcher}s at the | |
| 11 | * specified <cc>hz</cc> in sub second time frame chunks defined by the specified | |
| 12 | * <cc>framesPerSecond</cc> | |
| 13 | * | |
| 14 | * XXX: This cannot deal with fractional FPS such as that of the GameBoy | |
| 15 | */ | |
| 16 | public class FPSClock implements Clock, AutoCloseable { | |
| 17 | /** Number of clock ticks per second **/ | |
| 18 | private final long HZ; | |
| 19 | /** Number of frames per second at the user level */ | |
| 20 | private final int FPS; | |
| 21 | /** The size of a frame in nanoseconds */ | |
| 22 | private final long FRAME_TIME_NS; | |
| 23 | /** | |
| 24 | * How far behind schedule the clock may fall and still catch up by running frames back to back. | |
| 25 | * Beyond this the extra lost time is dropped, as real hardware would: a slow stretch plays slowly, | |
| 26 | * then carries on at normal speed rather than fast-forwarding through everything it missed. | |
| 27 | */ | |
| 28 | static final int MAX_CATCH_UP_FRAMES = 2; | |
| 29 | /** The number of approximate ticks per frame */ | |
| 30 | private final long TICKS_PER_FRAME; | |
| 31 | ||
| 32 | private final long TICKS_REMAINDER_PER_FRAME; | |
| 33 | private final TimeSource timeSource; | |
| 34 | private final Sleeper sleeper; | |
| 35 | ||
| 36 | /** List of subscribers to this {@link Clock}s <cc>tick()</cc> events **/ | |
| 37 | private final List<ClockWatcher> listeners = new ArrayList<>(); | |
| 38 | ||
| 39 | private volatile boolean running = false; | |
| 40 | ||
| 41 | //pause()/resume() handshake with run()'s loop - both guarded by pauseLock. pauseRequested is also | |
| 42 | //volatile so run() can check it every tick without taking the lock in the common, unpaused case | |
| 43 | private final Object pauseLock = new Object(); | |
| 44 | private volatile boolean pauseRequested = false; | |
| 45 | private boolean parked = false; | |
| 46 | /** {@link #prepareToRun()} called and {@link #run()} not yet begun - guarded by pauseLock. */ | |
| 47 | private boolean runPrepared = false; | |
| 48 | /** A {@link #stop()} arrived while {@link #runPrepared} - guarded by pauseLock. */ | |
| 49 | private boolean runCancelled = false; | |
| 50 | /** {@link #run()} is executing - guarded by pauseLock. */ | |
| 51 | private boolean inRun = false; | |
| 52 | ||
| 53 | /** See {@link #setSpeed} - read by the run loop, so volatile. */ | |
| 54 | private volatile double speed = 1; | |
| 55 | /** How long a frame takes at the current {@link #speed}; the ticks in it don't change. */ | |
| 56 | private volatile long frameTimeNs; | |
| 57 | ||
| 58 | /** Current total of remainder ticks after rounding */ | |
| 59 | private long tickRemainderBuffer = 0; | |
| 60 | ||
| 61 | public FPSClock(final long hz, | |
| 62 | final int framesPerSecond, | |
| 63 | final TimeSource timeSource, | |
| 64 | final Sleeper sleeper){ | |
| 65 | HZ = hz; | |
| 66 | FPS = framesPerSecond; | |
| 67 |
1
1. <init> : Replaced long division with multiplication → TIMED_OUT |
FRAME_TIME_NS = 1_000_000_000L / FPS; |
| 68 |
1
1. <init> : Replaced long division with multiplication → KILLED |
TICKS_PER_FRAME = HZ / FPS; |
| 69 |
1
1. <init> : Replaced long modulus with multiplication → KILLED |
TICKS_REMAINDER_PER_FRAME = HZ % FPS; |
| 70 | frameTimeNs = FRAME_TIME_NS; | |
| 71 | ||
| 72 | this.timeSource = timeSource; | |
| 73 | this.sleeper = sleeper; | |
| 74 | } | |
| 75 | ||
| 76 | /** | |
| 77 | * Keeps track fo fractional ticks while returning the rounded current set of ticks | |
| 78 | * | |
| 79 | * XXX this is package private to test but it modifies state (tickRemainderBuffer) | |
| 80 | * | |
| 81 | * @return the number of ticks this frame | |
| 82 | */ | |
| 83 | long ticksThisFrame(){ | |
| 84 | long ticks = TICKS_PER_FRAME; | |
| 85 |
1
1. ticksThisFrame : Replaced long addition with subtraction → KILLED |
tickRemainderBuffer += TICKS_REMAINDER_PER_FRAME; |
| 86 |
2
1. ticksThisFrame : changed conditional boundary → KILLED 2. ticksThisFrame : negated conditional → KILLED |
if (tickRemainderBuffer >= FPS) { |
| 87 |
1
1. ticksThisFrame : Replaced long addition with subtraction → KILLED |
ticks++; |
| 88 |
1
1. ticksThisFrame : Replaced long subtraction with addition → KILLED |
tickRemainderBuffer -= FPS; |
| 89 | } | |
| 90 |
1
1. ticksThisFrame : replaced long return with 0 for com/rox/clock/FPSClock::ticksThisFrame → KILLED |
return ticks; |
| 91 | } | |
| 92 | ||
| 93 | /** | |
| 94 | * Start the execution of this {@link Clock} | |
| 95 | */ | |
| 96 | public void run(){ | |
| 97 | synchronized (pauseLock){ | |
| 98 |
1
1. run : negated conditional → KILLED |
if (running){ |
| 99 | throw new IllegalStateException("Clock already running"); | |
| 100 | } | |
| 101 | final boolean cancelled = runCancelled; | |
| 102 | runPrepared = false; | |
| 103 | runCancelled = false; | |
| 104 |
1
1. run : negated conditional → TIMED_OUT |
if (cancelled){ |
| 105 | return; | |
| 106 | } | |
| 107 | running = true; | |
| 108 | inRun = true; | |
| 109 | } | |
| 110 | try { | |
| 111 |
1
1. run : removed call to com/rox/clock/FPSClock::runLoop → KILLED |
runLoop(); |
| 112 | } finally { | |
| 113 | //however the loop ended (stop(), an interrupt, a listener throwing), release a pause() or | |
| 114 | //stopAndWait() still waiting on it | |
| 115 | synchronized (pauseLock){ | |
| 116 | inRun = false; | |
| 117 |
1
1. run : removed call to com/rox/clock/FPSClock::stop → KILLED |
stop(); |
| 118 | } | |
| 119 | } | |
| 120 | } | |
| 121 | ||
| 122 | private void runLoop(){ | |
| 123 | //anchored to a single fixed reference point (runStartTime), not re-measured fresh each | |
| 124 | //frame - Thread.sleep() (via Sleeper) routinely oversleeps its requested duration by tens to | |
| 125 | //hundreds of microseconds (OS scheduler granularity), and a fresh-each-frame measurement | |
| 126 | //never recovers that lost time, so it accumulates: ~10ms/s of real-time drift measured | |
| 127 | //empirically, enough to exhaust a 200ms audio buffer margin in about 20 seconds. Anchoring | |
| 128 | //each frame's deadline to runStartTime + frameIndex*FRAME_TIME_NS means an oversleep on one | |
| 129 | //frame simply shortens (or skips) the next frame's sleep, so the schedule self-corrects | |
| 130 | //instead of drifting indefinitely. How far it may fall behind is bounded, though - see | |
| 131 | //MAX_CATCH_UP_FRAMES and throttle(). | |
| 132 | long frameStartTime = timeSource.nanoTime(); | |
| 133 | ||
| 134 |
1
1. runLoop : negated conditional → KILLED |
while (running) { |
| 135 | //XXX This could be wrapped in a executeWithinFrame(()->{}) | |
| 136 |
1
1. runLoop : removed call to com/rox/clock/FPSClock::runFrame → TIMED_OUT |
runFrame(); |
| 137 | try { | |
| 138 |
1
1. runLoop : negated conditional → TIMED_OUT |
if (parkWhilePaused()){ |
| 139 | //however long the pause lasted, it isn't time the schedule should try to catch up | |
| 140 | //on - re-anchor rather than racing through a burst of unthrottled frames | |
| 141 | frameStartTime = timeSource.nanoTime(); | |
| 142 | continue; | |
| 143 | } | |
| 144 | frameStartTime = throttle(frameStartTime); | |
| 145 | } catch (InterruptedException e) { | |
| 146 |
1
1. runLoop : removed call to java/io/PrintStream::println → SURVIVED |
/* LOG */System.out.println("Encountered issues using Thread.sleep(), terminating clock!"); |
| 147 | running = false; | |
| 148 | } | |
| 149 | } | |
| 150 | } | |
| 151 | ||
| 152 | /** | |
| 153 | * Parks the {@link #run()} loop (between frames, never mid-frame) for as long as a {@link #pause()} | |
| 154 | * is in effect. | |
| 155 | * | |
| 156 | * @return true if it parked at all | |
| 157 | */ | |
| 158 | private boolean parkWhilePaused() throws InterruptedException { | |
| 159 |
1
1. parkWhilePaused : negated conditional → TIMED_OUT |
if (!pauseRequested){ |
| 160 |
1
1. parkWhilePaused : replaced boolean return with true for com/rox/clock/FPSClock::parkWhilePaused → TIMED_OUT |
return false; |
| 161 | } | |
| 162 | synchronized (pauseLock){ | |
| 163 | parked = true; | |
| 164 |
1
1. parkWhilePaused : removed call to java/lang/Object::notifyAll → TIMED_OUT |
pauseLock.notifyAll(); //release pause()'s wait for this loop to genuinely stop ticking |
| 165 | try { | |
| 166 | //no separate running check needed: stop() clears pauseRequested under this same lock | |
| 167 |
1
1. parkWhilePaused : negated conditional → TIMED_OUT |
while (pauseRequested){ |
| 168 |
1
1. parkWhilePaused : removed call to java/lang/Object::wait → TIMED_OUT |
pauseLock.wait(); |
| 169 | } | |
| 170 | } finally { | |
| 171 | parked = false; | |
| 172 | } | |
| 173 | } | |
| 174 |
1
1. parkWhilePaused : replaced boolean return with false for com/rox/clock/FPSClock::parkWhilePaused → KILLED |
return true; |
| 175 | } | |
| 176 | ||
| 177 | /** | |
| 178 | * Execute all <cc>tick()</cc>s in this frame - or fewer, stopping after the tick in progress when a pause is asked for | |
| 179 | */ | |
| 180 | void runFrame() { | |
| 181 | final long ticks = ticksThisFrame(); | |
| 182 |
4
1. runFrame : negated conditional → TIMED_OUT 2. runFrame : negated conditional → TIMED_OUT 3. runFrame : Replaced long addition with subtraction → TIMED_OUT 4. runFrame : changed conditional boundary → KILLED |
for (long i = 0; i < ticks && !pauseRequested; i++){ |
| 183 |
1
1. runFrame : removed call to com/rox/clock/FPSClock::tick → TIMED_OUT |
tick(); |
| 184 | } | |
| 185 | } | |
| 186 | ||
| 187 | /** | |
| 188 | * Slow the rate of generating tickets by consuming the rest of the frame | |
| 189 | * | |
| 190 | * @param frameStartTime when this frame was scheduled to start | |
| 191 | * @return when the next frame is scheduled to start: one frame on, unless that is more than | |
| 192 | * {@link #MAX_CATCH_UP_FRAMES} behind now, in which case the excess is forgiven. A frame is as long | |
| 193 | * as the current {@link #setSpeed speed} makes it, so a speed change applies from the next frame. | |
| 194 | */ | |
| 195 | long throttle(final long frameStartTime) throws InterruptedException { | |
| 196 | final long frameTime = frameTimeNs; | |
| 197 | long now = timeSource.nanoTime(); | |
| 198 |
1
1. throttle : Replaced long addition with subtraction → KILLED |
final long nextFrameStartTime = frameStartTime + frameTime; |
| 199 |
1
1. throttle : Replaced long subtraction with addition → KILLED |
final long timeRemainingInFrame = nextFrameStartTime - now; |
| 200 | ||
| 201 |
2
1. throttle : changed conditional boundary → KILLED 2. throttle : negated conditional → KILLED |
if (timeRemainingInFrame > 0) { |
| 202 | ||
| 203 | //XXX Do we want to just throw the exception here? | |
| 204 |
1
1. throttle : removed call to com/rox/time/Sleeper::sleepFor → KILLED |
sleeper.sleepFor(timeRemainingInFrame); |
| 205 | //a sleep can overrun by whole frames (a descheduled thread, a suspended machine) - measure | |
| 206 | //the debt after it, or that overrun escapes the catch-up limit below | |
| 207 | now = timeSource.nanoTime(); | |
| 208 | } | |
| 209 |
3
1. throttle : Replaced long subtraction with addition → KILLED 2. throttle : Replaced long multiplication with division → KILLED 3. throttle : replaced long return with 0 for com/rox/clock/FPSClock::throttle → KILLED |
return Math.max(nextFrameStartTime, now - MAX_CATCH_UP_FRAMES * frameTime); |
| 210 | } | |
| 211 | ||
| 212 | /** Paces frames {@code multiplier} times faster (or slower) than real time; each frame still runs the same ticks. */ | |
| 213 | @Override | |
| 214 | public void setSpeed(final double multiplier){ | |
| 215 |
3
1. setSpeed : negated conditional → KILLED 2. setSpeed : changed conditional boundary → KILLED 3. setSpeed : negated conditional → KILLED |
if (!(multiplier > 0) || Double.isInfinite(multiplier)){ |
| 216 | throw new IllegalArgumentException("Speed must be a positive number, got " + multiplier); | |
| 217 | } | |
| 218 | speed = multiplier; | |
| 219 |
1
1. setSpeed : Replaced double division with multiplication → KILLED |
frameTimeNs = Math.round(FRAME_TIME_NS / multiplier); |
| 220 | } | |
| 221 | ||
| 222 | @Override | |
| 223 | public double speed(){ | |
| 224 |
1
1. speed : replaced double return with 0.0d for com/rox/clock/FPSClock::speed → KILLED |
return speed; |
| 225 | } | |
| 226 | ||
| 227 | /** | |
| 228 | * Stop the execution of this {@link Clock} | |
| 229 | */ | |
| 230 | public void stop(){ | |
| 231 | synchronized (pauseLock){ | |
| 232 | //inside the lock: run() checks for a cancel and sets running under it too | |
| 233 | running = false; | |
| 234 | runCancelled = runPrepared; | |
| 235 | //a stale request would otherwise park a later run() straight away with nobody to resume it | |
| 236 | pauseRequested = false; | |
| 237 |
1
1. stop : removed call to java/lang/Object::notifyAll → TIMED_OUT |
pauseLock.notifyAll(); //release a parked run() loop, and a pause() still waiting on one |
| 238 | } | |
| 239 | } | |
| 240 | ||
| 241 | /** | |
| 242 | * {@link #stop()}, then blocks until {@link #run()} has returned, so nothing ticks after - waiting out | |
| 243 | * interrupts, like {@link #pause()}. Never call it from a {@link ClockWatcher}: that's {@code run()}'s own thread. | |
| 244 | */ | |
| 245 | @Override | |
| 246 | public void stopAndWait(){ | |
| 247 | boolean interrupted = false; | |
| 248 | synchronized (pauseLock){ | |
| 249 |
1
1. stopAndWait : removed call to com/rox/clock/FPSClock::stop → TIMED_OUT |
stop(); |
| 250 |
1
1. stopAndWait : negated conditional → TIMED_OUT |
while (inRun){ |
| 251 | try { | |
| 252 |
1
1. stopAndWait : removed call to java/lang/Object::wait → TIMED_OUT |
pauseLock.wait(); |
| 253 | } catch (InterruptedException e){ | |
| 254 | interrupted = true; | |
| 255 | } | |
| 256 | } | |
| 257 | } | |
| 258 |
1
1. stopAndWait : negated conditional → KILLED |
if (interrupted){ |
| 259 |
1
1. stopAndWait : removed call to java/lang/Thread::interrupt → KILLED |
Thread.currentThread().interrupt(); |
| 260 | } | |
| 261 | } | |
| 262 | ||
| 263 | @Override | |
| 264 | public void prepareToRun(){ | |
| 265 | synchronized (pauseLock){ | |
| 266 | runPrepared = true; | |
| 267 | runCancelled = false; | |
| 268 | } | |
| 269 | } | |
| 270 | ||
| 271 | /** | |
| 272 | * Freezes {@link #run()} after the tick in progress, blocking until it's genuinely parked. Like | |
| 273 | * {@code NES.powerOn()}, waits out interrupts rather than returning early - the caller relies on a | |
| 274 | * {@code true} meaning nothing else is ticking. | |
| 275 | */ | |
| 276 | @Override | |
| 277 | public boolean pause(){ | |
| 278 | boolean interrupted = false; | |
| 279 | final boolean paused; | |
| 280 | synchronized (pauseLock){ | |
| 281 |
1
1. pause : removed call to com/rox/clock/FPSClock::requestPause → KILLED |
requestPause(); |
| 282 |
2
1. pause : negated conditional → TIMED_OUT 2. pause : negated conditional → KILLED |
while (pauseRequested && !parked){ |
| 283 | try { | |
| 284 |
1
1. pause : removed call to java/lang/Object::wait → TIMED_OUT |
pauseLock.wait(); |
| 285 | } catch (InterruptedException e){ | |
| 286 | interrupted = true; | |
| 287 | } | |
| 288 | } | |
| 289 | paused = parked; | |
| 290 | } | |
| 291 |
1
1. pause : negated conditional → KILLED |
if (interrupted){ |
| 292 |
1
1. pause : removed call to java/lang/Thread::interrupt → KILLED |
Thread.currentThread().interrupt(); |
| 293 | } | |
| 294 |
2
1. pause : replaced boolean return with false for com/rox/clock/FPSClock::pause → KILLED 2. pause : replaced boolean return with true for com/rox/clock/FPSClock::pause → KILLED |
return paused; |
| 295 | } | |
| 296 | ||
| 297 | @Override | |
| 298 | public void requestPause(){ | |
| 299 | synchronized (pauseLock){ | |
| 300 |
1
1. requestPause : negated conditional → TIMED_OUT |
if (running){ |
| 301 | pauseRequested = true; | |
| 302 | } | |
| 303 | } | |
| 304 | } | |
| 305 | ||
| 306 | @Override | |
| 307 | public void resume(){ | |
| 308 | synchronized (pauseLock){ | |
| 309 | pauseRequested = false; | |
| 310 |
1
1. resume : removed call to java/lang/Object::notifyAll → TIMED_OUT |
pauseLock.notifyAll(); |
| 311 | } | |
| 312 | } | |
| 313 | ||
| 314 | public boolean isRunning(){ | |
| 315 |
2
1. isRunning : replaced boolean return with false for com/rox/clock/FPSClock::isRunning → TIMED_OUT 2. isRunning : replaced boolean return with true for com/rox/clock/FPSClock::isRunning → KILLED |
return running; |
| 316 | } | |
| 317 | ||
| 318 | public void addListener(final ClockWatcher listener){ | |
| 319 | this.listeners.add(listener); | |
| 320 | } | |
| 321 | ||
| 322 | public void removeListener(final ClockWatcher listener){ | |
| 323 | this.listeners.remove(listener); | |
| 324 | } | |
| 325 | ||
| 326 | public void tick(){ | |
| 327 |
1
1. tick : removed call to java/util/List::forEach → TIMED_OUT |
listeners.forEach(ClockWatcher::tick); |
| 328 | } | |
| 329 | ||
| 330 | public int listeners(){ | |
| 331 |
1
1. listeners : replaced int return with 0 for com/rox/clock/FPSClock::listeners → KILLED |
return listeners.size(); |
| 332 | } | |
| 333 | ||
| 334 | @Override | |
| 335 | public void close() throws Exception { | |
| 336 |
1
1. close : removed call to com/rox/clock/FPSClock::stop → SURVIVED |
stop(); |
| 337 | } | |
| 338 | } | |
Mutations | ||
| 67 |
1.1 |
|
| 68 |
1.1 |
|
| 69 |
1.1 |
|
| 85 |
1.1 |
|
| 86 |
1.1 2.2 |
|
| 87 |
1.1 |
|
| 88 |
1.1 |
|
| 90 |
1.1 |
|
| 98 |
1.1 |
|
| 104 |
1.1 |
|
| 111 |
1.1 |
|
| 117 |
1.1 |
|
| 134 |
1.1 |
|
| 136 |
1.1 |
|
| 138 |
1.1 |
|
| 146 |
1.1 |
|
| 159 |
1.1 |
|
| 160 |
1.1 |
|
| 164 |
1.1 |
|
| 167 |
1.1 |
|
| 168 |
1.1 |
|
| 174 |
1.1 |
|
| 182 |
1.1 2.2 3.3 4.4 |
|
| 183 |
1.1 |
|
| 198 |
1.1 |
|
| 199 |
1.1 |
|
| 201 |
1.1 2.2 |
|
| 204 |
1.1 |
|
| 209 |
1.1 2.2 3.3 |
|
| 215 |
1.1 2.2 3.3 |
|
| 219 |
1.1 |
|
| 224 |
1.1 |
|
| 237 |
1.1 |
|
| 249 |
1.1 |
|
| 250 |
1.1 |
|
| 252 |
1.1 |
|
| 258 |
1.1 |
|
| 259 |
1.1 |
|
| 281 |
1.1 |
|
| 282 |
1.1 2.2 |
|
| 284 |
1.1 |
|
| 291 |
1.1 |
|
| 292 |
1.1 |
|
| 294 |
1.1 2.2 |
|
| 300 |
1.1 |
|
| 310 |
1.1 |
|
| 315 |
1.1 2.2 |
|
| 327 |
1.1 |
|
| 331 |
1.1 |
|
| 336 |
1.1 |