ProcessModel.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #include "ProcessModel.h"
  2. #include "GraphWidget.h"
  3. #include <AK/JsonArray.h>
  4. #include <AK/JsonObject.h>
  5. #include <AK/JsonValue.h>
  6. #include <LibC/SharedBuffer.h>
  7. #include <LibCore/CProcessStatisticsReader.h>
  8. #include <fcntl.h>
  9. #include <stdio.h>
  10. static ProcessModel* s_the;
  11. ProcessModel& ProcessModel::the()
  12. {
  13. ASSERT(s_the);
  14. return *s_the;
  15. }
  16. ProcessModel::ProcessModel()
  17. {
  18. ASSERT(!s_the);
  19. s_the = this;
  20. m_generic_process_icon = GraphicsBitmap::load_from_file("/res/icons/gear16.png");
  21. m_high_priority_icon = GraphicsBitmap::load_from_file("/res/icons/highpriority16.png");
  22. m_low_priority_icon = GraphicsBitmap::load_from_file("/res/icons/lowpriority16.png");
  23. m_normal_priority_icon = GraphicsBitmap::load_from_file("/res/icons/normalpriority16.png");
  24. }
  25. ProcessModel::~ProcessModel()
  26. {
  27. }
  28. int ProcessModel::row_count(const GModelIndex&) const
  29. {
  30. return m_pids.size();
  31. }
  32. int ProcessModel::column_count(const GModelIndex&) const
  33. {
  34. return Column::__Count;
  35. }
  36. String ProcessModel::column_name(int column) const
  37. {
  38. switch (column) {
  39. case Column::Icon:
  40. return "";
  41. case Column::PID:
  42. return "PID";
  43. case Column::TID:
  44. return "TID";
  45. case Column::State:
  46. return "State";
  47. case Column::User:
  48. return "User";
  49. case Column::Priority:
  50. return "Pr";
  51. case Column::Virtual:
  52. return "Virtual";
  53. case Column::Physical:
  54. return "Physical";
  55. case Column::CPU:
  56. return "CPU";
  57. case Column::Name:
  58. return "Name";
  59. case Column::Syscalls:
  60. return "Syscalls";
  61. case Column::InodeFaults:
  62. return "F:Inode";
  63. case Column::ZeroFaults:
  64. return "F:Zero";
  65. case Column::CowFaults:
  66. return "F:CoW";
  67. case Column::IPv4SocketReadBytes:
  68. return "IPv4 In";
  69. case Column::IPv4SocketWriteBytes:
  70. return "IPv4 Out";
  71. case Column::UnixSocketReadBytes:
  72. return "Unix In";
  73. case Column::UnixSocketWriteBytes:
  74. return "Unix Out";
  75. case Column::FileReadBytes:
  76. return "File In";
  77. case Column::FileWriteBytes:
  78. return "File Out";
  79. default:
  80. ASSERT_NOT_REACHED();
  81. }
  82. }
  83. GModel::ColumnMetadata ProcessModel::column_metadata(int column) const
  84. {
  85. switch (column) {
  86. case Column::Icon:
  87. return { 16, TextAlignment::CenterLeft };
  88. case Column::PID:
  89. return { 32, TextAlignment::CenterRight };
  90. case Column::TID:
  91. return { 32, TextAlignment::CenterRight };
  92. case Column::State:
  93. return { 75, TextAlignment::CenterLeft };
  94. case Column::Priority:
  95. return { 16, TextAlignment::CenterLeft };
  96. case Column::User:
  97. return { 50, TextAlignment::CenterLeft };
  98. case Column::Virtual:
  99. return { 65, TextAlignment::CenterRight };
  100. case Column::Physical:
  101. return { 65, TextAlignment::CenterRight };
  102. case Column::CPU:
  103. return { 32, TextAlignment::CenterRight };
  104. case Column::Name:
  105. return { 140, TextAlignment::CenterLeft };
  106. case Column::Syscalls:
  107. return { 60, TextAlignment::CenterRight };
  108. case Column::InodeFaults:
  109. return { 60, TextAlignment::CenterRight };
  110. case Column::ZeroFaults:
  111. return { 60, TextAlignment::CenterRight };
  112. case Column::CowFaults:
  113. return { 60, TextAlignment::CenterRight };
  114. case Column::FileReadBytes:
  115. return { 60, TextAlignment::CenterRight };
  116. case Column::FileWriteBytes:
  117. return { 60, TextAlignment::CenterRight };
  118. case Column::UnixSocketReadBytes:
  119. return { 60, TextAlignment::CenterRight };
  120. case Column::UnixSocketWriteBytes:
  121. return { 60, TextAlignment::CenterRight };
  122. case Column::IPv4SocketReadBytes:
  123. return { 60, TextAlignment::CenterRight };
  124. case Column::IPv4SocketWriteBytes:
  125. return { 60, TextAlignment::CenterRight };
  126. default:
  127. ASSERT_NOT_REACHED();
  128. }
  129. }
  130. static String pretty_byte_size(size_t size)
  131. {
  132. return String::format("%uK", size / 1024);
  133. }
  134. GVariant ProcessModel::data(const GModelIndex& index, Role role) const
  135. {
  136. ASSERT(is_valid(index));
  137. auto it = m_threads.find(m_pids[index.row()]);
  138. auto& thread = *(*it).value;
  139. if (role == Role::Sort) {
  140. switch (index.column()) {
  141. case Column::Icon:
  142. return 0;
  143. case Column::PID:
  144. return thread.current_state.pid;
  145. case Column::TID:
  146. return thread.current_state.tid;
  147. case Column::State:
  148. return thread.current_state.state;
  149. case Column::User:
  150. return thread.current_state.user;
  151. case Column::Priority:
  152. if (thread.current_state.priority == "Idle")
  153. return 0;
  154. if (thread.current_state.priority == "Low")
  155. return 1;
  156. if (thread.current_state.priority == "Normal")
  157. return 2;
  158. if (thread.current_state.priority == "High")
  159. return 3;
  160. ASSERT_NOT_REACHED();
  161. return 3;
  162. case Column::Virtual:
  163. return (int)thread.current_state.amount_virtual;
  164. case Column::Physical:
  165. return (int)thread.current_state.amount_resident;
  166. case Column::CPU:
  167. return thread.current_state.cpu_percent;
  168. case Column::Name:
  169. return thread.current_state.name;
  170. // FIXME: GVariant with unsigned?
  171. case Column::Syscalls:
  172. return (int)thread.current_state.syscall_count;
  173. case Column::InodeFaults:
  174. return (int)thread.current_state.inode_faults;
  175. case Column::ZeroFaults:
  176. return (int)thread.current_state.zero_faults;
  177. case Column::CowFaults:
  178. return (int)thread.current_state.cow_faults;
  179. case Column::IPv4SocketReadBytes:
  180. return (int)thread.current_state.ipv4_socket_read_bytes;
  181. case Column::IPv4SocketWriteBytes:
  182. return (int)thread.current_state.ipv4_socket_write_bytes;
  183. case Column::UnixSocketReadBytes:
  184. return (int)thread.current_state.unix_socket_read_bytes;
  185. case Column::UnixSocketWriteBytes:
  186. return (int)thread.current_state.unix_socket_write_bytes;
  187. case Column::FileReadBytes:
  188. return (int)thread.current_state.file_read_bytes;
  189. case Column::FileWriteBytes:
  190. return (int)thread.current_state.file_write_bytes;
  191. }
  192. ASSERT_NOT_REACHED();
  193. return {};
  194. }
  195. if (role == Role::Display) {
  196. switch (index.column()) {
  197. case Column::Icon:
  198. if (thread.current_state.icon_id != -1) {
  199. auto icon_buffer = SharedBuffer::create_from_shared_buffer_id(thread.current_state.icon_id);
  200. if (icon_buffer) {
  201. auto icon_bitmap = GraphicsBitmap::create_with_shared_buffer(GraphicsBitmap::Format::RGBA32, *icon_buffer, { 16, 16 });
  202. if (icon_bitmap)
  203. return *icon_bitmap;
  204. }
  205. }
  206. return *m_generic_process_icon;
  207. case Column::PID:
  208. return thread.current_state.pid;
  209. case Column::TID:
  210. return thread.current_state.tid;
  211. case Column::State:
  212. return thread.current_state.state;
  213. case Column::User:
  214. return thread.current_state.user;
  215. case Column::Priority:
  216. if (thread.current_state.priority == "Idle")
  217. return String::empty();
  218. if (thread.current_state.priority == "High")
  219. return *m_high_priority_icon;
  220. if (thread.current_state.priority == "Low")
  221. return *m_low_priority_icon;
  222. if (thread.current_state.priority == "Normal")
  223. return *m_normal_priority_icon;
  224. return thread.current_state.priority;
  225. case Column::Virtual:
  226. return pretty_byte_size(thread.current_state.amount_virtual);
  227. case Column::Physical:
  228. return pretty_byte_size(thread.current_state.amount_resident);
  229. case Column::CPU:
  230. return thread.current_state.cpu_percent;
  231. case Column::Name:
  232. return thread.current_state.name;
  233. // FIXME: It's weird that GVariant doesn't support unsigned ints. Should it?
  234. case Column::Syscalls:
  235. return (int)thread.current_state.syscall_count;
  236. case Column::InodeFaults:
  237. return (int)thread.current_state.inode_faults;
  238. case Column::ZeroFaults:
  239. return (int)thread.current_state.zero_faults;
  240. case Column::CowFaults:
  241. return (int)thread.current_state.cow_faults;
  242. case Column::IPv4SocketReadBytes:
  243. return (int)thread.current_state.ipv4_socket_read_bytes;
  244. case Column::IPv4SocketWriteBytes:
  245. return (int)thread.current_state.ipv4_socket_write_bytes;
  246. case Column::UnixSocketReadBytes:
  247. return (int)thread.current_state.unix_socket_read_bytes;
  248. case Column::UnixSocketWriteBytes:
  249. return (int)thread.current_state.unix_socket_write_bytes;
  250. case Column::FileReadBytes:
  251. return (int)thread.current_state.file_read_bytes;
  252. case Column::FileWriteBytes:
  253. return (int)thread.current_state.file_write_bytes;
  254. }
  255. }
  256. return {};
  257. }
  258. void ProcessModel::update()
  259. {
  260. auto all_processes = CProcessStatisticsReader::get_all();
  261. unsigned last_sum_times_scheduled = 0;
  262. for (auto& it : m_threads)
  263. last_sum_times_scheduled += it.value->current_state.times_scheduled;
  264. HashTable<PidAndTid> live_pids;
  265. unsigned sum_times_scheduled = 0;
  266. for (auto& it : all_processes) {
  267. for (auto& thread : it.value.threads) {
  268. ThreadState state;
  269. state.pid = it.value.pid;
  270. state.user = it.value.username;
  271. state.syscall_count = thread.syscall_count;
  272. state.inode_faults = thread.inode_faults;
  273. state.zero_faults = thread.zero_faults;
  274. state.cow_faults = thread.cow_faults;
  275. state.unix_socket_read_bytes = thread.unix_socket_read_bytes;
  276. state.unix_socket_write_bytes = thread.unix_socket_write_bytes;
  277. state.ipv4_socket_read_bytes = thread.ipv4_socket_read_bytes;
  278. state.ipv4_socket_write_bytes = thread.ipv4_socket_write_bytes;
  279. state.file_read_bytes = thread.file_read_bytes;
  280. state.file_write_bytes = thread.file_write_bytes;
  281. state.amount_virtual = it.value.amount_virtual;
  282. state.amount_resident = it.value.amount_resident;
  283. state.icon_id = it.value.icon_id;
  284. state.name = thread.name;
  285. state.tid = thread.tid;
  286. state.times_scheduled = thread.times_scheduled;
  287. state.priority = thread.priority;
  288. state.state = thread.state;
  289. sum_times_scheduled += thread.times_scheduled;
  290. {
  291. auto pit = m_threads.find({ it.value.pid, thread.tid });
  292. if (pit == m_threads.end())
  293. m_threads.set({ it.value.pid, thread.tid }, make<Thread>());
  294. }
  295. auto pit = m_threads.find({ it.value.pid, thread.tid });
  296. ASSERT(pit != m_threads.end());
  297. (*pit).value->previous_state = (*pit).value->current_state;
  298. (*pit).value->current_state = state;
  299. live_pids.set({ it.value.pid, thread.tid });
  300. }
  301. }
  302. m_pids.clear();
  303. float total_cpu_percent = 0;
  304. Vector<PidAndTid, 16> pids_to_remove;
  305. for (auto& it : m_threads) {
  306. if (!live_pids.contains(it.key)) {
  307. pids_to_remove.append(it.key);
  308. continue;
  309. }
  310. auto& process = *it.value;
  311. u32 times_scheduled_diff = process.current_state.times_scheduled - process.previous_state.times_scheduled;
  312. process.current_state.cpu_percent = ((float)times_scheduled_diff * 100) / (float)(sum_times_scheduled - last_sum_times_scheduled);
  313. if (it.key.pid != 0) {
  314. total_cpu_percent += process.current_state.cpu_percent;
  315. m_pids.append(it.key);
  316. }
  317. }
  318. for (auto pid : pids_to_remove)
  319. m_threads.remove(pid);
  320. if (on_new_cpu_data_point)
  321. on_new_cpu_data_point(total_cpu_percent);
  322. did_update();
  323. }