How timely are scheduled events in Sierra?

macOS provides two main mechanisms for running periodic events: Launch Agents, which are readily accessible to users and developers, and a sophisticated system within Grand Central Dispatch (GCD), which is used by many of the services in macOS, such as Time Machine backups. Although developers have limited access to GCD for their applications, users do not.

The old traditional Unix mechanism using cron has long been deprecated, but remains for compatibility. So when you, or a third-party product which you install, want to run periodic services or activities, you will naturally install it as a Launch Agent, by placing a Property List file in /Library/LaunchAgents, or ~/Library/LaunchAgents.

macOS doesn’t run Time Machine backups at particularly regular intervals: if you note the time that these start, or check in your log, you’ll see that it varies within about ten minutes. This is because GCD schedules it to minimise impact on your Mac, and to optimise its power consumption. If you use a third-party app to change the frequency of Time Machine backups, that cannot use the same GCD system, but runs backups as periodic events through the Launch Agent system. They then become much more regular, but can clash with other periodic tasks.

So if you use the Launch Agent system to run periodic tasks, how accurate is their start time? Using a simple log-writing task, my Blowhole tool, I have been watching over a period of 67 hours, and have now analysed the results.

Launch Agents do not use the normal system clock for their timing, but the Mach clock ‘tick’. When you schedule a task to run every hour, it is actually run every 3.6 x 10^12 clock ticks. Normal clock time drifts slowly from that, so the actual clock time is less accurate than that by Mach ticks.

Over a period of 67 hours, my periodic task was run an average of 0.035 seconds after the intended time by Mach ticks, with a standard deviation of 0.022. That means that, if we call the exact intended time T, 95% of events were run in the interval from (T – 0.09) to (T + 0.079) seconds. In other words, periodic tasks are almost always run within 0.1 second of the ‘correct’ time by Mach ticks, with a tendency to be around 0.04 second late.

In the 67 hours that I monitored my Mac (iMac17,1), its normal system clock – that, for example, shown in the menubar – drifted by 1.36 seconds, giving a drift rate of 0.02 seconds per hour. Assuming linear drift, the average time that the task was run was 0.006 seconds earlier than that expected by the drift, with a standard deviation of 0.046. So, using t as the expected time allowing for clock drift, 95% of events were run in the interval from (t – 0.097) to (t + 0.087) seconds. In other words, periodic tasks are almost always run within 0.1 second of the drifted system clock time, with a tendency to be around 0.006 seconds early.

The bottom line is that tasks which are scheduled to run periodically using the Launch Agent system in macOS are almost invariably run within 0.1 seconds of the correct time, whatever that should be. That is far more precise and predictable than load- and demand-based scheduling in GCD, as used for Time Machine backups, and almost good enough to set your watch by.