From d0885b4fbccc408b6f777d1afea9ec9d937cce71 Mon Sep 17 00:00:00 2001 From: Arvedui Date: Sat, 1 Feb 2014 21:13:56 +0100 Subject: [PATCH] added module cpu_usage --- i3pystatus/cpu_usage.py | 65 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 i3pystatus/cpu_usage.py diff --git a/i3pystatus/cpu_usage.py b/i3pystatus/cpu_usage.py new file mode 100644 index 0000000..3d6a9a2 --- /dev/null +++ b/i3pystatus/cpu_usage.py @@ -0,0 +1,65 @@ +# -*- coding:utf-8 -*- + +from i3pystatus import IntervalModule + + +class CpuUsage(IntervalModule): + """ + Shows CPU usage. + The first output will be inacurate + Linux only + + Available formatters: + + * {usage} + + """ + + + format = "{usage:02}%" + settings = ( + ("format", "format string. Default: '{usage:02}%'") + ) + + + def __init__(self): + IntervalModule.__init__(self) + self.prev_idle = 0 + self.prev_busy = 0 + self.interval = 1 + + + def get_usage(self): + """ + parses /proc/stat and calcualtes total and busy time + (more specific USER_HZ see man 5 proc for further informations ) + """ + with open('/proc/stat', 'r') as file_obj: + stats = file_obj.readline().strip().split() + + cpu_timings = [int(x) for x in stats[1:]] + cpu_total = sum(cpu_timings) + del cpu_timings[3:5] + cpu_busy = sum(cpu_timings) + + return cpu_total, cpu_busy + + + def run(self): + cpu_total, cpu_busy = self.get_usage() + + diff_cpu_total = cpu_total - self.prev_idle + diff_cpu_busy = cpu_busy - self.prev_busy + + self.prev_idle = cpu_total + self.prev_busy = cpu_busy + + cpu_busy_percentage = int(diff_cpu_busy / diff_cpu_total * 100) + + self.output = { + "full_text": self.format.format( + usage=cpu_busy_percentage + ) + } + +