def top(num_processes=5, interval=3): ''' Return a list of top CPU consuming processes during the interval. num_processes = return the top N CPU consuming processes interval = the number of seconds to sample CPU usage over CLI Examples: .. code-block:: bash salt '*' ps.top salt '*' ps.top 5 10 ''' result = [] start_usage = {} for pid in psutil.pids(): try: process = psutil.Process(pid) user, system = process.cpu_times() except ValueError: user, system, _, _ = process.cpu_times() except psutil.NoSuchProcess: continue start_usage[process] = user + system time.sleep(interval) usage = set() for process, start in six.iteritems(start_usage): try: user, system = process.cpu_times() except ValueError: user, system, _, _ = process.cpu_times() except psutil.NoSuchProcess: continue now = user + system diff = now - start usage.add((diff, process)) for idx, (diff, process) in enumerate(reversed(sorted(usage))): if num_processes and idx >= num_processes: break if len(_get_proc_cmdline(process)) == 0: cmdline = _get_proc_name(process) else: cmdline = _get_proc_cmdline(process) info = {'cmd': cmdline, 'user': _get_proc_username(process), 'status': _get_proc_status(process), 'pid': _get_proc_pid(process), 'create_time': _get_proc_create_time(process), 'cpu': {}, 'mem': {}, } for key, value in six.iteritems(process.cpu_times()._asdict()): info['cpu'][key] = value for key, value in six.iteritems(process.memory_info()._asdict()): info['mem'][key] = value result.append(info) return result
def get_pid_list(): ''' Return a list of process ids (PIDs) for all running processes. CLI Example: .. code-block:: bash salt '*' ps.get_pid_list ''' return psutil.pids()
def test_top_zombie_process(self): # Get 3 pids that are currently running on the system pids = psutil.pids()[:3] # Get a process instance for each of the pids processes = [psutil.Process(pid) for pid in pids] # Patch the middle process to raise ZombieProcess when .cpu_times is called def raise_exception(): raise psutil.ZombieProcess(processes[1].pid) processes[1].cpu_times = raise_exception # Make sure psutil.pids only returns the above 3 pids with patch("salt.utils.psutil_compat.pids", return_value=pids): # Make sure we use our process list from above with patch("salt.utils.psutil_compat.Process", side_effect=processes): result = ps.top(num_processes=1, interval=0) assert len(result) == 1
def top(num_processes=5, interval=3): """ Return a list of top CPU consuming processes during the interval. num_processes = return the top N CPU consuming processes interval = the number of seconds to sample CPU usage over CLI Examples: .. code-block:: bash salt '*' ps.top salt '*' ps.top 5 10 """ result = [] start_usage = {} for pid in psutil.pids(): try: process = psutil.Process(pid) except psutil.NoSuchProcess: continue else: try: user, system = process.cpu_times()[:2] except psutil.ZombieProcess: user = system = 0.0 start_usage[process] = user + system time.sleep(interval) usage = set() for process, start in six.iteritems(start_usage): try: user, system = process.cpu_times()[:2] except psutil.NoSuchProcess: continue now = user + system diff = now - start usage.add((diff, process)) for diff, process in sorted(usage, key=lambda x: x[0], reverse=True): info = { "cmd": _get_proc_cmdline(process) or _get_proc_name(process), "user": _get_proc_username(process), "status": _get_proc_status(process), "pid": _get_proc_pid(process), "create_time": _get_proc_create_time(process), "cpu": {}, "mem": {}, } try: for key, value in six.iteritems(process.cpu_times()._asdict()): info["cpu"][key] = value for key, value in six.iteritems(process.memory_info()._asdict()): info["mem"][key] = value except psutil.NoSuchProcess: # Process ended since psutil.pids() was run earlier in this # function. Ignore this process and do not include this process in # the return data. continue result.append(info) # Stop gathering process info since we've reached the desired number if len(result) >= num_processes: break return result