1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:18:11 +00:00

ProcessManager: Add CPU/memory usage graphs in a separate tab.

Finally we get some real use for the new GTabWidget. :^)
This commit is contained in:
Andreas Kling 2019-05-06 03:21:23 +02:00
parent 6df3df62be
commit 25bb7a59ac
10 changed files with 140 additions and 19 deletions

View file

@ -0,0 +1,48 @@
#include "GraphWidget.h"
#include <LibGUI/GPainter.h>
GraphWidget::GraphWidget(GWidget* parent)
: GFrame(parent)
{
set_frame_thickness(2);
set_frame_shape(FrameShape::Container);
set_frame_shadow(FrameShadow::Sunken);
}
GraphWidget::~GraphWidget()
{
}
void GraphWidget::add_value(int value)
{
m_values.enqueue(value);
update();
}
void GraphWidget::paint_event(GPaintEvent& event)
{
GFrame::paint_event(event);
GPainter painter(*this);
painter.add_clip_rect(event.rect());
painter.add_clip_rect(frame_inner_rect());
painter.fill_rect(event.rect(), Color::Black);
auto inner_rect = frame_inner_rect();
float scale = (float)inner_rect.height() / (float)m_max;
for (int i = 0; i < m_values.size(); ++i) {
int x = inner_rect.right() - i + 1;
if (x < 0)
break;
float scaled_value = (float)m_values.at(m_values.size() - i) * scale;
painter.draw_line({ x, inner_rect.bottom() }, { x, inner_rect.bottom() - (int)scaled_value }, m_graph_color);
}
if (!m_values.is_empty() && text_formatter) {
Rect text_rect = inner_rect.shrunken(8, 8);
text_rect.set_height(font().glyph_height());
auto text = text_formatter(m_values.last(), m_max);
painter.draw_text(text_rect.translated(1, 1), text.characters(), TextAlignment::CenterRight, Color::Black);
painter.draw_text(text_rect, text.characters(), TextAlignment::CenterRight, m_text_color);
}
}

View file

@ -0,0 +1,24 @@
#include <LibGUI/GFrame.h>
#include <AK/CircularQueue.h>
class GraphWidget final : public GFrame {
public:
explicit GraphWidget(GWidget* parent);
virtual ~GraphWidget() override;
void set_max(int max) { m_max = max; }
void add_value(int);
void set_graph_color(Color color) { m_graph_color = color; }
void set_text_color(Color color) { m_text_color = color; }
Function<String(int value, int max)> text_formatter;
private:
virtual void paint_event(GPaintEvent&) override;
int m_max { 100 };
CircularQueue<int, 4000> m_values;
Color m_graph_color;
Color m_text_color;
};

View file

@ -4,6 +4,7 @@ OBJS = \
ProcessModel.o \
ProcessTableView.o \
MemoryStatsWidget.o \
GraphWidget.o \
main.o
APP = ProcessManager

View file

@ -1,4 +1,5 @@
#include "MemoryStatsWidget.h"
#include "GraphWidget.h"
#include <LibGUI/GPainter.h>
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GLabel.h>
@ -6,8 +7,9 @@
#include <stdio.h>
#include <stdlib.h>
MemoryStatsWidget::MemoryStatsWidget(GWidget* parent)
MemoryStatsWidget::MemoryStatsWidget(GraphWidget& graph, GWidget* parent)
: GWidget(parent)
, m_graph(graph)
, m_proc_memstat("/proc/memstat")
{
if (!m_proc_memstat.open(CIODevice::OpenMode::ReadOnly))
@ -95,6 +97,9 @@ void MemoryStatsWidget::refresh()
m_user_physical_pages_label->set_text(String::format("%uK/%uK", page_count_to_kb(user_pages_alloc), page_count_to_kb(user_pages_available)));
m_supervisor_physical_pages_label->set_text(String::format("%uK/%uK", page_count_to_kb(supervisor_pages_alloc), page_count_to_kb(supervisor_pages_available)));
m_kmalloc_count_label->set_text(String::format("%u/%u (+%u)", kmalloc_call_count, kfree_call_count, kmalloc_call_count - kfree_call_count));
m_graph.set_max(page_count_to_kb(user_pages_available));
m_graph.add_value(page_count_to_kb(user_pages_alloc));
}
}

