forked from ish-app/ish
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
75 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,38 @@ | ||
#include <inttypes.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include "platform/platform.h" | ||
#include "debug.h" | ||
|
||
static void read_proc_stat_line(const char *name, char *buf) { | ||
FILE *proc_stat = fopen("/proc/stat", "r"); | ||
if (proc_stat == NULL) ERRNO_DIE("/proc/stat"); | ||
static void read_proc_line(const char *file, const char *name, char *buf) { | ||
FILE *f = fopen(file, "r"); | ||
if (f == NULL) ERRNO_DIE(file); | ||
do { | ||
fgets(buf, 1234, proc_stat); | ||
fgets(buf, 1234, f); | ||
} while (strncmp(name, buf, strlen(name)) != 0 || buf[strlen(name)] != ' '); | ||
fclose(f); | ||
} | ||
|
||
struct cpu_usage get_cpu_usage() { | ||
struct cpu_usage usage = {}; | ||
char buf[1234]; | ||
read_proc_stat_line("cpu", buf); | ||
sscanf(buf, "cpu %llu %llu %llu %llu\n", &usage.user_ticks, &usage.system_ticks, &usage.idle_ticks, &usage.nice_ticks); | ||
read_proc_line("/proc/stat", "cpu", buf); | ||
sscanf(buf, "cpu %"SCNu64" %"SCNu64" %"SCNu64" %"SCNu64"\n", &usage.user_ticks, &usage.system_ticks, &usage.idle_ticks, &usage.nice_ticks); | ||
return usage; | ||
} | ||
|
||
struct mem_usage get_mem_usage() { | ||
struct mem_usage usage; | ||
char buf[1234]; | ||
|
||
read_proc_line("/proc/meminfo", "MemTotal", buf); | ||
sscanf(buf, "MemTotal: %"PRIu64" kB\n", &usage.total); | ||
read_proc_line("/proc/meminfo", "MemFree", buf); | ||
sscanf(buf, "MemFree: %"PRIu64" kB\n", &usage.free); | ||
read_proc_line("/proc/meminfo", "MemActive", buf); | ||
sscanf(buf, "MemActive: %"PRIu64" kB\n", &usage.active); | ||
read_proc_line("/proc/meminfo", "MemInactive", buf); | ||
sscanf(buf, "MemInactive: %"PRIu64" kB\n", &usage.inactive); | ||
|
||
return usage; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters