Inactivity and Idle Time on OS X

OS X has a timer that tracks the last time you moved the mouse, typed a key, or interacted with the computer. This timer is used as the basis of idle time for when your Mac sleeps.

In technical terms, the timer goes by the name HIDIdleTime and is part of IOKit's IOHIDSystem.

We use this timer as the basis of Power Manager's inactivity trigger.

We are careful to use the term inactivity with Power Manager. Apple tend towards using the term idle. The difference between inactivity and idle is subtle but important.

The definition of OS X's idle is not published by Apple. The behaviour of idle has changed between major releases of OS X and thus is difficult to predict or rely upon. In the past idle time has involved a combination of factors, including recent disk activity and network connections.

In comparison, inactivity is the name we give to the period of time since activity was last detected by the HIDIdleTime timer. No other factors are taken into account. This makes the inactivity trigger easy to predict and work with.

Watching the Seconds

I recently helped a customer debug an event that used the inactivity trigger. The event was not triggering as expected.

To help reveal the state of HIDIdleTime I wrote the short script below:

#!/usr/bin/env perl

my $idle_seconds_command = 'echo $((`ioreg -c IOHIDSystem | sed -e \'/HIDIdleTime/ !{ d\' -e \'t\' -e \'}\' -e \'s/.* = //g\' -e \'q\'` / 1000000000))';

print "Counting seconds of inactivity... Command + Period (.) to quit\n\n";

do {

    my $idle_seconds = `$idle_seconds_command`;
    print "Idle for $idle_seconds seconds.\n";


} while(1);

When run this script shows the current idle time. A line is printed once a second showing the current idle value.

Leave the computer alone and the idle seconds tick up. Move the mouse or press a key, and the idle value resets back to zero.

Screenshot showing idle-counter.command running

The central line of the script that extracts the current HDIdleTime value is:

  echo $((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))

Copying and pasting this line into the will almost always result in 0 because pressing enter to execute the command will immediately reset the timer!

Download a copy of the idle-counter.command script. To run the script, control-click the command file and select "Open".

This article was posted in and tagged , and .

Published by Graham Miln on