View file

@ -4,10 +4,11 @@
#include <LibCore/CFile.h>
class GLabel;
class GraphWidget;
class MemoryStatsWidget final : public GWidget {
public:
explicit MemoryStatsWidget(GWidget* parent);
MemoryStatsWidget(GraphWidget& graph, GWidget* parent);
virtual ~MemoryStatsWidget() override;
void refresh();
@ -15,6 +16,7 @@ public:
private:
virtual void timer_event(CTimerEvent&) override;
GraphWidget& m_graph;
GLabel* m_user_physical_pages_label { nullptr };
GLabel* m_supervisor_physical_pages_label { nullptr };
GLabel* m_kmalloc_label { nullptr };

View file

@ -1,10 +1,12 @@
#include "ProcessModel.h"
#include "GraphWidget.h"
#include <LibCore/CFile.h>
#include <fcntl.h>
#include <stdio.h>
#include <pwd.h>
ProcessModel::ProcessModel()
ProcessModel::ProcessModel(GraphWidget& graph)
: m_graph(graph)
{
setpwent();
while (auto* passwd = getpwent())
@ -197,6 +199,7 @@ void ProcessModel::update()
}
m_pids.clear();
float total_cpu_percent = 0;
Vector<pid_t, 16> pids_to_remove;
for (auto& it : m_processes) {
if (!live_pids.contains(it.key)) {
@ -206,11 +209,15 @@ void ProcessModel::update()
auto& process = *it.value;
dword nsched_diff = process.current_state.nsched - process.previous_state.nsched;
process.current_state.cpu_percent = ((float)nsched_diff * 100) / (float)(sum_nsched - last_sum_nsched);
if (it.key != 0)
if (it.key != 0) {
total_cpu_percent += process.current_state.cpu_percent;
m_pids.append(it.key);
}
}
for (auto pid : pids_to_remove)
m_processes.remove(pid);
m_graph.add_value(total_cpu_percent);
did_update();
}

View file

@ -6,6 +6,8 @@
#include <LibGUI/GModel.h>
#include <unistd.h>
class GraphWidget;
class ProcessModel final : public GModel {
public:
enum Column {
@ -22,7 +24,7 @@ public:
__Count
};
static Retained<ProcessModel> create() { return adopt(*new ProcessModel); }
static Retained<ProcessModel> create(GraphWidget& graph) { return adopt(*new ProcessModel(graph)); }
virtual ~ProcessModel() override;
virtual int row_count(const GModelIndex&) const override;
@ -33,7 +35,9 @@ public:
virtual void update() override;
private:
ProcessModel();
explicit ProcessModel(GraphWidget&);
GraphWidget& m_graph;
struct ProcessState {
pid_t pid;

View file

@ -3,10 +3,10 @@
#include <LibGUI/GSortingProxyModel.h>
#include <stdio.h>
ProcessTableView::ProcessTableView(GWidget* parent)
ProcessTableView::ProcessTableView(GraphWidget& graph, GWidget* parent)
: GTableView(parent)
{
set_model(GSortingProxyModel::create(ProcessModel::create()));
set_model(GSortingProxyModel::create(ProcessModel::create(graph)));
model()->set_key_column_and_sort_order(ProcessModel::Column::CPU, GSortOrder::Descending);
refresh();
}

View file

@ -4,11 +4,12 @@
#include <AK/Function.h>
#include <unistd.h>
class GraphWidget;
class ProcessModel;
class ProcessTableView final : public GTableView {
public:
explicit ProcessTableView(GWidget* parent);
ProcessTableView(GraphWidget&, GWidget* parent);
virtual ~ProcessTableView() override;
pid_t selected_pid() const;

View file

@ -5,6 +5,7 @@
#include <LibGUI/GApplication.h>
#include <LibGUI/GToolBar.h>
#include <LibGUI/GMenuBar.h>
#include <LibGUI/GGroupBox.h>
#include <LibGUI/GAction.h>
#include <LibGUI/GTabWidget.h>
#include <LibGUI/GLabel.h>
@ -13,6 +14,7 @@
#include <signal.h>
#include "ProcessTableView.h"
#include "MemoryStatsWidget.h"
#include "GraphWidget.h"
int main(int argc, char** argv)
{
@ -28,21 +30,45 @@ int main(int argc, char** argv)
auto* widget = new GWidget(nullptr);
tabwidget->add_widget("Processes", widget);
auto* placeholder_label = new GLabel("Placeholder text");
placeholder_label->set_fill_with_background_color(true);
placeholder_label->set_background_color(Color::LightGray);
tabwidget->add_widget("Placeholder", placeholder_label);
auto* placeholder2_label = new GLabel("Placeholder2 text");
placeholder2_label->set_fill_with_background_color(true);
placeholder2_label->set_background_color(Color::LightGray);
tabwidget->add_widget("Another", placeholder2_label);
auto* graphs_container = new GWidget;
graphs_container->set_fill_with_background_color(true);
graphs_container->set_background_color(Color::LightGray);
graphs_container->set_layout(make<GBoxLayout>(Orientation::Vertical));
graphs_container->layout()->set_margins({ 4, 4, 4, 4 });
graphs_container->layout()->set_spacing(4);
auto* cpu_graph_group_box = new GGroupBox("CPU usage", graphs_container);
cpu_graph_group_box->set_layout(make<GBoxLayout>(Orientation::Vertical));
cpu_graph_group_box->layout()->set_margins({ 6, 16, 6, 6 });
cpu_graph_group_box->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
cpu_graph_group_box->set_preferred_size({ 0, 120 });
auto* cpu_graph = new GraphWidget(cpu_graph_group_box);
cpu_graph->set_max(100);
cpu_graph->set_text_color(Color::Green);
cpu_graph->set_graph_color(Color::from_rgb(0x00bb00));
cpu_graph->text_formatter = [] (int value, int) {
return String::format("%d%%", value);
};
auto* memory_graph_group_box = new GGroupBox("Memory usage", graphs_container);
memory_graph_group_box->set_layout(make<GBoxLayout>(Orientation::Vertical));
memory_graph_group_box->layout()->set_margins({ 4, 16, 4, 4 });
tabwidget->add_widget("Graphs", graphs_container);
memory_graph_group_box->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
memory_graph_group_box->set_preferred_size({ 0, 120 });
auto* memory_graph = new GraphWidget(memory_graph_group_box);
memory_graph->set_text_color(Color::Cyan);
memory_graph->set_graph_color(Color::from_rgb(0x00bbbb));
memory_graph->text_formatter = [] (int value, int max) {
return String::format("%d / %d KB", value, max);
};
widget->set_layout(make<GBoxLayout>(Orientation::Vertical));
auto* toolbar = new GToolBar(widget);
auto* process_table_view = new ProcessTableView(widget);
auto* memory_stats_widget = new MemoryStatsWidget(widget);
auto* process_table_view = new ProcessTableView(*cpu_graph, widget);
auto* memory_stats_widget = new MemoryStatsWidget(*memory_graph, widget);
auto* refresh_timer = new CTimer(1000, [&] {
process_table_view->refresh();
@ -86,6 +112,9 @@ int main(int argc, char** argv)
menubar->add_menu(move(process_menu));
auto frequency_menu = make<GMenu>("Frequency");
frequency_menu->add_action(GAction::create("0.25 sec", [refresh_timer] (auto&) {
refresh_timer->restart(250);
}));
frequency_menu->add_action(GAction::create("0.5 sec", [refresh_timer] (auto&) {
refresh_timer->restart(500);
}));