mirror of
https://github.com/RGBCube/serenity
synced 2025-07-02 22:42:08 +00:00
Kernel: Change the format of /proc/all to JSON.
Update ProcessManager, top and WSCPUMonitor to handle the new format. Since the kernel is not allowed to use floating-point math, we now compile the JSON classes in AK without JsonValue::Type::Double support. To accomodate large unsigned ints, I added a JsonValue::Type::UnsignedInt.
This commit is contained in:
parent
561bfd3ed6
commit
2bd8118843
9 changed files with 128 additions and 104 deletions
|
@ -146,7 +146,9 @@ JsonValue JsonParser::parse_number()
|
||||||
{
|
{
|
||||||
auto number_string = extract_while([](char ch) { return ch == '-' || (ch >= '0' && ch <= '9'); });
|
auto number_string = extract_while([](char ch) { return ch == '-' || (ch >= '0' && ch <= '9'); });
|
||||||
bool ok;
|
bool ok;
|
||||||
auto value = JsonValue(number_string.to_int(ok));
|
auto value = JsonValue(number_string.to_uint(ok));
|
||||||
|
if (!ok)
|
||||||
|
value = JsonValue(number_string.to_int(ok));
|
||||||
ASSERT(ok);
|
ASSERT(ok);
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,15 +68,15 @@ JsonValue::JsonValue(int value)
|
||||||
m_value.as_int = value;
|
m_value.as_int = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue::JsonValue(unsigned value)
|
JsonValue::JsonValue(long unsigned value)
|
||||||
|
: JsonValue((unsigned)value)
|
||||||
{
|
{
|
||||||
if (value > INT32_MAX) {
|
}
|
||||||
m_type = Type::Double;
|
|
||||||
m_value.as_double = value;
|
JsonValue::JsonValue(unsigned value)
|
||||||
} else {
|
: m_type(Type::UnsignedInt)
|
||||||
m_type = Type::Int;
|
{
|
||||||
m_value.as_int = (int)value;
|
m_value.as_uint = value;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonValue::JsonValue(const char* cstring)
|
JsonValue::JsonValue(const char* cstring)
|
||||||
|
@ -84,11 +84,13 @@ JsonValue::JsonValue(const char* cstring)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef KERNEL
|
||||||
JsonValue::JsonValue(double value)
|
JsonValue::JsonValue(double value)
|
||||||
: m_type(Type::Double)
|
: m_type(Type::Double)
|
||||||
{
|
{
|
||||||
m_value.as_double = value;
|
m_value.as_double = value;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
JsonValue::JsonValue(bool value)
|
JsonValue::JsonValue(bool value)
|
||||||
: m_type(Type::Bool)
|
: m_type(Type::Bool)
|
||||||
|
@ -153,12 +155,17 @@ void JsonValue::serialize(StringBuilder& builder) const
|
||||||
case Type::Bool:
|
case Type::Bool:
|
||||||
builder.append(m_value.as_bool ? "true" : "false");
|
builder.append(m_value.as_bool ? "true" : "false");
|
||||||
break;
|
break;
|
||||||
|
#ifndef KERNEL
|
||||||
case Type::Double:
|
case Type::Double:
|
||||||
builder.appendf("%g", m_value.as_double);
|
builder.appendf("%g", m_value.as_double);
|
||||||
break;
|
break;
|
||||||
|
#endif
|
||||||
case Type::Int:
|
case Type::Int:
|
||||||
builder.appendf("%d", m_value.as_int);
|
builder.appendf("%d", m_value.as_int);
|
||||||
break;
|
break;
|
||||||
|
case Type::UnsignedInt:
|
||||||
|
builder.appendf("%u", m_value.as_uint);
|
||||||
|
break;
|
||||||
case Type::Undefined:
|
case Type::Undefined:
|
||||||
builder.append("undefined");
|
builder.append("undefined");
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -14,7 +14,10 @@ public:
|
||||||
Undefined,
|
Undefined,
|
||||||
Null,
|
Null,
|
||||||
Int,
|
Int,
|
||||||
|
UnsignedInt,
|
||||||
|
#ifndef KERNEL
|
||||||
Double,
|
Double,
|
||||||
|
#endif
|
||||||
Bool,
|
Bool,
|
||||||
String,
|
String,
|
||||||
Array,
|
Array,
|
||||||
|
@ -34,7 +37,10 @@ public:
|
||||||
|
|
||||||
JsonValue(int);
|
JsonValue(int);
|
||||||
JsonValue(unsigned);
|
JsonValue(unsigned);
|
||||||
|
JsonValue(long unsigned);
|
||||||
|
#ifndef KERNEL
|
||||||
JsonValue(double);
|
JsonValue(double);
|
||||||
|
#endif
|
||||||
JsonValue(bool);
|
JsonValue(bool);
|
||||||
JsonValue(const char*);
|
JsonValue(const char*);
|
||||||
JsonValue(const String&);
|
JsonValue(const String&);
|
||||||
|
@ -75,18 +81,36 @@ public:
|
||||||
bool is_undefined() const { return m_type == Type::Undefined; }
|
bool is_undefined() const { return m_type == Type::Undefined; }
|
||||||
bool is_string() const { return m_type == Type::String; }
|
bool is_string() const { return m_type == Type::String; }
|
||||||
bool is_int() const { return m_type == Type::Int; }
|
bool is_int() const { return m_type == Type::Int; }
|
||||||
|
bool is_uint() const { return m_type == Type::UnsignedInt; }
|
||||||
|
#ifndef KERNEL
|
||||||
bool is_double() const { return m_type == Type::Double; }
|
bool is_double() const { return m_type == Type::Double; }
|
||||||
|
#endif
|
||||||
bool is_array() const { return m_type == Type::Array; }
|
bool is_array() const { return m_type == Type::Array; }
|
||||||
bool is_object() const { return m_type == Type::Object; }
|
bool is_object() const { return m_type == Type::Object; }
|
||||||
bool is_number() const { return m_type == Type::Int || m_type == Type::Double; }
|
bool is_number() const
|
||||||
|
{
|
||||||
|
if (m_type == Type::Int || m_type == Type::UnsignedInt)
|
||||||
|
return true;
|
||||||
|
#ifdef KERNEL
|
||||||
|
return false;
|
||||||
|
#else
|
||||||
|
return m_type == Type::Double;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
dword to_dword(dword default_value = 0) const
|
dword to_dword(dword default_value = 0) const
|
||||||
{
|
{
|
||||||
if (!is_number())
|
if (!is_number())
|
||||||
return default_value;
|
return default_value;
|
||||||
|
#ifdef KERNEL
|
||||||
|
return (dword)m_value.as_int;
|
||||||
|
#else
|
||||||
if (type() == Type::Int)
|
if (type() == Type::Int)
|
||||||
return (dword)m_value.as_int;
|
return (dword)m_value.as_int;
|
||||||
|
if (type() == Type::UnsignedInt)
|
||||||
|
return m_value.as_uint;
|
||||||
return (dword)m_value.as_double;
|
return (dword)m_value.as_double;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -99,8 +123,11 @@ private:
|
||||||
StringImpl* as_string { nullptr };
|
StringImpl* as_string { nullptr };
|
||||||
JsonArray* as_array;
|
JsonArray* as_array;
|
||||||
JsonObject* as_object;
|
JsonObject* as_object;
|
||||||
|
#ifndef KERNEL
|
||||||
double as_double;
|
double as_double;
|
||||||
|
#endif
|
||||||
int as_int;
|
int as_int;
|
||||||
|
unsigned int as_uint;
|
||||||
bool as_bool;
|
bool as_bool;
|
||||||
} m_value;
|
} m_value;
|
||||||
};
|
};
|
||||||
|
|
|
@ -13,7 +13,7 @@ typedef signed short signed_word;
|
||||||
typedef signed int signed_dword;
|
typedef signed int signed_dword;
|
||||||
typedef signed long long int signed_qword;
|
typedef signed long long int signed_qword;
|
||||||
|
|
||||||
typedef decltype(sizeof(void*)) size_t;
|
typedef __SIZE_TYPE__ size_t;
|
||||||
typedef signed_dword ssize_t;
|
typedef signed_dword ssize_t;
|
||||||
|
|
||||||
static_assert(sizeof(size_t) == sizeof(dword));
|
static_assert(sizeof(size_t) == sizeof(dword));
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
#include "ProcessModel.h"
|
#include "ProcessModel.h"
|
||||||
#include "GraphWidget.h"
|
#include "GraphWidget.h"
|
||||||
|
#include <AK/JsonArray.h>
|
||||||
|
#include <AK/JsonObject.h>
|
||||||
|
#include <AK/JsonValue.h>
|
||||||
#include <LibCore/CFile.h>
|
#include <LibCore/CFile.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
|
@ -191,24 +194,16 @@ void ProcessModel::update()
|
||||||
|
|
||||||
HashTable<pid_t> live_pids;
|
HashTable<pid_t> live_pids;
|
||||||
unsigned sum_nsched = 0;
|
unsigned sum_nsched = 0;
|
||||||
for (;;) {
|
auto file_contents = m_proc_all.read_all();
|
||||||
auto line = m_proc_all.read_line(1024);
|
auto json = JsonValue::from_string({ file_contents.data(), file_contents.size() });
|
||||||
if (line.is_empty())
|
json.as_array().for_each([&](auto& value) {
|
||||||
break;
|
const JsonObject& process_object = value.as_object();
|
||||||
auto chomped = String((const char*)line.pointer(), line.size() - 1, Chomp);
|
pid_t pid = process_object.get("pid").to_dword();
|
||||||
auto parts = chomped.split_view(',');
|
unsigned nsched = process_object.get("times_scheduled").to_dword();
|
||||||
if (parts.size() < 18)
|
|
||||||
break;
|
|
||||||
bool ok;
|
|
||||||
pid_t pid = parts[0].to_uint(ok);
|
|
||||||
ASSERT(ok);
|
|
||||||
unsigned nsched = parts[1].to_uint(ok);
|
|
||||||
ASSERT(ok);
|
|
||||||
ProcessState state;
|
ProcessState state;
|
||||||
state.pid = pid;
|
state.pid = pid;
|
||||||
state.nsched = nsched;
|
state.nsched = nsched;
|
||||||
unsigned uid = parts[5].to_uint(ok);
|
unsigned uid = process_object.get("uid").to_dword();
|
||||||
ASSERT(ok);
|
|
||||||
{
|
{
|
||||||
auto it = m_usernames.find((uid_t)uid);
|
auto it = m_usernames.find((uid_t)uid);
|
||||||
if (it != m_usernames.end())
|
if (it != m_usernames.end())
|
||||||
|
@ -216,15 +211,12 @@ void ProcessModel::update()
|
||||||
else
|
else
|
||||||
state.user = String::format("%u", uid);
|
state.user = String::format("%u", uid);
|
||||||
}
|
}
|
||||||
state.priority = parts[16];
|
state.priority = process_object.get("priority").to_string();
|
||||||
state.syscalls = parts[17].to_uint(ok);
|
state.syscalls = process_object.get("syscall_count").to_dword();
|
||||||
ASSERT(ok);
|
state.state = process_object.get("state").to_string();
|
||||||
state.state = parts[7];
|
state.name = process_object.get("name").to_string();
|
||||||
state.name = parts[11];
|
state.virtual_size = process_object.get("amount_virtual").to_dword();
|
||||||
state.virtual_size = parts[12].to_uint(ok);
|
state.physical_size = process_object.get("amount_resident").to_dword();
|
||||||
ASSERT(ok);
|
|
||||||
state.physical_size = parts[13].to_uint(ok);
|
|
||||||
ASSERT(ok);
|
|
||||||
sum_nsched += nsched;
|
sum_nsched += nsched;
|
||||||
{
|
{
|
||||||
auto it = m_processes.find(pid);
|
auto it = m_processes.find(pid);
|
||||||
|
@ -237,7 +229,7 @@ void ProcessModel::update()
|
||||||
(*it).value->current_state = state;
|
(*it).value->current_state = state;
|
||||||
|
|
||||||
live_pids.set(pid);
|
live_pids.set(pid);
|
||||||
}
|
});
|
||||||
|
|
||||||
m_pids.clear();
|
m_pids.clear();
|
||||||
float total_cpu_percent = 0;
|
float total_cpu_percent = 0;
|
||||||
|
|
|
@ -4,6 +4,9 @@
|
||||||
#include "Process.h"
|
#include "Process.h"
|
||||||
#include "Scheduler.h"
|
#include "Scheduler.h"
|
||||||
#include "StdLib.h"
|
#include "StdLib.h"
|
||||||
|
#include <AK/JsonArray.h>
|
||||||
|
#include <AK/JsonObject.h>
|
||||||
|
#include <AK/JsonValue.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <Kernel/Arch/i386/CPU.h>
|
#include <Kernel/Arch/i386/CPU.h>
|
||||||
#include <Kernel/FileSystem/Custody.h>
|
#include <Kernel/FileSystem/Custody.h>
|
||||||
|
@ -581,32 +584,33 @@ ByteBuffer procfs$all(InodeIdentifier)
|
||||||
{
|
{
|
||||||
InterruptDisabler disabler;
|
InterruptDisabler disabler;
|
||||||
auto processes = Process::all_processes();
|
auto processes = Process::all_processes();
|
||||||
|
JsonArray array;
|
||||||
StringBuilder builder(processes.size() * 80);
|
StringBuilder builder(processes.size() * 80);
|
||||||
auto build_process_line = [&builder](Process* process) {
|
auto build_process = [&](const Process& process) {
|
||||||
builder.appendf("%u,%u,%u,%u,%u,%u,%u,%s,%u,%u,%s,%s,%u,%u,%u,%u,%s,%u\n",
|
JsonObject process_object;
|
||||||
process->pid(),
|
process_object.set("pid", process.pid());
|
||||||
process->main_thread().times_scheduled(), // FIXME(Thread): Bill all scheds to the process
|
process_object.set("times_scheduled", process.main_thread().times_scheduled());
|
||||||
process->tty() ? process->tty()->pgid() : 0,
|
process_object.set("pgid", process.tty() ? process.tty()->pgid() : 0);
|
||||||
process->pgid(),
|
process_object.set("sid", process.sid());
|
||||||
process->sid(),
|
process_object.set("uid", process.uid());
|
||||||
process->uid(),
|
process_object.set("gid", process.gid());
|
||||||
process->gid(),
|
process_object.set("state", to_string(process.state()));
|
||||||
to_string(process->state()),
|
process_object.set("ppid", process.ppid());
|
||||||
process->ppid(),
|
process_object.set("nfds", process.number_of_open_file_descriptors());
|
||||||
process->number_of_open_file_descriptors(),
|
process_object.set("name", process.name());
|
||||||
process->tty() ? process->tty()->tty_name().characters() : "notty",
|
process_object.set("tty", process.tty() ? process.tty()->tty_name() : "notty");
|
||||||
process->name().characters(),
|
process_object.set("amount_virtual", process.amount_virtual());
|
||||||
process->amount_virtual(),
|
process_object.set("amount_resident", process.amount_resident());
|
||||||
process->amount_resident(),
|
process_object.set("amount_shared", process.amount_shared());
|
||||||
process->amount_shared(),
|
process_object.set("ticks", process.main_thread().ticks());
|
||||||
process->main_thread().ticks(), // FIXME(Thread): Bill all ticks to the process
|
process_object.set("priority", to_string(process.priority()));
|
||||||
to_string(process->priority()),
|
process_object.set("syscall_count", process.syscall_count());
|
||||||
process->syscall_count());
|
array.append(process_object);
|
||||||
};
|
};
|
||||||
build_process_line(Scheduler::colonel());
|
build_process(*Scheduler::colonel());
|
||||||
for (auto* process : processes)
|
for (auto* process : processes)
|
||||||
build_process_line(process);
|
build_process(*process);
|
||||||
return builder.to_byte_buffer();
|
return array.serialized().to_byte_buffer();
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuffer procfs$inodes(InodeIdentifier)
|
ByteBuffer procfs$inodes(InodeIdentifier)
|
||||||
|
|
|
@ -84,6 +84,10 @@ AK_OBJS = \
|
||||||
../AK/StringView.o \
|
../AK/StringView.o \
|
||||||
../AK/FileSystemPath.o \
|
../AK/FileSystemPath.o \
|
||||||
../AK/StdLibExtras.o \
|
../AK/StdLibExtras.o \
|
||||||
|
../AK/JsonObject.o \
|
||||||
|
../AK/JsonValue.o \
|
||||||
|
../AK/JsonArray.o \
|
||||||
|
../AK/JsonParser.o \
|
||||||
../AK/ELF/ELFImage.o \
|
../AK/ELF/ELFImage.o \
|
||||||
../AK/ELF/ELFLoader.o
|
../AK/ELF/ELFLoader.o
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
#include <AK/JsonArray.h>
|
||||||
|
#include <AK/JsonObject.h>
|
||||||
|
#include <AK/JsonValue.h>
|
||||||
#include <WindowServer/WSCPUMonitor.h>
|
#include <WindowServer/WSCPUMonitor.h>
|
||||||
#include <WindowServer/WSEventLoop.h>
|
#include <WindowServer/WSEventLoop.h>
|
||||||
#include <WindowServer/WSWindowManager.h>
|
#include <WindowServer/WSWindowManager.h>
|
||||||
|
@ -37,25 +40,17 @@ void WSCPUMonitor::get_cpu_usage(unsigned& busy, unsigned& idle)
|
||||||
idle = 0;
|
idle = 0;
|
||||||
|
|
||||||
m_proc_all.seek(0);
|
m_proc_all.seek(0);
|
||||||
for (;;) {
|
auto file_contents = m_proc_all.read_all();
|
||||||
auto line = m_proc_all.read_line(BUFSIZ);
|
auto json = JsonValue::from_string({ file_contents.data(), file_contents.size() });
|
||||||
if (line.is_null())
|
json.as_array().for_each([&](auto& value) {
|
||||||
break;
|
const JsonObject& process_object = value.as_object();
|
||||||
auto chomped = String((const char*)line.pointer(), line.size() - 1, Chomp);
|
pid_t pid = process_object.get("pid").to_dword();
|
||||||
auto parts = chomped.split_view(',');
|
unsigned nsched = process_object.get("times_scheduled").to_dword();
|
||||||
if (parts.size() < 18)
|
|
||||||
break;
|
|
||||||
bool ok;
|
|
||||||
pid_t pid = parts[0].to_uint(ok);
|
|
||||||
ASSERT(ok);
|
|
||||||
unsigned nsched = parts[1].to_uint(ok);
|
|
||||||
ASSERT(ok);
|
|
||||||
|
|
||||||
if (pid == 0)
|
if (pid == 0)
|
||||||
idle += nsched;
|
idle += nsched;
|
||||||
else
|
else
|
||||||
busy += nsched;
|
busy += nsched;
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void WSCPUMonitor::paint(Painter& painter, const Rect& rect)
|
void WSCPUMonitor::paint(Painter& painter, const Rect& rect)
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
#include <AK/AKString.h>
|
#include <AK/AKString.h>
|
||||||
#include <AK/HashMap.h>
|
#include <AK/HashMap.h>
|
||||||
|
#include <AK/JsonArray.h>
|
||||||
|
#include <AK/JsonObject.h>
|
||||||
|
#include <AK/JsonValue.h>
|
||||||
#include <AK/QuickSort.h>
|
#include <AK/QuickSort.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
|
#include <LibCore/CFile.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <pwd.h>
|
#include <pwd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
@ -31,44 +35,33 @@ struct Snapshot {
|
||||||
|
|
||||||
static Snapshot get_snapshot()
|
static Snapshot get_snapshot()
|
||||||
{
|
{
|
||||||
Snapshot snapshot;
|
CFile file("/proc/all");
|
||||||
|
if (!file.open(CIODevice::ReadOnly)) {
|
||||||
FILE* fp = fopen("/proc/all", "r");
|
fprintf(stderr, "Failed to open /proc/all: %s\n", file.error_string());
|
||||||
if (!fp) {
|
|
||||||
perror("failed to open /proc/all");
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
for (;;) {
|
|
||||||
char buf[4096];
|
Snapshot snapshot;
|
||||||
char* ptr = fgets(buf, sizeof(buf), fp);
|
|
||||||
if (!ptr)
|
auto file_contents = file.read_all();
|
||||||
break;
|
auto json = JsonValue::from_string({ file_contents.data(), file_contents.size() });
|
||||||
auto parts = String(buf, Chomp).split(',');
|
json.as_array().for_each([&](auto& value) {
|
||||||
if (parts.size() < 17)
|
const JsonObject& process_object = value.as_object();
|
||||||
break;
|
pid_t pid = process_object.get("pid").to_dword();
|
||||||
bool ok;
|
unsigned nsched = process_object.get("times_scheduled").to_dword();
|
||||||
pid_t pid = parts[0].to_uint(ok);
|
|
||||||
ASSERT(ok);
|
|
||||||
unsigned nsched = parts[1].to_uint(ok);
|
|
||||||
ASSERT(ok);
|
|
||||||
snapshot.sum_nsched += nsched;
|
snapshot.sum_nsched += nsched;
|
||||||
Process process;
|
Process process;
|
||||||
process.pid = pid;
|
process.pid = pid;
|
||||||
process.nsched = nsched;
|
process.nsched = nsched;
|
||||||
unsigned uid = parts[5].to_uint(ok);
|
unsigned uid = process_object.get("uid").to_dword();
|
||||||
ASSERT(ok);
|
|
||||||
process.user = s_usernames->get(uid);
|
process.user = s_usernames->get(uid);
|
||||||
process.priority = parts[16];
|
process.priority = process_object.get("priority").to_string();
|
||||||
process.state = parts[7];
|
process.state = process_object.get("state").to_string();
|
||||||
process.name = parts[11];
|
process.name = process_object.get("name").to_string();
|
||||||
process.virtual_size = parts[12].to_uint(ok);
|
process.virtual_size = process_object.get("amount_virtual").to_dword();
|
||||||
ASSERT(ok);
|
process.physical_size = process_object.get("amount_resident").to_dword();
|
||||||
process.physical_size = parts[13].to_uint(ok);
|
|
||||||
ASSERT(ok);
|
|
||||||
snapshot.map.set(pid, move(process));
|
snapshot.map.set(pid, move(process));
|
||||||
}
|
});
|
||||||
int rc = fclose(fp);
|
|
||||||
ASSERT(rc == 0);
|
|
||||||
return snapshot;
|
return snapshot;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue