ProcessModel.h 8.3 KB

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