From a1e133cc6bf8617d8299e4f1ae8df7b7d5289205 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 23 May 2021 11:08:32 +0200 Subject: [PATCH] LibCore: Make ProcessStatisticsReader return results in a Vector The HashMap API was overkill and made using this less ergonomic than it should be. --- Userland/Applets/ResourceGraph/main.cpp | 4 +-- .../SystemMonitor/ProcessModel.cpp | 34 +++++++++--------- Userland/DevTools/Profiler/main.cpp | 4 +-- .../LibCore/ProcessStatisticsReader.cpp | 12 +++---- .../LibCore/ProcessStatisticsReader.h | 7 ++-- .../LibGUI/RunningProcessesModel.cpp | 11 +++--- Userland/Utilities/killall.cpp | 8 ++--- Userland/Utilities/lsof.cpp | 18 +++++----- Userland/Utilities/pidof.cpp | 8 ++--- Userland/Utilities/ps.cpp | 15 ++++---- Userland/Utilities/top.cpp | 35 +++++++++---------- Userland/Utilities/w.cpp | 6 ++-- 12 files changed, 79 insertions(+), 83 deletions(-) diff --git a/Userland/Applets/ResourceGraph/main.cpp b/Userland/Applets/ResourceGraph/main.cpp index ec2844f9e6d..801f5822012 100644 --- a/Userland/Applets/ResourceGraph/main.cpp +++ b/Userland/Applets/ResourceGraph/main.cpp @@ -130,8 +130,8 @@ private: return false; for (auto& it : all_processes.value()) { - for (auto& jt : it.value.threads) { - if (it.value.pid == 0) + for (auto& jt : it.threads) { + if (it.pid == 0) idle += jt.ticks_user + jt.ticks_kernel; else busy += jt.ticks_user + jt.ticks_kernel; diff --git a/Userland/Applications/SystemMonitor/ProcessModel.cpp b/Userland/Applications/SystemMonitor/ProcessModel.cpp index 3fddf1e9e67..d0a1b55cedb 100644 --- a/Userland/Applications/SystemMonitor/ProcessModel.cpp +++ b/Userland/Applications/SystemMonitor/ProcessModel.cpp @@ -328,14 +328,14 @@ void ProcessModel::update() HashTable live_tids; u64 sum_ticks_scheduled = 0, sum_ticks_scheduled_kernel = 0; if (all_processes.has_value()) { - for (auto& it : all_processes.value()) { - for (auto& thread : it.value.threads) { + for (auto& process : all_processes.value()) { + for (auto& thread : process.threads) { ThreadState state; - state.kernel = it.value.kernel; - state.pid = it.value.pid; - state.user = it.value.username; - state.pledge = it.value.pledge; - state.veil = it.value.veil; + state.kernel = process.kernel; + state.pid = process.pid; + state.user = process.username; + state.pledge = process.pledge; + state.veil = process.veil; state.syscall_count = thread.syscall_count; state.inode_faults = thread.inode_faults; state.zero_faults = thread.zero_faults; @@ -346,20 +346,20 @@ void ProcessModel::update() state.ipv4_socket_write_bytes = thread.ipv4_socket_write_bytes; state.file_read_bytes = thread.file_read_bytes; state.file_write_bytes = thread.file_write_bytes; - state.amount_virtual = it.value.amount_virtual; - state.amount_resident = it.value.amount_resident; - state.amount_dirty_private = it.value.amount_dirty_private; - state.amount_clean_inode = it.value.amount_clean_inode; - state.amount_purgeable_volatile = it.value.amount_purgeable_volatile; - state.amount_purgeable_nonvolatile = it.value.amount_purgeable_nonvolatile; + state.amount_virtual = process.amount_virtual; + state.amount_resident = process.amount_resident; + state.amount_dirty_private = process.amount_dirty_private; + state.amount_clean_inode = process.amount_clean_inode; + state.amount_purgeable_volatile = process.amount_purgeable_volatile; + state.amount_purgeable_nonvolatile = process.amount_purgeable_nonvolatile; state.name = thread.name; - state.executable = it.value.executable; + state.executable = process.executable; - state.ppid = it.value.ppid; + state.ppid = process.ppid; state.tid = thread.tid; - state.pgid = it.value.pgid; - state.sid = it.value.sid; + state.pgid = process.pgid; + state.sid = process.sid; state.ticks_user = thread.ticks_user; state.ticks_kernel = thread.ticks_kernel; state.cpu = thread.cpu; diff --git a/Userland/DevTools/Profiler/main.cpp b/Userland/DevTools/Profiler/main.cpp index 6cf7f55beb2..6f85fef7a00 100644 --- a/Userland/DevTools/Profiler/main.cpp +++ b/Userland/DevTools/Profiler/main.cpp @@ -270,8 +270,8 @@ bool generate_profile(pid_t& pid) auto all_processes = Core::ProcessStatisticsReader::get_all(); if (all_processes.has_value()) { - if (auto it = all_processes.value().find(pid); it != all_processes.value().end()) - process_name = it->value.name; + if (auto it = all_processes.value().find_if([&](auto& entry) { return entry.pid == pid; }); it != all_processes.value().end()) + process_name = it->name; else process_name = "(unknown)"; } else { diff --git a/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp b/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp index 585088e92e5..1abdf6b5100 100644 --- a/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp +++ b/Userland/Libraries/LibCore/ProcessStatisticsReader.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -17,7 +17,7 @@ namespace Core { HashMap ProcessStatisticsReader::s_usernames; -Optional> ProcessStatisticsReader::get_all(RefPtr& proc_all_file) +Optional> ProcessStatisticsReader::get_all(RefPtr& proc_all_file) { if (proc_all_file) { if (!proc_all_file->seek(0, Core::SeekMode::SetPosition)) { @@ -32,7 +32,7 @@ Optional> ProcessStatisticsReader::get_a } } - HashMap map; + Vector processes; auto file_contents = proc_all_file->read_all(); auto json = JsonValue::from_string(file_contents); @@ -93,13 +93,13 @@ Optional> ProcessStatisticsReader::get_a // and synthetic data last process.username = username_from_uid(process.uid); - map.set(process.pid, process); + processes.append(move(process)); }); - return map; + return processes; } -Optional> ProcessStatisticsReader::get_all() +Optional> ProcessStatisticsReader::get_all() { RefPtr proc_all_file; return get_all(proc_all_file); diff --git a/Userland/Libraries/LibCore/ProcessStatisticsReader.h b/Userland/Libraries/LibCore/ProcessStatisticsReader.h index 381f6e759bf..f88b823f5a5 100644 --- a/Userland/Libraries/LibCore/ProcessStatisticsReader.h +++ b/Userland/Libraries/LibCore/ProcessStatisticsReader.h @@ -1,12 +1,11 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once -#include #include #include #include @@ -67,8 +66,8 @@ struct ProcessStatistics { class ProcessStatisticsReader { public: - static Optional> get_all(RefPtr&); - static Optional> get_all(); + static Optional> get_all(RefPtr&); + static Optional> get_all(); private: static String username_from_uid(uid_t); diff --git a/Userland/Libraries/LibGUI/RunningProcessesModel.cpp b/Userland/Libraries/LibGUI/RunningProcessesModel.cpp index 653c4901011..c7a9d321e4b 100644 --- a/Userland/Libraries/LibGUI/RunningProcessesModel.cpp +++ b/Userland/Libraries/LibGUI/RunningProcessesModel.cpp @@ -27,15 +27,14 @@ void RunningProcessesModel::update() { m_processes.clear(); - Core::ProcessStatisticsReader reader; - auto processes = reader.get_all(); + auto processes = Core::ProcessStatisticsReader::get_all(); if (processes.has_value()) { for (auto& it : processes.value()) { Process process; - process.pid = it.value.pid; - process.uid = it.value.uid; - process.icon = FileIconProvider::icon_for_executable(it.value.executable).bitmap_for_size(16); - process.name = it.value.name; + process.pid = it.pid; + process.uid = it.uid; + process.icon = FileIconProvider::icon_for_executable(it.executable).bitmap_for_size(16); + process.name = it.name; m_processes.append(move(process)); } } diff --git a/Userland/Utilities/killall.cpp b/Userland/Utilities/killall.cpp index 14f37232d48..fe1218a3938 100644 --- a/Userland/Utilities/killall.cpp +++ b/Userland/Utilities/killall.cpp @@ -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"); } diff --git a/Userland/Utilities/lsof.cpp b/Userland/Utilities/lsof.cpp index 09481fc9fd4..b639410f190 100644 --- a/Userland/Utilities/lsof.cpp +++ b/Userland/Utilities/lsof.cpp @@ -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; })); } } diff --git a/Userland/Utilities/pidof.cpp b/Userland/Utilities/pidof.cpp index 9a98fcb79b6..07603e9a5a4 100644 --- a/Userland/Utilities/pidof.cpp +++ b/Userland/Utilities/pidof.cpp @@ -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) diff --git a/Userland/Utilities/ps.cpp b/Userland/Utilities/ps.cpp index 3b83f3da237..b99a412276b 100644 --- a/Userland/Utilities/ps.cpp +++ b/Userland/Utilities/ps.cpp @@ -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); diff --git a/Userland/Utilities/top.cpp b/Userland/Utilities/top.cpp index d9e27409411..adce57d9b4f 100644 --- a/Userland/Utilities/top.cpp +++ b/Userland/Utilities/top.cpp @@ -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)); } } diff --git a/Userland/Utilities/w.cpp b/Userland/Utilities/w.cpp index 1014ddfa5d0..66dc7915b20 100644 --- a/Userland/Utilities/w.cpp +++ b/Userland/Utilities/w.cpp @@ -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",