2019-06-07 12:49:31 +03:00
|
|
|
#include <AK/AKString.h>
|
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/QuickSort.h>
|
|
|
|
#include <AK/Vector.h>
|
2019-02-04 12:28:12 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <pwd.h>
|
2019-06-07 12:49:31 +03:00
|
|
|
#include <stdio.h>
|
2019-02-04 12:28:12 +03:00
|
|
|
#include <stdlib.h>
|
2019-06-07 12:49:31 +03:00
|
|
|
#include <unistd.h>
|
2019-02-04 12:28:12 +03:00
|
|
|
|
|
|
|
static HashMap<unsigned, String>* s_usernames;
|
|
|
|
|
|
|
|
struct Process {
|
|
|
|
pid_t pid;
|
|
|
|
unsigned nsched;
|
|
|
|
String name;
|
|
|
|
String state;
|
|
|
|
String user;
|
2019-02-07 14:21:17 +03:00
|
|
|
String priority;
|
2019-06-07 13:44:29 +03:00
|
|
|
unsigned virtual_size;
|
|
|
|
unsigned physical_size;
|
2019-02-04 12:28:12 +03:00
|
|
|
unsigned nsched_since_prev;
|
|
|
|
unsigned cpu_percent;
|
|
|
|
unsigned cpu_percent_decimal;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Snapshot {
|
|
|
|
HashMap<unsigned, Process> map;
|
|
|
|
dword sum_nsched { 0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
static Snapshot get_snapshot()
|
|
|
|
{
|
|
|
|
Snapshot snapshot;
|
|
|
|
|
|
|
|
FILE* fp = fopen("/proc/all", "r");
|
|
|
|
if (!fp) {
|
|
|
|
perror("failed to open /proc/all");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
char buf[4096];
|
|
|
|
char* ptr = fgets(buf, sizeof(buf), fp);
|
|
|
|
if (!ptr)
|
|
|
|
break;
|
|
|
|
auto parts = String(buf, Chomp).split(',');
|
2019-02-17 03:16:38 +03:00
|
|
|
if (parts.size() < 17)
|
2019-02-04 12:28:12 +03:00
|
|
|
break;
|
|
|
|
bool ok;
|
|
|
|
pid_t pid = parts[0].to_uint(ok);
|
|
|
|
ASSERT(ok);
|
|
|
|
unsigned nsched = parts[1].to_uint(ok);
|
|
|
|
ASSERT(ok);
|
|
|
|
snapshot.sum_nsched += nsched;
|
|
|
|
Process process;
|
|
|
|
process.pid = pid;
|
|
|
|
process.nsched = nsched;
|
|
|
|
unsigned uid = parts[5].to_uint(ok);
|
|
|
|
ASSERT(ok);
|
|
|
|
process.user = s_usernames->get(uid);
|
2019-02-17 03:16:38 +03:00
|
|
|
process.priority = parts[16];
|
2019-02-04 12:28:12 +03:00
|
|
|
process.state = parts[7];
|
|
|
|
process.name = parts[11];
|
2019-06-07 13:44:29 +03:00
|
|
|
process.virtual_size = parts[12].to_uint(ok);
|
2019-02-04 12:28:12 +03:00
|
|
|
ASSERT(ok);
|
2019-06-07 13:44:29 +03:00
|
|
|
process.physical_size = parts[13].to_uint(ok);
|
2019-02-05 11:27:27 +03:00
|
|
|
ASSERT(ok);
|
2019-02-04 12:28:12 +03:00
|
|
|
snapshot.map.set(pid, move(process));
|
|
|
|
}
|
|
|
|
int rc = fclose(fp);
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
return snapshot;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int, char**)
|
|
|
|
{
|
|
|
|
s_usernames = new HashMap<unsigned, String>();
|
|
|
|
setpwent();
|
|
|
|
while (auto* passwd = getpwent())
|
|
|
|
s_usernames->set(passwd->pw_uid, passwd->pw_name);
|
|
|
|
endpwent();
|
|
|
|
|
|
|
|
Vector<Process*> processes;
|
|
|
|
auto prev = get_snapshot();
|
2019-02-05 11:27:27 +03:00
|
|
|
usleep(10000);
|
2019-02-04 12:28:12 +03:00
|
|
|
for (;;) {
|
|
|
|
auto current = get_snapshot();
|
|
|
|
auto sum_diff = current.sum_nsched - prev.sum_nsched;
|
|
|
|
|
|
|
|
printf("\033[3J\033[H\033[2J");
|
2019-06-22 16:52:45 +03:00
|
|
|
printf("\033[47;30m%6s %3s %-8s %-8s %6s %6s %4s %s\033[K\033[0m\n",
|
2019-06-07 12:49:31 +03:00
|
|
|
"PID",
|
|
|
|
"PRI",
|
|
|
|
"USER",
|
|
|
|
"STATE",
|
2019-06-07 13:44:29 +03:00
|
|
|
"VIRT",
|
|
|
|
"PHYS",
|
2019-06-07 12:49:31 +03:00
|
|
|
"%CPU",
|
|
|
|
"NAME");
|
2019-02-04 12:28:12 +03:00
|
|
|
for (auto& it : current.map) {
|
|
|
|
pid_t pid = it.key;
|
|
|
|
if (pid == 0)
|
|
|
|
continue;
|
|
|
|
dword nsched_now = it.value.nsched;
|
|
|
|
auto jt = prev.map.find(pid);
|
|
|
|
if (jt == prev.map.end())
|
|
|
|
continue;
|
|
|
|
dword nsched_before = (*jt).value.nsched;
|
|
|
|
dword nsched_diff = nsched_now - nsched_before;
|
|
|
|
it.value.nsched_since_prev = nsched_diff;
|
2019-06-07 12:49:31 +03:00
|
|
|
it.value.cpu_percent = ((nsched_diff * 100) / sum_diff);
|
|
|
|
it.value.cpu_percent_decimal = (((nsched_diff * 1000) / sum_diff) % 10);
|
2019-02-04 12:28:12 +03:00
|
|
|
processes.append(&it.value);
|
|
|
|
}
|
|
|
|
|
2019-06-07 12:49:31 +03:00
|
|
|
quick_sort(processes.begin(), processes.end(), [](auto* p1, auto* p2) {
|
2019-03-09 18:20:46 +03:00
|
|
|
return p2->nsched_since_prev < p1->nsched_since_prev;
|
2019-02-04 12:28:12 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
for (auto* process : processes) {
|
2019-06-22 16:52:45 +03:00
|
|
|
printf("%6d %c %-8s %-8s %6u %6u %2u.%1u %s\n",
|
2019-02-04 12:28:12 +03:00
|
|
|
process->pid,
|
2019-02-07 14:21:17 +03:00
|
|
|
process->priority[0],
|
2019-02-04 12:28:12 +03:00
|
|
|
process->user.characters(),
|
|
|
|
process->state.characters(),
|
2019-06-07 13:44:29 +03:00
|
|
|
process->virtual_size / 1024,
|
|
|
|
process->physical_size / 1024,
|
2019-02-04 12:28:12 +03:00
|
|
|
process->cpu_percent,
|
|
|
|
process->cpu_percent_decimal,
|
2019-06-07 12:49:31 +03:00
|
|
|
process->name.characters());
|
2019-02-04 12:28:12 +03:00
|
|
|
}
|
|
|
|
processes.clear_with_capacity();
|
|
|
|
prev = move(current);
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|