[Sugar-devel] [PATCH] Add cpu and memory resource indicator to frame
anishmangal2002
anishmangal2002 at gmail.com
Thu Jul 1 11:03:16 EDT 2010
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 <anishmangal2002 at gmail.com>
---
extensions/deviceicon/Makefile.am | 3 +-
extensions/deviceicon/resources.py | 141 ++++++++++++++++++++++++++++++++++++
2 files changed, 143 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..e76d3eb
--- /dev/null
+++ b/extensions/deviceicon/resources.py
@@ -0,0 +1,141 @@
+# Copyright (C) Anish Mangal <anishmangal2002 at 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
+
+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))
+ self.connect('button-release-event', self.__button_release_event_cb)
+ self.connect('expose-event', self.__expose_event_cb)
+
+ def create_palette(self):
+ palette = ResourcePalette(_('System Resources'))
+ palette.set_group_id('frame')
+ return palette
+
+ def __button_release_event_cb(self, widget, event):
+ return True
+
+ def __expose_event_cb(self, *args):
+ pass
+
+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.updating = False
+ self.proc_stat_old = []
+ self.proc_stat_new = []
+
+ 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()
+ gobject.timeout_add(1000, self.__timer_cb)
+
+ def __getTimeList(self):
+ statFile = file("/proc/stat", "r")
+ timeList = statFile.readline().split(" ")[2:6]
+ statFile.close()
+ for i in range(len(timeList)) :
+ timeList[i] = int(timeList[i])
+ return timeList
+
+ def __timer_cb(self):
+ if self.updating:
+ # Computing CPU usage statistics
+ self.proc_stat_new = self.__getTimeList()
+
+ for i in range(len(self.proc_stat_new)) :
+ self.proc_stat_new[i] -= self.proc_stat_old[i]
+
+ cpu_free = (self.proc_stat_new[len(self.proc_stat_new) - 1] * 100.00 /
+ sum(self.proc_stat_new))
+
+ self._cpu_text.set_label('CPU free: %d%%' % cpu_free)
+ self._cpu_text.show()
+ self._cpu_bar.set_fraction(cpu_free/100.0)
+
+ memory_free = self.__percentage_memory_available()
+ self._memory_text.set_label('Memory free: %d%%' % memory_free)
+ self._memory_text.show()
+ self._memory_bar.set_fraction(memory_free/100.0)
+
+ self.proc_stat_old = self.__getTimeList()
+ return True
+
+ def __popup_cb(self, gobject):
+ self.updating = True
+
+ def __popdown_cb(self, gobject):
+ self.updating = False
+
+ def __percentage_memory_available(self):
+
+ for line in file('/proc/meminfo').readlines():
+ name, value, unit = line.split()[:3]
+ if 'MemTotal:' == name: total = value
+ elif 'MemFree:' == name: free = value
+ elif 'Buffers:' == name: buffers = value
+ elif 'Cached:' == name: cached = value
+ elif 'Active:' == name: break
+
+ return 100 * (int(free)+int(buffers)+int(cached)) / int(total)
+
+def setup(tray):
+ tray.add_device(DeviceView())
--
1.7.0.4
More information about the Sugar-devel
mailing list