a4b143
From 3559039a97e1d3e28dd9b38202d3499652a58036 Mon Sep 17 00:00:00 2001
a4b143
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
a4b143
Date: Fri, 13 Sep 2013 19:41:52 -0400
a4b143
Subject: [PATCH] Advertise hibernation only if there's enough free swap
a4b143
a4b143
Condition that is checked is taken from upower:
a4b143
  active(anon) < free swap * 0.98
a4b143
a4b143
This is really stupid, because the kernel knows the situation better,
a4b143
e.g. there could be two swap files, and then hibernation would be
a4b143
impossible despite passing this check, or the kernel could start
a4b143
supporting compressed swap and/or compressed hibernation images, and
a4b143
then this this check would be too stringent. Nevertheless, until
a4b143
we have something better, this should at least return a true negative
a4b143
if there's no swap.
a4b143
a4b143
Logging of capabilities in the journal is changed to not strip leading
a4b143
zeros. I consider this more readable anyway.
a4b143
a4b143
http://cgit.freedesktop.org/upower/tree/src/up-daemon.c#n613
a4b143
https://bugzilla.redhat.com/show_bug.cgi?id=1007059
a4b143
---
a4b143
 src/shared/fileio.c       | 34 ++++++++++++++++++++++++++++++++++
a4b143
 src/shared/fileio.h       |  2 ++
a4b143
 src/shared/logs-show.c    |  2 +-
a4b143
 src/shared/sleep-config.c | 45 ++++++++++++++++++++++++++++++++++++++++++++-
a4b143
 src/shared/util.c         | 23 +----------------------
a4b143
 src/test/test-fileio.c    | 20 ++++++++++++++++++++
a4b143
 src/test/test-sleep.c     | 16 ++++++++--------
a4b143
 7 files changed, 110 insertions(+), 32 deletions(-)
a4b143
a4b143
diff --git a/src/shared/fileio.c b/src/shared/fileio.c
a4b143
index 77fd059..4e2b444 100644
a4b143
--- a/src/shared/fileio.c
a4b143
+++ b/src/shared/fileio.c
a4b143
@@ -648,3 +648,37 @@ int executable_is_script(const char *path, char **interpreter) {
a4b143
         *interpreter = ans;
a4b143
         return 1;
a4b143
 }
a4b143
+
a4b143
+/**
a4b143
+ * Retrieve one field from a file like /proc/self/status.
a4b143
+ * pattern should start with '\n' and end with ':'. Whitespace
a4b143
+ * after ':' will be skipped. field must be freed afterwards.
a4b143
+ */
a4b143
+int get_status_field(const char *filename, const char *pattern, char **field) {
a4b143
+        _cleanup_free_ char *status = NULL;
a4b143
+        char *t;
a4b143
+        size_t len;
a4b143
+        int r;
a4b143
+
a4b143
+        assert(filename);
a4b143
+        assert(field);
a4b143
+
a4b143
+        r = read_full_file(filename, &status, NULL);
a4b143
+        if (r < 0)
a4b143
+                return r;
a4b143
+
a4b143
+        t = strstr(status, pattern);
a4b143
+        if (!t)
a4b143
+                return -ENOENT;
a4b143
+
a4b143
+        t += strlen(pattern);
a4b143
+        t += strspn(t, WHITESPACE);
a4b143
+
a4b143
+        len = strcspn(t, WHITESPACE);
a4b143
+
a4b143
+        *field = strndup(t, len);
a4b143
+        if (!*field)
a4b143
+                return -ENOMEM;
a4b143
+
a4b143
+        return 0;
a4b143
+}
a4b143
diff --git a/src/shared/fileio.h b/src/shared/fileio.h
a4b143
index a0aae28..59e4150 100644
a4b143
--- a/src/shared/fileio.h
a4b143
+++ b/src/shared/fileio.h
a4b143
@@ -37,3 +37,5 @@ int load_env_file(const char *fname, const char *separator, char ***l);
a4b143
 int write_env_file(const char *fname, char **l);
a4b143
 
a4b143
 int executable_is_script(const char *path, char **interpreter);
