diff options
Diffstat (limited to '')
-rw-r--r-- | TODO | 2 | ||||
-rw-r--r-- | examples/plugin-test | 34 | ||||
-rw-r--r-- | src/plugins/cpu.py | 126 |
3 files changed, 155 insertions, 7 deletions
@@ -4,7 +4,6 @@ List of plugins to implement: SMART monitoring Solar position Network usage - CPU usage Current CPU speed Xmonad Battery @@ -20,7 +19,6 @@ List of plugins to implement: Line reader Blueshift integration Backlight control - /proc/stat /sys/devices/system/cpu/cpuX hdparm Thermal monitoring diff --git a/examples/plugin-test b/examples/plugin-test index 1bbf465..cecf26f 100644 --- a/examples/plugin-test +++ b/examples/plugin-test @@ -3,6 +3,7 @@ # A simple xpybar configuration example that tests the plugins import os +import time from plugins.uptime import Uptime from plugins.loadavg import AverageLoad @@ -14,6 +15,7 @@ from plugins.mem import Memory from plugins.discstats import DiscStats from plugins.xdisplay import XDisplay from plugins.moc import MOC +from plugins.cpu import CPU OUTPUT, HEIGHT, YPOS, TOP = 0, 24, 24, True @@ -22,7 +24,7 @@ OUTPUT, HEIGHT, YPOS, TOP = 0, 24, 24, True def redraw(): bar.clear() - time = spawn_read('date', '+%Y-(%m)%b-%d %T, %a w%V, %Z') + date = spawn_read('date', '+%Y-(%m)%b-%d %T, %a w%V, %Z') uptime_ = Uptime() uptime = '%id %02i:%02i:%0.2f' % uptime_.uptime @@ -93,7 +95,7 @@ def redraw(): if value > 30: colour = '32' if value > 50: colour = '33' if value > 80: colour = '31' - return '\033[%sm%s%%\033[0m' % (colour, format % value) + return '\033[%sm%s\033[0m%%' % (colour, format % value) mem = 'Mem: ' + colourise(memory_.mem_used * 100 / memory_.mem_total) swp = 'Swp: ' + colourise(memory_.swap_used * 100 / memory_.swap_total) shm = 'Shm: ' + colourise(memory_.shmem * 100 / memory_.mem_total) @@ -122,7 +124,7 @@ def redraw(): if use > 99: colour, colour_ = '31', '31' discs.append((mp, use, colour, colour_)) discs.sort(key = lambda d : d[0]) - discs = ['\033[%sm%s:\033[%sm%.1f\033[%sm%%\033[0m' % (d[3], d[0], d[2], d[1], d[3]) for d in discs] + discs = ['\033[%sm%s:\033[%sm%.1f\033[%sm%%\033[0m' % (d[3], d[0][-4:], d[2], d[1], d[3]) for d in discs] use = discs_used * 100 / discs_total colour = '39' if use < 50: colour = '32' @@ -153,8 +155,30 @@ def redraw(): moc = {None : 'dead', MOC.STOPPED : 'stopped', MOC.PAUSED : 'paused', MOC.PLAYING : 'playing'} moc = 'Moc: %s' % moc[moc_] + cpu_ = CPU() + last_cpu_idle, last_cpus_idle = cpu_.cpu[CPU.idle], [cpu[CPU.idle] for cpu in cpu_.cpus] + last_cpu_total, last_cpus_total = sum(cpu_.cpu), [sum(cpu) for cpu in cpu_.cpus] + time.sleep(1) + cpu_ = CPU() + now_cpu_idle, now_cpus_idle = cpu_.cpu[CPU.idle], [cpu[CPU.idle] for cpu in cpu_.cpus] + now_cpu_total, now_cpus_total = sum(cpu_.cpu), [sum(cpu) for cpu in cpu_.cpus] + def cpu_usage(now_idle, now_total, last_idle, last_total): + total = now_total - last_total + idle = now_idle - last_idle + return (total - idle) * 100 / total + def colourise(value): + colour = '39' + if value >= 5: colour = '32' + if value >= 50: colour = '33' + if value >= 90: colour = '31' + return '\033[%sm%2.0f\033[0m%%' % (colour, value) + cpus = zip(now_cpus_idle, now_cpus_total, last_cpus_idle, last_cpus_total) + cpus = ' '.join([colourise(cpu_usage(*c)) for c in cpus]) + cpu = colourise(cpu_usage(now_cpu_idle, now_cpu_total, last_cpu_idle, last_cpu_total)) + cpu = 'Cpu: %s : %s' % (cpus, cpu) + - text = '%s │ %s │ %s │ %s │ %s │ %s %s │ %s │ %s │ %s │ %s\n%s │ %s' - text %= (time, uptime, idle, loadavg, users, uname, xdisplay, mem, swp, shm, moc, discs, discstats) + text = '%s │ %s │ %s │ %s │ %s │ %s %s │ %s │ %s │ %s │ %s\n%s │ %s │ %s' + text %= (date, uptime, idle, loadavg, users, uname, xdisplay, mem, swp, shm, moc, discs, discstats, cpu) bar.draw_coloured_text(0, 10, 0, 2, text) diff --git a/src/plugins/cpu.py b/src/plugins/cpu.py new file mode 100644 index 0000000..be19e30 --- /dev/null +++ b/src/plugins/cpu.py @@ -0,0 +1,126 @@ +# -*- python -*- +''' +xpybar – xmobar replacement written in python +Copyright © 2014 Mattias Andrée (maandree@member.fsf.org) + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see <http://www.gnu.org/licenses/>. +''' + + +class CPU: + ''' + Retrieve processor statistics + + @variable cpu_total:list<int> Accumulative processors statistics + @variable cpus:list<list<int>> Individual processors statistics + @variable intr:list<int> Counts of individual interrupts serviced since boot time + @variable intr_total:int Sum of `intr` + @variable ctxt:int The number of context switches that the system underwent. + @variable btime:int Boot time, in seconds since the Epoch, 1970-01-01 00:00:00 UTC + @variable processes:int Number of forks and clones (and similar) created since boot + @variable procs_running:int? Number of processes in runnable state (linux>=2.5.45) + @variable procs_blocked:int? Number of processes blocked waiting for I/O to complete (linux>=2.5.45) + @variable softirq:list<int>? Counts of individual software IRQ since boot time + @variable softirq_total:int? Sum of `softirq` + @variable fields:dict<str, list<int>> Table with all information + ''' + + + # These contants are index in the CPU statistcs, use for example `CPU_instance.cpu[UPC.user]` + + user = 0 + ''' + :int Time spent in user mode, in USER_HZ (normally 1/100ths of a second, it is a time not a frequency) + ''' + + nice = 1 + ''' + :int Time spent in user mode with low priority, in USER_HZ + ''' + + system = 2 + ''' + :int Time spent in system mode, in USER_HZ + ''' + + idle = 3 + ''' + :int Time spent in the idle task, in USER_HZ + ''' + + iowait = 4 + ''' + :int Time waiting for I/O to complete, in USER_HZ (linux>=2.5.41) + ''' + + irq = 5 + ''' + :int Time servicing interrupts, in USER_HZ (linux>=2.6.0-test4) + ''' + + softirq = 6 + ''' + :int Time servicing softirqs, in USER_HZ (linux>=2.6.0-test4) + ''' + + steal = 7 + ''' + :int Stolen time, which is the time spent in other operating systems + when running in a virtualized environment, in USER_HZ (linux>=2.6.11) + ''' + + guest = 8 + ''' + :int Time spent running a virtual CPU for guest operating systems + under the control of the Linux kernel, in USER_HZ (linux>=2.6.24) + ''' + + guest_nice = 9 + ''' + :int Time spent running a niced guest, in USER_HZ (linux>=2.6.33). + ''' + + + def __init__(self): + ''' + Constructor + ''' + stat = None + with open('/proc/stat', 'rb') as file: + stat = file.read() + stat = stat.decode('utf-8', 'replace') + stat = filter(lambda x : not x == '', stat.split('\n')) + + fields = {} + for line in stat: + line = list(filter(lambda x : not x == '', line.split(' '))) + fields[line[0]] = [int(x) for x in line[1:]] + + self.cpu = fields['cpu'] + i = 0 + self.cpus = [] + while ('cpu%i' % i) in fields: + self.cpus.append(fields['cpu%i' % i]) + i += 1 + self.ctxt = fields['ctxt'][0] + self.btime = fields['btime'][0] + self.processes = fields['processes'][0] + self.procs_running = fields['procs_running'][0] if 'procs_running' in fields else None + self.procs_blocked = fields['procs_blocked'][0] if 'procs_blocked' in fields else None + self.intr = fields['intr'][1:] + self.intr_total = fields['intr'][0] + self.softirq = fields['softirq'][1:] if 'softirq' in fields else None + self.softirq_total = fields['softirq'][0] if 'softirq' in fields else None + self.fields = fields + |