Serenity Operating System
at master 310 lines 10 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <AK/DeprecatedString.h> 8#include <AK/HashMap.h> 9#include <AK/JsonArray.h> 10#include <AK/JsonObject.h> 11#include <AK/QuickSort.h> 12#include <AK/Vector.h> 13#include <LibCore/ArgsParser.h> 14#include <LibCore/ProcessStatisticsReader.h> 15#include <LibCore/System.h> 16#include <LibMain/Main.h> 17#include <signal.h> 18#include <stdio.h> 19#include <stdlib.h> 20#include <sys/ioctl.h> 21#include <unistd.h> 22 23struct TopOption { 24 enum class SortBy { 25 Pid, 26 Tid, 27 Priority, 28 UserName, 29 State, 30 Virt, 31 Phys, 32 Cpu, 33 Name 34 }; 35 36 SortBy sort_by { SortBy::Cpu }; 37 int delay_time { 1 }; 38}; 39 40struct ThreadData { 41 int tid; 42 pid_t pid; 43 pid_t pgid; 44 pid_t pgp; 45 pid_t sid; 46 uid_t uid; 47 gid_t gid; 48 pid_t ppid; 49 unsigned nfds; 50 DeprecatedString name; 51 DeprecatedString tty; 52 size_t amount_virtual; 53 size_t amount_resident; 54 size_t amount_shared; 55 unsigned syscall_count; 56 unsigned inode_faults; 57 unsigned zero_faults; 58 unsigned cow_faults; 59 u64 time_scheduled; 60 61 u64 time_scheduled_since_prev { 0 }; 62 unsigned cpu_percent { 0 }; 63 unsigned cpu_percent_decimal { 0 }; 64 65 u32 priority; 66 DeprecatedString username; 67 DeprecatedString state; 68}; 69 70struct PidAndTid { 71 bool operator==(PidAndTid const& other) const 72 { 73 return pid == other.pid && tid == other.tid; 74 } 75 pid_t pid; 76 int tid; 77}; 78 79namespace AK { 80template<> 81struct Traits<PidAndTid> : public GenericTraits<PidAndTid> { 82 static unsigned hash(PidAndTid const& value) { return pair_int_hash(value.pid, value.tid); } 83}; 84} 85 86struct Snapshot { 87 HashMap<PidAndTid, ThreadData> map; 88 u64 total_time_scheduled { 0 }; 89 u64 total_time_scheduled_kernel { 0 }; 90}; 91 92static ErrorOr<Snapshot> get_snapshot() 93{ 94 auto all_processes = TRY(Core::ProcessStatisticsReader::get_all()); 95 96 Snapshot snapshot; 97 for (auto& process : all_processes.processes) { 98 for (auto& thread : process.threads) { 99 ThreadData thread_data; 100 thread_data.tid = thread.tid; 101 thread_data.pid = process.pid; 102 thread_data.pgid = process.pgid; 103 thread_data.pgp = process.pgp; 104 thread_data.sid = process.sid; 105 thread_data.uid = process.uid; 106 thread_data.gid = process.gid; 107 thread_data.ppid = process.ppid; 108 thread_data.nfds = process.nfds; 109 thread_data.name = process.name; 110 thread_data.tty = process.tty; 111 thread_data.amount_virtual = process.amount_virtual; 112 thread_data.amount_resident = process.amount_resident; 113 thread_data.amount_shared = process.amount_shared; 114 thread_data.syscall_count = thread.syscall_count; 115 thread_data.inode_faults = thread.inode_faults; 116 thread_data.zero_faults = thread.zero_faults; 117 thread_data.cow_faults = thread.cow_faults; 118 thread_data.time_scheduled = (u64)thread.time_user + (u64)thread.time_kernel; 119 thread_data.priority = thread.priority; 120 thread_data.state = thread.state; 121 thread_data.username = process.username; 122 123 snapshot.map.set({ process.pid, thread.tid }, move(thread_data)); 124 } 125 } 126 127 snapshot.total_time_scheduled = all_processes.total_time_scheduled; 128 snapshot.total_time_scheduled_kernel = all_processes.total_time_scheduled_kernel; 129 130 return snapshot; 131} 132 133static bool g_window_size_changed = true; 134static struct winsize g_window_size; 135 136static void parse_args(Main::Arguments arguments, TopOption& top_option) 137{ 138 Core::ArgsParser::Option sort_by_option { 139 Core::ArgsParser::OptionArgumentMode::Required, 140 "Sort by field [pid, tid, pri, user, state, virt, phys, cpu, name]", 141 "sort-by", 142 's', 143 nullptr, 144 [&top_option](StringView sort_by_option) { 145 if (sort_by_option == "pid"sv) 146 top_option.sort_by = TopOption::SortBy::Pid; 147 else if (sort_by_option == "tid"sv) 148 top_option.sort_by = TopOption::SortBy::Tid; 149 else if (sort_by_option == "pri"sv) 150 top_option.sort_by = TopOption::SortBy::Priority; 151 else if (sort_by_option == "user"sv) 152 top_option.sort_by = TopOption::SortBy::UserName; 153 else if (sort_by_option == "state"sv) 154 top_option.sort_by = TopOption::SortBy::State; 155 else if (sort_by_option == "virt"sv) 156 top_option.sort_by = TopOption::SortBy::Virt; 157 else if (sort_by_option == "phys"sv) 158 top_option.sort_by = TopOption::SortBy::Phys; 159 else if (sort_by_option == "cpu"sv) 160 top_option.sort_by = TopOption::SortBy::Cpu; 161 else if (sort_by_option == "name"sv) 162 top_option.sort_by = TopOption::SortBy::Name; 163 else 164 return false; 165 return true; 166 } 167 }; 168 169 Core::ArgsParser args_parser; 170 171 args_parser.set_general_help("Display information about processes"); 172 args_parser.add_option(top_option.delay_time, "Delay time interval in seconds", "delay-time", 'd', nullptr); 173 args_parser.add_option(move(sort_by_option)); 174 args_parser.parse(arguments); 175} 176 177static bool check_quit() 178{ 179 char c = '\0'; 180 read(STDIN_FILENO, &c, sizeof(c)); 181 return c == 'q' || c == 'Q'; 182} 183 184static int g_old_stdin; 185 186static void restore_stdin() 187{ 188 fcntl(STDIN_FILENO, F_SETFL, g_old_stdin); 189} 190 191static void enable_nonblocking_stdin() 192{ 193 g_old_stdin = fcntl(STDIN_FILENO, F_GETFL); 194 fcntl(STDIN_FILENO, F_SETFL, g_old_stdin | O_NONBLOCK); 195 atexit(restore_stdin); 196} 197 198ErrorOr<int> serenity_main(Main::Arguments arguments) 199{ 200 TRY(Core::System::pledge("stdio rpath tty sigaction")); 201 TRY(Core::System::unveil("/sys/kernel/processes", "r")); 202 TRY(Core::System::unveil("/etc/passwd", "r")); 203 unveil(nullptr, nullptr); 204 205 TRY(Core::System::signal(SIGWINCH, [](int) { 206 g_window_size_changed = true; 207 })); 208 TRY(Core::System::signal(SIGINT, [](int) { 209 exit(0); 210 })); 211 TRY(Core::System::pledge("stdio rpath tty")); 212 213 TopOption top_option; 214 parse_args(arguments, top_option); 215 216 enable_nonblocking_stdin(); 217 218 Vector<ThreadData*> threads; 219 auto prev = TRY(get_snapshot()); 220 usleep(10000); 221 for (;;) { 222 if (g_window_size_changed) { 223 TRY(Core::System::ioctl(STDOUT_FILENO, TIOCGWINSZ, &g_window_size)); 224 g_window_size_changed = false; 225 } 226 227 auto current = TRY(get_snapshot()); 228 auto total_scheduled_diff = current.total_time_scheduled - prev.total_time_scheduled; 229 230 printf("\033[3J\033[H\033[2J"); 231 printf("\033[47;30m%6s %3s %3s %-9s %-13s %6s %6s %4s %s\033[K\033[0m\n", 232 "PID", 233 "TID", 234 "PRI", 235 "USER", 236 "STATE", 237 "VIRT", 238 "PHYS", 239 "%CPU", 240 "NAME"); 241 for (auto& it : current.map) { 242 auto pid_and_tid = it.key; 243 if (pid_and_tid.pid == 0) 244 continue; 245 auto jt = prev.map.find(pid_and_tid); 246 if (jt == prev.map.end()) 247 continue; 248 auto time_scheduled_before = (*jt).value.time_scheduled; 249 auto time_scheduled_diff = it.value.time_scheduled - time_scheduled_before; 250 it.value.time_scheduled_since_prev = time_scheduled_diff; 251 it.value.cpu_percent = total_scheduled_diff > 0 ? ((time_scheduled_diff * 100) / total_scheduled_diff) : 0; 252 it.value.cpu_percent_decimal = total_scheduled_diff > 0 ? (((time_scheduled_diff * 1000) / total_scheduled_diff) % 10) : 0; 253 threads.append(&it.value); 254 } 255 256 quick_sort(threads, [&top_option](auto* p1, auto* p2) { 257 switch (top_option.sort_by) { 258 case TopOption::SortBy::Pid: 259 return p2->pid > p1->pid; 260 case TopOption::SortBy::Tid: 261 return p2->tid > p1->tid; 262 case TopOption::SortBy::Priority: 263 return p2->priority > p1->priority; 264 case TopOption::SortBy::UserName: 265 return p2->username > p1->username; 266 case TopOption::SortBy::State: 267 return p2->state > p1->state; 268 case TopOption::SortBy::Virt: 269 return p2->amount_virtual < p1->amount_virtual; 270 case TopOption::SortBy::Phys: 271 return p2->amount_resident < p1->amount_resident; 272 case TopOption::SortBy::Name: 273 return p2->name > p1->name; 274 case TopOption::SortBy::Cpu: 275 return p2->cpu_percent * 10 + p2->cpu_percent_decimal < p1->cpu_percent * 10 + p1->cpu_percent_decimal; 276 default: 277 return p2->time_scheduled_since_prev < p1->time_scheduled_since_prev; 278 } 279 }); 280 281 int row = 0; 282 for (auto* thread : threads) { 283 int nprinted = printf("%6d %3d %2u %-9s %-13s %6zu %6zu %2u.%1u ", 284 thread->pid, 285 thread->tid, 286 thread->priority, 287 thread->username.characters(), 288 thread->state.characters(), 289 thread->amount_virtual / 1024, 290 thread->amount_resident / 1024, 291 thread->cpu_percent, 292 thread->cpu_percent_decimal); 293 294 int remaining = g_window_size.ws_col - nprinted; 295 fwrite(thread->name.characters(), 1, max(0, min(remaining, (int)thread->name.length())), stdout); 296 putchar('\n'); 297 298 if (++row >= (g_window_size.ws_row - 2)) 299 break; 300 } 301 threads.clear_with_capacity(); 302 prev = move(current); 303 304 for (int sleep_slice = 0; sleep_slice < top_option.delay_time * 1000; sleep_slice += 100) { 305 if (check_quit()) 306 exit(0); 307 usleep(100 * 1000); 308 } 309 } 310}