ProcessModel.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/HashMap.h>
  8. #include <AK/NonnullOwnPtrVector.h>
  9. #include <AK/String.h>
  10. #include <AK/Vector.h>
  11. #include <LibGUI/Model.h>
  12. #include <unistd.h>
  13. class GraphWidget;
  14. class ProcessModel final : public GUI::Model {
  15. public:
  16. enum Column {
  17. Icon = 0,
  18. PID,
  19. Name,
  20. CPU,
  21. State,
  22. User,
  23. Virtual,
  24. DirtyPrivate,
  25. Pledge,
  26. Physical,
  27. CleanInode,
  28. PurgeableVolatile,
  29. PurgeableNonvolatile,
  30. Veil,
  31. Processor,
  32. Priority,
  33. TID,
  34. PPID,
  35. PGID,
  36. SID,
  37. Syscalls,
  38. InodeFaults,
  39. ZeroFaults,
  40. CowFaults,
  41. FileReadBytes,
  42. FileWriteBytes,
  43. UnixSocketReadBytes,
  44. UnixSocketWriteBytes,
  45. IPv4SocketReadBytes,
  46. IPv4SocketWriteBytes,
  47. __Count
  48. };
  49. static ProcessModel& the();
  50. static NonnullRefPtr<ProcessModel> create() { return adopt_ref(*new ProcessModel); }
  51. virtual ~ProcessModel() override;
  52. virtual int row_count(const GUI::ModelIndex&) const override;
  53. virtual int column_count(const GUI::ModelIndex&) const override;
  54. virtual String column_name(int column) const override;
  55. virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
  56. virtual bool is_searchable() const override { return true; }
  57. virtual Vector<GUI::ModelIndex> matches(const StringView&, unsigned = MatchesFlag::AllMatching, const GUI::ModelIndex& = GUI::ModelIndex()) override;
  58. virtual bool is_column_sortable(int column_index) const override { return column_index != Column::Icon; }
  59. void update();
  60. struct CpuInfo {
  61. u32 id;
  62. float total_cpu_percent { 0.0 };
  63. float total_cpu_percent_kernel { 0.0 };
  64. explicit CpuInfo(u32 id)
  65. : id(id)
  66. {
  67. }
  68. };
  69. Function<void(const NonnullOwnPtrVector<CpuInfo>&)> on_cpu_info_change;
  70. Function<void(int process_count, int thread_count)> on_state_update;
  71. const NonnullOwnPtrVector<CpuInfo>& cpus() const { return m_cpus; }
  72. private:
  73. ProcessModel();
  74. struct ThreadState {
  75. pid_t tid;
  76. pid_t pid;
  77. pid_t ppid;
  78. pid_t pgid;
  79. pid_t sid;
  80. u64 time_user;
  81. u64 time_kernel;
  82. bool kernel;
  83. String executable;
  84. String name;
  85. String state;
  86. String user;
  87. String pledge;
  88. String veil;
  89. u32 cpu;
  90. u32 priority;
  91. size_t amount_virtual;
  92. size_t amount_resident;
  93. size_t amount_dirty_private;
  94. size_t amount_clean_inode;
  95. size_t amount_purgeable_volatile;
  96. size_t amount_purgeable_nonvolatile;
  97. unsigned syscall_count;
  98. unsigned inode_faults;
  99. unsigned zero_faults;
  100. unsigned cow_faults;
  101. unsigned unix_socket_read_bytes;
  102. unsigned unix_socket_write_bytes;
  103. unsigned ipv4_socket_read_bytes;
  104. unsigned ipv4_socket_write_bytes;
  105. unsigned file_read_bytes;
  106. unsigned file_write_bytes;
  107. float cpu_percent;
  108. float cpu_percent_kernel;
  109. };
  110. struct Thread {
  111. ThreadState current_state;
  112. ThreadState previous_state;
  113. };
  114. HashMap<int, NonnullOwnPtr<Thread>> m_threads;
  115. NonnullOwnPtrVector<CpuInfo> m_cpus;
  116. Vector<int> m_tids;
  117. RefPtr<Core::File> m_proc_all;
  118. GUI::Icon m_kernel_process_icon;
  119. u64 m_total_time_scheduled { 0 };
  120. u64 m_total_time_scheduled_kernel { 0 };
  121. bool m_has_total_scheduled_time { false };
  122. };