CueTimer is a an application for Mac and Windows that helps people stay on schedule. You should expect this app to show the time precisely and reliably. In this text I want to discuss how we approach this issue from a technical point of view, and show some lessons that we have learned along the way:
We can’t do everything ourselves inside the software. We have to rely on other timing-devices that keeps the world in sync.
Part 1: How to create an accurate software countdown
An oscillator inside the computer creates ticks. These are electronical pulses that happen at a fixed interval. The concept of time inside a computer comes from counting these ticks, and converting them to a readable time-format.
If the computer is a musical orchestra, then the Oscillator is the drummer. It creates the rhythm that keeps the rest of the computer in sync: without the oscillator, the CPU would be lost.
Counting the ticks
When we tried to make our first countdown-timer, we used the simplest approach we could think of: a script counted the ticks created by the internal oscillator, and this was subtracted from the total duration. The result of this calculation was the countdown.
It seemed like an easy task to solve for our software. But to our surprise the countdowns created didn’t follow the accurate time displayed by our reference clocks: Our countdown would always run a little bit slower. Not much, maybe on an average 1 second offset every 10 minutes, but still unacceptable for a software aimed at accurate timings. And the longer the countdown lasted, the worse was the offset.
So we tried writing better code and run the program from different machines, with the same results. It was a little bit better with faster computers, but the offset was always there.
Then we realized that a software will never be able to count all the ticks generated from the oscillator inside the computer.
It’s a bit like asking your friend if he can count all the cars passing by the street: He can do it with one eye closed. But at some point the phone will ring, a cute girl passes by, or something else distracts his attention. Sooner or later he will miss some of the cars.
With a software there are a lot of distractions too. Even with a very simple countdown-application, the OS will sometimes ask the software to do something else, and take the eyes off the ticks for a little while. Or maybe the computer is run so hard that Windows decides that the CPU-power must be used for more important stuff. Whatever the reason, it is impossible for a software to count all the ticks, forever. When a tick is missed, the timer is delayed. And the longer the timer lasts, the more delay is accumulated. A clock that runs inside the software can never be completely accurate because there are too many things that disturbs the process.
Count on the System Clock
Luckily for us we don’t have to count ticks from the software itself. Instead we can use the System Clock. This is a device inside the computer that runs independently from the operating-system, it even runs when the computer is turned off! When you turn on the computer after it has been off for a while, it still shows the correct time of the day. That’s because the System Clock has its own battery-supply to keep it self-preserved while the computer is off.
The system-clock has one job only, and it does this extremely well: It counts the ticks created by the computer-oscillator and turns this into values that we can read as time. And although it hates disturbances, the clock can answer one simple question: “What time is it?”
The answer to this is all we need to create precise countdown-timers for Mac and Windows.
Countdowns in CueTimer
In CueTimer, when we create a countdown of 10 minutes, what happens is this:
- When we start the countdown, we ask the system-clock what time it is. From this answer we add 10 minutes, and get the End Time. Then we repeatedly ask the system-clock the same question again, and we can call this value the Current Time. The countdown that is displayed is then: End Time – Current Time
- When the End Time equals Current Time, the countdown has reached its end.
This approach gives the most accurate and robust countdown: Just like with counting ticks, we can’t completely trust that our software always remembers to do its job. But if CueTimer fails to ask the system-clock what time it is, then the countdown is only delayed until the next time the process succeeds. So, for example, if you want to use the computer for Bitcoin-mining while running CueTimer (not a very good idea) and the computer is almost running out of processing-power, CueTimer might respond slowly, and countdown might appear choppy on the output-windows, but the synchronization is not lost: When you turn off the mining and the computer can breathe again, the countdown continues as if nothing has happened. This is because it still uses the same End Time that we use to calculate the countdown.
Part 2: Sending a countdown over the Internet
With CueTimer you can send the countdown to a remote computer over the Internet. Then a speaker in Australia can view the countdown on an Internet browser, while the CueTimer operator sits in Norway and controls the timer.
Sending the countdown over the Internet, and creating a countdown just for one computer, is not that different: The most important data that we share is still the End Time, which we have in UTC-format so that the timezone is irrelevant.
This value is sent to all the remote machines, and used to generate the countdown. The countdown that is shown is a little script that runs inside the remote browser. This means that Internet can fail and the countdown will still run on the remote machine, as long as the browser has all the data it needs to generate the countdown.
Syncronize the clocks
So the method with sharing End Time is very robust, and it doesn’t require much data. But it has one great challenge: The clocks on the CueTimer host-computer, and the remote computer viewing the countdown, must be synchronized: If the current time on these two systems are not the same, then the countdown won’t show the correct time.
When we started developing the web-countdown, we didn’t trust that the clocks on the different computers would be synchronized. So we tried ways of synchronizing these clocks ourselves, by finding the exact time it took for the commands to travel from CueTimer to the remote computer. But this turned out to be a frustrating task: No matter how hard we tried, we couldn’t get the accuracy down to less than 1 second.
Then we tried not to use any custom synchronization at all, and just let the computers take care of the synchronization . To our surprise the accuracy immediately became much better. What we realized was the clocks on modern computers are quite reliable as long as the computer is connected to the Internet. And we argued that it’s not necessarily our responsibility to keep our customers clocks in sync: If I am 5 minutes late for a meeting, can I come up with the excuse that my watch was not synchronized? Probably not. So we feel that the same can be said about CueTimer: The remote speaker that views the timer on his/her browser should be responsible for using clocks that are in sync.
Although this sounds like a big responsibility to put on the users of CueTimer, the reality is that most people never have to worry about their clocks being in sync or not. Today, this happens automatically on every computer, tablet and laptop. If this method becomes unreliable, there will be much greater consequences than CueTimer showing the incorrect time: Without accurate clocks, the world will be in chaos.
Luckily for us, this is not a big threat. The method for synchronizing clocks over the Internet, which is almost older than Internet itself, works very well.
Syncronizing clocks over the Internet with NTP servers
There are clocks that are much more accurate than the system-clock that runs inside your computer. Some of them are connected in a network of NTP-servers. Every modern computer can connect to this servers to update the clocks. This usually happens automatically. If the speed of the internal oscillator runs different than the more accurate NTP-server, then this will be corrected every time the computer connects to the server. Usually this drift is never more than one second.
Summary: Keeping CueTimer in sync
For CueTimer to show the correct time, it needs to rely on the oscillator that runs inside the computer, and the online NTP timer-servers which the computer is connected to.
Nowadays, these issues shouldn’t keep you awake at night: On modern computers the internal oscillators work very well, and the world has enough NTP servers to feed any computer in most parts of the world.
However, if need the absolutely best performance and accuracy, there are some steps you can take:
- Try to use a computer with the best possible internal oscillator
- You can buy your own hardware NTP-server that all your computers are connected to.
But probably these steps are overkill for most users. I am sure there things that has higher priority, like remembering to start the countdown when the speaker starts. (I always seem to forget this myself!) Besides, if you feel that the countdown is not going where you want it, you can also speed up and down the countdown to make it end on time. This is not the most scientific approach, but in some situations this can be the easiest way to stay on schedule using CueTimer.