ProcessModel.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #pragma once
  27. #include <AK/HashMap.h>
  28. #include <AK/NonnullOwnPtrVector.h>
  29. #include <AK/String.h>
  30. #include <AK/Vector.h>
  31. #include <LibGUI/Model.h>
  32. #include <unistd.h>
  33. class GraphWidget;
  34. class ProcessModel final : public GUI::Model {
  35. public:
  36. enum Column {
  37. Icon = 0,
  38. PID,
  39. Name,
  40. CPU,
  41. State,
  42. User,
  43. Virtual,
  44. DirtyPrivate,
  45. Pledge,
  46. Physical,
  47. CleanInode,
  48. PurgeableVolatile,
  49. PurgeableNonvolatile,
  50. Veil,
  51. Processor,
  52. Priority,
  53. TID,
  54. PPID,
  55. PGID,
  56. SID,
  57. Syscalls,
  58. InodeFaults,
  59. ZeroFaults,
  60. CowFaults,
  61. FileReadBytes,
  62. FileWriteBytes,
  63. UnixSocketReadBytes,
  64. UnixSocketWriteBytes,
  65. IPv4SocketReadBytes,
  66. IPv4SocketWriteBytes,
  67. __Count
  68. };
  69. static ProcessModel& the();
  70. static NonnullRefPtr<ProcessModel> create() { return adopt(*new ProcessModel); }
  71. virtual ~ProcessModel() override;
  72. virtual int row_count(const GUI::ModelIndex&) const override;
  73. virtual int column_count(const GUI::ModelIndex&) const override;
  74. virtual String column_name(int column) const override;
  75. virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override;
  76. virtual void update() override;
  77. struct CpuInfo {
  78. u32 id;
  79. float total_cpu_percent { 0.0 };
  80. float total_cpu_percent_kernel { 0.0 };
  81. explicit CpuInfo(u32 id)
  82. : id(id)
  83. {
  84. }
  85. };
  86. Function<void(const NonnullOwnPtrVector<CpuInfo>&)> on_cpu_info_change;
  87. Function<void(int process_count, int thread_count)> on_state_update;
  88. const NonnullOwnPtrVector<CpuInfo>& cpus() const { return m_cpus; }
  89. private:
  90. ProcessModel();
  91. struct ThreadState {
  92. pid_t tid;
  93. pid_t pid;
  94. pid_t ppid;
  95. pid_t pgid;
  96. pid_t sid;
  97. unsigned ticks_user;
  98. unsigned ticks_kernel;
  99. String executable;
  100. String name;
  101. String state;
  102. String user;
  103. String pledge;
  104. String veil;
  105. u32 cpu;
  106. u32 priority;
  107. size_t amount_virtual;
  108. size_t amount_resident;
  109. size_t amount_dirty_private;
  110. size_t amount_clean_inode;
  111. size_t amount_purgeable_volatile;
  112. size_t amount_purgeable_nonvolatile;
  113. unsigned syscall_count;
  114. unsigned inode_faults;
  115. unsigned zero_faults;
  116. unsigned cow_faults;
  117. unsigned unix_socket_read_bytes;
  118. unsigned unix_socket_write_bytes;
  119. unsigned ipv4_socket_read_bytes;
  120. unsigned ipv4_socket_write_bytes;
  121. unsigned file_read_bytes;
  122. unsigned file_write_bytes;
  123. float cpu_percent;
  124. float cpu_percent_kernel;
  125. };
  126. struct Thread {
  127. ThreadState current_state;
  128. ThreadState previous_state;
  129. };
  130. HashMap<int, NonnullOwnPtr<Thread>> m_threads;
  131. NonnullOwnPtrVector<CpuInfo> m_cpus;
  132. Vector<int> m_tids;
  133. RefPtr<Core::File> m_proc_all;
  134. };