ProcessModel.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "ProcessModel.h"
  8. #include <AK/JsonObject.h>
  9. #include <AK/JsonValue.h>
  10. #include <AK/NonnullRefPtr.h>
  11. #include <AK/NumberFormat.h>
  12. #include <LibCore/ProcessStatisticsReader.h>
  13. #include <LibGUI/FileIconProvider.h>
  14. #include <LibGUI/Icon.h>
  15. #include <LibGUI/ModelIndex.h>
  16. #include <LibGUI/ModelRole.h>
  17. #include <unistd.h>
  18. static ProcessModel* s_the;
  19. ProcessModel& ProcessModel::the()
  20. {
  21. VERIFY(s_the);
  22. return *s_the;
  23. }
  24. ProcessModel::ProcessModel()
  25. {
  26. VERIFY(!s_the);
  27. s_the = this;
  28. auto file_or_error = Core::File::open("/sys/kernel/cpuinfo"sv, Core::File::OpenMode::Read);
  29. if (!file_or_error.is_error()) {
  30. auto buffer_or_error = file_or_error.value()->read_until_eof();
  31. if (!buffer_or_error.is_error()) {
  32. auto json = JsonValue::from_string({ buffer_or_error.value() });
  33. auto cpuinfo_array = json.value().as_array();
  34. cpuinfo_array.for_each([&](auto& value) {
  35. auto& cpu_object = value.as_object();
  36. auto cpu_id = cpu_object.get_u32("processor"sv).value();
  37. m_cpus.append(make<CpuInfo>(cpu_id));
  38. });
  39. }
  40. }
  41. if (m_cpus.is_empty())
  42. m_cpus.append(make<CpuInfo>(0));
  43. m_kernel_process_icon = GUI::Icon::default_icon("gear"sv);
  44. }
  45. int ProcessModel::row_count(GUI::ModelIndex const& index) const
  46. {
  47. if (!index.is_valid())
  48. return m_processes.size();
  49. // Anything in the second level (threads of processes) doesn't have children.
  50. // This way, we don't get infinitely recursing main threads without having to handle that special case elsewhere.
  51. if (index.parent().is_valid())
  52. return 0;
  53. auto const& thread = *static_cast<Thread const*>(index.internal_data());
  54. // Only the main thread has the other threads as its children.
  55. // Also, if there's not more than one thread, we won't draw that.
  56. if (thread.is_main_thread() && thread.current_state.process.threads.size() > 1)
  57. return thread.current_state.process.threads.size() - 1;
  58. return 0;
  59. }
  60. int ProcessModel::column_count(GUI::ModelIndex const&) const
  61. {
  62. return Column::__Count;
  63. }
  64. ErrorOr<String> ProcessModel::column_name(int column) const
  65. {
  66. switch (column) {
  67. case Column::Icon:
  68. return String {};
  69. case Column::PID:
  70. return "PID"_short_string;
  71. case Column::TID:
  72. return "TID"_short_string;
  73. case Column::PPID:
  74. return "PPID"_short_string;
  75. case Column::PGID:
  76. return "PGID"_short_string;
  77. case Column::SID:
  78. return "SID"_short_string;
  79. case Column::State:
  80. return "State"_short_string;
  81. case Column::User:
  82. return "User"_short_string;
  83. case Column::Priority:
  84. return "Pr"_short_string;
  85. case Column::Virtual:
  86. return "Virtual"_short_string;
  87. case Column::Physical:
  88. return TRY("Physical"_string);
  89. case Column::DirtyPrivate:
  90. return "Private"_short_string;
  91. case Column::CleanInode:
  92. return "CleanI"_short_string;
  93. case Column::PurgeableVolatile:
  94. return "Purg:V"_short_string;
  95. case Column::PurgeableNonvolatile:
  96. return "Purg:N"_short_string;
  97. case Column::CPU:
  98. return "CPU"_short_string;
  99. case Column::Processor:
  100. return TRY("Processor"_string);
  101. case Column::Name:
  102. return "Name"_short_string;
  103. case Column::Syscalls:
  104. return TRY("Syscalls"_string);
  105. case Column::InodeFaults:
  106. return "F:Inode"_short_string;
  107. case Column::ZeroFaults:
  108. return "F:Zero"_short_string;
  109. case Column::CowFaults:
  110. return "F:CoW"_short_string;
  111. case Column::IPv4SocketReadBytes:
  112. return "IPv4 In"_short_string;
  113. case Column::IPv4SocketWriteBytes:
  114. return TRY("IPv4 Out"_string);
  115. case Column::UnixSocketReadBytes:
  116. return "Unix In"_short_string;
  117. case Column::UnixSocketWriteBytes:
  118. return TRY("Unix Out"_string);
  119. case Column::FileReadBytes:
  120. return "File In"_short_string;
  121. case Column::FileWriteBytes:
  122. return TRY("File Out"_string);
  123. case Column::Pledge:
  124. return "Pledge"_short_string;
  125. case Column::Veil:
  126. return "Veil"_short_string;
  127. case Column::Command:
  128. return "Command"_short_string;
  129. default:
  130. VERIFY_NOT_REACHED();
  131. }
  132. }
  133. GUI::Variant ProcessModel::data(GUI::ModelIndex const& index, GUI::ModelRole role) const
  134. {
  135. VERIFY(is_within_range(index));
  136. if (role == GUI::ModelRole::TextAlignment) {
  137. switch (index.column()) {
  138. case Column::Icon:
  139. case Column::Name:
  140. case Column::State:
  141. case Column::User:
  142. case Column::Pledge:
  143. case Column::Veil:
  144. case Column::Command:
  145. return Gfx::TextAlignment::CenterLeft;
  146. case Column::PID:
  147. case Column::TID:
  148. case Column::PPID:
  149. case Column::PGID:
  150. case Column::SID:
  151. case Column::Priority:
  152. case Column::Virtual:
  153. case Column::Physical:
  154. case Column::DirtyPrivate:
  155. case Column::CleanInode:
  156. case Column::PurgeableVolatile:
  157. case Column::PurgeableNonvolatile:
  158. case Column::CPU:
  159. case Column::Processor:
  160. case Column::Syscalls:
  161. case Column::InodeFaults:
  162. case Column::ZeroFaults:
  163. case Column::CowFaults:
  164. case Column::FileReadBytes:
  165. case Column::FileWriteBytes:
  166. case Column::UnixSocketReadBytes:
  167. case Column::UnixSocketWriteBytes:
  168. case Column::IPv4SocketReadBytes:
  169. case Column::IPv4SocketWriteBytes:
  170. return Gfx::TextAlignment::CenterRight;
  171. default:
  172. VERIFY_NOT_REACHED();
  173. }
  174. }
  175. auto const& thread = *static_cast<Thread const*>(index.internal_data());
  176. if (role == GUI::ModelRole::Sort) {
  177. switch (index.column()) {
  178. case Column::Icon:
  179. return 0;
  180. case Column::PID:
  181. return thread.current_state.pid;
  182. case Column::TID:
  183. return thread.current_state.tid;
  184. case Column::PPID:
  185. return thread.current_state.ppid;
  186. case Column::PGID:
  187. return thread.current_state.pgid;
  188. case Column::SID:
  189. return thread.current_state.sid;
  190. case Column::State:
  191. return thread.current_state.state;
  192. case Column::User:
  193. return thread.current_state.user;
  194. case Column::Priority:
  195. return thread.current_state.priority;
  196. case Column::Virtual:
  197. return (int)thread.current_state.amount_virtual;
  198. case Column::Physical:
  199. return (int)thread.current_state.amount_resident;
  200. case Column::DirtyPrivate:
  201. return (int)thread.current_state.amount_dirty_private;
  202. case Column::CleanInode:
  203. return (int)thread.current_state.amount_clean_inode;
  204. case Column::PurgeableVolatile:
  205. return (int)thread.current_state.amount_purgeable_volatile;
  206. case Column::PurgeableNonvolatile:
  207. return (int)thread.current_state.amount_purgeable_nonvolatile;
  208. case Column::CPU:
  209. return thread.current_state.cpu_percent;
  210. case Column::Processor:
  211. return thread.current_state.cpu;
  212. case Column::Name:
  213. return thread.current_state.name;
  214. case Column::Command:
  215. return thread.current_state.command.visit([](String const& cmdline) { return cmdline; }, [](auto const&) { return ""_short_string; });
  216. case Column::Syscalls:
  217. return thread.current_state.syscall_count;
  218. case Column::InodeFaults:
  219. return thread.current_state.inode_faults;
  220. case Column::ZeroFaults:
  221. return thread.current_state.zero_faults;
  222. case Column::CowFaults:
  223. return thread.current_state.cow_faults;
  224. case Column::IPv4SocketReadBytes:
  225. return thread.current_state.ipv4_socket_read_bytes;
  226. case Column::IPv4SocketWriteBytes:
  227. return thread.current_state.ipv4_socket_write_bytes;
  228. case Column::UnixSocketReadBytes:
  229. return thread.current_state.unix_socket_read_bytes;
  230. case Column::UnixSocketWriteBytes:
  231. return thread.current_state.unix_socket_write_bytes;
  232. case Column::FileReadBytes:
  233. return thread.current_state.file_read_bytes;
  234. case Column::FileWriteBytes:
  235. return thread.current_state.file_write_bytes;
  236. case Column::Pledge:
  237. return thread.current_state.pledge;
  238. case Column::Veil:
  239. return thread.current_state.veil;
  240. }
  241. VERIFY_NOT_REACHED();
  242. }
  243. if (role == GUI::ModelRole::Display) {
  244. switch (index.column()) {
  245. case Column::Icon:
  246. return icon_for(thread);
  247. case Column::PID:
  248. return thread.current_state.pid;
  249. case Column::TID:
  250. return thread.current_state.tid;
  251. case Column::PPID:
  252. return thread.current_state.ppid;
  253. case Column::PGID:
  254. return thread.current_state.pgid;
  255. case Column::SID:
  256. return thread.current_state.sid;
  257. case Column::State:
  258. return thread.current_state.state;
  259. case Column::User:
  260. return thread.current_state.user;
  261. case Column::Priority:
  262. return thread.current_state.priority;
  263. case Column::Virtual:
  264. return human_readable_size(thread.current_state.amount_virtual);
  265. case Column::Physical:
  266. return human_readable_size(thread.current_state.amount_resident);
  267. case Column::DirtyPrivate:
  268. return human_readable_size(thread.current_state.amount_dirty_private);
  269. case Column::CleanInode:
  270. return human_readable_size(thread.current_state.amount_clean_inode);
  271. case Column::PurgeableVolatile:
  272. return human_readable_size(thread.current_state.amount_purgeable_volatile);
  273. case Column::PurgeableNonvolatile:
  274. return human_readable_size(thread.current_state.amount_purgeable_nonvolatile);
  275. case Column::CPU:
  276. return DeprecatedString::formatted("{:.2}", thread.current_state.cpu_percent);
  277. case Column::Processor:
  278. return thread.current_state.cpu;
  279. case Column::Name:
  280. if (thread.current_state.kernel)
  281. return DeprecatedString::formatted("{} (*)", thread.current_state.name);
  282. return thread.current_state.name;
  283. case Column::Command:
  284. return thread.current_state.command.visit([](String const& cmdline) { return cmdline; }, [](auto const&) { return ""_short_string; });
  285. case Column::Syscalls:
  286. return thread.current_state.syscall_count;
  287. case Column::InodeFaults:
  288. return thread.current_state.inode_faults;
  289. case Column::ZeroFaults:
  290. return thread.current_state.zero_faults;
  291. case Column::CowFaults:
  292. return thread.current_state.cow_faults;
  293. case Column::IPv4SocketReadBytes:
  294. return human_readable_size_long(thread.current_state.ipv4_socket_read_bytes, UseThousandsSeparator::Yes);
  295. case Column::IPv4SocketWriteBytes:
  296. return human_readable_size_long(thread.current_state.ipv4_socket_write_bytes, UseThousandsSeparator::Yes);
  297. case Column::UnixSocketReadBytes:
  298. return human_readable_size_long(thread.current_state.unix_socket_read_bytes, UseThousandsSeparator::Yes);
  299. case Column::UnixSocketWriteBytes:
  300. return human_readable_size_long(thread.current_state.unix_socket_write_bytes, UseThousandsSeparator::Yes);
  301. case Column::FileReadBytes:
  302. return human_readable_size_long(thread.current_state.file_read_bytes, UseThousandsSeparator::Yes);
  303. case Column::FileWriteBytes:
  304. return human_readable_size_long(thread.current_state.file_write_bytes, UseThousandsSeparator::Yes);
  305. case Column::Pledge:
  306. return thread.current_state.pledge;
  307. case Column::Veil:
  308. return thread.current_state.veil;
  309. }
  310. }
  311. if (role == GUI::ModelRole::Icon)
  312. return icon_for(thread);
  313. if (role == GUI::ModelRole::IconOpacity) {
  314. if (thread.current_state.uid != getuid())
  315. return 0.5f;
  316. return {};
  317. }
  318. return {};
  319. }
  320. GUI::Icon ProcessModel::icon_for(Thread const& thread) const
  321. {
  322. if (thread.current_state.kernel)
  323. return m_kernel_process_icon;
  324. return GUI::FileIconProvider::icon_for_executable(thread.current_state.executable);
  325. }
  326. GUI::ModelIndex ProcessModel::index(int row, int column, GUI::ModelIndex const& parent) const
  327. {
  328. if (row < 0 || column < 0)
  329. return {};
  330. // Process index; we display the main thread here.
  331. if (!parent.is_valid()) {
  332. if (row >= static_cast<int>(m_processes.size()))
  333. return {};
  334. auto corresponding_thread = m_processes[row]->main_thread();
  335. if (!corresponding_thread.has_value())
  336. return {};
  337. return create_index(row, column, corresponding_thread.release_value().ptr());
  338. }
  339. // Thread under process.
  340. auto const& parent_thread = *static_cast<Thread const*>(parent.internal_data());
  341. auto const& process = parent_thread.current_state.process;
  342. // dbgln("Getting thread model index in process {} for col {} row {}", process.pid, column, row);
  343. if (row >= static_cast<int>(process.threads.size()))
  344. return {};
  345. return create_index(row, column, &process.non_main_thread(row));
  346. }
  347. int ProcessModel::thread_model_row(Thread const& thread) const
  348. {
  349. auto const& process = thread.current_state.process;
  350. // A process's main thread uses the global process index.
  351. if (process.pid == thread.current_state.pid) {
  352. auto it = m_processes.find_if([&](auto& entry) {
  353. return entry.ptr() == &process;
  354. });
  355. if (it == m_processes.end())
  356. return 0;
  357. return it.index();
  358. }
  359. return process.threads.find_first_index(thread).value_or(0);
  360. }
  361. GUI::ModelIndex ProcessModel::parent_index(GUI::ModelIndex const& index) const
  362. {
  363. if (!index.is_valid())
  364. return {};
  365. auto const& thread = *static_cast<Thread*>(index.internal_data());
  366. // There's no parent for the main thread.
  367. if (thread.current_state.pid == thread.current_state.tid)
  368. return {};
  369. // FIXME: We can't use first_matching here (not even a const version) because Optional cannot contain references.
  370. auto const& parent = thread.current_state.process;
  371. if (!parent.main_thread().has_value())
  372. return {};
  373. auto process_index = [&]() -> size_t {
  374. auto it = m_processes.find_if([&](auto& entry) {
  375. return entry.ptr() == &parent;
  376. });
  377. if (it == m_processes.end())
  378. return 0;
  379. return it.index();
  380. }();
  381. return create_index(process_index, index.column(), parent.main_thread().value().ptr());
  382. }
  383. Vector<GUI::ModelIndex> ProcessModel::matches(StringView searching, unsigned flags, GUI::ModelIndex const&)
  384. {
  385. Vector<GUI::ModelIndex> found_indices;
  386. for (auto const& thread : m_threads) {
  387. if (string_matches(thread.value->current_state.name, searching, flags)) {
  388. auto tid_row = thread_model_row(thread.value);
  389. found_indices.append(create_index(tid_row, Column::Name, reinterpret_cast<void const*>(thread.value.ptr())));
  390. if (flags & FirstMatchOnly)
  391. break;
  392. }
  393. }
  394. return found_indices;
  395. }
  396. ErrorOr<String> ProcessModel::read_command_line(pid_t pid)
  397. {
  398. auto file = TRY(Core::File::open(TRY(String::formatted("/proc/{}/cmdline", pid)), Core::File::OpenMode::Read));
  399. auto data = TRY(file->read_until_eof());
  400. auto json = TRY(JsonValue::from_string(StringView { data.bytes() }));
  401. auto array = json.as_array().values();
  402. return String::join(" "sv, array);
  403. }
  404. ErrorOr<void> ProcessModel::ensure_process_statistics_file()
  405. {
  406. if (!m_process_statistics_file || !m_process_statistics_file->is_open())
  407. m_process_statistics_file = TRY(Core::File::open("/sys/kernel/processes"sv, Core::File::OpenMode::Read));
  408. return {};
  409. }
  410. void ProcessModel::update()
  411. {
  412. auto result = ensure_process_statistics_file();
  413. if (result.is_error()) {
  414. dbgln("Process model couldn't be updated: {}", result.release_error());
  415. return;
  416. }
  417. auto all_processes = Core::ProcessStatisticsReader::get_all(*m_process_statistics_file, true);
  418. auto previous_tid_count = m_threads.size();
  419. HashTable<int> live_tids;
  420. u64 total_time_scheduled_diff = 0;
  421. if (!all_processes.is_error()) {
  422. if (m_has_total_scheduled_time)
  423. total_time_scheduled_diff = all_processes.value().total_time_scheduled - m_total_time_scheduled;
  424. m_total_time_scheduled = all_processes.value().total_time_scheduled;
  425. m_total_time_scheduled_kernel = all_processes.value().total_time_scheduled_kernel;
  426. m_has_total_scheduled_time = true;
  427. for (size_t i = 0; i < all_processes.value().processes.size(); ++i) {
  428. auto const& process = all_processes.value().processes[i];
  429. NonnullOwnPtr<Process>* process_state = nullptr;
  430. for (size_t i = 0; i < m_processes.size(); ++i) {
  431. auto* other_process = &m_processes[i];
  432. if ((*other_process)->pid == process.pid) {
  433. process_state = other_process;
  434. break;
  435. }
  436. }
  437. if (!process_state) {
  438. m_processes.append(make<Process>());
  439. process_state = &m_processes.last();
  440. }
  441. auto add_thread_data = [&live_tids, this](int tid, Process& process_state, ThreadState state) {
  442. auto thread_data = m_threads.ensure(tid, [&] { return make_ref_counted<Thread>(process_state); });
  443. thread_data->previous_state = move(thread_data->current_state);
  444. thread_data->current_state = move(state);
  445. thread_data->read_command_line_if_necessary();
  446. if (auto maybe_thread_index = process_state.threads.find_first_index(thread_data); maybe_thread_index.has_value()) {
  447. process_state.threads[maybe_thread_index.value()] = thread_data;
  448. } else {
  449. process_state.threads.append(thread_data);
  450. }
  451. live_tids.set(tid);
  452. };
  453. (*process_state)->pid = process.pid;
  454. if (!process.threads.is_empty()) {
  455. for (auto& thread : process.threads) {
  456. ThreadState state(**process_state);
  457. state.tid = thread.tid;
  458. state.pid = process.pid;
  459. state.ppid = process.ppid;
  460. state.pgid = process.pgid;
  461. state.sid = process.sid;
  462. state.time_user = thread.time_user;
  463. state.time_kernel = thread.time_kernel;
  464. state.kernel = process.kernel;
  465. state.executable = process.executable;
  466. state.name = thread.name;
  467. state.uid = process.uid;
  468. state.state = thread.state;
  469. state.user = process.username;
  470. state.pledge = process.pledge;
  471. state.veil = process.veil;
  472. state.cpu = thread.cpu;
  473. state.priority = thread.priority;
  474. state.amount_virtual = process.amount_virtual;
  475. state.amount_resident = process.amount_resident;
  476. state.amount_dirty_private = process.amount_dirty_private;
  477. state.amount_clean_inode = process.amount_clean_inode;
  478. state.amount_purgeable_volatile = process.amount_purgeable_volatile;
  479. state.amount_purgeable_nonvolatile = process.amount_purgeable_nonvolatile;
  480. state.syscall_count = thread.syscall_count;
  481. state.inode_faults = thread.inode_faults;
  482. state.zero_faults = thread.zero_faults;
  483. state.cow_faults = thread.cow_faults;
  484. state.unix_socket_read_bytes = thread.unix_socket_read_bytes;
  485. state.unix_socket_write_bytes = thread.unix_socket_write_bytes;
  486. state.ipv4_socket_read_bytes = thread.ipv4_socket_read_bytes;
  487. state.ipv4_socket_write_bytes = thread.ipv4_socket_write_bytes;
  488. state.file_read_bytes = thread.file_read_bytes;
  489. state.file_write_bytes = thread.file_write_bytes;
  490. state.cpu_percent = 0;
  491. add_thread_data(thread.tid, **process_state, move(state));
  492. }
  493. } else {
  494. // FIXME: If there are no threads left in a process this is an indication
  495. // for a zombie process, so it should be handled differently - we add a mock thread
  496. // just to simulate a process with a single thread.
  497. // Find a way to untie the process representation from a main thread so we can
  498. // just represent a zombie process without creating a mock thread.
  499. ThreadState state(**process_state);
  500. state.tid = process.pid;
  501. state.pid = process.pid;
  502. state.ppid = process.ppid;
  503. state.pgid = process.pgid;
  504. state.sid = process.sid;
  505. state.kernel = process.kernel;
  506. state.executable = process.executable;
  507. state.name = process.name;
  508. state.uid = process.uid;
  509. state.state = "Zombie";
  510. state.user = process.username;
  511. state.pledge = process.pledge;
  512. state.veil = process.veil;
  513. state.amount_virtual = process.amount_virtual;
  514. state.amount_resident = process.amount_resident;
  515. state.amount_dirty_private = process.amount_dirty_private;
  516. state.amount_clean_inode = process.amount_clean_inode;
  517. state.amount_purgeable_volatile = process.amount_purgeable_volatile;
  518. state.amount_purgeable_nonvolatile = process.amount_purgeable_nonvolatile;
  519. add_thread_data(process.pid, **process_state, move(state));
  520. }
  521. }
  522. }
  523. for (auto& c : m_cpus) {
  524. c->total_cpu_percent = 0.0;
  525. c->total_cpu_percent_kernel = 0.0;
  526. }
  527. Vector<int, 16> tids_to_remove;
  528. for (auto& it : m_threads) {
  529. if (!live_tids.contains(it.key)) {
  530. tids_to_remove.append(it.key);
  531. continue;
  532. }
  533. if (it.value->current_state.state == "Zombie") {
  534. continue;
  535. }
  536. auto& thread = *it.value;
  537. u64 time_scheduled_diff = (thread.current_state.time_user + thread.current_state.time_kernel)
  538. - (thread.previous_state.time_user + thread.previous_state.time_kernel);
  539. u64 time_scheduled_diff_kernel = thread.current_state.time_kernel - thread.previous_state.time_kernel;
  540. thread.current_state.cpu_percent = total_time_scheduled_diff > 0 ? (float)((time_scheduled_diff * 1000) / total_time_scheduled_diff) / 10.0f : 0;
  541. thread.current_state.cpu_percent_kernel = total_time_scheduled_diff > 0 ? (float)((time_scheduled_diff_kernel * 1000) / total_time_scheduled_diff) / 10.0f : 0;
  542. if (it.value->current_state.pid != 0) {
  543. auto& cpu_info = m_cpus[thread.current_state.cpu];
  544. cpu_info->total_cpu_percent += thread.current_state.cpu_percent;
  545. cpu_info->total_cpu_percent_kernel += thread.current_state.cpu_percent_kernel;
  546. }
  547. }
  548. // FIXME: Also remove dead threads from processes
  549. for (auto tid : tids_to_remove) {
  550. m_threads.remove(tid);
  551. for (size_t i = 0; i < m_processes.size(); ++i) {
  552. auto& process = m_processes[i];
  553. process->threads.remove_all_matching([&](auto const& thread) { return thread->current_state.tid == tid; });
  554. if (process->threads.size() == 0) {
  555. m_processes.remove(i);
  556. --i;
  557. }
  558. }
  559. }
  560. if (on_cpu_info_change)
  561. on_cpu_info_change(m_cpus);
  562. if (on_state_update)
  563. on_state_update(!all_processes.is_error() ? all_processes.value().processes.size() : 0, m_threads.size());
  564. // FIXME: This is a rather hackish way of invalidating indices.
  565. // It would be good if GUI::Model had a way to orchestrate removal/insertion while preserving indices.
  566. did_update(previous_tid_count == m_threads.size() ? GUI::Model::UpdateFlag::DontInvalidateIndices : GUI::Model::UpdateFlag::InvalidateAllIndices);
  567. }
  568. bool ProcessModel::is_default_column(int index) const
  569. {
  570. switch (index) {
  571. case Column::PID:
  572. case Column::TID:
  573. case Column::Name:
  574. case Column::CPU:
  575. case Column::User:
  576. case Column::Virtual:
  577. case Column::DirtyPrivate:
  578. return true;
  579. default:
  580. return false;
  581. }
  582. }