Skip to content

Commit

Permalink
Implement /proc/meminfo
Browse files Browse the repository at this point in the history
  • Loading branch information
tbodt committed Jan 6, 2019
1 parent 88987ed commit 900f7bf
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 deletions.
26 changes: 26 additions & 0 deletions fs/proc/root.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <sys/stat.h>
#include <inttypes.h>
#include "kernel/calls.h"
#include "fs/proc.h"
#include "platform/platform.h"
Expand All @@ -16,6 +17,30 @@ static ssize_t proc_show_stat(struct proc_entry *UNUSED(entry), char *buf) {
return n;
}

static void show_kb(char *buf, size_t *n, const char *name, uint64_t value) {
*n += sprintf(buf + *n, "%s%8"PRIu64" kB\n", name, value / 1000);
}

static ssize_t proc_show_meminfo(struct proc_entry *UNUSED(entry), char *buf) {
struct mem_usage usage = get_mem_usage();
size_t n = 0;
show_kb(buf, &n, "MemTotal: ", usage.total);
show_kb(buf, &n, "MemFree: ", usage.free);
show_kb(buf, &n, "MemShared: ", usage.free);
// a bunch of crap busybox top needs to see or else it gets stack garbage
show_kb(buf, &n, "Shmem: ", 0);
show_kb(buf, &n, "Buffers: ", 0);
show_kb(buf, &n, "Cached: ", 0);
show_kb(buf, &n, "SwapTotal: ", 0);
show_kb(buf, &n, "SwapFree: ", 0);
show_kb(buf, &n, "Dirty: ", 0);
show_kb(buf, &n, "Writeback: ", 0);
show_kb(buf, &n, "AnonPages: ", 0);
show_kb(buf, &n, "Mapped: ", 0);
show_kb(buf, &n, "Slab: ", 0);
return n;
}

static int proc_readlink_self(struct proc_entry *UNUSED(entry), char *buf) {
sprintf(buf, "%d/", current->pid);
return 0;
Expand All @@ -25,6 +50,7 @@ static int proc_readlink_self(struct proc_entry *UNUSED(entry), char *buf) {
struct proc_dir_entry proc_root_entries[] = {
{"version", .show = proc_show_version},
{"stat", .show = proc_show_stat},
{"meminfo", .show = proc_show_meminfo},
{"self", S_IFLNK, .readlink = proc_readlink_self},
};
#define PROC_ROOT_LEN sizeof(proc_root_entries)/sizeof(proc_root_entries[0])
Expand Down
18 changes: 17 additions & 1 deletion platform/darwin.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include "platform/platform.h"

struct cpu_usage get_cpu_usage() {
struct host_cpu_load_info load;
host_cpu_load_info_data_t load;
mach_msg_type_number_t fuck = HOST_CPU_LOAD_INFO_COUNT;
host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t) &load, &fuck);
struct cpu_usage usage;
Expand All @@ -12,3 +12,19 @@ struct cpu_usage get_cpu_usage() {
usage.nice_ticks = load.cpu_ticks[CPU_STATE_NICE];
return usage;
}

struct mem_usage get_mem_usage() {
host_basic_info_data_t basic;
mach_msg_type_number_t fuck = HOST_BASIC_INFO;
host_statistics(mach_host_self(), HOST_BASIC_INFO, (host_info_t) &basic, &fuck);
vm_statistics64_data_t vm;
fuck = HOST_VM_INFO64_COUNT;
host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info_t) &vm, &fuck);

struct mem_usage usage;
usage.total = basic.max_mem;
usage.free = vm.free_count * vm_page_size;
usage.active = vm.active_count * vm_page_size;
usage.inactive = vm.inactive_count * vm_page_size;
return usage;
}
30 changes: 24 additions & 6 deletions platform/linux.c
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;
}
8 changes: 8 additions & 0 deletions platform/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@ struct cpu_usage {
};
struct cpu_usage get_cpu_usage(void);

struct mem_usage {
uint64_t total;
uint64_t free;
uint64_t active;
uint64_t inactive;
};
struct mem_usage get_mem_usage(void);

#endif

0 comments on commit 900f7bf

Please sign in to comment.