macOS App Nap

From Lazarus wiki
Revision as of 04:23, 13 February 2021 by Trev (talk | contribs) (Created page with "{{macOS_App_Nap}} {{Warning| Work in progress ...}} == Overview == OS X Mavericks 10.9 introduced substantial power-saving features under the App Nap umbrella, especially f...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

This article applies to macOS only.

See also: Multiplatform Programming Guide

English (en)


Warning: Work in progress ...


OS X Mavericks 10.9 introduced substantial power-saving features under the App Nap umbrella, especially for laptops. App Nap is based on several key principles:

  • The features work without the developer needing to modify existing applications.
  • The features keep the hardware as idle as possible given the demand for resources.
  • When a Mac is on battery power, only the work the user requests or that is absolutely essential is done.

App Nap is activated when:

  • The application's windows are not visible.
  • The application is not playing audio.
  • The developer has not explicitly exempted the application from App Nap by using the IOKit IOPAssertion API.
  • The application is in the background and has not drawn recently.

The App Nap power-saving measures include:

  • Timer throttling: The frequency with which an application's timers are executed is reduced, increasing CPU idle time when running applications that, for example, check for data.
  • I/O throttling: Disk and network activity is assigned the lowest priority for applications that are "napping" thereby reducing the speed at which the application can read/write data from/to a device. This also reduces the likelihood that a napping application will impact an application which is actively being used.
  • Priority reduction: The UNIX process priority oil an application is reduced so that it receives less available CPU time.

Timer coalescing

Timer coalescing, although not an App Nap feature, was introduced in Mavericks at the same time. To maximise the amount of time that the CPU spends at idle, timer coalescing shifts the execution of timers by a small amount so that timers of multiple applications are executed at the same time. This is done by applying a time window to every timer based on the importance of the process. A timer can be executed at any time during this window, so may be shifted backward or forward a small amount so that it lines up with other timers that need to be executed at similar times. The timer windows are:

Process type Timer window
Application (default) 1ms
System daemon 70-90ms
Background process 80-120ms
Critical/Real time process 0ms

The NSTimer class in Mavericks introduced a new tolerance parameter which enables developers to tune how timely timer-event driven events need to be.

Managing App Nap

App Nap introduced a new API in NSProcessInfo which gives developers the ability to inform the operating system when an application is performing a long-running operation that may need to prevent App Nap or system sleep.

NSProcessInfoActivity = objccategory external (NSProcessInfo)
    function beginActivityWithOptions_reason (options: NSActivityOptions; reason: NSString): NSObjectProtocol; message 'beginActivityWithOptions:reason:';
    procedure endActivity (activity: NSObjectProtocol); message 'endActivity:';
    procedure performActivityWithOptions_reason_usingBlock (options: NSActivityOptions; reason: NSString; block: OpaqueCBlock); message 'performActivityWithOptions:reason:usingBlock:';

The NSActivityOptions are:

Option Effect
NSActivityIdleDisplaySleepDisabled require the screen to stay powered on
NSActivityIdleSystemSleepDisabled prevent idle sleep
NSActivitySuddenTerminationDisabled prevent sudden termination
NSActivityAutomaticTerminationDisabled prevent automatic termination
NSActivityUserInitiated indicate the application is performing a user-requested action
NSActivityUserInitiatedAllowingIdleSystemSleep indicate the application is performing a user-requested action, but that the system can sleep on idle
NSActivityBackground indicate the application has initiated some kind of work, but not as the direct result of user request
NSActivityLatencyCritical indicate the activity requires the highest amount of timer and I/O precision available

See also