macOS runs hundreds of services. Some like
logd run in the background all the time, while others are scheduled to run every now and then. The most prominent example of the latter are Time Machine backups performed by
backupd, which usually occur every hour, although you have other options available in Ventura. This article outlines how those background activities are scheduled.
The traditional service for running scheduled background activities in Unix is
cron, and that’s what the first versions of Mac OS X used. But Dave Zarzycki was busy designing and coding a replacement service for
cron and much more:
launchd, which first shipped in Mac OS X 10.4 Tiger, in 2005.
In this new model, one of the early tasks of the kernel after it has been booted is to load
launchd, which then becomes the master launcher and launches everything else, in conjunction with
launchctl and property lists in LaunchDaemons and LaunchAgents folders in Library folders. In replacing
launchd took on the responsibility of running scheduled background activities too.
The first couple of years with
launchd had their moments, as it took a while before it became reliable. Just over two years later, when Apple released Time Machine in Mac OS X 10.5, its hourly backups were scheduled using
launchd. The disadvantage of that was that, like
launchd isn’t flexible on timing: the moment it decides to run a large backup could coincide with intense user activity, resulting in noticeably slowed response and prolonged backup duration.
With OS X 10.10 Yosemite, in 2014, Apple introduced a whole new system designed to schedule background activities more flexibly, Duet Activity Scheduler (DAS). This is related to CoreDuet, a subsystem concerned with monitoring environmental and process load conditions. DAS maintains a list of scheduled activities, and frequently evaluates those activities to determine which should be run next. It does that by scoring them, taking into account the window in which the activity should occur, its priority expressed in terms of Quality of Service (QoS), current load on CPU cores, and environmental conditions.
Those are combined to produce a score: when that exceeds a threshold, DAS decides to run that activity, and will in turn tell Centralized Task Scheduling (CTS) via XPC to do so. Once that’s complete, the next run will be scheduled with DAS, which puts that activity back into its scheduling list. This and other processes are summarised in the following diagram, based on what happens when scheduling routine hourly backups using Time Machine.
Most scheduled background activities in macOS are now managed by this combination of DAS and CTS, which has proved itself to be superior to fixed time intervals managed by
launchd. macOS 10.12 Sierra was an exception, because of a bug, presumably a memory leak, which led to DAS failing to evaluate its list of scheduled activities after several days of continuous running. The unfortunate consequence was that all scheduled activities, including automatic backups, became infrequent or ceased altogether. That wasn’t fixed until 10.13 High Sierra, and remains in Sierra to this day.
In Ventura, it’s not unusual for DAS to be scheduling well over 300 different background processes. Some of these are well-known like Time Machine’s
backupd, others are vaguely familiar like com.apple.appstored.AutoUpdates, but most are obscure like com.apple.proactive.ProactiveHarvesting.Harvest.PeriodicBackground. These are now divided into groups, most notably com.apple.dasd.default which bears the brunt of those in macOS. You can view DAS and CTS at work using the DAS Scheduling log window in my free utility Mints.
Today’s macOS is of course more sophisticated than Yosemite in terms of the environmental assessments made by DAS. These ‘policies’ can now include:
- application, 25.0
- battery level, 1.0
- boot time (how long since the last boot), 0.01
- call in progress (a carry-over from iOS), 2.0
- carplay connected (a carry-over from iOS), 0.01
- CPU usage, 5.0
- energy budget, 1.0
- memory pressure, 5.0
- network quality, 8.4
- power nap, 5.0
- charger connected, 10.0 or 20.0
- thermal, 5.0
where the figure given for each is a typical value of its weighting in the overall scoring system.
As noted above, one of the significant factors when scheduling background activities is the QoS set for that activity. The great majority of these have a low QoS, so that when they’re scheduled to run on an Apple silicon Mac, they do so on the Efficiency (E) cores. That’s why Time Machine backups occur at slightly irregular intervals, and when they do, they occupy the E cores and leave the Performance cores free for the user.
In the coming week or two, I’ll be looking in more detail at DAS in Ventura and its scheduling.
Wikipedia on launchd