ProcessModel.h 7.9 KB

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