2019-09-06 13:34:26 +00:00
|
|
|
#include <AK/String.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <AK/HashMap.h>
|
2019-06-29 07:04:45 +00:00
|
|
|
#include <AK/JsonArray.h>
|
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/JsonValue.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <AK/QuickSort.h>
|
|
|
|
#include <AK/Vector.h>
|
2019-07-10 11:56:28 +00:00
|
|
|
#include <LibCore/CProcessStatisticsReader.h>
|
2019-02-04 09:28:12 +00:00
|
|
|
#include <fcntl.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <stdio.h>
|
2019-02-04 09:28:12 +00:00
|
|
|
#include <stdlib.h>
|
2019-06-07 09:49:31 +00:00
|
|
|
#include <unistd.h>
|
2019-02-04 09:28:12 +00:00
|
|
|
|
2019-07-10 11:56:28 +00:00
|
|
|
struct ProcessData {
|
|
|
|
CProcessStatistics stats;
|
2019-07-17 20:22:22 +00:00
|
|
|
unsigned times_scheduled_since_prev { 0 };
|
2019-07-10 11:56:28 +00:00
|
|
|
unsigned cpu_percent { 0 };
|
|
|
|
unsigned cpu_percent_decimal { 0 };
|
2019-02-04 09:28:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Snapshot {
|
2019-07-10 11:56:28 +00:00
|
|
|
HashMap<unsigned, ProcessData> map;
|
2019-07-17 20:22:22 +00:00
|
|
|
u32 sum_times_scheduled { 0 };
|
2019-02-04 09:28:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static Snapshot get_snapshot()
|
|
|
|
{
|
2019-06-29 07:04:45 +00:00
|
|
|
Snapshot snapshot;
|
|
|
|
|
2019-07-10 11:56:28 +00:00
|
|
|
auto all_processes = CProcessStatisticsReader::get_all();
|
|
|
|
|
|
|
|
for (auto& it : all_processes) {
|
|
|
|
auto& stats = it.value;
|
2019-07-17 20:22:22 +00:00
|
|
|
snapshot.sum_times_scheduled += stats.times_scheduled;
|
2019-07-10 11:56:28 +00:00
|
|
|
ProcessData process_data;
|
|
|
|
process_data.stats = stats;
|
|
|
|
snapshot.map.set(stats.pid, move(process_data));
|
|
|
|
}
|
|
|
|
|
2019-02-04 09:28:12 +00:00
|
|
|
return snapshot;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int, char**)
|
|
|
|
{
|
2019-07-10 11:56:28 +00:00
|
|
|
Vector<ProcessData*> processes;
|
2019-02-04 09:28:12 +00:00
|
|
|
auto prev = get_snapshot();
|
2019-02-05 08:27:27 +00:00
|
|
|
usleep(10000);
|
2019-02-04 09:28:12 +00:00
|
|
|
for (;;) {
|
|
|
|
auto current = get_snapshot();
|
2019-07-17 20:22:22 +00:00
|
|
|
auto sum_diff = current.sum_times_scheduled - prev.sum_times_scheduled;
|
2019-02-04 09:28:12 +00:00
|
|
|
|
|
|
|
printf("\033[3J\033[H\033[2J");
|
2019-06-22 13:52:45 +00:00
|
|
|
printf("\033[47;30m%6s %3s %-8s %-8s %6s %6s %4s %s\033[K\033[0m\n",
|
2019-06-07 09:49:31 +00:00
|
|
|
"PID",
|
|
|
|
"PRI",
|
|
|
|
"USER",
|
|
|
|
"STATE",
|
2019-06-07 10:44:29 +00:00
|
|
|
"VIRT",
|
|
|
|
"PHYS",
|
2019-06-07 09:49:31 +00:00
|
|
|
"%CPU",
|
|
|
|
"NAME");
|
2019-02-04 09:28:12 +00:00
|
|
|
for (auto& it : current.map) {
|
|
|
|
pid_t pid = it.key;
|
|
|
|
if (pid == 0)
|
|
|
|
continue;
|
2019-07-17 20:22:22 +00:00
|
|
|
u32 times_scheduled_now = it.value.stats.times_scheduled;
|
2019-02-04 09:28:12 +00:00
|
|
|
auto jt = prev.map.find(pid);
|
|
|
|
if (jt == prev.map.end())
|
|
|
|
continue;
|
2019-07-17 20:22:22 +00:00
|
|
|
u32 times_scheduled_before = (*jt).value.stats.times_scheduled;
|
|
|
|
u32 times_scheduled_diff = times_scheduled_now - times_scheduled_before;
|
|
|
|
it.value.times_scheduled_since_prev = times_scheduled_diff;
|
|
|
|
it.value.cpu_percent = ((times_scheduled_diff * 100) / sum_diff);
|
|
|
|
it.value.cpu_percent_decimal = (((times_scheduled_diff * 1000) / sum_diff) % 10);
|
2019-02-04 09:28:12 +00:00
|
|
|
processes.append(&it.value);
|
|
|
|
}
|
|
|
|
|
2019-06-07 09:49:31 +00:00
|
|
|
quick_sort(processes.begin(), processes.end(), [](auto* p1, auto* p2) {
|
2019-07-17 20:22:22 +00:00
|
|
|
return p2->times_scheduled_since_prev < p1->times_scheduled_since_prev;
|
2019-02-04 09:28:12 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
for (auto* process : processes) {
|
2019-07-17 09:23:31 +00:00
|
|
|
printf("%6d %c %-8s %-10s %6zu %6zu %2u.%1u %s\n",
|
2019-07-10 11:56:28 +00:00
|
|
|
process->stats.pid,
|
|
|
|
process->stats.priority[0],
|
|
|
|
process->stats.username.characters(),
|
|
|
|
process->stats.state.characters(),
|
2019-07-17 20:22:22 +00:00
|
|
|
process->stats.amount_virtual / 1024,
|
|
|
|
process->stats.amount_resident / 1024,
|
2019-02-04 09:28:12 +00:00
|
|
|
process->cpu_percent,
|
|
|
|
process->cpu_percent_decimal,
|
2019-07-10 11:56:28 +00:00
|
|
|
process->stats.name.characters());
|
2019-02-04 09:28:12 +00:00
|
|
|
}
|
|
|
|
processes.clear_with_capacity();
|
|
|
|
prev = move(current);
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|