ProcessModel.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. #pragma once
  27. #include <AK/HashMap.h>
  28. #include <AK/String.h>
  29. #include <AK/Vector.h>
  30. #include <LibGUI/GModel.h>
  31. #include <unistd.h>
  32. class GraphWidget;
  33. struct PidAndTid {
  34. bool operator==(const PidAndTid& other) const
  35. {
  36. return pid == other.pid && tid == other.tid;
  37. }
  38. pid_t pid;
  39. int tid;
  40. };
  41. class ProcessModel final : public GUI::Model {
  42. public:
  43. enum Column {
  44. Icon = 0,
  45. Name,
  46. CPU,
  47. State,
  48. Priority,
  49. EffectivePriority,
  50. User,
  51. PID,
  52. TID,
  53. Virtual,
  54. Physical,
  55. DirtyPrivate,
  56. CleanInode,
  57. PurgeableVolatile,
  58. PurgeableNonvolatile,
  59. Veil,
  60. Pledge,
  61. Syscalls,
  62. InodeFaults,
  63. ZeroFaults,
  64. CowFaults,
  65. FileReadBytes,
  66. FileWriteBytes,
  67. UnixSocketReadBytes,
  68. UnixSocketWriteBytes,
  69. IPv4SocketReadBytes,
  70. IPv4SocketWriteBytes,
  71. __Count
  72. };
  73. static ProcessModel& the();
  74. static NonnullRefPtr<ProcessModel> create() { return adopt(*new ProcessModel); }
  75. virtual ~ProcessModel() override;
  76. virtual int row_count(const GUI::ModelIndex&) const override;
  77. virtual int column_count(const GUI::ModelIndex&) const override;
  78. virtual String column_name(int column) const override;
  79. virtual ColumnMetadata column_metadata(int column) const override;
  80. virtual GUI::Variant data(const GUI::ModelIndex&, Role = Role::Display) const override;
  81. virtual void update() override;
  82. Function<void(float)> on_new_cpu_data_point;
  83. private:
  84. ProcessModel();
  85. struct ThreadState {
  86. int tid;
  87. pid_t pid;
  88. unsigned times_scheduled;
  89. String name;
  90. String state;
  91. String user;
  92. String pledge;
  93. String veil;
  94. u32 priority;
  95. u32 effective_priority;
  96. size_t amount_virtual;
  97. size_t amount_resident;
  98. size_t amount_dirty_private;
  99. size_t amount_clean_inode;
  100. size_t amount_purgeable_volatile;
  101. size_t amount_purgeable_nonvolatile;
  102. unsigned syscall_count;
  103. unsigned inode_faults;
  104. unsigned zero_faults;
  105. unsigned cow_faults;
  106. unsigned unix_socket_read_bytes;
  107. unsigned unix_socket_write_bytes;
  108. unsigned ipv4_socket_read_bytes;
  109. unsigned ipv4_socket_write_bytes;
  110. unsigned file_read_bytes;
  111. unsigned file_write_bytes;
  112. float cpu_percent;
  113. int icon_id;
  114. };
  115. struct Thread {
  116. ThreadState current_state;
  117. ThreadState previous_state;
  118. };
  119. HashMap<uid_t, String> m_usernames;
  120. HashMap<PidAndTid, NonnullOwnPtr<Thread>> m_threads;
  121. Vector<PidAndTid> m_pids;
  122. RefPtr<Gfx::Bitmap> m_generic_process_icon;
  123. RefPtr<Gfx::Bitmap> m_high_priority_icon;
  124. RefPtr<Gfx::Bitmap> m_low_priority_icon;
  125. RefPtr<Gfx::Bitmap> m_normal_priority_icon;
  126. };
  127. namespace AK {
  128. template<>
  129. struct Traits<PidAndTid> : public GenericTraits<PidAndTid> {
  130. static unsigned hash(const PidAndTid& value) { return pair_int_hash(value.pid, value.tid); }
  131. };
  132. }