Two ways to run background tasks: comparing launchd and GCD

Just over a month ago, I published some impressive figures for the timeliness of events scheduled using macOS Sierra’s launchd system. This article provides some comparable results obtained using Grand Central Dispatch’s scheduling system.

As far as I am aware, until the first alpha release of DispatchRider (see Downloads), users were unable to configure their own scheduled tasks for dispatch by Grand Central Dispatch (GCD). The only activity which is readily monitored in the log which does use GCD is the making of Time Machine backups. However, that is an atypical GCD activity, being run by an elaborate chain of services, and is integral to macOS.

To assess the timeliness of a simple activity scheduled by GCD, and compare it against that of launchd, I set my Blowhole log marking tool (also in Downloads) to run at hourly intervals using both mechanisms, distinguishing the scheduler used by the content of the log message. For GCD, I left the tolerance and Quality of Service at the default settings, which equates to a GCD priority of ‘maintenance’.

Over a typical twenty-four hour period of running, in which the iMac17,1 was not allowed to sleep the system, only the display, user activity varied greatly. This was reflected in the GCD dispatch timeliness, which was closest when the Mac was not in use, and most distant when it was being used heavily. No changes were seen in the timeliness of the activity scheduled by launchd. This matches Apple’s descriptions of the performance of the two mechanisms.

Log entries scheduled by launchd occurred at almost the exact same time each hour, with a range in seconds of -0.112 to +0.095, and a standard deviation of 0.053 seconds. This is in keeping with my longer period of observations reported before. launchd results in extremely timely execution of scheduled activities.

Log entries scheduled by GCD occurred over a very wide range of times. They were executed at an average of once hourly, but on average were almost ten minutes later than the earliest scheduled time. They ranged in delay after the scheduled time from a few seconds to 45 minutes, according to computer use at the time, with a standard deviation of around 12 minutes.

The timeliness of activities scheduled using GCD will vary according to the tolerances and priorities accorded them, and concurrent activities both by the user and the system. As Apple does not disclose the heuristics which GCD uses, it is impossible to determine whether these result are within GCD’s expected performance. However they demonstrate that scheduling background activities using GCD – as is now possible with DispatchRider – gets the job done, but individual activities may face substantial delays at times.

GCD seems to do the job that it sets out to achieve: if you want to run a background activity or task at exact times, then launchd is ideal; for all other activities, where it is more important that they do not adversely affect the user, using GCD is preferable.

Example background activities which should be managed by GCD include:

  • making backups (as Time Machine already does),
  • running periodic anti-virus or security checks or scans,
  • synchronising folders with servers or cloud services,
  • data and database maintenance,
  • checking for and installing updates,
  • updating local content from online sources,
  • housekeeping tasks.

Currently third-party products which perform these activities schedule them using launchd, which often affects the user. And don’t we know it!