diff --git a/system.c b/system.c index 77e261a98fedbec605665cc0067b1d57ab375a41..3e3f00224c6ba355c50014d89ec1e2de8e53de1c 100644 --- a/system.c +++ b/system.c @@ -31,6 +31,7 @@ #include <libubox/blobmsg.h> #include <libubus.h> +#include <time.h> #include "system.h" #include "tools.h" @@ -261,7 +262,8 @@ quest_router_processes(struct ubus_context *ctx, struct ubus_object *obj, struct ubus_request_data *req, const char *method, struct blob_attr *msg) { - FILE *top; + int rv; + FILE *top, *input; char line[1024]; void *f, *l, *t; int pid, ppid, vsz; @@ -270,6 +272,13 @@ quest_router_processes(struct ubus_context *ctx, struct ubus_object *obj, char vszp[8]; char cpup[8]; char command[128]; + char *procstatfile = NULL; + long priority; + unsigned long utime; + unsigned long stime; + long cutime; + long cstime; + long cputime; blob_buf_init(&bb, 0); @@ -282,6 +291,8 @@ quest_router_processes(struct ubus_context *ctx, struct ubus_object *obj, blobmsg_add_string(&bb, "", "%VSZ"); blobmsg_add_string(&bb, "", "%CPU"); blobmsg_add_string(&bb, "", "COMMAND"); + blobmsg_add_string(&bb, "", "PRIORITY"); + blobmsg_add_string(&bb, "", "CPUTIME"); blobmsg_close_array(&bb, f); if ((top = popen("top -bn1", "r"))) { @@ -300,6 +311,23 @@ quest_router_processes(struct ubus_context *ctx, struct ubus_object *obj, blobmsg_add_string(&bb, "%VSZ", vszp); blobmsg_add_string(&bb, "%CPU", cpup); blobmsg_add_string(&bb, "COMMAND", command); + + priority = 0; + cputime = 0; + rv = asprintf(&procstatfile, "/proc/%d/stat", pid); + if (rv != -1) { + input = fopen(procstatfile, "r"); + if (input) { + rv = fscanf(input, "%*d %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %lu %lu %ld %ld %ld", &utime, &stime, &cutime, &cstime, &priority); + if (rv == 5) + cputime = ((stime + utime + cstime + cutime) * 1000)/CLOCKS_PER_SEC; + fclose(input); + } + free(procstatfile); + procstatfile = NULL; + } + blobmsg_add_u32(&bb, "PRIORITY", priority); + blobmsg_add_u32(&bb, "CPUTIME", cputime); blobmsg_close_table(&bb, t); } }