ProcessModel.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "ProcessModel.h"
  27. #include "GraphWidget.h"
  28. #include <AK/JsonArray.h>
  29. #include <AK/JsonObject.h>
  30. #include <AK/JsonValue.h>
  31. #include <AK/SharedBuffer.h>
  32. #include <LibCore/ProcessStatisticsReader.h>
  33. #include <fcntl.h>
  34. #include <stdio.h>
  35. static ProcessModel* s_the;
  36. ProcessModel& ProcessModel::the()
  37. {
  38. ASSERT(s_the);
  39. return *s_the;
  40. }
  41. ProcessModel::ProcessModel()
  42. {
  43. ASSERT(!s_the);
  44. s_the = this;
  45. m_generic_process_icon = Gfx::Bitmap::load_from_file("/res/icons/gear16.png");
  46. m_high_priority_icon = Gfx::Bitmap::load_from_file("/res/icons/highpriority16.png");
  47. m_low_priority_icon = Gfx::Bitmap::load_from_file("/res/icons/lowpriority16.png");
  48. m_normal_priority_icon = Gfx::Bitmap::load_from_file("/res/icons/normalpriority16.png");
  49. }
  50. ProcessModel::~ProcessModel()
  51. {
  52. }
  53. int ProcessModel::row_count(const GUI::ModelIndex&) const
  54. {
  55. return m_pids.size();
  56. }
  57. int ProcessModel::column_count(const GUI::ModelIndex&) const
  58. {
  59. return Column::__Count;
  60. }
  61. String ProcessModel::column_name(int column) const
  62. {
  63. switch (column) {
  64. case Column::Icon:
  65. return "";
  66. case Column::PID:
  67. return "PID";
  68. case Column::TID:
  69. return "TID";
  70. case Column::State:
  71. return "State";
  72. case Column::User:
  73. return "User";
  74. case Column::Priority:
  75. return "Pr";
  76. case Column::EffectivePriority:
  77. return "EPr";
  78. case Column::Virtual:
  79. return "Virtual";
  80. case Column::Physical:
  81. return "Physical";
  82. case Column::DirtyPrivate:
  83. return "DirtyP";
  84. case Column::CleanInode:
  85. return "CleanI";
  86. case Column::PurgeableVolatile:
  87. return "Purg:V";
  88. case Column::PurgeableNonvolatile:
  89. return "Purg:N";
  90. case Column::CPU:
  91. return "CPU";
  92. case Column::Name:
  93. return "Name";
  94. case Column::Syscalls:
  95. return "Syscalls";
  96. case Column::InodeFaults:
  97. return "F:Inode";
  98. case Column::ZeroFaults:
  99. return "F:Zero";
  100. case Column::CowFaults:
  101. return "F:CoW";
  102. case Column::IPv4SocketReadBytes:
  103. return "IPv4 In";
  104. case Column::IPv4SocketWriteBytes:
  105. return "IPv4 Out";
  106. case Column::UnixSocketReadBytes:
  107. return "Unix In";
  108. case Column::UnixSocketWriteBytes:
  109. return "Unix Out";
  110. case Column::FileReadBytes:
  111. return "File In";
  112. case Column::FileWriteBytes:
  113. return "File Out";
  114. case Column::Pledge:
  115. return "Pledge";
  116. case Column::Veil:
  117. return "Veil";
  118. default:
  119. ASSERT_NOT_REACHED();
  120. }
  121. }
  122. GUI::Model::ColumnMetadata ProcessModel::column_metadata(int column) const
  123. {
  124. switch (column) {
  125. case Column::Icon:
  126. return { 16, Gfx::TextAlignment::CenterLeft };
  127. case Column::PID:
  128. return { 32, Gfx::TextAlignment::CenterRight };
  129. case Column::TID:
  130. return { 32, Gfx::TextAlignment::CenterRight };
  131. case Column::State:
  132. return { 75, Gfx::TextAlignment::CenterLeft };
  133. case Column::Priority:
  134. return { 16, Gfx::TextAlignment::CenterRight };
  135. case Column::EffectivePriority:
  136. return { 16, Gfx::TextAlignment::CenterRight };
  137. case Column::User:
  138. return { 50, Gfx::TextAlignment::CenterLeft };
  139. case Column::Virtual:
  140. return { 65, Gfx::TextAlignment::CenterRight };
  141. case Column::Physical:
  142. return { 65, Gfx::TextAlignment::CenterRight };
  143. case Column::DirtyPrivate:
  144. return { 65, Gfx::TextAlignment::CenterRight };
  145. case Column::CleanInode:
  146. return { 65, Gfx::TextAlignment::CenterRight };
  147. case Column::PurgeableVolatile:
  148. return { 65, Gfx::TextAlignment::CenterRight };
  149. case Column::PurgeableNonvolatile:
  150. return { 65, Gfx::TextAlignment::CenterRight };
  151. case Column::CPU:
  152. return { 32, Gfx::TextAlignment::CenterRight };
  153. case Column::Name:
  154. return { 140, Gfx::TextAlignment::CenterLeft };
  155. case Column::Syscalls:
  156. return { 60, Gfx::TextAlignment::CenterRight };
  157. case Column::InodeFaults:
  158. return { 60, Gfx::TextAlignment::CenterRight };
  159. case Column::ZeroFaults:
  160. return { 60, Gfx::TextAlignment::CenterRight };
  161. case Column::CowFaults:
  162. return { 60, Gfx::TextAlignment::CenterRight };
  163. case Column::FileReadBytes:
  164. return { 60, Gfx::TextAlignment::CenterRight };
  165. case Column::FileWriteBytes:
  166. return { 60, Gfx::TextAlignment::CenterRight };
  167. case Column::UnixSocketReadBytes:
  168. return { 60, Gfx::TextAlignment::CenterRight };
  169. case Column::UnixSocketWriteBytes:
  170. return { 60, Gfx::TextAlignment::CenterRight };
  171. case Column::IPv4SocketReadBytes:
  172. return { 60, Gfx::TextAlignment::CenterRight };
  173. case Column::IPv4SocketWriteBytes:
  174. return { 60, Gfx::TextAlignment::CenterRight };
  175. case Column::Pledge:
  176. return { 60, Gfx::TextAlignment::CenterLeft };
  177. case Column::Veil:
  178. return { 60, Gfx::TextAlignment::CenterLeft };
  179. default:
  180. ASSERT_NOT_REACHED();
  181. }
  182. }
  183. static String pretty_byte_size(size_t size)
  184. {
  185. return String::format("%uK", size / 1024);
  186. }
  187. GUI::Variant ProcessModel::data(const GUI::ModelIndex& index, Role role) const
  188. {
  189. ASSERT(is_valid(index));
  190. auto it = m_threads.find(m_pids[index.row()]);
  191. auto& thread = *(*it).value;
  192. if (role == Role::Sort) {
  193. switch (index.column()) {
  194. case Column::Icon:
  195. return 0;
  196. case Column::PID:
  197. return thread.current_state.pid;
  198. case Column::TID:
  199. return thread.current_state.tid;
  200. case Column::State:
  201. return thread.current_state.state;
  202. case Column::User:
  203. return thread.current_state.user;
  204. case Column::Priority:
  205. return thread.current_state.priority;
  206. case Column::EffectivePriority:
  207. return thread.current_state.effective_priority;
  208. case Column::Virtual:
  209. return (int)thread.current_state.amount_virtual;
  210. case Column::Physical:
  211. return (int)thread.current_state.amount_resident;
  212. case Column::DirtyPrivate:
  213. return (int)thread.current_state.amount_dirty_private;
  214. case Column::CleanInode:
  215. return (int)thread.current_state.amount_clean_inode;
  216. case Column::PurgeableVolatile:
  217. return (int)thread.current_state.amount_purgeable_volatile;
  218. case Column::PurgeableNonvolatile:
  219. return (int)thread.current_state.amount_purgeable_nonvolatile;
  220. case Column::CPU:
  221. return thread.current_state.cpu_percent;
  222. case Column::Name:
  223. return thread.current_state.name;
  224. case Column::Syscalls:
  225. return thread.current_state.syscall_count;
  226. case Column::InodeFaults:
  227. return thread.current_state.inode_faults;
  228. case Column::ZeroFaults:
  229. return thread.current_state.zero_faults;
  230. case Column::CowFaults:
  231. return thread.current_state.cow_faults;
  232. case Column::IPv4SocketReadBytes:
  233. return thread.current_state.ipv4_socket_read_bytes;
  234. case Column::IPv4SocketWriteBytes:
  235. return thread.current_state.ipv4_socket_write_bytes;
  236. case Column::UnixSocketReadBytes:
  237. return thread.current_state.unix_socket_read_bytes;
  238. case Column::UnixSocketWriteBytes:
  239. return thread.current_state.unix_socket_write_bytes;
  240. case Column::FileReadBytes:
  241. return thread.current_state.file_read_bytes;
  242. case Column::FileWriteBytes:
  243. return thread.current_state.file_write_bytes;
  244. case Column::Pledge:
  245. return thread.current_state.pledge;
  246. case Column::Veil:
  247. return thread.current_state.veil;
  248. }
  249. ASSERT_NOT_REACHED();
  250. return {};
  251. }
  252. if (role == Role::Display) {
  253. switch (index.column()) {
  254. case Column::Icon:
  255. if (thread.current_state.icon_id != -1) {
  256. auto icon_buffer = SharedBuffer::create_from_shared_buffer_id(thread.current_state.icon_id);
  257. if (icon_buffer) {
  258. auto icon_bitmap = Gfx::Bitmap::create_with_shared_buffer(Gfx::Bitmap::Format::RGBA32, *icon_buffer, { 16, 16 });
  259. if (icon_bitmap)
  260. return *icon_bitmap;
  261. }
  262. }
  263. return *m_generic_process_icon;
  264. case Column::PID:
  265. return thread.current_state.pid;
  266. case Column::TID:
  267. return thread.current_state.tid;
  268. case Column::State:
  269. return thread.current_state.state;
  270. case Column::User:
  271. return thread.current_state.user;
  272. case Column::Priority:
  273. return thread.current_state.priority;
  274. case Column::EffectivePriority:
  275. return thread.current_state.effective_priority;
  276. case Column::Virtual:
  277. return pretty_byte_size(thread.current_state.amount_virtual);
  278. case Column::Physical:
  279. return pretty_byte_size(thread.current_state.amount_resident);
  280. case Column::DirtyPrivate:
  281. return pretty_byte_size(thread.current_state.amount_dirty_private);
  282. case Column::CleanInode:
  283. return pretty_byte_size(thread.current_state.amount_clean_inode);
  284. case Column::PurgeableVolatile:
  285. return pretty_byte_size(thread.current_state.amount_purgeable_volatile);
  286. case Column::PurgeableNonvolatile:
  287. return pretty_byte_size(thread.current_state.amount_purgeable_nonvolatile);
  288. case Column::CPU:
  289. return thread.current_state.cpu_percent;
  290. case Column::Name:
  291. return thread.current_state.name;
  292. case Column::Syscalls:
  293. return thread.current_state.syscall_count;
  294. case Column::InodeFaults:
  295. return thread.current_state.inode_faults;
  296. case Column::ZeroFaults:
  297. return thread.current_state.zero_faults;
  298. case Column::CowFaults:
  299. return thread.current_state.cow_faults;
  300. case Column::IPv4SocketReadBytes:
  301. return thread.current_state.ipv4_socket_read_bytes;
  302. case Column::IPv4SocketWriteBytes:
  303. return thread.current_state.ipv4_socket_write_bytes;
  304. case Column::UnixSocketReadBytes:
  305. return thread.current_state.unix_socket_read_bytes;
  306. case Column::UnixSocketWriteBytes:
  307. return thread.current_state.unix_socket_write_bytes;
  308. case Column::FileReadBytes:
  309. return thread.current_state.file_read_bytes;
  310. case Column::FileWriteBytes:
  311. return thread.current_state.file_write_bytes;
  312. case Column::Pledge:
  313. return thread.current_state.pledge;
  314. case Column::Veil:
  315. return thread.current_state.veil;
  316. }
  317. }
  318. return {};
  319. }
  320. void ProcessModel::update()
  321. {
  322. auto all_processes = Core::ProcessStatisticsReader::get_all();
  323. unsigned last_sum_times_scheduled = 0;
  324. for (auto& it : m_threads)
  325. last_sum_times_scheduled += it.value->current_state.times_scheduled;
  326. HashTable<PidAndTid> live_pids;
  327. unsigned sum_times_scheduled = 0;
  328. for (auto& it : all_processes) {
  329. for (auto& thread : it.value.threads) {
  330. ThreadState state;
  331. state.pid = it.value.pid;
  332. state.user = it.value.username;
  333. state.pledge = it.value.pledge;
  334. state.veil = it.value.veil;
  335. state.syscall_count = thread.syscall_count;
  336. state.inode_faults = thread.inode_faults;
  337. state.zero_faults = thread.zero_faults;
  338. state.cow_faults = thread.cow_faults;
  339. state.unix_socket_read_bytes = thread.unix_socket_read_bytes;
  340. state.unix_socket_write_bytes = thread.unix_socket_write_bytes;
  341. state.ipv4_socket_read_bytes = thread.ipv4_socket_read_bytes;
  342. state.ipv4_socket_write_bytes = thread.ipv4_socket_write_bytes;
  343. state.file_read_bytes = thread.file_read_bytes;
  344. state.file_write_bytes = thread.file_write_bytes;
  345. state.amount_virtual = it.value.amount_virtual;
  346. state.amount_resident = it.value.amount_resident;
  347. state.amount_dirty_private = it.value.amount_dirty_private;
  348. state.amount_clean_inode = it.value.amount_clean_inode;
  349. state.amount_purgeable_volatile = it.value.amount_purgeable_volatile;
  350. state.amount_purgeable_nonvolatile = it.value.amount_purgeable_nonvolatile;
  351. state.icon_id = it.value.icon_id;
  352. state.name = thread.name;
  353. state.tid = thread.tid;
  354. state.times_scheduled = thread.times_scheduled;
  355. state.priority = thread.priority;
  356. state.effective_priority = thread.effective_priority;
  357. state.state = thread.state;
  358. sum_times_scheduled += thread.times_scheduled;
  359. {
  360. auto pit = m_threads.find({ it.value.pid, thread.tid });
  361. if (pit == m_threads.end())
  362. m_threads.set({ it.value.pid, thread.tid }, make<Thread>());
  363. }
  364. auto pit = m_threads.find({ it.value.pid, thread.tid });
  365. ASSERT(pit != m_threads.end());
  366. (*pit).value->previous_state = (*pit).value->current_state;
  367. (*pit).value->current_state = state;
  368. live_pids.set({ it.value.pid, thread.tid });
  369. }
  370. }
  371. m_pids.clear();
  372. float total_cpu_percent = 0;
  373. Vector<PidAndTid, 16> pids_to_remove;
  374. for (auto& it : m_threads) {
  375. if (!live_pids.contains(it.key)) {
  376. pids_to_remove.append(it.key);
  377. continue;
  378. }
  379. auto& process = *it.value;
  380. u32 times_scheduled_diff = process.current_state.times_scheduled - process.previous_state.times_scheduled;
  381. process.current_state.cpu_percent = ((float)times_scheduled_diff * 100) / (float)(sum_times_scheduled - last_sum_times_scheduled);
  382. if (it.key.pid != 0) {
  383. total_cpu_percent += process.current_state.cpu_percent;
  384. m_pids.append(it.key);
  385. }
  386. }
  387. for (auto pid : pids_to_remove)
  388. m_threads.remove(pid);
  389. if (on_new_cpu_data_point)
  390. on_new_cpu_data_point(total_cpu_percent);
  391. did_update();
  392. }