summaryrefslogtreecommitdiff
path: root/src/linux/up-backend.c
diff options
context:
space:
mode:
authorRichard Hughes <richard@hughsie.com>2010-03-29 14:21:51 +0100
committerRichard Hughes <richard@hughsie.com>2010-03-29 14:21:51 +0100
commit4b5dc883e60adcd41d005eb6f9e2feb012a1a7de (patch)
treef30883772346c6692fc1561d8da6c3c2f9bbf8cd /src/linux/up-backend.c
parent0198498350363a79cf42707543b43b31cedbc731 (diff)
downloadupower-4b5dc883e60adcd41d005eb6f9e2feb012a1a7de.tar.gz
Get the swap size from the backend, rather than hardcoding Linux specifics
Diffstat (limited to 'src/linux/up-backend.c')
-rw-r--r--src/linux/up-backend.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/linux/up-backend.c b/src/linux/up-backend.c
index b528c67..c12102d 100644
--- a/src/linux/up-backend.c
+++ b/src/linux/up-backend.c
@@ -460,6 +460,68 @@ out:
}
/**
+ * up_backend_get_used_swap:
+ *
+ * Return value: a percentage value
+ **/
+gfloat
+up_backend_get_used_swap (UpBackend *backend)
+{
+ gchar *contents = NULL;
+ gchar **lines = NULL;
+ GError *error = NULL;
+ gchar **tokens;
+ gboolean ret;
+ guint active = 0;
+ guint swap_free = 0;
+ guint swap_total = 0;
+ guint len;
+ guint i;
+ gfloat percentage = 0.0f;
+ const gchar *filename = "/proc/meminfo";
+
+ /* get memory data */
+ ret = g_file_get_contents (filename, &contents, NULL, &error);
+ if (!ret) {
+ egg_warning ("failed to open %s: %s", filename, error->message);
+ g_error_free (error);
+ goto out;
+ }
+
+ /* process each line */
+ lines = g_strsplit (contents, "\n", -1);
+ for (i=1; lines[i] != NULL; i++) {
+ tokens = g_strsplit_set (lines[i], ": ", -1);
+ len = g_strv_length (tokens);
+ if (len > 3) {
+ if (g_strcmp0 (tokens[0], "SwapFree") == 0)
+ swap_free = atoi (tokens[len-2]);
+ if (g_strcmp0 (tokens[0], "SwapTotal") == 0)
+ swap_total = atoi (tokens[len-2]);
+ else if (g_strcmp0 (tokens[0], "Active") == 0)
+ active = atoi (tokens[len-2]);
+ }
+ g_strfreev (tokens);
+ }
+
+ /* first check if we even have swap, if not consider all swap space used */
+ if (swap_total == 0) {
+ egg_debug ("no swap space found");
+ percentage = 100.0f;
+ goto out;
+ }
+
+ /* work out how close to the line we are */
+ if (swap_free > 0 && active > 0)
+ percentage = (active * 100) / swap_free;
+ egg_debug ("total swap available %i kb, active memory %i kb (%.1f%%)", swap_free, active, percentage);
+out:
+ g_free (contents);
+ g_strfreev (lines);
+ return percentage;
+}
+
+/**
* up_backend_class_init:
* @klass: The UpBackendClass
**/