Kernel+ProcessManager: Convert /proc/memstat to JSON.

This commit is contained in:
Andreas Kling 2019-07-01 18:43:01 +02:00
parent 438a14c597
commit aaedc24f15
Notes: sideshowbarker 2024-07-19 13:24:45 +09:00
2 changed files with 34 additions and 50 deletions

View file

@ -1,5 +1,6 @@
#include "MemoryStatsWidget.h" #include "MemoryStatsWidget.h"
#include "GraphWidget.h" #include "GraphWidget.h"
#include <AK/JsonObject.h>
#include <LibGUI/GBoxLayout.h> #include <LibGUI/GBoxLayout.h>
#include <LibGUI/GLabel.h> #include <LibGUI/GLabel.h>
#include <LibGUI/GPainter.h> #include <LibGUI/GPainter.h>
@ -60,47 +61,31 @@ void MemoryStatsWidget::refresh()
{ {
m_proc_memstat.seek(0); m_proc_memstat.seek(0);
for (;;) { auto file_contents = m_proc_memstat.read_all();
auto line = m_proc_memstat.read_line(BUFSIZ); auto json = JsonValue::from_string(file_contents).as_object();
if (line.is_null())
break;
auto chomped = String((const char*)line.pointer(), line.size() - 1, Chomp);
auto parts = chomped.split_view(',');
if (parts.size() < 9)
break;
bool ok;
unsigned kmalloc_sum_eternal = parts[0].to_uint(ok);
ASSERT(ok);
(void)kmalloc_sum_eternal;
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);
unsigned kmalloc_call_count = parts[7].to_uint(ok);
ASSERT(ok);
unsigned kfree_call_count = parts[8].to_uint(ok);
ASSERT(ok);
size_t kmalloc_sum_available = kmalloc_sum_alloc + kmalloc_sum_free; unsigned kmalloc_eternal_allocated = json.get("kmalloc_eternal_allocated").to_dword();
size_t user_pages_available = user_pages_alloc + user_pages_free; (void)kmalloc_eternal_allocated;
size_t supervisor_pages_available = supervisor_pages_alloc + supervisor_pages_free; unsigned kmalloc_allocated = json.get("kmalloc_allocated").to_dword();
unsigned kmalloc_available = json.get("kmalloc_available").to_dword();
unsigned user_physical_allocated = json.get("user_physical_allocated").to_dword();
unsigned user_physical_available = json.get("user_physical_available").to_dword();
unsigned super_physical_alloc = json.get("super_physical_allocated").to_dword();
unsigned super_physical_free = json.get("super_physical_available").to_dword();
unsigned kmalloc_call_count = json.get("kmalloc_call_count").to_dword();
unsigned kfree_call_count = json.get("kfree_call_count").to_dword();
m_kmalloc_label->set_text(String::format("%uK/%uK", bytes_to_kb(kmalloc_sum_alloc), bytes_to_kb(kmalloc_sum_available))); size_t kmalloc_sum_available = kmalloc_allocated + kmalloc_available;
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))); size_t user_pages_available = user_physical_allocated + user_physical_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))); size_t supervisor_pages_available = super_physical_alloc + super_physical_free;
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_kmalloc_label->set_text(String::format("%uK/%uK", bytes_to_kb(kmalloc_allocated), bytes_to_kb(kmalloc_sum_available)));
m_graph.add_value(page_count_to_kb(user_pages_alloc)); m_user_physical_pages_label->set_text(String::format("%uK/%uK", page_count_to_kb(user_physical_allocated), page_count_to_kb(user_pages_available)));
} m_supervisor_physical_pages_label->set_text(String::format("%uK/%uK", page_count_to_kb(super_physical_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_physical_allocated));
} }
void MemoryStatsWidget::timer_event(CTimerEvent&) void MemoryStatsWidget::timer_event(CTimerEvent&)

View file

@ -566,18 +566,17 @@ ByteBuffer procfs$summary(InodeIdentifier)
ByteBuffer procfs$memstat(InodeIdentifier) ByteBuffer procfs$memstat(InodeIdentifier)
{ {
InterruptDisabler disabler; InterruptDisabler disabler;
StringBuilder builder(128); JsonObject json;
builder.appendf("%u,%u,%u,%u,%u,%u,%u,%u,%u\n", json.set("kmalloc_allocated", sum_alloc);
kmalloc_sum_eternal, json.set("kmalloc_available", sum_free);
sum_alloc, json.set("kmalloc_eternal_allocated", kmalloc_sum_eternal);
sum_free, json.set("user_physical_allocated", MM.user_physical_pages_used());
MM.user_physical_pages_used(), json.set("user_physical_available", MM.user_physical_pages());
MM.user_physical_pages() - MM.user_physical_pages_used(), json.set("super_physical_allocated", MM.super_physical_pages_used());
MM.super_physical_pages_used(), json.set("super_physical_available", MM.super_physical_pages());
MM.super_physical_pages() - MM.super_physical_pages_used(), json.set("kmalloc_call_count", g_kmalloc_call_count);
g_kmalloc_call_count, json.set("kfree_call_count", g_kfree_call_count);
g_kfree_call_count); return json.serialized().to_byte_buffer();
return builder.to_byte_buffer();
} }
ByteBuffer procfs$all(InodeIdentifier) ByteBuffer procfs$all(InodeIdentifier)