1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 11:37:45 +00:00

LibCore: Make ProcessStatisticsReader return results in a Vector

The HashMap API was overkill and made using this less ergonomic than
it should be.
This commit is contained in:
Andreas Kling 2021-05-23 11:08:32 +02:00
parent a345a1f4a1
commit a1e133cc6b
12 changed files with 79 additions and 83 deletions

View file

@ -20,13 +20,13 @@ static void print_usage_and_exit()
static int kill_all(const String& process_name, const unsigned signum)
{
auto processes = Core::ProcessStatisticsReader().get_all();
auto processes = Core::ProcessStatisticsReader::get_all();
if (!processes.has_value())
return 1;
for (auto& it : processes.value()) {
if (it.value.name == process_name) {
int ret = kill(it.value.pid, signum);
for (auto& process : processes.value()) {
if (process.name == process_name) {
int ret = kill(process.pid, signum);
if (ret < 0)
perror("kill");
}

View file

@ -150,22 +150,22 @@ int main(int argc, char* argv[])
if (!processes.has_value())
return 1;
if (arg_pid == -1) {
for (auto process : processes.value()) {
if (process.key == 0)
for (auto& process : processes.value()) {
if (process.pid == 0)
continue;
auto open_files = get_open_files_by_pid(process.key);
auto open_files = get_open_files_by_pid(process.pid);
if (open_files.is_empty())
continue;
for (auto file : open_files) {
for (auto& file : open_files) {
if ((arg_all_processes)
|| (arg_fd != -1 && file.fd == arg_fd)
|| (arg_uid_int != -1 && (int)process.value.uid == arg_uid_int)
|| (arg_uid != nullptr && process.value.username == arg_uid)
|| (arg_pgid != -1 && (int)process.value.pgid == arg_pgid)
|| (arg_uid_int != -1 && (int)process.uid == arg_uid_int)
|| (arg_uid != nullptr && process.username == arg_uid)
|| (arg_pgid != -1 && (int)process.pgid == arg_pgid)
|| (arg_filename != nullptr && file.name == arg_filename))
display_entry(file, process.value);
display_entry(file, process);
}
}
} else {
@ -175,7 +175,7 @@ int main(int argc, char* argv[])
return 0;
for (auto file : open_files) {
display_entry(file, processes.value().get(arg_pid).value());
display_entry(file, *processes->find_if([&](auto& entry) { return entry.pid == arg_pid; }));
}
}

View file

@ -19,14 +19,14 @@ static int pid_of(const String& process_name, bool single_shot, bool omit_pid, p
{
bool displayed_at_least_one = false;
auto processes = Core::ProcessStatisticsReader().get_all();
auto processes = Core::ProcessStatisticsReader::get_all();
if (!processes.has_value())
return 1;
for (auto& it : processes.value()) {
if (it.value.name == process_name) {
if (!omit_pid || it.value.pid != pid) {
printf(" %d" + (displayed_at_least_one ? 0 : 1), it.value.pid);
if (it.name == process_name) {
if (!omit_pid || it.pid != pid) {
printf(" %d" + (displayed_at_least_one ? 0 : 1), it.pid);
displayed_at_least_one = true;
if (single_shot)

View file

@ -103,9 +103,8 @@ int main(int argc, char** argv)
if (!all_processes.has_value())
return 1;
for (const auto& it : all_processes.value()) {
const auto& proc = it.value;
auto tty = proc.tty;
for (auto const& process : all_processes.value()) {
auto tty = process.tty;
if (!every_process_flag && tty != this_tty)
continue;
@ -115,20 +114,20 @@ int main(int argc, char** argv)
else
tty = "n/a";
auto* state = proc.threads.is_empty() ? "Zombie" : proc.threads.first().state.characters();
auto* state = process.threads.is_empty() ? "Zombie" : process.threads.first().state.characters();
if (uid_column != -1)
columns[uid_column].buffer = proc.username;
columns[uid_column].buffer = process.username;
if (pid_column != -1)
columns[pid_column].buffer = String::number(proc.pid);
columns[pid_column].buffer = String::number(process.pid);
if (ppid_column != -1)
columns[ppid_column].buffer = String::number(proc.ppid);
columns[ppid_column].buffer = String::number(process.ppid);
if (tty_column != -1)
columns[tty_column].buffer = tty;
if (state_column != -1)
columns[state_column].buffer = state;
if (cmd_column != -1)
columns[cmd_column].buffer = proc.name;
columns[cmd_column].buffer = process.name;
for (auto& column : columns)
print_column(column, column.buffer);

View file

@ -77,25 +77,24 @@ static Snapshot get_snapshot()
return {};
Snapshot snapshot;
for (auto& it : all_processes.value()) {
auto& stats = it.value;
for (auto& thread : stats.threads) {
for (auto& process : all_processes.value()) {
for (auto& thread : process.threads) {
snapshot.sum_times_scheduled += thread.times_scheduled;
ThreadData thread_data;
thread_data.tid = thread.tid;
thread_data.pid = stats.pid;
thread_data.pgid = stats.pgid;
thread_data.pgp = stats.pgp;
thread_data.sid = stats.sid;
thread_data.uid = stats.uid;
thread_data.gid = stats.gid;
thread_data.ppid = stats.ppid;
thread_data.nfds = stats.nfds;
thread_data.name = stats.name;
thread_data.tty = stats.tty;
thread_data.amount_virtual = stats.amount_virtual;
thread_data.amount_resident = stats.amount_resident;
thread_data.amount_shared = stats.amount_shared;
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;
thread_data.syscall_count = thread.syscall_count;
thread_data.inode_faults = thread.inode_faults;
thread_data.zero_faults = thread.zero_faults;
@ -103,9 +102,9 @@ static Snapshot get_snapshot()
thread_data.times_scheduled = thread.times_scheduled;
thread_data.priority = thread.priority;
thread_data.state = thread.state;
thread_data.username = stats.username;
thread_data.username = process.username;
snapshot.map.set({ stats.pid, thread.tid }, move(thread_data));
snapshot.map.set({ process.pid, thread.tid }, move(thread_data));
}
}

View file

@ -95,9 +95,9 @@ int main()
String what = "n/a";
for (auto& it : process_statistics.value()) {
if (it.value.tty == tty && it.value.pid == it.value.pgid)
what = it.value.name;
for (auto& process : process_statistics.value()) {
if (process.tty == tty && process.pid == process.pgid)
what = process.name;
}
printf("%-10s %-12s %-16s %-6s %s\n",