OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # |
| 3 # $Id: _psosx.py 794 2010-11-12 13:29:52Z g.rodola $ |
| 4 # |
| 5 |
| 6 import errno |
| 7 import os |
| 8 |
| 9 try: |
| 10 from collections import namedtuple |
| 11 except ImportError: |
| 12 from psutil.compat import namedtuple # python < 2.6 |
| 13 |
| 14 import _psutil_osx |
| 15 import _psposix |
| 16 from psutil.error import AccessDenied, NoSuchProcess |
| 17 |
| 18 # --- constants |
| 19 |
| 20 NUM_CPUS = _psutil_osx.get_num_cpus() |
| 21 TOTAL_PHYMEM = _psutil_osx.get_total_phymem() |
| 22 |
| 23 # --- functions |
| 24 |
| 25 def avail_phymem(): |
| 26 "Return the amount of physical memory available on the system, in bytes." |
| 27 return _psutil_osx.get_avail_phymem() |
| 28 |
| 29 def used_phymem(): |
| 30 "Return the amount of physical memory currently in use on the system, in byt
es." |
| 31 return TOTAL_PHYMEM - _psutil_osx.get_avail_phymem() |
| 32 |
| 33 def total_virtmem(): |
| 34 "Return the amount of total virtual memory available on the system, in bytes
." |
| 35 return _psutil_osx.get_total_virtmem() |
| 36 |
| 37 def avail_virtmem(): |
| 38 "Return the amount of virtual memory currently in use on the system, in byte
s." |
| 39 return _psutil_osx.get_avail_virtmem() |
| 40 |
| 41 def used_virtmem(): |
| 42 """Return the amount of used memory currently in use on the system, in bytes
.""" |
| 43 return _psutil_osx.get_total_virtmem() - _psutil_osx.get_avail_virtmem() |
| 44 |
| 45 def get_system_cpu_times(): |
| 46 """Return a dict representing the following CPU times: |
| 47 user, nice, system, idle.""" |
| 48 values = _psutil_osx.get_system_cpu_times() |
| 49 return dict(user=values[0], nice=values[1], system=values[2], idle=values[3]
) |
| 50 |
| 51 def get_pid_list(): |
| 52 """Returns a list of PIDs currently running on the system.""" |
| 53 return _psutil_osx.get_pid_list() |
| 54 |
| 55 def pid_exists(pid): |
| 56 """Check For the existence of a unix pid.""" |
| 57 return _psposix.pid_exists(pid) |
| 58 |
| 59 # --- decorator |
| 60 |
| 61 def wrap_exceptions(callable): |
| 62 """Call callable into a try/except clause so that if an |
| 63 OSError EPERM exception is raised we translate it into |
| 64 psutil.AccessDenied. |
| 65 """ |
| 66 def wrapper(self, *args, **kwargs): |
| 67 try: |
| 68 return callable(self, *args, **kwargs) |
| 69 except OSError, err: |
| 70 if err.errno == errno.ESRCH: |
| 71 raise NoSuchProcess(self.pid, self._process_name) |
| 72 if err.errno in (errno.EPERM, errno.EACCES): |
| 73 raise AccessDenied(self.pid, self._process_name) |
| 74 raise |
| 75 return wrapper |
| 76 |
| 77 |
| 78 class OSXProcess(object): |
| 79 """Wrapper class around underlying C implementation.""" |
| 80 |
| 81 _meminfo_ntuple = namedtuple('meminfo', 'rss vms') |
| 82 _cputimes_ntuple = namedtuple('cputimes', 'user system') |
| 83 __slots__ = ["pid", "_process_name"] |
| 84 |
| 85 def __init__(self, pid): |
| 86 self.pid = pid |
| 87 self._process_name = None |
| 88 |
| 89 @wrap_exceptions |
| 90 def get_process_name(self): |
| 91 """Return process name as a string of limited len (15).""" |
| 92 return _psutil_osx.get_process_name(self.pid) |
| 93 |
| 94 def get_process_exe(self): |
| 95 # no such thing as "exe" on OS X; it will maybe be determined |
| 96 # later from cmdline[0] |
| 97 if not pid_exists(self.pid): |
| 98 raise NoSuchProcess(self.pid, self._process_name) |
| 99 return "" |
| 100 |
| 101 @wrap_exceptions |
| 102 def get_process_cmdline(self): |
| 103 """Return process cmdline as a list of arguments.""" |
| 104 if not pid_exists(self.pid): |
| 105 raise NoSuchProcess(self.pid, self._process_name) |
| 106 return _psutil_osx.get_process_cmdline(self.pid) |
| 107 |
| 108 @wrap_exceptions |
| 109 def get_process_ppid(self): |
| 110 """Return process parent pid.""" |
| 111 return _psutil_osx.get_process_ppid(self.pid) |
| 112 |
| 113 @wrap_exceptions |
| 114 def get_process_uid(self): |
| 115 """Return process real user id.""" |
| 116 return _psutil_osx.get_process_uid(self.pid) |
| 117 |
| 118 @wrap_exceptions |
| 119 def get_process_gid(self): |
| 120 """Return process real group id.""" |
| 121 return _psutil_osx.get_process_gid(self.pid) |
| 122 |
| 123 @wrap_exceptions |
| 124 def get_memory_info(self): |
| 125 """Return a tuple with the process' RSS and VMS size.""" |
| 126 rss, vms = _psutil_osx.get_memory_info(self.pid) |
| 127 return self._meminfo_ntuple(rss, vms) |
| 128 |
| 129 @wrap_exceptions |
| 130 def get_cpu_times(self): |
| 131 user, system = _psutil_osx.get_cpu_times(self.pid) |
| 132 return self._cputimes_ntuple(user, system) |
| 133 |
| 134 @wrap_exceptions |
| 135 def get_process_create_time(self): |
| 136 """Return the start time of the process as a number of seconds since |
| 137 the epoch.""" |
| 138 return _psutil_osx.get_process_create_time(self.pid) |
| 139 |
| 140 @wrap_exceptions |
| 141 def get_process_num_threads(self): |
| 142 """Return the number of threads belonging to the process.""" |
| 143 return _psutil_osx.get_process_num_threads(self.pid) |
| 144 |
| 145 def get_open_files(self): |
| 146 """Return files opened by process by parsing lsof output.""" |
| 147 lsof = _psposix.LsofParser(self.pid, self._process_name) |
| 148 return lsof.get_process_open_files() |
| 149 |
| 150 def get_connections(self): |
| 151 """Return etwork connections opened by a process as a list of |
| 152 namedtuples.""" |
| 153 lsof = _psposix.LsofParser(self.pid, self._process_name) |
| 154 return lsof.get_process_connections() |
| 155 |
| 156 PlatformProcess = OSXProcess |
| 157 |
OLD | NEW |