ProcessView.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <AK/FileSystemPath.h>
  5. #include <AK/HashMap.h>
  6. #include <SharedGraphics/GraphicsBitmap.h>
  7. #include <SharedGraphics/Painter.h>
  8. #include <LibGUI/GScrollBar.h>
  9. #include <LibGUI/GTableModel.h>
  10. #include "ProcessView.h"
  11. static HashMap<unsigned, String>* s_usernames;
  12. class ProcessTableModel final : public GTableModel {
  13. public:
  14. ProcessTableModel()
  15. {
  16. }
  17. virtual ~ProcessTableModel() override { }
  18. virtual int row_count() const override { return m_processes.size(); }
  19. virtual int column_count() const override { return 4; }
  20. virtual String column_name(int column) const override
  21. {
  22. switch (column) {
  23. case 0: return "PID";
  24. case 1: return "State";
  25. case 2: return "CPU";
  26. case 3: return "Name";
  27. default: ASSERT_NOT_REACHED();
  28. }
  29. }
  30. virtual int column_width(int column) const override
  31. {
  32. switch (column) {
  33. case 0: return 30;
  34. case 1: return 80;
  35. case 2: return 30;
  36. case 3: return 200;
  37. default: ASSERT_NOT_REACHED();
  38. }
  39. }
  40. virtual GModelIndex selected_index() const override { return { m_selected_row, 0 }; }
  41. virtual void set_selected_index(GModelIndex index) override
  42. {
  43. if (index.row() >= 0 && index.row() < m_pids.size())
  44. m_selected_row = index.row();
  45. }
  46. virtual String data(int row, int column) const override
  47. {
  48. if (row < 0 || row >= row_count())
  49. return { };
  50. if (column < 0 || column >= column_count())
  51. return { };
  52. auto it = m_processes.find(m_pids[row]);
  53. auto& process = *(*it).value;
  54. switch (column) {
  55. case 0: return String::format("%d", process.current_state.pid);
  56. case 1: return process.current_state.state;
  57. case 2: return String::format("%d", (int)process.current_state.cpu_percent);
  58. case 3: return process.current_state.name;
  59. }
  60. ASSERT_NOT_REACHED();
  61. }
  62. virtual void update() override
  63. {
  64. FILE* fp = fopen("/proc/all", "r");
  65. if (!fp) {
  66. perror("failed to open /proc/all");
  67. exit(1);
  68. }
  69. unsigned last_sum_nsched = 0;
  70. for (auto& it : m_processes)
  71. last_sum_nsched += it.value->current_state.nsched;
  72. HashTable<pid_t> live_pids;
  73. unsigned sum_nsched = 0;
  74. for (;;) {
  75. char buf[BUFSIZ];
  76. char* ptr = fgets(buf, sizeof(buf), fp);
  77. if (!ptr)
  78. break;
  79. auto parts = String(buf, Chomp).split(',');
  80. if (parts.size() < 17)
  81. break;
  82. bool ok;
  83. pid_t pid = parts[0].to_uint(ok);
  84. ASSERT(ok);
  85. unsigned nsched = parts[1].to_uint(ok);
  86. ASSERT(ok);
  87. ProcessState state;
  88. state.pid = pid;
  89. state.nsched = nsched;
  90. unsigned uid = parts[5].to_uint(ok);
  91. ASSERT(ok);
  92. //state.user = s_usernames->get(uid);
  93. state.user = String::format("%u", uid);
  94. state.priority = parts[16];
  95. state.state = parts[7];
  96. state.name = parts[11];
  97. state.linear = parts[12].to_uint(ok);
  98. ASSERT(ok);
  99. state.committed = parts[13].to_uint(ok);
  100. ASSERT(ok);
  101. {
  102. auto it = m_processes.find(pid);
  103. if (it == m_processes.end())
  104. m_processes.set(pid, make<Process>());
  105. }
  106. auto it = m_processes.find(pid);
  107. ASSERT(it != m_processes.end());
  108. (*it).value->previous_state = (*it).value->current_state;
  109. (*it).value->current_state = state;
  110. live_pids.set(pid);
  111. sum_nsched += nsched;
  112. }
  113. int rc = fclose(fp);
  114. ASSERT(rc == 0);
  115. m_pids.clear();
  116. Vector<pid_t> pids_to_remove;
  117. for (auto& it : m_processes) {
  118. if (!live_pids.contains(it.key)) {
  119. pids_to_remove.append(it.key);
  120. continue;
  121. }
  122. auto& process = *it.value;
  123. dword nsched_diff = process.current_state.nsched - process.previous_state.nsched;
  124. process.current_state.cpu_percent = ((float)nsched_diff * 100) / (float)(sum_nsched - last_sum_nsched);
  125. m_pids.append(it.key);
  126. }
  127. for (auto pid : pids_to_remove)
  128. m_processes.remove(pid);
  129. }
  130. pid_t selected_pid() const
  131. {
  132. if (m_selected_row == -1)
  133. return -1;
  134. return m_pids[m_selected_row];
  135. }
  136. private:
  137. struct ProcessState {
  138. pid_t pid;
  139. unsigned nsched;
  140. String name;
  141. String state;
  142. String user;
  143. String priority;
  144. unsigned linear;
  145. unsigned committed;
  146. float cpu_percent;
  147. };
  148. struct Process {
  149. ProcessState current_state;
  150. ProcessState previous_state;
  151. };
  152. HashMap<pid_t, OwnPtr<Process>> m_processes;
  153. Vector<pid_t> m_pids;
  154. int m_selected_row { -1 };
  155. };
  156. ProcessView::ProcessView(GWidget* parent)
  157. : GWidget(parent)
  158. {
  159. m_process_icon = GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/gear16.rgb", { 16, 16 });
  160. m_scrollbar = new GScrollBar(Orientation::Vertical, this);
  161. m_scrollbar->set_step(4);
  162. m_scrollbar->set_big_step(30);
  163. m_scrollbar->on_change = [this] (int) {
  164. update();
  165. };
  166. m_model = make<ProcessTableModel>();
  167. start_timer(1000);
  168. reload();
  169. }
  170. ProcessView::~ProcessView()
  171. {
  172. }
  173. void ProcessView::timer_event(GTimerEvent&)
  174. {
  175. reload();
  176. }
  177. void ProcessView::resize_event(GResizeEvent& event)
  178. {
  179. m_scrollbar->set_relative_rect(event.size().width() - m_scrollbar->preferred_size().width(), 0, m_scrollbar->preferred_size().width(), event.size().height());
  180. }
  181. void ProcessView::reload()
  182. {
  183. m_model->update();
  184. int excess_height = max(0, (item_count() * item_height()) - height());
  185. m_scrollbar->set_range(0, excess_height);
  186. set_status_message(String::format("%d processes", item_count()));
  187. update();
  188. }
  189. Rect ProcessView::row_rect(int item_index) const
  190. {
  191. return { 0, header_height() + (item_index * item_height()), width(), item_height() };
  192. }
  193. void ProcessView::mousedown_event(GMouseEvent& event)
  194. {
  195. if (event.button() == GMouseButton::Left) {
  196. for (int i = 0; i < item_count(); ++i) {
  197. if (!row_rect(i).contains(event.position()))
  198. continue;
  199. m_model->set_selected_index({ i, 0 });
  200. update();
  201. }
  202. }
  203. }
  204. void ProcessView::paint_event(GPaintEvent&)
  205. {
  206. Painter painter(*this);
  207. int horizontal_padding = 5;
  208. int painted_item_index = 0;
  209. int x_offset = 0;
  210. for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
  211. Rect cell_rect(horizontal_padding + x_offset, 0, m_model->column_width(column_index), item_height());
  212. painter.draw_text(cell_rect, m_model->column_name(column_index), TextAlignment::CenterLeft, Color::Black);
  213. x_offset += m_model->column_width(column_index) + horizontal_padding;
  214. }
  215. painter.draw_line({ 0, 0 }, { width() - 1, 0 }, Color::White);
  216. painter.draw_line({ 0, header_height() - 1 }, { width() - 1, header_height() - 1 }, Color::DarkGray);
  217. int y_offset = header_height();
  218. for (int row_index = 0; row_index < m_model->row_count(); ++row_index) {
  219. int y = y_offset + painted_item_index * item_height();
  220. Color background_color;
  221. Color text_color;
  222. if (row_index == m_model->selected_index().row()) {
  223. background_color = Color::from_rgb(0x84351a);
  224. text_color = Color::White;
  225. } else {
  226. background_color = painted_item_index % 2 ? Color(210, 210, 210) : Color::White;
  227. text_color = Color::Black;
  228. }
  229. painter.fill_rect(row_rect(painted_item_index), background_color);
  230. int x_offset = 0;
  231. for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
  232. Rect cell_rect(horizontal_padding + x_offset, y, m_model->column_width(column_index), item_height());
  233. painter.draw_text(cell_rect, m_model->data(row_index, column_index), TextAlignment::CenterLeft, text_color);
  234. x_offset += m_model->column_width(column_index) + horizontal_padding;
  235. }
  236. ++painted_item_index;
  237. };
  238. Rect unpainted_rect(0, painted_item_index * item_height(), width(), height());
  239. unpainted_rect.intersect(rect());
  240. painter.fill_rect(unpainted_rect, Color::White);
  241. }
  242. void ProcessView::set_status_message(String&& message)
  243. {
  244. if (on_status_message)
  245. on_status_message(move(message));
  246. }
  247. int ProcessView::item_count() const
  248. {
  249. return m_model->row_count();
  250. }
  251. pid_t ProcessView::selected_pid() const
  252. {
  253. return m_model->selected_pid();
  254. }