ProcessModel.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * Copyright (c) 2018-2021, 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 <AK/JsonObject.h>
  28. #include <AK/JsonValue.h>
  29. #include <LibCore/File.h>
  30. #include <LibCore/ProcessStatisticsReader.h>
  31. #include <LibGUI/FileIconProvider.h>
  32. static ProcessModel* s_the;
  33. ProcessModel& ProcessModel::the()
  34. {
  35. VERIFY(s_the);
  36. return *s_the;
  37. }
  38. ProcessModel::ProcessModel()
  39. {
  40. VERIFY(!s_the);
  41. s_the = this;
  42. auto file = Core::File::construct("/proc/cpuinfo");
  43. if (file->open(Core::IODevice::ReadOnly)) {
  44. auto json = JsonValue::from_string({ file->read_all() });
  45. auto cpuinfo_array = json.value().as_array();
  46. cpuinfo_array.for_each([&](auto& value) {
  47. auto& cpu_object = value.as_object();
  48. auto cpu_id = cpu_object.get("processor").as_u32();
  49. m_cpus.append(make<CpuInfo>(cpu_id));
  50. });
  51. }
  52. if (m_cpus.is_empty())
  53. m_cpus.append(make<CpuInfo>(0));
  54. }
  55. ProcessModel::~ProcessModel()
  56. {
  57. }
  58. int ProcessModel::row_count(const GUI::ModelIndex&) const
  59. {
  60. return m_tids.size();
  61. }
  62. int ProcessModel::column_count(const GUI::ModelIndex&) const
  63. {
  64. return Column::__Count;
  65. }
  66. String ProcessModel::column_name(int column) const
  67. {
  68. switch (column) {
  69. case Column::Icon:
  70. return "";
  71. case Column::PID:
  72. return "PID";
  73. case Column::TID:
  74. return "TID";
  75. case Column::PPID:
  76. return "PPID";
  77. case Column::PGID:
  78. return "PGID";
  79. case Column::SID:
  80. return "SID";
  81. case Column::State:
  82. return "State";
  83. case Column::User:
  84. return "User";
  85. case Column::Priority:
  86. return "Pr";
  87. case Column::Virtual:
  88. return "Virtual";
  89. case Column::Physical:
  90. return "Physical";
  91. case Column::DirtyPrivate:
  92. return "Private";
  93. case Column::CleanInode:
  94. return "CleanI";
  95. case Column::PurgeableVolatile:
  96. return "Purg:V";
  97. case Column::PurgeableNonvolatile:
  98. return "Purg:N";
  99. case Column::CPU:
  100. return "CPU";
  101. case Column::Processor:
  102. return "Processor";
  103. case Column::Name:
  104. return "Name";
  105. case Column::Syscalls:
  106. return "Syscalls";
  107. case Column::InodeFaults:
  108. return "F:Inode";
  109. case Column::ZeroFaults:
  110. return "F:Zero";
  111. case Column::CowFaults:
  112. return "F:CoW";
  113. case Column::IPv4SocketReadBytes:
  114. return "IPv4 In";
  115. case Column::IPv4SocketWriteBytes:
  116. return "IPv4 Out";
  117. case Column::UnixSocketReadBytes:
  118. return "Unix In";
  119. case Column::UnixSocketWriteBytes:
  120. return "Unix Out";
  121. case Column::FileReadBytes:
  122. return "File In";
  123. case Column::FileWriteBytes:
  124. return "File Out";
  125. case Column::Pledge:
  126. return "Pledge";
  127. case Column::Veil:
  128. return "Veil";
  129. default:
  130. VERIFY_NOT_REACHED();
  131. }
  132. }
  133. static String pretty_byte_size(size_t size)
  134. {
  135. return String::formatted("{}K", size / 1024);
  136. }
  137. GUI::Variant ProcessModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  138. {
  139. VERIFY(is_valid(index));
  140. if (role == GUI::ModelRole::TextAlignment) {
  141. switch (index.column()) {
  142. case Column::Icon:
  143. case Column::Name:
  144. case Column::State:
  145. case Column::User:
  146. case Column::Pledge:
  147. case Column::Veil:
  148. return Gfx::TextAlignment::CenterLeft;
  149. case Column::PID:
  150. case Column::TID:
  151. case Column::PPID:
  152. case Column::PGID:
  153. case Column::SID:
  154. case Column::Priority:
  155. case Column::Virtual:
  156. case Column::Physical:
  157. case Column::DirtyPrivate:
  158. case Column::CleanInode:
  159. case Column::PurgeableVolatile:
  160. case Column::PurgeableNonvolatile:
  161. case Column::CPU:
  162. case Column::Processor:
  163. case Column::Syscalls:
  164. case Column::InodeFaults:
  165. case Column::ZeroFaults:
  166. case Column::CowFaults:
  167. case Column::FileReadBytes:
  168. case Column::FileWriteBytes:
  169. case Column::UnixSocketReadBytes:
  170. case Column::UnixSocketWriteBytes:
  171. case Column::IPv4SocketReadBytes:
  172. case Column::IPv4SocketWriteBytes:
  173. return Gfx::TextAlignment::CenterRight;
  174. default:
  175. VERIFY_NOT_REACHED();
  176. }
  177. }
  178. auto it = m_threads.find(m_tids[index.row()]);
  179. auto& thread = *(*it).value;
  180. if (role == GUI::ModelRole::Sort) {
  181. switch (index.column()) {
  182. case Column::Icon:
  183. return 0;
  184. case Column::PID:
  185. return thread.current_state.pid;
  186. case Column::TID:
  187. return thread.current_state.tid;
  188. case Column::PPID:
  189. return thread.current_state.ppid;
  190. case Column::PGID:
  191. return thread.current_state.pgid;
  192. case Column::SID:
  193. return thread.current_state.sid;
  194. case Column::State:
  195. return thread.current_state.state;
  196. case Column::User:
  197. return thread.current_state.user;
  198. case Column::Priority:
  199. return thread.current_state.priority;
  200. case Column::Virtual:
  201. return (int)thread.current_state.amount_virtual;
  202. case Column::Physical:
  203. return (int)thread.current_state.amount_resident;
  204. case Column::DirtyPrivate:
  205. return (int)thread.current_state.amount_dirty_private;
  206. case Column::CleanInode:
  207. return (int)thread.current_state.amount_clean_inode;
  208. case Column::PurgeableVolatile:
  209. return (int)thread.current_state.amount_purgeable_volatile;
  210. case Column::PurgeableNonvolatile:
  211. return (int)thread.current_state.amount_purgeable_nonvolatile;
  212. case Column::CPU:
  213. return thread.current_state.cpu_percent;
  214. case Column::Processor:
  215. return thread.current_state.cpu;
  216. case Column::Name:
  217. return thread.current_state.name;
  218. case Column::Syscalls:
  219. return thread.current_state.syscall_count;
  220. case Column::InodeFaults:
  221. return thread.current_state.inode_faults;
  222. case Column::ZeroFaults:
  223. return thread.current_state.zero_faults;
  224. case Column::CowFaults:
  225. return thread.current_state.cow_faults;
  226. case Column::IPv4SocketReadBytes:
  227. return thread.current_state.ipv4_socket_read_bytes;
  228. case Column::IPv4SocketWriteBytes:
  229. return thread.current_state.ipv4_socket_write_bytes;
  230. case Column::UnixSocketReadBytes:
  231. return thread.current_state.unix_socket_read_bytes;
  232. case Column::UnixSocketWriteBytes:
  233. return thread.current_state.unix_socket_write_bytes;
  234. case Column::FileReadBytes:
  235. return thread.current_state.file_read_bytes;
  236. case Column::FileWriteBytes:
  237. return thread.current_state.file_write_bytes;
  238. case Column::Pledge:
  239. return thread.current_state.pledge;
  240. case Column::Veil:
  241. return thread.current_state.veil;
  242. }
  243. VERIFY_NOT_REACHED();
  244. }
  245. if (role == GUI::ModelRole::Display) {
  246. switch (index.column()) {
  247. case Column::Icon: {
  248. auto icon = GUI::FileIconProvider::icon_for_executable(thread.current_state.executable).bitmap_for_size(16);
  249. if (!icon)
  250. return GUI::Icon();
  251. return GUI::Icon(*icon);
  252. }
  253. case Column::PID:
  254. return thread.current_state.pid;
  255. case Column::TID:
  256. return thread.current_state.tid;
  257. case Column::PPID:
  258. return thread.current_state.ppid;
  259. case Column::PGID:
  260. return thread.current_state.pgid;
  261. case Column::SID:
  262. return thread.current_state.sid;
  263. case Column::State:
  264. return thread.current_state.state;
  265. case Column::User:
  266. return thread.current_state.user;
  267. case Column::Priority:
  268. return thread.current_state.priority;
  269. case Column::Virtual:
  270. return pretty_byte_size(thread.current_state.amount_virtual);
  271. case Column::Physical:
  272. return pretty_byte_size(thread.current_state.amount_resident);
  273. case Column::DirtyPrivate:
  274. return pretty_byte_size(thread.current_state.amount_dirty_private);
  275. case Column::CleanInode:
  276. return pretty_byte_size(thread.current_state.amount_clean_inode);
  277. case Column::PurgeableVolatile:
  278. return pretty_byte_size(thread.current_state.amount_purgeable_volatile);
  279. case Column::PurgeableNonvolatile:
  280. return pretty_byte_size(thread.current_state.amount_purgeable_nonvolatile);
  281. case Column::CPU:
  282. return thread.current_state.cpu_percent;
  283. case Column::Processor:
  284. return thread.current_state.cpu;
  285. case Column::Name:
  286. return thread.current_state.name;
  287. case Column::Syscalls:
  288. return thread.current_state.syscall_count;
  289. case Column::InodeFaults:
  290. return thread.current_state.inode_faults;
  291. case Column::ZeroFaults:
  292. return thread.current_state.zero_faults;
  293. case Column::CowFaults:
  294. return thread.current_state.cow_faults;
  295. case Column::IPv4SocketReadBytes:
  296. return thread.current_state.ipv4_socket_read_bytes;
  297. case Column::IPv4SocketWriteBytes:
  298. return thread.current_state.ipv4_socket_write_bytes;
  299. case Column::UnixSocketReadBytes:
  300. return thread.current_state.unix_socket_read_bytes;
  301. case Column::UnixSocketWriteBytes:
  302. return thread.current_state.unix_socket_write_bytes;
  303. case Column::FileReadBytes:
  304. return thread.current_state.file_read_bytes;
  305. case Column::FileWriteBytes:
  306. return thread.current_state.file_write_bytes;
  307. case Column::Pledge:
  308. return thread.current_state.pledge;
  309. case Column::Veil:
  310. return thread.current_state.veil;
  311. }
  312. }
  313. return {};
  314. }
  315. void ProcessModel::update()
  316. {
  317. auto previous_tid_count = m_tids.size();
  318. auto all_processes = Core::ProcessStatisticsReader::get_all(m_proc_all);
  319. u64 last_sum_ticks_scheduled = 0, last_sum_ticks_scheduled_kernel = 0;
  320. for (auto& it : m_threads) {
  321. auto& current_state = it.value->current_state;
  322. last_sum_ticks_scheduled += current_state.ticks_user + current_state.ticks_kernel;
  323. last_sum_ticks_scheduled_kernel += current_state.ticks_kernel;
  324. }
  325. HashTable<int> live_tids;
  326. u64 sum_ticks_scheduled = 0, sum_ticks_scheduled_kernel = 0;
  327. if (all_processes.has_value()) {
  328. for (auto& it : all_processes.value()) {
  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.name = thread.name;
  352. state.executable = it.value.executable;
  353. state.ppid = it.value.ppid;
  354. state.tid = thread.tid;
  355. state.pgid = it.value.pgid;
  356. state.sid = it.value.sid;
  357. state.ticks_user = thread.ticks_user;
  358. state.ticks_kernel = thread.ticks_kernel;
  359. state.cpu = thread.cpu;
  360. state.cpu_percent = 0;
  361. state.priority = thread.priority;
  362. state.state = thread.state;
  363. sum_ticks_scheduled += thread.ticks_user + thread.ticks_kernel;
  364. sum_ticks_scheduled_kernel += thread.ticks_kernel;
  365. {
  366. auto pit = m_threads.find(thread.tid);
  367. if (pit == m_threads.end())
  368. m_threads.set(thread.tid, make<Thread>());
  369. }
  370. auto pit = m_threads.find(thread.tid);
  371. VERIFY(pit != m_threads.end());
  372. (*pit).value->previous_state = (*pit).value->current_state;
  373. (*pit).value->current_state = state;
  374. live_tids.set(thread.tid);
  375. }
  376. }
  377. }
  378. m_tids.clear();
  379. for (auto& c : m_cpus) {
  380. c.total_cpu_percent = 0.0;
  381. c.total_cpu_percent_kernel = 0.0;
  382. }
  383. Vector<int, 16> tids_to_remove;
  384. for (auto& it : m_threads) {
  385. if (!live_tids.contains(it.key)) {
  386. tids_to_remove.append(it.key);
  387. continue;
  388. }
  389. auto& thread = *it.value;
  390. u32 ticks_scheduled_diff = (thread.current_state.ticks_user + thread.current_state.ticks_kernel)
  391. - (thread.previous_state.ticks_user + thread.previous_state.ticks_kernel);
  392. u32 ticks_scheduled_diff_kernel = thread.current_state.ticks_kernel - thread.previous_state.ticks_kernel;
  393. thread.current_state.cpu_percent = ((float)ticks_scheduled_diff * 100) / (float)(sum_ticks_scheduled - last_sum_ticks_scheduled);
  394. thread.current_state.cpu_percent_kernel = ((float)ticks_scheduled_diff_kernel * 100) / (float)(sum_ticks_scheduled - last_sum_ticks_scheduled);
  395. if (it.value->current_state.pid != 0) {
  396. auto& cpu_info = m_cpus[thread.current_state.cpu];
  397. cpu_info.total_cpu_percent += thread.current_state.cpu_percent;
  398. cpu_info.total_cpu_percent_kernel += thread.current_state.cpu_percent_kernel;
  399. m_tids.append(it.key);
  400. }
  401. }
  402. for (auto tid : tids_to_remove)
  403. m_threads.remove(tid);
  404. if (on_cpu_info_change)
  405. on_cpu_info_change(m_cpus);
  406. if (on_state_update)
  407. on_state_update(all_processes->size(), m_threads.size());
  408. // FIXME: This is a rather hackish way of invalidating indexes.
  409. // It would be good if GUI::Model had a way to orchestrate removal/insertion while preserving indexes.
  410. did_update(previous_tid_count == m_tids.size() ? GUI::Model::UpdateFlag::DontInvalidateIndexes : GUI::Model::UpdateFlag::InvalidateAllIndexes);
  411. }