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