From f13f91f06a06f9d290ac6da85417c766112ddccc Mon Sep 17 00:00:00 2001 From: Sonali Srivastava Date: Wed, 17 Aug 2022 12:54:20 +0530 Subject: [PATCH] sleep: freeze and thaw user.slice to save resource while suspend --- src/sleep/sleep.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/sleep/sleep.c b/src/sleep/sleep.c index d8e6380d0a6..3461d3e45f0 100644 --- a/src/sleep/sleep.c +++ b/src/sleep/sleep.c @@ -14,10 +14,13 @@ #include #include +#include "sd-bus.h" #include "sd-messages.h" #include "btrfs-util.h" #include "bus-error.h" +#include "bus-locator.h" +#include "bus-util.h" #include "def.h" #include "exec-util.h" #include "fd-util.h" @@ -29,6 +32,7 @@ #include "parse-util.h" #include "pretty-print.h" #include "sleep-config.h" +#include "special.h" #include "stdio-util.h" #include "string-util.h" #include "strv.h" @@ -349,11 +353,38 @@ static int custom_timer_suspend(const SleepConfig *sleep_config) { return 1; } +/* Freeze when invoked and thaw on cleanup */ +static int freeze_thaw_user_slice(const char **method) { + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; + int r; + + if (!method || !*method) + return 0; + + r = bus_connect_system_systemd(&bus); + if (r < 0) + return log_debug_errno(r, "Failed to open connection to systemd: %m"); + + r = bus_call_method(bus, bus_systemd_mgr, *method, &error, NULL, "s", SPECIAL_USER_SLICE); + if (r < 0) + return log_debug_errno(r, "Failed to execute operation: %s", bus_error_message(&error, r)); + + return 1; +} + static int execute_s2h(const SleepConfig *sleep_config) { + _unused_ _cleanup_(freeze_thaw_user_slice) const char *auto_method_thaw = NULL; int r, k; assert(sleep_config); + r = freeze_thaw_user_slice(&(const char*) { "FreezeUnit" }); + if (r < 0) + log_debug_errno(r, "Failed to freeze unit user.slice, ignoring: %m"); + else + auto_method_thaw = "ThawUnit"; /* from now on we want automatic thawing */; + r = check_wakeup_type(); if (r < 0) log_debug_errno(r, "Failed to check hardware wakeup type, ignoring: %m"); -- 2.47.3