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

SystemMonitor: Parse /proc/cpuinfo as JSON

This commit is contained in:
Linus Groh 2020-07-11 21:08:40 +01:00 committed by Andreas Kling
parent fc0ec60d82
commit 858f6dc1d1
2 changed files with 12 additions and 39 deletions

View file

@ -54,38 +54,13 @@ ProcessModel::ProcessModel()
auto file = Core::File::construct("/proc/cpuinfo"); auto file = Core::File::construct("/proc/cpuinfo");
if (file->open(Core::IODevice::ReadOnly)) { if (file->open(Core::IODevice::ReadOnly)) {
OwnPtr<CpuInfo> cpu; auto json = JsonValue::from_string({ file->read_all() });
u32 cpu_id = 0; auto cpuinfo_array = json.value().as_array();
while (file->can_read_line()) { cpuinfo_array.for_each([&](auto& value) {
auto line = file->read_line(1024); auto& cpu_object = value.as_object();
if (line.is_null()) auto cpu_id = cpu_object.get("processor").as_u32();
continue; m_cpus.append(make<CpuInfo>(cpu_id));
});
auto str = String::copy(line, Chomp);
if (str.is_empty() && cpu)
m_cpus.append(cpu.release_nonnull());
size_t i;
bool have_val = false;
for (i = 0; i < str.length(); i++) {
if (str[i] == ':') {
have_val = true;
break;
}
}
if (!have_val)
continue;
auto key = str.substring(0, i);
auto val = str.substring(i + 1, str.length() - i - 1).trim_whitespace();
if (!cpu)
cpu = make<CpuInfo>(cpu_id++);
cpu->values.set(key, val);
}
if (cpu)
m_cpus.append(cpu.release_nonnull());
} }
if (m_cpus.is_empty()) if (m_cpus.is_empty())

View file

@ -89,14 +89,12 @@ public:
virtual GUI::Variant data(const GUI::ModelIndex&, Role = Role::Display) const override; virtual GUI::Variant data(const GUI::ModelIndex&, Role = Role::Display) const override;
virtual void update() override; virtual void update() override;
struct CpuInfo struct CpuInfo {
{
u32 id; u32 id;
float total_cpu_percent{0.0}; float total_cpu_percent { 0.0 };
HashMap<String, String> values;
CpuInfo(u32 id)
CpuInfo(u32 id): : id(id)
id(id)
{ {
} }
}; };