From f36b2c0c11957445e57180ed36f342c0e919fc4d Mon Sep 17 00:00:00 2001 From: nimelehin Date: Wed, 18 Nov 2020 16:54:09 +0300 Subject: [PATCH] Add /proc/loadavg --- fs/proc/root.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/fs/proc/root.c b/fs/proc/root.c index 54ec9fb7c0..928d78e5f8 100644 --- a/fs/proc/root.c +++ b/fs/proc/root.c @@ -1,7 +1,9 @@ +#include // for MIN and MAX #include #include #include #include "kernel/calls.h" +#include "kernel/task.h" #include "fs/proc.h" #include "platform/platform.h" @@ -48,6 +50,21 @@ static int proc_show_uptime(struct proc_entry *UNUSED(entry), struct proc_data * return 0; } +static int proc_show_loadavg(struct proc_entry *UNUSED(entry), struct proc_data *buf) { + struct uptime_info uptime = get_uptime(); + struct pid *last_pid = pid_get_last_allocated(); + int last_pid_id = last_pid ? last_pid->id : 0; + double load_1m = uptime.load_1m / 65536.0; + double load_5m = uptime.load_5m / 65536.0; + double load_15m = uptime.load_15m / 65536.0; + int blocked_task_count = get_count_of_blocked_tasks(); + int alive_task_count = get_count_of_alive_tasks(); + // running_task_count is calculated approximetly, since we don't know the real number of currently running tasks. + int running_task_count = MIN(get_cpu_count(), (int)(alive_task_count - blocked_task_count)); + proc_printf(buf, "%.2f %.2f %.2f %u/%u %u\n", load_1m, load_5m, load_15m, running_task_count, alive_task_count, last_pid_id); + return 0; +} + static int proc_readlink_self(struct proc_entry *UNUSED(entry), char *buf) { sprintf(buf, "%d/", current->pid); return 0; @@ -97,6 +114,7 @@ static int proc_show_mounts(struct proc_entry *UNUSED(entry), struct proc_data * // in alphabetical order struct proc_dir_entry proc_root_entries[] = { + {"loadavg", .show = proc_show_loadavg}, {"meminfo", .show = proc_show_meminfo}, {"mounts", .show = proc_show_mounts}, {"self", S_IFLNK, .readlink = proc_readlink_self},