Bläddra i källkod

WindowServer: Use CProcessStatisticsReader in WSCPUMonitor.

Andreas Kling 6 år sedan
förälder
incheckning
64d9b43734
2 ändrade filer med 8 tillägg och 17 borttagningar
  1. 8 15
      Servers/WindowServer/WSCPUMonitor.cpp
  2. 0 2
      Servers/WindowServer/WSCPUMonitor.h

+ 8 - 15
Servers/WindowServer/WSCPUMonitor.cpp

@@ -1,6 +1,7 @@
 #include <AK/JsonArray.h>
 #include <AK/JsonArray.h>
 #include <AK/JsonObject.h>
 #include <AK/JsonObject.h>
 #include <AK/JsonValue.h>
 #include <AK/JsonValue.h>
+#include <LibCore/CProcessStatisticsReader.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>
@@ -8,11 +9,7 @@
 #include <unistd.h>
 #include <unistd.h>
 
 
 WSCPUMonitor::WSCPUMonitor()
 WSCPUMonitor::WSCPUMonitor()
-    : m_proc_all("/proc/all")
 {
 {
-    if (!m_proc_all.open(CIODevice::OpenMode::ReadOnly))
-        ASSERT_NOT_REACHED();
-
     create_thread([](void* context) -> int {
     create_thread([](void* context) -> int {
         auto& monitor = *(WSCPUMonitor*)context;
         auto& monitor = *(WSCPUMonitor*)context;
         for (;;) {
         for (;;) {
@@ -39,18 +36,14 @@ void WSCPUMonitor::get_cpu_usage(unsigned& busy, unsigned& idle)
     busy = 0;
     busy = 0;
     idle = 0;
     idle = 0;
 
 
-    m_proc_all.seek(0);
-    auto file_contents = m_proc_all.read_all();
-    auto json = JsonValue::from_string({ file_contents.data(), file_contents.size() });
-    json.as_array().for_each([&](auto& value) {
-        const JsonObject& process_object = value.as_object();
-        pid_t pid = process_object.get("pid").to_u32();
-        unsigned nsched = process_object.get("times_scheduled").to_u32();
-        if (pid == 0)
-            idle += nsched;
+    auto all_processes = CProcessStatisticsReader::get_all();
+
+    for (auto& it : all_processes) {
+        if (it.value.pid == 0)
+            idle += it.value.nsched;
         else
         else
-            busy += nsched;
-    });
+            busy += it.value.nsched;
+    }
 }
 }
 
 
 void WSCPUMonitor::paint(Painter& painter, const Rect& rect)
 void WSCPUMonitor::paint(Painter& painter, const Rect& rect)

+ 0 - 2
Servers/WindowServer/WSCPUMonitor.h

@@ -1,7 +1,6 @@
 #pragma once
 #pragma once
 
 
 #include <AK/CircularQueue.h>
 #include <AK/CircularQueue.h>
-#include <LibCore/CFile.h>
 #include <stdio.h>
 #include <stdio.h>
 
 
 class Painter;
 class Painter;
@@ -20,6 +19,5 @@ private:
     void get_cpu_usage(unsigned& busy, unsigned& idle);
     void get_cpu_usage(unsigned& busy, unsigned& idle);
 
 
     CircularQueue<float, 30> m_cpu_history;
     CircularQueue<float, 30> m_cpu_history;
-    CFile m_proc_all;
     bool m_dirty { false };
     bool m_dirty { false };
 };
 };