From: Michael Tremer Date: Mon, 21 Sep 2020 09:50:03 +0000 (+0000) Subject: processors: Collect usage for all individual cores X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5579c922a9e908243784461d5884ab726a999feb;p=telemetry.git processors: Collect usage for all individual cores Signed-off-by: Michael Tremer --- diff --git a/src/collecty/plugins/base.py b/src/collecty/plugins/base.py index 8f9cb78..6045a45 100644 --- a/src/collecty/plugins/base.py +++ b/src/collecty/plugins/base.py @@ -497,6 +497,22 @@ class Object(object): except ValueError: return None + def read_proc_stat(self): + """ + Reads /proc/stat and returns it as a dictionary + """ + ret = {} + + with open("/proc/stat") as f: + for line in f: + # Split the key from the rest of the line + key, line = line.split(" ", 1) + + # Remove any line breaks + ret[key] = line.rstrip() + + return ret + class GraphTemplate(object): # A unique name to identify this graph template. diff --git a/src/collecty/plugins/processor.py b/src/collecty/plugins/processor.py index 11b3aa8..4629743 100644 --- a/src/collecty/plugins/processor.py +++ b/src/collecty/plugins/processor.py @@ -19,6 +19,8 @@ # # ############################################################################### +import multiprocessing + from . import base from ..colours import * @@ -99,34 +101,38 @@ class ProcessorObject(base.Object): "DS:wait:DERIVE:0:U", "DS:irq:DERIVE:0:U", "DS:sirq:DERIVE:0:U", + "DS:steal:DERIVE:0:U", + "DS:guest:DERIVE:0:U", ] + def init(self, cpu_id=None): + self.cpu_id = cpu_id + @property def id(self): + if self.cpu_id is not None: + return "%s" % self.cpu_id + return "default" def collect(self): """ Reads the CPU usage. """ - with open("/proc/stat") as f: - for line in f: - if not line.startswith("cpu"): - continue - - columns = line.split() - if len(columns) < 8: - continue - - return ( - columns[1], # user - columns[2], # nice - columns[3], # sys - columns[4], # idle - columns[5], # wait - columns[6], # irq - columns[7], # sirq - ) + stat = self.read_proc_stat() + + if self.cpu_id is None: + values = stat.get("cpu") + else: + values = stat.get("cpu%s" % self.cpu_id) + + # Convert values into a list + values = values.split() + + if not len(values) == 10: + raise ValueError("Received unexpected output from /proc/stat: %s" % values) + + return values class ProcessorPlugin(base.Plugin): @@ -138,3 +144,7 @@ class ProcessorPlugin(base.Plugin): @property def objects(self): yield ProcessorObject(self) + + num = multiprocessing.cpu_count() + for i in range(num): + yield ProcessorObject(self, cpu_id=i)