a4b143
+
a4b143
+int get_status_field(const char *filename, const char *pattern, char **field);
a4b143
diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c
a4b143
index 87633e7..f50777c 100644
a4b143
--- a/src/shared/logs-show.c
a4b143
+++ b/src/shared/logs-show.c
a4b143
@@ -201,7 +201,7 @@ static int output_short(
a4b143
         assert(j);
a4b143
 
a4b143
         /* Set the threshold to one bigger than the actual print
a4b143
-         * treshold, so that if the line is actually longer than what
a4b143
+         * threshold, so that if the line is actually longer than what
a4b143
          * we're willing to print, ellipsization will occur. This way
a4b143
          * we won't output a misleading line without any indication of
a4b143
          * truncation.
a4b143
diff --git a/src/shared/sleep-config.c b/src/shared/sleep-config.c
a4b143
index cd3238b..5ec7cce 100644
a4b143
--- a/src/shared/sleep-config.c
a4b143
+++ b/src/shared/sleep-config.c
a4b143
@@ -163,6 +163,46 @@ int can_sleep_disk(char **types) {
a4b143
         return false;
a4b143
 }
a4b143
 
a4b143
+#define HIBERNATION_SWAP_THRESHOLD 0.98
a4b143
+
a4b143
+static bool enough_memory_for_hibernation(void) {
a4b143
+        _cleanup_free_ char *active = NULL, *swapfree = NULL;
a4b143
+        unsigned long long act, swap;
a4b143
+        int r;
a4b143
+
a4b143
+        r = get_status_field("/proc/meminfo", "\nSwapFree:", &swapfree);
a4b143
+        if (r < 0) {
a4b143
+                log_error("Failed to retrieve SwapFree from /proc/meminfo: %s", strerror(-r));
a4b143
+                return false;
a4b143
+        }
a4b143
+
a4b143
+        r = safe_atollu(swapfree, &swap;;
a4b143
+        if (r < 0) {
a4b143
+                log_error("Failed to parse SwapFree from /proc/meminfo: %s: %s",
a4b143
+                          swapfree, strerror(-r));
a4b143
+                return false;
a4b143
+        }
a4b143
+
a4b143
+        r = get_status_field("/proc/meminfo", "\nActive(anon):", &active);
a4b143
+        if (r < 0) {
a4b143
+                log_error("Failed to retrieve Active(anon) from /proc/meminfo: %s", strerror(-r));
a4b143
+                return false;
a4b143
+        }
a4b143
+
a4b143
+        r = safe_atollu(active, &act;;
a4b143
+        if (r < 0) {
a4b143
+                log_error("Failed to parse Active(anon) from /proc/meminfo: %s: %s",
a4b143
+                          active, strerror(-r));
a4b143
+                return false;
a4b143
+        }
a4b143
+
a4b143
+        r = act <= swap * HIBERNATION_SWAP_THRESHOLD;
a4b143
+        log_debug("Hibernation is %spossible, Active(anon)=%llu kB, SwapFree=%llu kB, threshold=%.2g%%",
a4b143
+                  r ? "" : "im", act, swap, 100*HIBERNATION_SWAP_THRESHOLD);
a4b143
+
a4b143
+        return r;
a4b143
+}
a4b143
+
a4b143
 int can_sleep(const char *verb) {
a4b143
         _cleanup_strv_free_ char **modes = NULL, **states = NULL;
a4b143
         int r;
a4b143
@@ -175,5 +215,8 @@ int can_sleep(const char *verb) {
a4b143
         if (r < 0)
a4b143
                 return false;
a4b143
 
a4b143
-        return can_sleep_state(states) && can_sleep_disk(modes);
a4b143
+        if (!can_sleep_state(states) || !can_sleep_disk(modes))
a4b143
+                return false;
a4b143
+
a4b143
+        return streq(verb, "suspend") || enough_memory_for_hibernation();
a4b143
 }
a4b143
diff --git a/src/shared/util.c b/src/shared/util.c
a4b143
index 9a075fa..f6f3b18 100644
a4b143
--- a/src/shared/util.c
a4b143
+++ b/src/shared/util.c
a4b143
@@ -694,9 +694,6 @@ int is_kernel_thread(pid_t pid) {
a4b143
 
a4b143
 int get_process_capeff(pid_t pid, char **capeff) {
a4b143
         const char *p;
a4b143
-        _cleanup_free_ char *status = NULL;
a4b143
-        char *t = NULL;
a4b143
-        int r;
a4b143
 
a4b143
         assert(capeff);
a4b143
         assert(pid >= 0);
a4b143
@@ -706,25 +703,7 @@ int get_process_capeff(pid_t pid, char **capeff) {
a4b143
         else
a4b143
                 p = procfs_file_alloca(pid, "status");
a4b143
 
a4b143
-        r = read_full_file(p, &status, NULL);
a4b143
-        if (r < 0)
a4b143
-                return r;
a4b143
-
a4b143
-        t = strstr(status, "\nCapEff:\t");
a4b143
-        if (!t)
a4b143
-                return -ENOENT;
a4b143
-
a4b143
-        for (t += strlen("\nCapEff:\t"); t[0] == '0'; t++)
a4b143
-                continue;
a4b143
-
a4b143
-        if (t[0] == '\n')
a4b143
-                t--;
a4b143
-
a4b143
-        *capeff = strndup(t, strchr(t, '\n') - t);
a4b143
-        if (!*capeff)
a4b143
-                return -ENOMEM;
a4b143
-
a4b143
-        return 0;
a4b143
+        return get_status_field(p, "\nCapEff:", capeff);
a4b143
 }
a4b143
 
a4b143
 int get_process_exe(pid_t pid, char **name) {
a4b143
diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c
a4b143
index 1184e7e..4a4ed79 100644
a4b143
--- a/src/test/test-fileio.c
a4b143
+++ b/src/test/test-fileio.c
a4b143
@@ -229,9 +229,29 @@ static void test_executable_is_script(void) {
a4b143
         unlink(t);
a4b143
 }
a4b143
 
a4b143
+static void test_status_field(void) {
a4b143
+        _cleanup_free_ char *t = NULL, *p = NULL, *s = NULL;
a4b143
+        unsigned long long total, buffers;
a4b143
+
a4b143
+        assert_se(get_status_field("/proc/self/status", "\nThreads:", &t) == 0);
a4b143
+        puts(t);
a4b143
+        assert_se(streq(t, "1"));
a4b143
+
a4b143
+        assert_se(get_status_field("/proc/meminfo", "MemTotal:", &p) == 0);
a4b143
+        puts(p);
a4b143
+        assert_se(safe_atollu(p, &total) == 0);
a4b143
+
a4b143
+        assert_se(get_status_field("/proc/meminfo", "\nBuffers:", &s) == 0);
a4b143
+        puts(s);
a4b143
+        assert_se(safe_atollu(s, &buffers) == 0);
a4b143
+
a4b143
+        assert(buffers < total);
a4b143
+}
a4b143
+
a4b143
 int main(int argc, char *argv[]) {
a4b143
         test_parse_env_file();
a4b143
         test_parse_multiline_env_file();
a4b143
         test_executable_is_script();
a4b143
+        test_status_field();
a4b143
         return 0;
a4b143
 }
a4b143
diff --git a/src/test/test-sleep.c b/src/test/test-sleep.c
a4b143
index c3cb9c5..545dfab 100644
a4b143
--- a/src/test/test-sleep.c
a4b143
+++ b/src/test/test-sleep.c
a4b143
@@ -40,14 +40,14 @@ int main(int argc, char* argv[]) {
a4b143
                 **shutdown = strv_new("shutdown", NULL),
a4b143
                 **freez = strv_new("freeze", NULL);
a4b143
 
a4b143
-        log_info("Can Standby: %s", yes_no(can_sleep_state(standby) > 0));
a4b143
-        log_info("Can Suspend: %s", yes_no(can_sleep_state(mem) > 0));
a4b143
-        log_info("Can Hibernate: %s", yes_no(can_sleep_state(disk) > 0));
a4b143
-        log_info("Can Hibernate+Suspend (Hybrid-Sleep): %s", yes_no(can_sleep_disk(suspend) > 0));
a4b143
-        log_info("Can Hibernate+Reboot: %s", yes_no(can_sleep_disk(reboot) > 0));
a4b143
-        log_info("Can Hibernate+Platform: %s", yes_no(can_sleep_disk(platform) > 0));
a4b143
-        log_info("Can Hibernate+Shutdown: %s", yes_no(can_sleep_disk(shutdown) > 0));
a4b143
-        log_info("Can Freeze: %s", yes_no(can_sleep_disk(freez) > 0));
a4b143
+        log_info("Standby configured: %s", yes_no(can_sleep_state(standby) > 0));
a4b143
+        log_info("Suspend configured: %s", yes_no(can_sleep_state(mem) > 0));
a4b143
+        log_info("Hibernate configured: %s", yes_no(can_sleep_state(disk) > 0));
a4b143
+        log_info("Hibernate+Suspend (Hybrid-Sleep) configured: %s", yes_no(can_sleep_disk(suspend) > 0));
a4b143
+        log_info("Hibernate+Reboot configured: %s", yes_no(can_sleep_disk(reboot) > 0));
a4b143
+        log_info("Hibernate+Platform configured: %s", yes_no(can_sleep_disk(platform) > 0));
a4b143
+        log_info("Hibernate+Shutdown configured: %s", yes_no(can_sleep_disk(shutdown) > 0));
a4b143
+        log_info("Freeze configured: %s", yes_no(can_sleep_state(freez) > 0));
a4b143
 
a4b143
         log_info("Suspend configured and possible: %s", yes_no(can_sleep("suspend") > 0));
a4b143
         log_info("Hibernation configured and possible: %s", yes_no(can_sleep("hibernate") > 0));