ProcessModel.cpp 16 KB

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