ProcessStateWidget.cpp 3.6 KB

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