How does ubuntu determine inactivity before suspending?

Solution 1:

Good question. Let's find out!

  1. Beginning by investigating the "Suspend when inactive for" option,

    <property ...>Suspend when inactive for</property> ...
    <object ... id="combobox_sleep_ac">
    

    we can learn that it sets a GSettings key called sleep-inactive-ac-timeout:

    widget = GTK_WIDGET (gtk_builder_get_object (..., "combobox_sleep_ac")); ...
    g_object_set_data (G_OBJECT(widget), "_gsettings_key", "sleep-inactive-ac-timeout");
    

    The documentation for this key provides a brief description:

    The amount of time in seconds the computer on AC power needs to be inactive before it goes to sleep. A value of 0 means never.

    but still doesn't explain what "inactive" means.

  2. Searching for sleep-inactive-ac-timeout leads us to GNOME Settings Daemon,

    timeout_sleep = g_settings_get_int (..., "sleep-inactive-ac-timeout");
    

    which periodically checks a property of GNOME Session called Presence.status:

    result = g_dbus_proxy_get_cached_property (...->session_presence_proxy, "status");
    

    If it finds that the status is idle, it puts the system to sleep:

    idle_set_mode (..., GSD_POWER_IDLE_MODE_SLEEP);
    

    So we need to learn how GNOME Session decides whether the system is "idle."

  3. Following backwards from where GNOME Session updates the value of Presence.status,

    gsm_presence_set_status (presence, GSM_PRESENCE_STATUS_IDLE, ...);
    

    we can see that it uses the IDLETIME counter from Xorg:

    if (... && strcmp (counters[i].name, "IDLETIME") == 0) {
        ...->counter = counters[i].counter;
    
  4. The IDLETIME counter's behavior is summarized in a blog post by the author of GNOME Power Manager:

    gnome-power-manager uses a counter inside Xorg called IDLETIME. This counter is incremented only when the user does not move the mouse, or click some keys. When the user clicks something, the IDLECOUNTER is reset.

This tells us that Ubuntu determines inactivity by measuring the amount of time that has passed since the last keystroke or mouse motion. CPU usage and network activity do not factor in.