Serenity Operating System
at master 252 lines 8.6 kB view raw
1/* 2 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> 3 * Copyright (c) 2022, the SerenityOS developers. 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#pragma once 9 10#include <AK/DeprecatedString.h> 11#include <AK/HashMap.h> 12#include <AK/Vector.h> 13#include <LibGUI/Icon.h> 14#include <LibGUI/Model.h> 15#include <LibGUI/ModelIndex.h> 16#include <sys/types.h> 17#include <unistd.h> 18 19class GraphWidget; 20 21class ProcessModel final : public GUI::Model { 22public: 23 enum Column { 24 Icon = 0, 25 Name, 26 PID, 27 TID, 28 CPU, 29 State, 30 User, 31 Virtual, 32 DirtyPrivate, 33 Pledge, 34 Physical, 35 CleanInode, 36 PurgeableVolatile, 37 PurgeableNonvolatile, 38 Veil, 39 Processor, 40 Priority, 41 PPID, 42 PGID, 43 SID, 44 Syscalls, 45 InodeFaults, 46 ZeroFaults, 47 CowFaults, 48 FileReadBytes, 49 FileWriteBytes, 50 UnixSocketReadBytes, 51 UnixSocketWriteBytes, 52 IPv4SocketReadBytes, 53 IPv4SocketWriteBytes, 54 Command, 55 __Count 56 }; 57 58 static ProcessModel& the(); 59 60 static NonnullRefPtr<ProcessModel> create() { return adopt_ref(*new ProcessModel); } 61 virtual ~ProcessModel() override = default; 62 63 virtual int tree_column() const override { return Column::Name; } 64 virtual int row_count(GUI::ModelIndex const&) const override; 65 virtual int column_count(GUI::ModelIndex const&) const override; 66 virtual DeprecatedString column_name(int column) const override; 67 virtual GUI::Variant data(GUI::ModelIndex const&, GUI::ModelRole) const override; 68 virtual GUI::ModelIndex index(int row, int column, GUI::ModelIndex const& parent = {}) const override; 69 virtual GUI::ModelIndex parent_index(GUI::ModelIndex const&) const override; 70 virtual bool is_searchable() const override { return true; } 71 virtual Vector<GUI::ModelIndex> matches(StringView, unsigned = MatchesFlag::AllMatching, GUI::ModelIndex const& = GUI::ModelIndex()) override; 72 virtual bool is_column_sortable(int column_index) const override { return column_index != Column::Icon; } 73 void update(); 74 bool is_default_column(int index) const; 75 76 struct CpuInfo { 77 u32 id; 78 float total_cpu_percent { 0.0 }; 79 float total_cpu_percent_kernel { 0.0 }; 80 81 explicit CpuInfo(u32 id) 82 : id(id) 83 { 84 } 85 }; 86 87 Function<void(Vector<NonnullOwnPtr<CpuInfo>> const&)> on_cpu_info_change; 88 Function<void(int process_count, int thread_count)> on_state_update; 89 90 Vector<NonnullOwnPtr<CpuInfo>> const& cpus() const { return m_cpus; } 91 92private: 93 ProcessModel(); 94 95 struct Process; 96 97 struct ThreadState { 98 pid_t tid { 0 }; 99 pid_t pid { 0 }; 100 pid_t ppid { 0 }; 101 pid_t pgid { 0 }; 102 pid_t sid { 0 }; 103 u64 time_user { 0 }; 104 u64 time_kernel { 0 }; 105 bool kernel { false }; 106 DeprecatedString executable { "" }; 107 DeprecatedString name { "" }; 108 DeprecatedString command { "" }; 109 uid_t uid { 0 }; 110 DeprecatedString state { "" }; 111 DeprecatedString user { "" }; 112 DeprecatedString pledge { "" }; 113 DeprecatedString veil { "" }; 114 u32 cpu { 0 }; 115 u32 priority { 0 }; 116 size_t amount_virtual { 0 }; 117 size_t amount_resident { 0 }; 118 size_t amount_dirty_private { 0 }; 119 size_t amount_clean_inode { 0 }; 120 size_t amount_purgeable_volatile { 0 }; 121 size_t amount_purgeable_nonvolatile { 0 }; 122 unsigned syscall_count { 0 }; 123 unsigned inode_faults { 0 }; 124 unsigned zero_faults { 0 }; 125 unsigned cow_faults { 0 }; 126 unsigned unix_socket_read_bytes { 0 }; 127 unsigned unix_socket_write_bytes { 0 }; 128 unsigned ipv4_socket_read_bytes { 0 }; 129 unsigned ipv4_socket_write_bytes { 0 }; 130 unsigned file_read_bytes { 0 }; 131 unsigned file_write_bytes { 0 }; 132 float cpu_percent { 0 }; 133 float cpu_percent_kernel { 0 }; 134 Process& process; 135 136 ThreadState(Process& argument_process) 137 : process(argument_process) 138 { 139 } 140 ThreadState(ThreadState&& other) = default; 141 ThreadState& operator=(ThreadState&& other) 142 { 143 this->tid = other.tid; 144 this->pid = other.pid; 145 this->ppid = other.ppid; 146 this->pgid = other.pgid; 147 this->sid = other.sid; 148 this->time_user = other.time_user; 149 this->time_kernel = other.time_kernel; 150 this->kernel = other.kernel; 151 this->executable = other.executable; 152 this->name = other.name; 153 this->command = other.command; 154 this->uid = other.uid; 155 this->state = other.state; 156 this->user = other.user; 157 this->pledge = other.pledge; 158 this->veil = other.veil; 159 this->cpu = other.cpu; 160 this->priority = other.priority; 161 this->amount_virtual = other.amount_virtual; 162 this->amount_resident = other.amount_resident; 163 this->amount_dirty_private = other.amount_dirty_private; 164 this->amount_clean_inode = other.amount_clean_inode; 165 this->amount_purgeable_volatile = other.amount_purgeable_volatile; 166 this->amount_purgeable_nonvolatile = other.amount_purgeable_nonvolatile; 167 this->syscall_count = other.syscall_count; 168 this->inode_faults = other.inode_faults; 169 this->zero_faults = other.zero_faults; 170 this->cow_faults = other.cow_faults; 171 this->unix_socket_read_bytes = other.unix_socket_read_bytes; 172 this->unix_socket_write_bytes = other.unix_socket_write_bytes; 173 this->ipv4_socket_read_bytes = other.ipv4_socket_read_bytes; 174 this->ipv4_socket_write_bytes = other.ipv4_socket_write_bytes; 175 this->file_read_bytes = other.file_read_bytes; 176 this->file_write_bytes = other.file_write_bytes; 177 this->cpu_percent = other.cpu_percent; 178 this->cpu_percent_kernel = other.cpu_percent_kernel; 179 this->process = other.process; 180 181 return *this; 182 } 183 ~ThreadState() = default; 184 }; 185 186 struct Thread : public RefCounted<Thread> { 187 ThreadState current_state; 188 ThreadState previous_state; 189 190 Thread(Process& process) 191 : current_state(process) 192 , previous_state(process) 193 { 194 } 195 196 bool operator==(Thread const& other) const 197 { 198 return current_state.tid == other.current_state.tid; 199 } 200 201 bool is_main_thread() const 202 { 203 return current_state.tid == current_state.process.pid; 204 } 205 }; 206 207 struct Process { 208 pid_t pid; 209 Vector<NonnullRefPtr<Thread>> threads; 210 211 bool operator==(Process const& other) const 212 { 213 return this->pid == other.pid; 214 } 215 216 Optional<NonnullRefPtr<Thread>> main_thread() const 217 { 218 return threads.first_matching([this](auto const thread) { return thread->current_state.tid == pid; }); 219 } 220 221 // Return anything but the main thread; therefore, valid indices are anything up to threads.size()-1 exclusive. 222 Thread const& non_main_thread(size_t index) const 223 { 224 auto main_thread_index = -1; 225 for (size_t i = 0; i < threads.size(); ++i) { 226 if (threads[i]->is_main_thread()) { 227 main_thread_index = static_cast<int>(i); 228 break; 229 } 230 } 231 VERIFY(main_thread_index >= 0); 232 // Shift all indices starting from the main thread's index upwards, so that the user doesn't have to worry about index discontinuities. 233 if (index >= static_cast<size_t>(main_thread_index)) 234 return threads[index + 1]; 235 return threads[index]; 236 } 237 }; 238 239 GUI::Icon icon_for(Thread const& thread) const; 240 241 int thread_model_row(Thread const& thread) const; 242 243 // The thread list contains the same threads as the Process structs. 244 HashMap<int, NonnullRefPtr<Thread>> m_threads; 245 Vector<NonnullOwnPtr<Process>> m_processes; 246 Vector<NonnullOwnPtr<CpuInfo>> m_cpus; 247 RefPtr<Core::DeprecatedFile> m_proc_all; 248 GUI::Icon m_kernel_process_icon; 249 u64 m_total_time_scheduled { 0 }; 250 u64 m_total_time_scheduled_kernel { 0 }; 251 bool m_has_total_scheduled_time { false }; 252};