ProcessModel.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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::PurgeableVolatile:
  56. return "Purg:V";
  57. case Column::PurgeableNonvolatile:
  58. return "Purg:N";
  59. case Column::CPU:
  60. return "CPU";
  61. case Column::Name:
  62. return "Name";
  63. case Column::Syscalls:
  64. return "Syscalls";
  65. case Column::InodeFaults:
  66. return "F:Inode";
  67. case Column::ZeroFaults:
  68. return "F:Zero";
  69. case Column::CowFaults:
  70. return "F:CoW";
  71. case Column::IPv4SocketReadBytes:
  72. return "IPv4 In";
  73. case Column::IPv4SocketWriteBytes:
  74. return "IPv4 Out";
  75. case Column::UnixSocketReadBytes:
  76. return "Unix In";
  77. case Column::UnixSocketWriteBytes:
  78. return "Unix Out";
  79. case Column::FileReadBytes:
  80. return "File In";
  81. case Column::FileWriteBytes:
  82. return "File Out";
  83. default:
  84. ASSERT_NOT_REACHED();
  85. }
  86. }
  87. GModel::ColumnMetadata ProcessModel::column_metadata(int column) const
  88. {
  89. switch (column) {
  90. case Column::Icon:
  91. return { 16, TextAlignment::CenterLeft };
  92. case Column::PID:
  93. return { 32, TextAlignment::CenterRight };
  94. case Column::TID:
  95. return { 32, TextAlignment::CenterRight };
  96. case Column::State:
  97. return { 75, TextAlignment::CenterLeft };
  98. case Column::Priority:
  99. return { 16, TextAlignment::CenterLeft };
  100. case Column::User:
  101. return { 50, TextAlignment::CenterLeft };
  102. case Column::Virtual:
  103. return { 65, TextAlignment::CenterRight };
  104. case Column::Physical:
  105. return { 65, TextAlignment::CenterRight };
  106. case Column::PurgeableVolatile:
  107. return { 65, TextAlignment::CenterRight };
  108. case Column::PurgeableNonvolatile:
  109. return { 65, TextAlignment::CenterRight };
  110. case Column::CPU:
  111. return { 32, TextAlignment::CenterRight };
  112. case Column::Name:
  113. return { 140, TextAlignment::CenterLeft };
  114. case Column::Syscalls:
  115. return { 60, TextAlignment::CenterRight };
  116. case Column::InodeFaults:
  117. return { 60, TextAlignment::CenterRight };
  118. case Column::ZeroFaults:
  119. return { 60, TextAlignment::CenterRight };
  120. case Column::CowFaults:
  121. return { 60, TextAlignment::CenterRight };
  122. case Column::FileReadBytes:
  123. return { 60, TextAlignment::CenterRight };
  124. case Column::FileWriteBytes:
  125. return { 60, TextAlignment::CenterRight };
  126. case Column::UnixSocketReadBytes:
  127. return { 60, TextAlignment::CenterRight };
  128. case Column::UnixSocketWriteBytes:
  129. return { 60, TextAlignment::CenterRight };
  130. case Column::IPv4SocketReadBytes:
  131. return { 60, TextAlignment::CenterRight };
  132. case Column::IPv4SocketWriteBytes:
  133. return { 60, TextAlignment::CenterRight };
  134. default:
  135. ASSERT_NOT_REACHED();
  136. }
  137. }
  138. static String pretty_byte_size(size_t size)
  139. {
  140. return String::format("%uK", size / 1024);
  141. }
  142. GVariant ProcessModel::data(const GModelIndex& index, Role role) const
  143. {
  144. ASSERT(is_valid(index));
  145. auto it = m_threads.find(m_pids[index.row()]);
  146. auto& thread = *(*it).value;
  147. if (role == Role::Sort) {
  148. switch (index.column()) {
  149. case Column::Icon:
  150. return 0;
  151. case Column::PID:
  152. return thread.current_state.pid;
  153. case Column::TID:
  154. return thread.current_state.tid;
  155. case Column::State:
  156. return thread.current_state.state;
  157. case Column::User:
  158. return thread.current_state.user;
  159. case Column::Priority:
  160. if (thread.current_state.priority == "Idle")
  161. return 0;
  162. if (thread.current_state.priority == "Low")
  163. return 1;
  164. if (thread.current_state.priority == "Normal")
  165. return 2;
  166. if (thread.current_state.priority == "High")
  167. return 3;
  168. ASSERT_NOT_REACHED();
  169. return 3;
  170. case Column::Virtual:
  171. return (int)thread.current_state.amount_virtual;
  172. case Column::Physical:
  173. return (int)thread.current_state.amount_resident;
  174. case Column::PurgeableVolatile:
  175. return (int)thread.current_state.amount_purgeable_volatile;
  176. case Column::PurgeableNonvolatile:
  177. return (int)thread.current_state.amount_purgeable_nonvolatile;
  178. case Column::CPU:
  179. return thread.current_state.cpu_percent;
  180. case Column::Name:
  181. return thread.current_state.name;
  182. case Column::Syscalls:
  183. return thread.current_state.syscall_count;
  184. case Column::InodeFaults:
  185. return thread.current_state.inode_faults;
  186. case Column::ZeroFaults:
  187. return thread.current_state.zero_faults;
  188. case Column::CowFaults:
  189. return thread.current_state.cow_faults;
  190. case Column::IPv4SocketReadBytes:
  191. return thread.current_state.ipv4_socket_read_bytes;
  192. case Column::IPv4SocketWriteBytes:
  193. return thread.current_state.ipv4_socket_write_bytes;
  194. case Column::UnixSocketReadBytes:
  195. return thread.current_state.unix_socket_read_bytes;
  196. case Column::UnixSocketWriteBytes:
  197. return thread.current_state.unix_socket_write_bytes;
  198. case Column::FileReadBytes:
  199. return thread.current_state.file_read_bytes;
  200. case Column::FileWriteBytes:
  201. return thread.current_state.file_write_bytes;
  202. }
  203. ASSERT_NOT_REACHED();
  204. return {};
  205. }
  206. if (role == Role::Display) {
  207. switch (index.column()) {
  208. case Column::Icon:
  209. if (thread.current_state.icon_id != -1) {
  210. auto icon_buffer = SharedBuffer::create_from_shared_buffer_id(thread.current_state.icon_id);
  211. if (icon_buffer) {
  212. auto icon_bitmap = GraphicsBitmap::create_with_shared_buffer(GraphicsBitmap::Format::RGBA32, *icon_buffer, { 16, 16 });
  213. if (icon_bitmap)
  214. return *icon_bitmap;
  215. }
  216. }
  217. return *m_generic_process_icon;
  218. case Column::PID:
  219. return thread.current_state.pid;
  220. case Column::TID:
  221. return thread.current_state.tid;
  222. case Column::State:
  223. return thread.current_state.state;
  224. case Column::User:
  225. return thread.current_state.user;
  226. case Column::Priority:
  227. if (thread.current_state.priority == "Idle")
  228. return String::empty();
  229. if (thread.current_state.priority == "High")
  230. return *m_high_priority_icon;
  231. if (thread.current_state.priority == "Low")
  232. return *m_low_priority_icon;
  233. if (thread.current_state.priority == "Normal")
  234. return *m_normal_priority_icon;
  235. return thread.current_state.priority;
  236. case Column::Virtual:
  237. return pretty_byte_size(thread.current_state.amount_virtual);
  238. case Column::Physical:
  239. return pretty_byte_size(thread.current_state.amount_resident);
  240. case Column::PurgeableVolatile:
  241. return pretty_byte_size(thread.current_state.amount_purgeable_volatile);
  242. case Column::PurgeableNonvolatile:
  243. return pretty_byte_size(thread.current_state.amount_purgeable_nonvolatile);
  244. case Column::CPU:
  245. return thread.current_state.cpu_percent;
  246. case Column::Name:
  247. return thread.current_state.name;
  248. case Column::Syscalls:
  249. return thread.current_state.syscall_count;
  250. case Column::InodeFaults:
  251. return thread.current_state.inode_faults;
  252. case Column::ZeroFaults:
  253. return thread.current_state.zero_faults;
  254. case Column::CowFaults:
  255. return thread.current_state.cow_faults;
  256. case Column::IPv4SocketReadBytes:
  257. return thread.current_state.ipv4_socket_read_bytes;
  258. case Column::IPv4SocketWriteBytes:
  259. return thread.current_state.ipv4_socket_write_bytes;
  260. case Column::UnixSocketReadBytes:
  261. return thread.current_state.unix_socket_read_bytes;
  262. case Column::UnixSocketWriteBytes:
  263. return thread.current_state.unix_socket_write_bytes;
  264. case Column::FileReadBytes:
  265. return thread.current_state.file_read_bytes;
  266. case Column::FileWriteBytes:
  267. return thread.current_state.file_write_bytes;
  268. }
  269. }
  270. return {};
  271. }
  272. void ProcessModel::update()
  273. {
  274. auto all_processes = CProcessStatisticsReader::get_all();
  275. unsigned last_sum_times_scheduled = 0;
  276. for (auto& it : m_threads)
  277. last_sum_times_scheduled += it.value->current_state.times_scheduled;
  278. HashTable<PidAndTid> live_pids;
  279. unsigned sum_times_scheduled = 0;
  280. for (auto& it : all_processes) {
  281. for (auto& thread : it.value.threads) {
  282. ThreadState state;
  283. state.pid = it.value.pid;
  284. state.user = it.value.username;
  285. state.syscall_count = thread.syscall_count;
  286. state.inode_faults = thread.inode_faults;
  287. state.zero_faults = thread.zero_faults;
  288. state.cow_faults = thread.cow_faults;
  289. state.unix_socket_read_bytes = thread.unix_socket_read_bytes;
  290. state.unix_socket_write_bytes = thread.unix_socket_write_bytes;
  291. state.ipv4_socket_read_bytes = thread.ipv4_socket_read_bytes;
  292. state.ipv4_socket_write_bytes = thread.ipv4_socket_write_bytes;
  293. state.file_read_bytes = thread.file_read_bytes;
  294. state.file_write_bytes = thread.file_write_bytes;
  295. state.amount_virtual = it.value.amount_virtual;
  296. state.amount_resident = it.value.amount_resident;
  297. state.amount_purgeable_volatile = it.value.amount_purgeable_volatile;
  298. state.amount_purgeable_nonvolatile = it.value.amount_purgeable_nonvolatile;
  299. state.icon_id = it.value.icon_id;
  300. state.name = thread.name;
  301. state.tid = thread.tid;
  302. state.times_scheduled = thread.times_scheduled;
  303. state.priority = thread.priority;
  304. state.state = thread.state;
  305. sum_times_scheduled += thread.times_scheduled;
  306. {
  307. auto pit = m_threads.find({ it.value.pid, thread.tid });
  308. if (pit == m_threads.end())
  309. m_threads.set({ it.value.pid, thread.tid }, make<Thread>());
  310. }
  311. auto pit = m_threads.find({ it.value.pid, thread.tid });
  312. ASSERT(pit != m_threads.end());
  313. (*pit).value->previous_state = (*pit).value->current_state;
  314. (*pit).value->current_state = state;
  315. live_pids.set({ it.value.pid, thread.tid });
  316. }
  317. }
  318. m_pids.clear();
  319. float total_cpu_percent = 0;
  320. Vector<PidAndTid, 16> pids_to_remove;
  321. for (auto& it : m_threads) {
  322. if (!live_pids.contains(it.key)) {
  323. pids_to_remove.append(it.key);
  324. continue;
  325. }
  326. auto& process = *it.value;
  327. u32 times_scheduled_diff = process.current_state.times_scheduled - process.previous_state.times_scheduled;
  328. process.current_state.cpu_percent = ((float)times_scheduled_diff * 100) / (float)(sum_times_scheduled - last_sum_times_scheduled);
  329. if (it.key.pid != 0) {
  330. total_cpu_percent += process.current_state.cpu_percent;
  331. m_pids.append(it.key);
  332. }
  333. }
  334. for (auto pid : pids_to_remove)
  335. m_threads.remove(pid);
  336. if (on_new_cpu_data_point)
  337. on_new_cpu_data_point(total_cpu_percent);
  338. did_update();
  339. }