Add functions for measuring swap
authorJody Leonard <me@jodyleonard.com>
Fri, 14 Oct 2016 09:44:46 +0000 (05:44 -0400)
committerJody Leonard <me@jodyleonard.com>
Sat, 15 Oct 2016 09:48:46 +0000 (05:48 -0400)
slstatus.c

index efdc9b06a9525e8e1fcf6da22fa8ba0de332cce6..f39a2dc5b8b45945ca67dd4550d58335a4363c76 100644 (file)
@@ -57,6 +57,10 @@ static char *ram_perc(void);
 static char *ram_used(void);
 static char *ram_total(void);
 static char *run_command(const char *cmd);
+static char *swap_free(void);
+static char *swap_perc(void);
+static char *swap_used(void);
+static char *swap_total(void);
 static char *temp(const char *file);
 static char *uid(void);
 static char *uptime(void);
@@ -417,6 +421,130 @@ run_command(const char *cmd)
        return smprintf("%s", buf);
 }
 
+static char *
+swap_free(void)
+{
+       long free;
+       FILE *fp;
+       char buf[2048];
+       size_t bytes_read;
+       char *match;
+
+       fp = fopen("/proc/meminfo", "r");
+       if (fp == NULL) {
+               warn("Failed to open file /proc/meminfo");
+               return smprintf(UNKNOWN_STR);
+       }
+       bytes_read = fread(buf, sizeof(char), sizeof(buf), fp);
+       buf[bytes_read] = '\0';
+       fclose(fp);
+       if (bytes_read == 0 || bytes_read == sizeof(buf)) {
+               warn("Failed to read /proc/meminfo\n");
+               return smprintf(UNKNOWN_STR);
+       }
+
+       match = strstr(buf, "SwapFree");
+       sscanf(match, "SwapFree: %ld kB\n", &free);
+
+       return smprintf("%f", (float)free / 1024 / 1024);
+}
+
+static char *
+swap_perc(void)
+{
+       long total, free, cached;
+       FILE *fp;
+       char buf[2048];
+       size_t bytes_read;
+       char *match;
+
+       fp = fopen("/proc/meminfo", "r");
+       if (fp == NULL) {
+               warn("Failed to open file /proc/meminfo");
+               return smprintf(UNKNOWN_STR);
+       }
+       bytes_read = fread(buf, sizeof(char), sizeof(buf), fp);
+       buf[bytes_read] = '\0';
+       fclose(fp);
+       if (bytes_read == 0 || bytes_read == sizeof(buf)) {
+               warn("Failed to read /proc/meminfo\n");
+               return smprintf(UNKNOWN_STR);
+       }
+
+       match = strstr(buf, "SwapCached");
+       sscanf(match, "SwapCached: %ld kB\n", &cached);
+
+       match = strstr(buf, "SwapTotal");
+       sscanf(match, "SwapTotal: %ld kB\n", &total);
+
+       match = strstr(buf, "SwapFree");
+       sscanf(match, "SwapFree: %ld kB\n", &free);
+
+       return smprintf("%d%%", 100 * (total - free - cached) / total);
+}
+
+static char *
+swap_total(void)
+{
+       long total;
+       FILE *fp;
+       char buf[2048];
+       size_t bytes_read;
+       char *match;
+
+       fp = fopen("/proc/meminfo", "r");
+       if (fp == NULL) {
+               warn("Failed to open file /proc/meminfo");
+               return smprintf(UNKNOWN_STR);
+       }
+       bytes_read = fread(buf, sizeof(char), sizeof(buf), fp);
+       buf[bytes_read] = '\0';
+       fclose(fp);
+       if (bytes_read == 0 || bytes_read == sizeof(buf)) {
+               warn("Failed to read /proc/meminfo\n");
+               return smprintf(UNKNOWN_STR);
+       }
+
+       match = strstr(buf, "SwapTotal");
+       sscanf(match, "SwapTotal: %ld kB\n", &total);
+
+       return smprintf("%f", (float)total / 1024 / 1024);
+}
+
+static char *
+swap_used(void)
+{
+       long total, free, cached;
+       FILE *fp;
+       char buf[2048];
+       size_t bytes_read;
+       char *match;
+
+       fp = fopen("/proc/meminfo", "r");
+       if (fp == NULL) {
+               warn("Failed to open file /proc/meminfo");
+               return smprintf(UNKNOWN_STR);
+       }
+       bytes_read = fread(buf, sizeof(char), sizeof(buf), fp);
+       buf[bytes_read] = '\0';
+       fclose(fp);
+       if (bytes_read == 0 || bytes_read == sizeof(buf)) {
+               warn("Failed to read /proc/meminfo\n");
+               return smprintf(UNKNOWN_STR);
+       }
+
+       match = strstr(buf, "SwapCached");
+       sscanf(match, "SwapCached: %ld kB\n", &cached);
+
+       match = strstr(buf, "SwapTotal");
+       sscanf(match, "SwapTotal: %ld kB\n", &total);
+
+       match = strstr(buf, "SwapFree");
+       sscanf(match, "SwapFree: %ld kB\n", &free);
+
+       return smprintf("%f", (float)(total - free - cached) / 1024 / 1024);
+}
+
 static char *
 temp(const char *file)
 {