This patch adds an icon to the frame, whose palette menu displays the memory and cpu resources. For computing free memory, the code reads the /proc/meminfo file (thanks quozl) and for computing cpu usage, the code reads the /proc/stat file.
The palette menu entries are only updated (in one second intervals) when the palette menu is visible thus possibly saving cpu cycles. Signed-off-by: anishmangal2002 <anishmangal2...@gmail.com> --- extensions/deviceicon/Makefile.am | 3 +- extensions/deviceicon/resources.py | 159 ++++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+), 1 deletions(-) create mode 100644 extensions/deviceicon/resources.py diff --git a/extensions/deviceicon/Makefile.am b/extensions/deviceicon/Makefile.am index 8a2e765..038c059 100644 --- a/extensions/deviceicon/Makefile.am +++ b/extensions/deviceicon/Makefile.am @@ -5,4 +5,5 @@ sugar_PYTHON = \ battery.py \ network.py \ speaker.py \ - volume.py + volume.py \ + resources.py diff --git a/extensions/deviceicon/resources.py b/extensions/deviceicon/resources.py new file mode 100644 index 0000000..ab3fc08 --- /dev/null +++ b/extensions/deviceicon/resources.py @@ -0,0 +1,159 @@ +# Copyright (C) Anish Mangal <anishmangal2...@gmail.com> +# +# 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 2 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, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +from gettext import gettext as _ + +import logging +import gobject +import gtk +import gconf +import os + +from sugar.graphics.tray import TrayIcon +from sugar.graphics.xocolor import XoColor +from sugar.graphics.palette import Palette +from sugar.graphics import style +from jarabe.frame.frameinvoker import FrameWidgetInvoker + +class DeviceView(TrayIcon): + + FRAME_POSITION_RELATIVE = 500 + + def __init__(self): + icon_name = 'computer' + + client = gconf.client_get_default() + color = XoColor(client.get_string('/desktop/sugar/user/color')) + TrayIcon.__init__(self, icon_name=icon_name, xo_color=color) + self.set_palette_invoker(FrameWidgetInvoker(self)) + + def create_palette(self): + palette = ResourcePalette(_('System resources')) + palette.set_group_id('frame') + return palette + +class ResourcePalette(Palette): + + def __init__(self, primary_text): + Palette.__init__(self, label=primary_text) + + self.connect('popup', self._popup_cb) + self.connect('popdown', self._popdown_cb) + + self._popped_up = False + self._cpu_times = self._get_cpu_times_list() + + vbox = gtk.VBox() + self.set_content(vbox) + + self._cpu_text = gtk.Label() + vbox.pack_start(self._cpu_text, padding=10) + self._cpu_text.show() + + self._cpu_bar = gtk.ProgressBar() + self._cpu_bar.set_size_request( + style.zoom(style.GRID_CELL_SIZE * 4), -1) + vbox.pack_start(self._cpu_bar, padding=10) + self._cpu_bar.show() + + self._memory_text = gtk.Label() + vbox.pack_start(self._memory_text, padding=10) + self._memory_text.show() + + self._memory_bar = gtk.ProgressBar() + self._memory_bar.set_size_request( + style.zoom(style.GRID_CELL_SIZE * 4), -1) + vbox.pack_start(self._memory_bar, padding=10) + self._memory_bar.show() + + vbox.show() + + def _get_cpu_times_list(self): + """Return various cpu times as read from /proc/stat + + This method returns the following cpu times as an ordered + list of numbers - [user, nice, system, idle, iowait] where, + + user: normal processes executing in user mode + nice: niced processes executing in user mode + system: processes executing in kernel mode + idle: twiddling thumbs + iowait: waiting for I/O to complete + + """ + return [int(count) + for count in file('/proc/stat').readline().split()[1:6]] + + def _percentage_cpu_available(self): + """ + Return free CPU resources as a percentage + + """ + _cpu_times_new = self._get_cpu_times_list() + _cpu_times_current = [(new - old) + for new, old in zip(_cpu_times_new, + self._cpu_times)] + user, nice, system, idle, iowait = _cpu_times_current + cpu_free = (idle + iowait) * 100.0 / sum(_cpu_times_current) + self._cpu_times = self._get_cpu_times_list() + return cpu_free + + def _percentage_memory_available(self): + """ + Return free memory as a percentage + + """ + for line in file('/proc/meminfo'): + name, value, unit = line.split()[:3] + if 'MemTotal:' == name: total = int(value) + elif 'MemFree:' == name: free = int(value) + elif 'Buffers:' == name: buffers = int(value) + elif 'Cached:' == name: cached = int(value) + elif 'Active:' == name: break + + return (free + buffers + cached) * 100.0 / total + + def __timer_cb(self): + # Get free CPU resources + cpu_free = self._percentage_cpu_available() + # Get free memory resources + memory_free = self._percentage_memory_available() + + # Update CPU and Memory labels and progressbars + self._cpu_text.set_label('CPU free: %d%%' % cpu_free) + self._cpu_text.show() + self._cpu_bar.set_fraction(cpu_free/100.0) + self._memory_text.set_label('Memory free: %d%%' % memory_free) + self._memory_text.show() + self._memory_bar.set_fraction(memory_free/100.0) + + # Keep invoking this method if we are popped up + return self._popped_up + + def _popup_cb(self, gobject_ref): + gobject.timeout_add(1000, self.__timer_cb) + self._popped_up = True + + def _popdown_cb(self, gobject_ref): + # Kill timer if we are not popped up + self._popped_up = False + + +def setup(tray): + if os.path.exists('/proc/stat') and os.path.exists('/proc/meminfo'): + tray.add_device(DeviceView()) + else: + logging.warning('CPU and Memory statistics cannot be computed') -- 1.7.0.1 _______________________________________________ Sugar-devel mailing list Sugar-devel@lists.sugarlabs.org http://lists.sugarlabs.org/listinfo/sugar-devel