2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-06-07 12:49:31 +03:00
|
|
|
#include <AK/HashMap.h>
|
2019-06-29 10:04:45 +03:00
|
|
|
#include <AK/JsonArray.h>
|
|
|
|
#include <AK/JsonObject.h>
|
2019-06-07 12:49:31 +03:00
|
|
|
#include <AK/QuickSort.h>
|
2019-11-26 23:25:45 +03:00
|
|
|
#include <AK/String.h>
|
2019-06-07 12:49:31 +03:00
|
|
|
#include <AK/Vector.h>
|
2021-06-26 22:31:31 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-02-06 17:04:03 +03:00
|
|
|
#include <LibCore/ProcessStatisticsReader.h>
|
2021-11-29 21:39:24 +03:00
|
|
|
#include <LibCore/System.h>
|
|
|
|
#include <LibMain/Main.h>
|
2020-07-27 18:00:53 +03:00
|
|
|
#include <signal.h>
|
2019-06-07 12:49:31 +03:00
|
|
|
#include <stdio.h>
|
2019-02-04 12:28:12 +03:00
|
|
|
#include <stdlib.h>
|
2020-07-27 18:00:53 +03:00
|
|
|
#include <sys/ioctl.h>
|
2019-06-07 12:49:31 +03:00
|
|
|
#include <unistd.h>
|
2019-02-04 12:28:12 +03:00
|
|
|
|
2021-06-26 22:31:31 +03:00
|
|
|
struct TopOption {
|
|
|
|
enum class SortBy {
|
|
|
|
Pid,
|
|
|
|
Tid,
|
|
|
|
Priority,
|
|
|
|
UserName,
|
|
|
|
State,
|
|
|
|
Virt,
|
|
|
|
Phys,
|
|
|
|
Cpu,
|
|
|
|
Name
|
|
|
|
};
|
|
|
|
|
|
|
|
SortBy sort_by { SortBy::Cpu };
|
|
|
|
int delay_time { 1 };
|
|
|
|
};
|
|
|
|
|
2019-11-26 23:25:45 +03:00
|
|
|
struct ThreadData {
|
|
|
|
int tid;
|
|
|
|
pid_t pid;
|
2020-09-06 20:04:47 +03:00
|
|
|
pid_t pgid;
|
|
|
|
pid_t pgp;
|
|
|
|
pid_t sid;
|
2019-11-26 23:25:45 +03:00
|
|
|
uid_t uid;
|
|
|
|
gid_t gid;
|
|
|
|
pid_t ppid;
|
|
|
|
unsigned nfds;
|
|
|
|
String name;
|
|
|
|
String tty;
|
|
|
|
size_t amount_virtual;
|
|
|
|
size_t amount_resident;
|
|
|
|
size_t amount_shared;
|
|
|
|
unsigned syscall_count;
|
|
|
|
unsigned inode_faults;
|
|
|
|
unsigned zero_faults;
|
|
|
|
unsigned cow_faults;
|
2021-07-15 06:46:32 +03:00
|
|
|
u64 time_scheduled;
|
2019-11-26 23:25:45 +03:00
|
|
|
|
2021-07-15 06:46:32 +03:00
|
|
|
u64 time_scheduled_since_prev { 0 };
|
2019-07-10 14:56:28 +03:00
|
|
|
unsigned cpu_percent { 0 };
|
|
|
|
unsigned cpu_percent_decimal { 0 };
|
2019-11-26 23:25:45 +03:00
|
|
|
|
2019-12-30 20:46:17 +03:00
|
|
|
u32 priority;
|
2019-11-26 23:25:45 +03:00
|
|
|
String username;
|
|
|
|
String state;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct PidAndTid {
|
2022-04-01 20:58:27 +03:00
|
|
|
bool operator==(PidAndTid const& other) const
|
2019-11-26 23:25:45 +03:00
|
|
|
{
|
|
|
|
return pid == other.pid && tid == other.tid;
|
|
|
|
}
|
|
|
|
pid_t pid;
|
|
|
|
int tid;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
template<>
|
|
|
|
struct Traits<PidAndTid> : public GenericTraits<PidAndTid> {
|
2022-04-01 20:58:27 +03:00
|
|
|
static unsigned hash(PidAndTid const& value) { return pair_int_hash(value.pid, value.tid); }
|
2019-02-04 12:28:12 +03:00
|
|
|
};
|
2019-11-26 23:25:45 +03:00
|
|
|
}
|
2019-02-04 12:28:12 +03:00
|
|
|
|
|
|
|
struct Snapshot {
|
2019-11-26 23:25:45 +03:00
|
|
|
HashMap<PidAndTid, ThreadData> map;
|
2021-07-15 06:46:32 +03:00
|
|
|
u64 total_time_scheduled { 0 };
|
|
|
|
u64 total_time_scheduled_kernel { 0 };
|
2019-02-04 12:28:12 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static Snapshot get_snapshot()
|
|
|
|
{
|
2020-02-02 14:34:39 +03:00
|
|
|
auto all_processes = Core::ProcessStatisticsReader::get_all();
|
2021-01-02 06:58:47 +03:00
|
|
|
if (!all_processes.has_value())
|
|
|
|
return {};
|
2019-07-10 14:56:28 +03:00
|
|
|
|
2021-01-02 06:58:47 +03:00
|
|
|
Snapshot snapshot;
|
2021-07-14 21:05:59 +03:00
|
|
|
for (auto& process : all_processes.value().processes) {
|
2021-05-23 12:08:32 +03:00
|
|
|
for (auto& thread : process.threads) {
|
2019-11-26 23:25:45 +03:00
|
|
|
ThreadData thread_data;
|
|
|
|
thread_data.tid = thread.tid;
|
2021-05-23 12:08:32 +03:00
|
|
|
thread_data.pid = process.pid;
|
|
|
|
thread_data.pgid = process.pgid;
|
|
|
|
thread_data.pgp = process.pgp;
|
|
|
|
thread_data.sid = process.sid;
|
|
|
|
thread_data.uid = process.uid;
|
|
|
|
thread_data.gid = process.gid;
|
|
|
|
thread_data.ppid = process.ppid;
|
|
|
|
thread_data.nfds = process.nfds;
|
|
|
|
thread_data.name = process.name;
|
|
|
|
thread_data.tty = process.tty;
|
|
|
|
thread_data.amount_virtual = process.amount_virtual;
|
|
|
|
thread_data.amount_resident = process.amount_resident;
|
|
|
|
thread_data.amount_shared = process.amount_shared;
|
2019-11-26 23:35:24 +03:00
|
|
|
thread_data.syscall_count = thread.syscall_count;
|
|
|
|
thread_data.inode_faults = thread.inode_faults;
|
|
|
|
thread_data.zero_faults = thread.zero_faults;
|
|
|
|
thread_data.cow_faults = thread.cow_faults;
|
2021-07-15 06:46:32 +03:00
|
|
|
thread_data.time_scheduled = (u64)thread.time_user + (u64)thread.time_kernel;
|
2019-11-26 23:25:45 +03:00
|
|
|
thread_data.priority = thread.priority;
|
|
|
|
thread_data.state = thread.state;
|
2021-05-23 12:08:32 +03:00
|
|
|
thread_data.username = process.username;
|
2019-11-26 23:25:45 +03:00
|
|
|
|
2021-05-23 12:08:32 +03:00
|
|
|
snapshot.map.set({ process.pid, thread.tid }, move(thread_data));
|
2019-11-26 23:25:45 +03:00
|
|
|
}
|
2019-07-10 14:56:28 +03:00
|
|
|
}
|
|
|
|
|
2021-07-15 06:46:32 +03:00
|
|
|
snapshot.total_time_scheduled = all_processes->total_time_scheduled;
|
|
|
|
snapshot.total_time_scheduled_kernel = all_processes->total_time_scheduled_kernel;
|
2021-07-14 21:05:59 +03:00
|
|
|
|
2019-02-04 12:28:12 +03:00
|
|
|
return snapshot;
|
|
|
|
}
|
|
|
|
|
2020-07-27 18:00:53 +03:00
|
|
|
static bool g_window_size_changed = true;
|
|
|
|
static struct winsize g_window_size;
|
|
|
|
|
2021-11-29 21:39:24 +03:00
|
|
|
static void parse_args(Main::Arguments arguments, TopOption& top_option)
|
2021-06-26 22:31:31 +03:00
|
|
|
{
|
|
|
|
Core::ArgsParser::Option sort_by_option {
|
2022-07-12 23:13:38 +03:00
|
|
|
Core::ArgsParser::OptionArgumentMode::Required,
|
2021-06-26 22:31:31 +03:00
|
|
|
"Sort by field [pid, tid, pri, user, state, virt, phys, cpu, name]",
|
|
|
|
"sort-by",
|
|
|
|
's',
|
|
|
|
nullptr,
|
2022-04-01 20:58:27 +03:00
|
|
|
[&top_option](char const* s) {
|
2022-07-11 22:53:29 +03:00
|
|
|
StringView sort_by_option { s, strlen(s) };
|
2021-06-26 22:31:31 +03:00
|
|
|
if (sort_by_option == "pid"sv)
|
|
|
|
top_option.sort_by = TopOption::SortBy::Pid;
|
|
|
|
else if (sort_by_option == "tid"sv)
|
|
|
|
top_option.sort_by = TopOption::SortBy::Tid;
|
|
|
|
else if (sort_by_option == "pri"sv)
|
|
|
|
top_option.sort_by = TopOption::SortBy::Priority;
|
|
|
|
else if (sort_by_option == "user"sv)
|
|
|
|
top_option.sort_by = TopOption::SortBy::UserName;
|
|
|
|
else if (sort_by_option == "state"sv)
|
|
|
|
top_option.sort_by = TopOption::SortBy::State;
|
|
|
|
else if (sort_by_option == "virt"sv)
|
|
|
|
top_option.sort_by = TopOption::SortBy::Virt;
|
|
|
|
else if (sort_by_option == "phys"sv)
|
|
|
|
top_option.sort_by = TopOption::SortBy::Phys;
|
|
|
|
else if (sort_by_option == "cpu"sv)
|
|
|
|
top_option.sort_by = TopOption::SortBy::Cpu;
|
|
|
|
else if (sort_by_option == "name"sv)
|
|
|
|
top_option.sort_by = TopOption::SortBy::Name;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
|
|
|
|
|
|
|
args_parser.set_general_help("Display information about processes");
|
|
|
|
args_parser.add_option(top_option.delay_time, "Delay time interval in seconds", "delay-time", 'd', nullptr);
|
|
|
|
args_parser.add_option(move(sort_by_option));
|
2021-11-29 21:39:24 +03:00
|
|
|
args_parser.parse(arguments);
|
2021-06-26 22:31:31 +03:00
|
|
|
}
|
|
|
|
|
2022-04-01 22:47:59 +03:00
|
|
|
static bool check_quit()
|
|
|
|
{
|
|
|
|
char c = '\0';
|
|
|
|
read(STDIN_FILENO, &c, sizeof(c));
|
|
|
|
return c == 'q' || c == 'Q';
|
|
|
|
}
|
|
|
|
|
|
|
|
static int g_old_stdin;
|
|
|
|
|
|
|
|
static void restore_stdin()
|
|
|
|
{
|
|
|
|
fcntl(STDIN_FILENO, F_SETFL, g_old_stdin);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void enable_nonblocking_stdin()
|
|
|
|
{
|
|
|
|
g_old_stdin = fcntl(STDIN_FILENO, F_GETFL);
|
|
|
|
fcntl(STDIN_FILENO, F_SETFL, g_old_stdin | O_NONBLOCK);
|
|
|
|
atexit(restore_stdin);
|
|
|
|
}
|
|
|
|
|
2021-11-29 21:39:24 +03:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-02-04 12:28:12 +03:00
|
|
|
{
|
2021-11-29 21:39:24 +03:00
|
|
|
TRY(Core::System::pledge("stdio rpath tty sigaction"));
|
|
|
|
TRY(Core::System::unveil("/proc/all", "r"));
|
|
|
|
TRY(Core::System::unveil("/etc/passwd", "r"));
|
2020-02-18 12:41:37 +03:00
|
|
|
unveil(nullptr, nullptr);
|
|
|
|
|
2020-07-27 18:00:53 +03:00
|
|
|
signal(SIGWINCH, [](int) {
|
|
|
|
g_window_size_changed = true;
|
|
|
|
});
|
|
|
|
|
2021-11-29 21:39:24 +03:00
|
|
|
TRY(Core::System::pledge("stdio rpath tty"));
|
2021-06-26 22:31:31 +03:00
|
|
|
TopOption top_option;
|
2021-11-29 21:39:24 +03:00
|
|
|
parse_args(arguments, top_option);
|
2021-06-26 22:31:31 +03:00
|
|
|
|
2022-04-01 22:47:59 +03:00
|
|
|
enable_nonblocking_stdin();
|
|
|
|
|
2019-11-26 23:25:45 +03:00
|
|
|
Vector<ThreadData*> threads;
|
2019-02-04 12:28:12 +03:00
|
|
|
auto prev = get_snapshot();
|
2019-02-05 11:27:27 +03:00
|
|
|
usleep(10000);
|
2019-02-04 12:28:12 +03:00
|
|
|
for (;;) {
|
2020-07-27 18:00:53 +03:00
|
|
|
if (g_window_size_changed) {
|
2021-11-30 01:08:32 +03:00
|
|
|
TRY(Core::System::ioctl(STDOUT_FILENO, TIOCGWINSZ, &g_window_size));
|
2020-07-27 18:00:53 +03:00
|
|
|
g_window_size_changed = false;
|
|
|
|
}
|
|
|
|
|
2019-02-04 12:28:12 +03:00
|
|
|
auto current = get_snapshot();
|
2021-07-15 06:46:32 +03:00
|
|
|
auto total_scheduled_diff = current.total_time_scheduled - prev.total_time_scheduled;
|
2019-02-04 12:28:12 +03:00
|
|
|
|
|
|
|
printf("\033[3J\033[H\033[2J");
|
2021-05-18 01:47:38 +03:00
|
|
|
printf("\033[47;30m%6s %3s %3s %-9s %-13s %6s %6s %4s %s\033[K\033[0m\n",
|
2019-06-07 12:49:31 +03:00
|
|
|
"PID",
|
2019-11-26 23:25:45 +03:00
|
|
|
"TID",
|
2019-06-07 12:49:31 +03:00
|
|
|
"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) {
|
2019-11-26 23:25:45 +03:00
|
|
|
auto pid_and_tid = it.key;
|
|
|
|
if (pid_and_tid.pid == 0)
|
2019-02-04 12:28:12 +03:00
|
|
|
continue;
|
2019-11-26 23:25:45 +03:00
|
|
|
auto jt = prev.map.find(pid_and_tid);
|
2019-02-04 12:28:12 +03:00
|
|
|
if (jt == prev.map.end())
|
|
|
|
continue;
|
2021-07-15 06:46:32 +03:00
|
|
|
auto time_scheduled_before = (*jt).value.time_scheduled;
|
|
|
|
auto time_scheduled_diff = it.value.time_scheduled - time_scheduled_before;
|
|
|
|
it.value.time_scheduled_since_prev = time_scheduled_diff;
|
|
|
|
it.value.cpu_percent = total_scheduled_diff > 0 ? ((time_scheduled_diff * 100) / total_scheduled_diff) : 0;
|
|
|
|
it.value.cpu_percent_decimal = total_scheduled_diff > 0 ? (((time_scheduled_diff * 1000) / total_scheduled_diff) % 10) : 0;
|
2019-11-26 23:25:45 +03:00
|
|
|
threads.append(&it.value);
|
2019-02-04 12:28:12 +03:00
|
|
|
}
|
|
|
|
|
2021-06-26 22:31:31 +03:00
|
|
|
quick_sort(threads, [&top_option](auto* p1, auto* p2) {
|
|
|
|
switch (top_option.sort_by) {
|
|
|
|
case TopOption::SortBy::Pid:
|
|
|
|
return p2->pid > p1->pid;
|
|
|
|
case TopOption::SortBy::Tid:
|
|
|
|
return p2->tid > p1->tid;
|
|
|
|
case TopOption::SortBy::Priority:
|
|
|
|
return p2->priority > p1->priority;
|
|
|
|
case TopOption::SortBy::UserName:
|
|
|
|
return p2->username > p1->username;
|
|
|
|
case TopOption::SortBy::State:
|
|
|
|
return p2->state > p1->state;
|
|
|
|
case TopOption::SortBy::Virt:
|
|
|
|
return p2->amount_virtual < p1->amount_virtual;
|
|
|
|
case TopOption::SortBy::Phys:
|
|
|
|
return p2->amount_resident < p1->amount_resident;
|
|
|
|
case TopOption::SortBy::Name:
|
|
|
|
return p2->name > p1->name;
|
|
|
|
case TopOption::SortBy::Cpu:
|
2021-07-14 21:05:59 +03:00
|
|
|
return p2->cpu_percent * 10 + p2->cpu_percent_decimal < p1->cpu_percent * 10 + p1->cpu_percent_decimal;
|
2021-06-26 22:31:31 +03:00
|
|
|
default:
|
2021-07-15 06:46:32 +03:00
|
|
|
return p2->time_scheduled_since_prev < p1->time_scheduled_since_prev;
|
2021-06-26 22:31:31 +03:00
|
|
|
}
|
2019-02-04 12:28:12 +03:00
|
|
|
});
|
|
|
|
|
2020-07-27 18:04:47 +03:00
|
|
|
int row = 0;
|
2019-11-26 23:25:45 +03:00
|
|
|
for (auto* thread : threads) {
|
2021-05-18 01:47:38 +03:00
|
|
|
int nprinted = printf("%6d %3d %2u %-9s %-13s %6zu %6zu %2u.%1u ",
|
2019-11-26 23:25:45 +03:00
|
|
|
thread->pid,
|
|
|
|
thread->tid,
|
2019-12-30 20:46:17 +03:00
|
|
|
thread->priority,
|
2019-11-26 23:25:45 +03:00
|
|
|
thread->username.characters(),
|
|
|
|
thread->state.characters(),
|
|
|
|
thread->amount_virtual / 1024,
|
|
|
|
thread->amount_resident / 1024,
|
|
|
|
thread->cpu_percent,
|
2020-07-27 18:00:53 +03:00
|
|
|
thread->cpu_percent_decimal);
|
|
|
|
|
|
|
|
int remaining = g_window_size.ws_col - nprinted;
|
|
|
|
fwrite(thread->name.characters(), 1, max(0, min(remaining, (int)thread->name.length())), stdout);
|
|
|
|
putchar('\n');
|
2020-07-27 18:04:47 +03:00
|
|
|
|
|
|
|
if (++row >= (g_window_size.ws_row - 2))
|
|
|
|
break;
|
2019-02-04 12:28:12 +03:00
|
|
|
}
|
2019-11-26 23:25:45 +03:00
|
|
|
threads.clear_with_capacity();
|
2019-02-04 12:28:12 +03:00
|
|
|
prev = move(current);
|
2022-04-01 22:47:59 +03:00
|
|
|
|
|
|
|
for (int sleep_slice = 0; sleep_slice < top_option.delay_time * 1000; sleep_slice += 100) {
|
|
|
|
if (check_quit())
|
|
|
|
exit(0);
|
|
|
|
usleep(100 * 1000);
|
|
|
|
}
|
2019-02-04 12:28:12 +03:00
|
|
|
}
|
|
|
|
}
|