def main(argv):
    p = Popen(argv[1:])
    
    try:
        client = ExitCodeClient()
        try:
            proc = Process(pid=p.pid)
            while p.poll() is None:
                total_cpu_percent = proc.get_cpu_percent(interval=0)
                for child_proc in proc.get_children(recursive=True):
                    total_cpu_percent += child_proc.get_cpu_percent(interval=0)
                client.send_status(total_cpu_percent)
                time.sleep(0.1) # recommended waiting period from psutil docs
        except NoSuchProcess:
            pass
    except KeyboardInterrupt:
        try:
            p.terminate()
        except OSError:
            pass
    
    client.send_status(LEDMode.Success if p.returncode == 0 else LEDMode.Error)
    time.sleep(0.5) # Give server time to read value before connection closes
    client.shutdown()
    
    return p.returncode
Exemple #2
0
 def run(self):
     self._isRunning = True
     process = Process(os.getpid())
     while self._isRunning:
         self.cpuUsage = process.get_cpu_percent(self._pollInterval)
         self.memUsage = process.get_memory_info()[0]
         if self.delegate is not None:
             self.delegate.processCpuUsage(self.cpuUsage)
             self.delegate.processMemUsage(self.memUsage)
Exemple #3
0
def update_process_list():
    global PS
    plist = []
    for pid in psutil.get_pid_list():
        try:
            p = Process(pid)
            plist.append((p,p.get_cpu_percent(),p.get_memory_percent())) 
        except psutil.NoSuchProcess:
            continue
    # copy plist to PS
    PS = sorted(plist[:], key=lambda e: e[1] if e[1] > e[2] else e[2], reverse=True)[:25]
Exemple #4
0
def update_process_list():
    global PS
    plist = []
    for pid in psutil.get_pid_list():
        try:
            p = Process(pid)
            plist.append((p, p.get_cpu_percent(), p.get_memory_percent()))
        except psutil.NoSuchProcess:
            continue
    # copy plist to PS
    PS = sorted(plist[:],
                key=lambda e: e[1] if e[1] > e[2] else e[2],
                reverse=True)[:25]
Exemple #5
0
class Tracer:
    def __init__(self, pid):
        self.process = Process(pid)
        self.gdb = Gdb(pid)
        self.io_read_bytes = 0
        self.io_write_bytes = 0
        self.about = (self.process.name, pid, datetime.now())

    def snapshot(self):
        cpu = self.process.get_cpu_percent()
        mem = self.process.get_memory_info()[0] # only RSS for now
        io = self.get_io_bytes()
        stack = self.gdb.get_stack()
        return (cpu, mem, io, stack)

    def get_io_bytes(self):
        _, _, read_bytes, write_bytes = self.process.get_io_counters()
        io = (read_bytes - self.io_read_bytes, write_bytes - self.io_write_bytes)
        self.io_read_bytes, self.io_write_bytes = read_bytes, write_bytes
        return io
Exemple #6
0
class Tracer:
    def __init__(self, pid):
        self.process = Process(pid)
        self.gdb = Gdb(pid)
        self.io_read_bytes = 0
        self.io_write_bytes = 0
        self.about = (self.process.name, pid, datetime.now())

    def snapshot(self):
        cpu = self.process.get_cpu_percent()
        mem = self.process.get_memory_info()[0]  # only RSS for now
        io = self.get_io_bytes()
        stack = self.gdb.get_stack()
        return (cpu, mem, io, stack)

    def get_io_bytes(self):
        _, _, read_bytes, write_bytes = self.process.get_io_counters()
        io = (read_bytes - self.io_read_bytes,
              write_bytes - self.io_write_bytes)
        self.io_read_bytes, self.io_write_bytes = read_bytes, write_bytes
        return io
Exemple #7
0
def get_info(process=None, interval=0):
    """Return information about a process.

    If process is None, will return the information about the current process
    """
    if process is None:
        process = Process(os.getpid())

    info = {}
    try:
        mem_info = process.get_memory_info()
        info['mem_info1'] = bytes2human(mem_info[0])
        info['mem_info2'] = bytes2human(mem_info[1])
    except AccessDenied:
        info['mem_info1'] = info['mem_info2'] = "N/A"

    try:
        info['cpu'] = process.get_cpu_percent(interval=interval)
    except AccessDenied:
        info['cpu'] = "N/A"

    try:
        info['mem'] = round(process.get_memory_percent(), 1)
    except AccessDenied:
        info['mem'] = "N/A"

    try:
        cpu_times = process.get_cpu_times()
        ctime = timedelta(seconds=sum(cpu_times))
        ctime = "%s:%s.%s" % (ctime.seconds // 60 % 60,
                        str((ctime.seconds % 60)).zfill(2),
                        str(ctime.microseconds)[:2])
    except AccessDenied:
        ctime = "N/A"

    info['ctime'] = ctime

    try:
        info['pid'] = process.pid
    except AccessDenied:
        info['pid'] = 'N/A'

    try:
        info['username'] = process.username
    except AccessDenied:
        info['username'] = '******'

    try:
        info['nice'] = process.nice
    except AccessDenied:
        info['nice'] = 'N/A'
    except NoSuchProcess:
        info['nice'] = 'Zombie'

    try:
        cmdline = os.path.basename(shlex.split(process.cmdline[0])[0])
    except (AccessDenied, IndexError):
        cmdline = "N/A"

    info['cmdline'] = cmdline

    info['children'] = []
    for child in process.get_children():
        info['children'].append(get_info(child, interval=interval))

    return info