sleep: add HibernateOnACPower= option (#33846)

* Add HibernateOnACPower= systemd-sleep configuration option
This commit is contained in:
Marc Reisner 2024-08-04 18:01:34 -05:00 committed by GitHub
parent bd5b586ad0
commit 2d00f4c306
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 31 additions and 2 deletions

View File

@ -227,6 +227,23 @@
</listitem>
</varlistentry>
<varlistentry>
<term><varname>HibernateOnACPower=</varname></term>
<listitem>
<para>Whether to allow hibernation when the system has AC power. Only used by
<citerefentry><refentrytitle>systemd-suspend-then-hibernate.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>
when <varname>HibernateDelaySec=</varname> is set.</para>
<para>If this option is disabled, the countdown of <varname>HibernateDelaySec=</varname> starts only
after AC power is disconnected, keeping the system in the suspend state otherwise.</para>
<para>This option is only effective on systems with a battery.</para>
<xi:include href="version-info.xml" xpointer="v257"/>
</listitem>
</varlistentry>
<varlistentry>
<term><varname>SuspendEstimationSec=</varname></term>

View File

@ -124,7 +124,8 @@ int parse_sleep_config(SleepConfig **ret) {
return log_oom();
*sc = (SleepConfig) {
.hibernate_delay_usec = USEC_INFINITY,
.hibernate_delay_usec = USEC_INFINITY,
.hibernate_on_ac_power = true,
};
const ConfigTableItem items[] = {
@ -145,6 +146,7 @@ int parse_sleep_config(SleepConfig **ret) {
{ "Sleep", "MemorySleepMode", config_parse_sleep_mode, 0, &sc->mem_modes },
{ "Sleep", "HibernateDelaySec", config_parse_sec, 0, &sc->hibernate_delay_usec },
{ "Sleep", "HibernateOnACPower", config_parse_bool, 0, &sc->hibernate_on_ac_power },
{ "Sleep", "SuspendEstimationSec", config_parse_sec, 0, &sc->suspend_estimation_usec },
{}
};

View File

@ -33,6 +33,7 @@ typedef struct SleepConfig {
char **mem_modes; /* /sys/power/mem_sleep */
usec_t hibernate_delay_usec;
bool hibernate_on_ac_power;
usec_t suspend_estimation_usec;
} SleepConfig;

View File

@ -390,7 +390,15 @@ static int custom_timer_suspend(const SleepConfig *sleep_config) {
}
}
/* Do not suspend more than HibernateDelaySec= */
/* Do not suspend more than HibernateDelaySec= unless HibernateOnACPower=no and currently on AC power */
if (!sleep_config->hibernate_on_ac_power) {
/* Do not allow "decay" to suspend if the system has no battery. */
if (hashmap_isempty(last_capacity))
log_once(LOG_WARNING, "HibernateOnACPower=no was ignored because the system does not have a battery.");
else if (on_ac_power() > 0)
hibernate_timestamp = usec_add(now(CLOCK_BOOTTIME), sleep_config->hibernate_delay_usec);
}
usec_t before_timestamp = now(CLOCK_BOOTTIME);
suspend_interval = MIN(suspend_interval, usec_sub_unsigned(hibernate_timestamp, before_timestamp));
if (suspend_interval <= 0)

View File

@ -25,4 +25,5 @@
#HibernateMode=platform shutdown
#MemorySleepMode=
#HibernateDelaySec=
#HibernateOnACPower=yes
#SuspendEstimationSec=60min