From 858f6dc1d1b7026a857567f59bf4a7d315b31cbf Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sat, 11 Jul 2020 21:08:40 +0100 Subject: [PATCH] SystemMonitor: Parse /proc/cpuinfo as JSON --- Applications/SystemMonitor/ProcessModel.cpp | 39 ++++----------------- Applications/SystemMonitor/ProcessModel.h | 12 +++---- 2 files changed, 12 insertions(+), 39 deletions(-) diff --git a/Applications/SystemMonitor/ProcessModel.cpp b/Applications/SystemMonitor/ProcessModel.cpp index 47a0418216..5fe3353781 100644 --- a/Applications/SystemMonitor/ProcessModel.cpp +++ b/Applications/SystemMonitor/ProcessModel.cpp @@ -54,38 +54,13 @@ ProcessModel::ProcessModel() auto file = Core::File::construct("/proc/cpuinfo"); if (file->open(Core::IODevice::ReadOnly)) { - OwnPtr cpu; - u32 cpu_id = 0; - while (file->can_read_line()) { - auto line = file->read_line(1024); - if (line.is_null()) - continue; - - 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(cpu_id++); - - cpu->values.set(key, val); - } - - if (cpu) - m_cpus.append(cpu.release_nonnull()); + auto json = JsonValue::from_string({ file->read_all() }); + auto cpuinfo_array = json.value().as_array(); + cpuinfo_array.for_each([&](auto& value) { + auto& cpu_object = value.as_object(); + auto cpu_id = cpu_object.get("processor").as_u32(); + m_cpus.append(make(cpu_id)); + }); } if (m_cpus.is_empty()) diff --git a/Applications/SystemMonitor/ProcessModel.h b/Applications/SystemMonitor/ProcessModel.h index a30ab4ff29..e6f254d9a4 100644 --- a/Applications/SystemMonitor/ProcessModel.h +++ b/Applications/SystemMonitor/ProcessModel.h @@ -89,14 +89,12 @@ public: virtual GUI::Variant data(const GUI::ModelIndex&, Role = Role::Display) const override; virtual void update() override; - struct CpuInfo - { + struct CpuInfo { u32 id; - float total_cpu_percent{0.0}; - HashMap values; - - CpuInfo(u32 id): - id(id) + float total_cpu_percent { 0.0 }; + + CpuInfo(u32 id) + : id(id) { } };