2007-01-10 22:19:05 +01:00
|
|
|
# Copyright (C) 2007, Eduardo Silva (edsiper@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
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import gtk
|
|
|
|
import string
|
|
|
|
import gobject
|
2007-06-10 23:19:59 +02:00
|
|
|
import cairo
|
2007-03-29 03:50:25 +02:00
|
|
|
import procmem
|
|
|
|
|
2007-06-10 23:19:59 +02:00
|
|
|
from graphics.frequency import HorizontalGraphic
|
|
|
|
|
2007-01-10 22:19:05 +01:00
|
|
|
class CPU_Usage:
|
2007-06-09 17:01:35 +02:00
|
|
|
_CPU_HZ = 0
|
|
|
|
_last_jiffies = 0
|
|
|
|
_times = 0
|
|
|
|
|
2007-01-10 22:19:05 +01:00
|
|
|
def __init__(self):
|
2007-06-09 17:01:35 +02:00
|
|
|
self._CPU_HZ = os.sysconf(2)
|
|
|
|
|
2007-01-10 22:19:05 +01:00
|
|
|
def _get_CPU_data(self):
|
|
|
|
# Uptime info
|
|
|
|
stat_file = "/proc/stat"
|
2007-06-09 17:01:35 +02:00
|
|
|
|
2007-01-10 22:19:05 +01:00
|
|
|
try:
|
|
|
|
infile = file(stat_file, "r")
|
|
|
|
except:
|
|
|
|
print "Error trying uptime file"
|
|
|
|
return -1
|
2007-06-09 17:01:35 +02:00
|
|
|
|
2007-01-10 22:19:05 +01:00
|
|
|
stat_line = infile.readline()
|
|
|
|
cpu_info = string.split(stat_line, ' ')
|
|
|
|
infile.close()
|
2007-06-09 17:01:35 +02:00
|
|
|
|
2007-01-10 22:19:05 +01:00
|
|
|
return cpu_info
|
|
|
|
|
|
|
|
def _get_CPU_usage(self):
|
|
|
|
|
|
|
|
cpu_info = self._get_CPU_data()
|
|
|
|
|
|
|
|
used_jiffies = (int(cpu_info[2]) + int(cpu_info[3]) + int(cpu_info[4]))
|
2007-06-10 23:19:59 +02:00
|
|
|
|
2007-06-09 17:01:35 +02:00
|
|
|
if self._times ==0:
|
|
|
|
self._last_jiffies = used_jiffies
|
|
|
|
self._times +=1
|
|
|
|
return 0
|
|
|
|
|
|
|
|
new_ujiffies = (used_jiffies - self._last_jiffies)
|
|
|
|
new_ajiffies = ((self.frequency/1000) * self._CPU_HZ)
|
2007-01-10 22:19:05 +01:00
|
|
|
|
|
|
|
if new_ajiffies <= 0:
|
|
|
|
pcpu = 0.0
|
|
|
|
else:
|
|
|
|
pcpu = ((new_ujiffies*100)/new_ajiffies)
|
|
|
|
|
|
|
|
if pcpu >100:
|
|
|
|
pcpu = 100
|
2007-06-09 17:01:35 +02:00
|
|
|
|
|
|
|
self._times +=1
|
|
|
|
self._last_jiffies = used_jiffies
|
2007-06-12 20:32:48 +02:00
|
|
|
|
2007-01-10 22:19:05 +01:00
|
|
|
return pcpu
|
2007-06-09 17:01:35 +02:00
|
|
|
|
2007-01-10 22:19:05 +01:00
|
|
|
class XO_CPU(gtk.Frame):
|
2007-06-09 17:01:35 +02:00
|
|
|
_frequency_timer = 1
|
|
|
|
|
2007-01-10 22:19:05 +01:00
|
|
|
def __init__(self):
|
|
|
|
gtk.Frame.__init__(self, 'System CPU Usage')
|
|
|
|
self.set_border_width(10)
|
|
|
|
|
2007-06-09 17:01:35 +02:00
|
|
|
width = (gtk.gdk.screen_width() * 99 / 100) - 50
|
|
|
|
height = (gtk.gdk.screen_height() * 15 / 100) - 20
|
2007-03-10 04:39:55 +01:00
|
|
|
|
2007-06-10 23:19:59 +02:00
|
|
|
# Create graphic
|
2007-06-09 17:01:35 +02:00
|
|
|
self._graphic = HorizontalGraphic()
|
|
|
|
self._graphic.set_size_request(width, height)
|
2007-01-10 22:19:05 +01:00
|
|
|
|
2007-03-10 04:39:55 +01:00
|
|
|
fixed = gtk.Fixed()
|
2007-01-10 22:19:05 +01:00
|
|
|
fixed.set_border_width(10)
|
2007-06-09 17:01:35 +02:00
|
|
|
fixed.add(self._graphic)
|
|
|
|
|
2007-01-10 22:19:05 +01:00
|
|
|
self.add(fixed)
|
2007-06-09 17:01:35 +02:00
|
|
|
|
2007-03-29 03:50:25 +02:00
|
|
|
self._DRW_CPU = CPU_Usage()
|
2007-06-13 23:59:31 +02:00
|
|
|
self._DRW_CPU.frequency = 1000 # 1 Second
|
2007-01-10 22:19:05 +01:00
|
|
|
|
2007-03-29 03:50:25 +02:00
|
|
|
gobject.timeout_add(self._DRW_CPU.frequency, self._update_cpu_usage)
|
2007-01-10 22:19:05 +01:00
|
|
|
|
2007-03-29 03:50:25 +02:00
|
|
|
def _update_cpu_usage(self):
|
|
|
|
self._cpu = self._DRW_CPU._get_CPU_usage()
|
2007-06-09 17:01:35 +02:00
|
|
|
self.set_label('System CPU Usage: ' + str(self._cpu) + '%')
|
2007-06-12 20:32:48 +02:00
|
|
|
|
2007-06-10 23:19:59 +02:00
|
|
|
# Draw the value into the graphic
|
2007-06-09 17:01:35 +02:00
|
|
|
self._graphic.draw_value(self._cpu)
|
2007-06-12 20:32:48 +02:00
|
|
|
|
2007-01-10 22:19:05 +01:00
|
|
|
return True
|