Skip to content

Commit

Permalink
Add /proc/loadavg
Browse files Browse the repository at this point in the history
  • Loading branch information
nimelehin committed Dec 7, 2020
1 parent ffad9d8 commit f36b2c0
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions fs/proc/root.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <sys/param.h> // for MIN and MAX
#include <sys/stat.h>
#include <inttypes.h>
#include <string.h>
#include "kernel/calls.h"
#include "kernel/task.h"
#include "fs/proc.h"
#include "platform/platform.h"

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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},
Expand Down

0 comments on commit f36b2c0

Please sign in to comment.