1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 02:47:35 +00:00

ProcessManager: Show some basic system memory stats below the process table.

This really improves the feeling of "system overview" :^)
This commit is contained in:
Andreas Kling 2019-03-10 12:13:22 +01:00
parent 8017c1e17c
commit 37388b311f
9 changed files with 158 additions and 9 deletions

View file

@ -1,6 +1,7 @@
OBJS = \
ProcessTableModel.o \
ProcessTableView.o \
MemoryStatsWidget.o \
main.o
APP = ProcessManager

View file

@ -0,0 +1,101 @@
#include "MemoryStatsWidget.h"
#include <SharedGraphics/Painter.h>
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GLabel.h>
#include <LibGUI/GStyle.h>
#include <stdio.h>
#include <stdlib.h>
MemoryStatsWidget::MemoryStatsWidget(GWidget* parent)
: GWidget(parent)
{
set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
set_preferred_size({ 0, 60 });
set_layout(make<GBoxLayout>(Orientation::Vertical));
layout()->set_margins({ 0, 8, 0, 0 });
layout()->set_spacing(3);
m_user_physical_pages_label = new GLabel(this);
m_user_physical_pages_label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
m_user_physical_pages_label->set_preferred_size({ 0, 12 });
m_user_physical_pages_label->set_fill_with_background_color(false);
m_supervisor_physical_pages_label = new GLabel(this);
m_supervisor_physical_pages_label->set_fill_with_background_color(false);
m_supervisor_physical_pages_label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
m_supervisor_physical_pages_label->set_preferred_size({ 0, 12 });
m_kmalloc_label = new GLabel(this);
m_kmalloc_label->set_fill_with_background_color(false);
m_kmalloc_label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
m_kmalloc_label->set_preferred_size({ 0, 12 });
start_timer(1000);
refresh();
}
MemoryStatsWidget::~MemoryStatsWidget()
{
}
static inline size_t page_count_to_kb(size_t kb)
{
return (kb * 4096) / 1024;
}
static inline size_t bytes_to_kb(size_t bytes)
{
return bytes / 1024;
}
void MemoryStatsWidget::refresh()
{
FILE* fp = fopen("/proc/memstat", "r");
if (!fp) {
perror("failed to open /proc/memstat");
exit(1);
}
for (;;) {
char buf[BUFSIZ];
char* ptr = fgets(buf, sizeof(buf), fp);
if (!ptr)
break;
auto parts = String(buf, Chomp).split(',');
if (parts.size() < 7)
break;
bool ok;
unsigned kmalloc_sum_eternal = parts[0].to_uint(ok);
ASSERT(ok);
unsigned kmalloc_sum_alloc = parts[1].to_uint(ok);
ASSERT(ok);
unsigned kmalloc_sum_free = parts[2].to_uint(ok);
ASSERT(ok);
unsigned user_pages_alloc = parts[3].to_uint(ok);
ASSERT(ok);
unsigned user_pages_free = parts[4].to_uint(ok);
ASSERT(ok);
unsigned supervisor_pages_alloc = parts[5].to_uint(ok);
ASSERT(ok);
unsigned supervisor_pages_free = parts[6].to_uint(ok);
ASSERT(ok);
m_kmalloc_label->set_text(String::format("Kernel heap: %uK allocated, %uK free\n", bytes_to_kb(kmalloc_sum_alloc), bytes_to_kb(kmalloc_sum_free)));
m_user_physical_pages_label->set_text(String::format("Userspace physical: %uK allocated, %uK free\n", page_count_to_kb(user_pages_alloc), page_count_to_kb(user_pages_free)));
m_supervisor_physical_pages_label->set_text(String::format("Supervisor physical: %uK allocated, %uK free\n", page_count_to_kb(supervisor_pages_alloc), page_count_to_kb(supervisor_pages_free)));
break;
}
fclose(fp);
}
void MemoryStatsWidget::timer_event(GTimerEvent&)
{
refresh();
}
void MemoryStatsWidget::paint_event(GPaintEvent& event)
{
Painter painter(*this);
painter.set_clip_rect(event.rect());
GStyle::the().paint_surface(painter, rect());
}

View file

@ -0,0 +1,21 @@
#pragma once
#include <LibGUI/GWidget.h>
class GLabel;
class MemoryStatsWidget final : public GWidget {
public:
explicit MemoryStatsWidget(GWidget* parent);
virtual ~MemoryStatsWidget() override;
void refresh();
private:
virtual void timer_event(GTimerEvent&) override;
virtual void paint_event(GPaintEvent&) override;
GLabel* m_user_physical_pages_label { nullptr };
GLabel* m_supervisor_physical_pages_label { nullptr };
GLabel* m_kmalloc_label { nullptr };
};

View file

@ -24,8 +24,7 @@ void ProcessTableView::timer_event(GTimerEvent&)
void ProcessTableView::model_notification(const GModelNotification& notification)
{
if (notification.type() == GModelNotification::ModelUpdated) {
if (on_status_message)
on_status_message(String::format("%d processes", model()->row_count()));
// Do something?
return;
}
}

View file

@ -13,8 +13,6 @@ public:
pid_t selected_pid() const;
Function<void(String)> on_status_message;
protected:
virtual void model_notification(const GModelNotification&) override;

View file

@ -2,7 +2,6 @@
#include <LibGUI/GWidget.h>
#include <LibGUI/GBoxLayout.h>
#include <LibGUI/GApplication.h>
#include <LibGUI/GStatusBar.h>
#include <LibGUI/GToolBar.h>
#include <LibGUI/GMenuBar.h>
#include <LibGUI/GAction.h>
@ -10,6 +9,7 @@
#include <stdio.h>
#include <signal.h>
#include "ProcessTableView.h"
#include "MemoryStatsWidget.h"
int main(int argc, char** argv)
{
@ -21,10 +21,8 @@ int main(int argc, char** argv)
auto* toolbar = new GToolBar(widget);
auto* process_table_view = new ProcessTableView(widget);
auto* statusbar = new GStatusBar(widget);
process_table_view->on_status_message = [statusbar] (String message) {
statusbar->set_text(move(message));
};
new MemoryStatsWidget(widget);
auto kill_action = GAction::create("Kill process", GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, "/res/icons/kill16.rgb", { 16, 16 }), [process_table_view] (const GAction&) {
pid_t pid = process_table_view->selected_pid();