ProcessStateWidget.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #include "ProcessStateWidget.h"
  27. #include <LibCore/ProcessStatisticsReader.h>
  28. #include <LibCore/Timer.h>
  29. #include <LibGUI/BoxLayout.h>
  30. #include <LibGUI/Label.h>
  31. #include <LibGfx/Font.h>
  32. #include <unistd.h>
  33. namespace HackStudio {
  34. ProcessStateWidget::ProcessStateWidget()
  35. {
  36. set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  37. set_preferred_size(0, 20);
  38. set_visible(false);
  39. set_layout<GUI::HorizontalBoxLayout>();
  40. auto& pid_label_label = add<GUI::Label>("Process:");
  41. pid_label_label.set_font(Gfx::Font::default_bold_font());
  42. m_pid_label = add<GUI::Label>("");
  43. auto& state_label_label = add<GUI::Label>("State:");
  44. state_label_label.set_font(Gfx::Font::default_bold_font());
  45. m_state_label = add<GUI::Label>("");
  46. // FIXME: This should show CPU% instead.
  47. auto& cpu_label_label = add<GUI::Label>("Times scheduled:");
  48. cpu_label_label.set_font(Gfx::Font::default_bold_font());
  49. m_cpu_label = add<GUI::Label>("");
  50. auto& memory_label_label = add<GUI::Label>("Memory (resident):");
  51. memory_label_label.set_font(Gfx::Font::default_bold_font());
  52. m_memory_label = add<GUI::Label>("");
  53. m_timer = add<Core::Timer>(500, [this] {
  54. refresh();
  55. });
  56. }
  57. ProcessStateWidget::~ProcessStateWidget()
  58. {
  59. }
  60. void ProcessStateWidget::refresh()
  61. {
  62. pid_t pid = tcgetpgrp(m_tty_fd);
  63. auto processes = Core::ProcessStatisticsReader::get_all();
  64. auto child_process_data = processes.get(pid);
  65. if (!child_process_data.has_value())
  66. return;
  67. auto active_process_data = processes.get(child_process_data.value().pgid);
  68. auto& data = active_process_data.value();
  69. m_pid_label->set_text(String::formatted("{}({})", data.name, pid));
  70. m_state_label->set_text(data.threads.first().state);
  71. m_cpu_label->set_text(String::formatted("{}", data.threads.first().times_scheduled));
  72. m_memory_label->set_text(String::formatted("{}", data.amount_resident));
  73. }
  74. void ProcessStateWidget::set_tty_fd(int tty_fd)
  75. {
  76. m_tty_fd = tty_fd;
  77. if (m_tty_fd == -1) {
  78. set_visible(false);
  79. return;
  80. }
  81. set_visible(true);
  82. refresh();
  83. }
  84. }