def _get_time_list(self, cpu): ''' Returns a 4 element list containing the amount of time the CPU has spent performing the different types of work 0 user 1 nice 2 system 3 idle Values are in USER_HZ or Jiffies ''' if cpu.number == -1: cpu_times = gtop.cpu() else: cpu_times = gtop.cpu().cpus[cpu.number] return [cpu_times.user, cpu_times.nice, cpu_times.sys, cpu_times.idle]
def __init__(self): self.__initialised = False self.__cpu_total = CPU.Total() self.__cpu_cpus = [ CPU.CPU(i) for i in range(len(gtop.cpu().cpus)) ] self.__net_devices = {} self.__swap = Swap.Swap() self.__os, self.__name, self.__kernel = os.uname()[:3]
def measure(arg, commandline, delay, maxtime, outFile=None, errFile=None, inFile=None, logger=None, affinitymask=None): r, w = os.pipe() forkedPid = os.fork() if forkedPid: # read pickled measurements from the pipe os.close(w) rPipe = os.fdopen(r) r = cPickle.Unpickler(rPipe) measurements = r.load() rPipe.close() os.waitpid(forkedPid, 0) return measurements else: # Sample thread will be destroyed when the forked process _exits class Sample(threading.Thread): def __init__(self, program): threading.Thread.__init__(self) self.setDaemon(1) self.timedout = False self.p = program self.maxMem = 0 self.childpids = None self.start() def run(self): try: remaining = maxtime while remaining > 0: mem = gtop.proc_mem(self.p).resident time.sleep(delay) remaining -= delay # race condition - will child processes have been created yet? self.maxMem = max((mem + self.childmem()) / 1024, self.maxMem) else: self.timedout = True os.kill(self.p, signal.SIGKILL) except OSError, (e, err): if logger: logger.error('%s %s', e, err) def childmem(self): if self.childpids == None: self.childpids = set() for each in gtop.proclist(): if gtop.proc_uid(each).ppid == self.p: self.childpids.add(each) mem = 0 for each in self.childpids: mem += gtop.proc_mem(each).resident return mem try: m = Record(arg) # only write pickles to the pipe os.close(r) wPipe = os.fdopen(w, 'w') w = cPickle.Pickler(wPipe) # gtop cpu is since machine boot, so we need a before measurement cpus0 = gtop.cpu().cpus start = time.time() # spawn the program in a separate process p = Popen(commandline, stdout=outFile, stderr=errFile, stdin=inFile) # start a thread to sample the program's resident memory use t = Sample(program=p.pid) # wait for program exit status and resource usage rusage = os.wait3(0) # gtop cpu is since machine boot, so we need an after measurement elapsed = time.time() - start cpus1 = gtop.cpu().cpus # summarize measurements if t.timedout: m.setTimedout() elif rusage[1] == os.EX_OK: m.setOkay() else: m.setError() m.userSysTime = rusage[2][0] + rusage[2][1] m.maxMem = t.maxMem load = map( lambda t0, t1: int( round(100.0 * (1.0 - float(t1.idle - t0.idle) / (t1.total - t0.total)))), cpus0, cpus1) #load.sort(reverse=1) # maybe more obvious unsorted m.cpuLoad = ("% ".join([str(i) for i in load])) + "%" m.elapsed = elapsed except KeyboardInterrupt: os.kill(p.pid, signal.SIGKILL) except ZeroDivisionError, (e, err): if logger: logger.warn('%s %s', err, 'too fast to measure?')
def measure(arg,commandline,delay,maxtime, outFile=None,errFile=None,inFile=None,logger=None,affinitymask=None): r,w = os.pipe() forkedPid = os.fork() if forkedPid: # read pickled measurements from the pipe os.close(w); rPipe = os.fdopen(r); r = cPickle.Unpickler(rPipe) measurements = r.load() rPipe.close() os.waitpid(forkedPid,0) return measurements else: # Sample thread will be destroyed when the forked process _exits class Sample(threading.Thread): def __init__(self,program): threading.Thread.__init__(self) self.setDaemon(1) self.timedout = False self.p = program self.maxMem = 0 self.childpids = None self.start() def run(self): try: remaining = maxtime while remaining > 0: mem = gtop.proc_mem(self.p).resident time.sleep(delay) remaining -= delay # race condition - will child processes have been created yet? self.maxMem = max((mem + self.childmem())/1024, self.maxMem) else: self.timedout = True os.kill(self.p, signal.SIGKILL) except OSError, (e,err): if logger: logger.error('%s %s',e,err) def childmem(self): if self.childpids == None: self.childpids = set() for each in gtop.proclist(): if gtop.proc_uid(each).ppid == self.p: self.childpids.add(each) mem = 0 for each in self.childpids: mem += gtop.proc_mem(each).resident return mem try: def setAffinity(): if affinitymask: set_process_affinity_mask(os.getpid(),affinitymask) m = Record(arg) # only write pickles to the pipe os.close(r); wPipe = os.fdopen(w, 'w'); w = cPickle.Pickler(wPipe) # gtop cpu is since machine boot, so we need a before measurement cpus0 = gtop.cpu().cpus start = time.time() # spawn the program in a separate process p = Popen(commandline,stdout=outFile,stderr=errFile,stdin=inFile,preexec_fn=setAffinity) # start a thread to sample the program's resident memory use t = Sample( program = p.pid ) # wait for program exit status and resource usage rusage = os.wait3(0) # gtop cpu is since machine boot, so we need an after measurement elapsed = time.time() - start cpus1 = gtop.cpu().cpus # summarize measurements if t.timedout: m.setTimedout() elif rusage[1] == os.EX_OK: m.setOkay() else: m.setError() m.userSysTime = rusage[2][0] + rusage[2][1] m.maxMem = t.maxMem load = map( lambda t0,t1: int(round( 100.0 * (1.0 - float(t1.idle-t0.idle)/(t1.total-t0.total)) )) ,cpus0 ,cpus1 ) #load.sort(reverse=1) # maybe more obvious unsorted m.cpuLoad = ("% ".join([str(i) for i in load]))+"%" m.elapsed = elapsed except KeyboardInterrupt: os.kill(p.pid, signal.SIGKILL) except ZeroDivisionError, (e,err): if logger: logger.warn('%s %s',err,'too fast to measure?')
def activate(self): self._net_icon = g15icontools.get_icon_path([ "network-transmit-receive", "gnome-fs-network", "network-server" ], self.screen.height) self._cpu_icon = g15icontools.get_icon_path( CPU_ICONS, self.screen.height) self._mem_icon = g15icontools.get_icon_path( [ "media-memory", "media-flash" ], self.screen.height) self._thumb_icon = g15cairo.load_surface_from_file(self._cpu_icon) self.variant = 0 self.graphs = {} self.last_time_list = None self.last_times_list = [] self.last_time = 0 # CPU self.selected_cpu = None self.cpu_no = 0 self.cpu_data = [] selected_cpu_name = self.gconf_client.get_string(self.gconf_key + "/cpu") cpus = gtop.cpu().cpus for i in range(-1, len(cpus)): cpu = CPU(i) self.cpu_data.append(cpu) if cpu.name == selected_cpu_name: self.selected_cpu = cpu if self.selected_cpu is None: self.selected_cpu = self.cpu_data[0] # Net self.selected_net = None _, self.net_list = self._get_net_stats() net_name = self.gconf_client.get_string(self.gconf_key + "/net") self.net_data = [] for idx, n in enumerate(self.net_list): net = Net(idx, n) self.net_data.append(net) if net.name == net_name: self.selected_net = net if self.selected_net is None and len(self.net_data) > 0: self.selected_net = self.net_data[0] # Memory self.max_total_mem = 0 self.total = 1.0 self.cached = 0 self.free = 0 self.used = 0 self.cached_history = [0] * GRAPH_SIZE self.used_history = [0] * GRAPH_SIZE g15plugin.G15RefreshingPlugin.activate(self) self._set_panel() self.watch(["show_cpu_on_panel","theme"], self._config_changed) self.screen.key_handler.action_listeners.append(self) # Start refreshing self.do_refresh()
def _get_cpu(self): return gtop.cpu() def poll(self):
def _get_cpu_info(self): return gtop.cpu().dict()
def activate(self): self._net_icon = g15icontools.get_icon_path( ["network-transmit-receive", "gnome-fs-network", "network-server"], self.screen.height) self._cpu_icon = g15icontools.get_icon_path(CPU_ICONS, self.screen.height) self._mem_icon = g15icontools.get_icon_path( ["media-memory", "media-flash"], self.screen.height) self._thumb_icon = g15cairo.load_surface_from_file(self._cpu_icon) self.variant = 0 self.graphs = {} self.last_time_list = None self.last_times_list = [] self.last_time = 0 # CPU self.selected_cpu = None self.cpu_no = 0 self.cpu_data = [] selected_cpu_name = self.gconf_client.get_string(self.gconf_key + "/cpu") cpus = gtop.cpu().cpus for i in range(-1, len(cpus)): cpu = CPU(i) self.cpu_data.append(cpu) if cpu.name == selected_cpu_name: self.selected_cpu = cpu if self.selected_cpu is None: self.selected_cpu = self.cpu_data[0] # Net self.selected_net = None _, self.net_list = self._get_net_stats() net_name = self.gconf_client.get_string(self.gconf_key + "/net") self.net_data = [] for idx, n in enumerate(self.net_list): net = Net(idx, n) self.net_data.append(net) if net.name == net_name: self.selected_net = net if self.selected_net is None and len(self.net_data) > 0: self.selected_net = self.net_data[0] # Memory self.max_total_mem = 0 self.total = 1.0 self.cached = 0 self.free = 0 self.used = 0 self.cached_history = [0] * GRAPH_SIZE self.used_history = [0] * GRAPH_SIZE g15plugin.G15RefreshingPlugin.activate(self) self._set_panel() self.watch(["show_cpu_on_panel", "theme"], self._config_changed) self.screen.key_handler.action_listeners.append(self) # Start refreshing self.do_refresh()