From 943367f969ac3a49cae2da3186500a68cc3ea6ae Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Thu, 28 Sep 2017 01:00:01 +0800 Subject: 1129: have sensors_temperatures() on Linux skip entry on IOError --- psutil/_pslinux.py | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'psutil/_pslinux.py') diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index e9ee7df0..c4abacc7 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -1142,21 +1142,22 @@ def sensors_temperatures(): basenames = sorted(set([x.split('_')[0] for x in basenames])) for base in basenames: + try: + current = float(cat(base + '_input')) / 1000.0 + except (IOError, OSError) as err: + # A lot of things can go wrong here, so let's just skip the + # whole entry. + # https://github.com/giampaolo/psutil/issues/1009 + # https://github.com/giampaolo/psutil/issues/1101 + # https://github.com/giampaolo/psutil/issues/1129 + warnings.warn("ignoring %r" % err, RuntimeWarning) + continue + unit_name = cat(os.path.join(os.path.dirname(base), 'name'), binary=False) high = cat(base + '_max', fallback=None) critical = cat(base + '_crit', fallback=None) label = cat(base + '_label', fallback='', binary=False) - try: - current = float(cat(base + '_input')) / 1000.0 - except OSError as err: - # https://github.com/giampaolo/psutil/issues/1009 - # https://github.com/giampaolo/psutil/issues/1101 - if err.errno in (errno.EIO, errno.ENODEV): - warnings.warn("ignoring %r" % err, RuntimeWarning) - continue - else: - raise if high is not None: high = float(high) / 1000.0 -- cgit v1.2.1 From d9247ed08bab0c6795492fb2cb847f6e9e6572f7 Mon Sep 17 00:00:00 2001 From: Sebastian Saip Date: Thu, 28 Sep 2017 16:29:58 +0200 Subject: 1129: have sensors_fans() on Linux skip entry on IOError (#1141) --- psutil/_pslinux.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'psutil/_pslinux.py') diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index c4abacc7..154a8d63 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -1191,7 +1191,11 @@ def sensors_fans(): unit_name = cat(os.path.join(os.path.dirname(base), 'name'), binary=False) label = cat(base + '_label', fallback='', binary=False) - current = int(cat(base + '_input')) + try: + current = int(cat(base + '_input')) + except (IOError, OSError) as err: + warnings.warn("ignoring %r" % err, RuntimeWarning) + continue ret[unit_name].append(_common.sfan(label, current)) -- cgit v1.2.1 From 9632fb1969c526129eec63a267a6bcf91010228d Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Thu, 28 Sep 2017 22:40:19 +0800 Subject: #1141: sensors_fans / linux: skip unreadable _input label for first --- psutil/_pslinux.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'psutil/_pslinux.py') diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 154a8d63..2fec8ea7 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -1170,8 +1170,8 @@ def sensors_temperatures(): def sensors_fans(): - """Return hardware (CPU and others) fans as a dict - including hardware label, current speed. + """Return hardware fans info (for CPU and other peripherals) as a + dict including hardware label and current speed. Implementation notes: - /sys/class/hwmon looks like the most recent interface to @@ -1188,15 +1188,14 @@ def sensors_fans(): basenames = sorted(set([x.split('_')[0] for x in basenames])) for base in basenames: - unit_name = cat(os.path.join(os.path.dirname(base), 'name'), - binary=False) - label = cat(base + '_label', fallback='', binary=False) try: current = int(cat(base + '_input')) except (IOError, OSError) as err: warnings.warn("ignoring %r" % err, RuntimeWarning) continue - + unit_name = cat(os.path.join(os.path.dirname(base), 'name'), + binary=False) + label = cat(base + '_label', fallback='', binary=False) ret[unit_name].append(_common.sfan(label, current)) return dict(ret) -- cgit v1.2.1 From fe68b30dacec3255b023fcefac5c9095d96a692f Mon Sep 17 00:00:00 2001 From: wiggin15 Date: Mon, 13 Nov 2017 00:38:12 +0200 Subject: Move exceptions to separate file (#1174) --- psutil/_pslinux.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'psutil/_pslinux.py') diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 2fec8ea7..228dbd98 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -41,6 +41,10 @@ from ._compat import b from ._compat import basestring from ._compat import long from ._compat import PY3 +from ._exceptions import NoSuchProcess +from ._exceptions import ZombieProcess +from ._exceptions import AccessDenied +from ._exceptions import TimeoutExpired if sys.version_info >= (3, 4): import enum @@ -137,12 +141,6 @@ TCP_STATUSES = { "0B": _common.CONN_CLOSING } -# these get overwritten on "import psutil" from the __init__.py file -NoSuchProcess = None -ZombieProcess = None -AccessDenied = None -TimeoutExpired = None - # ===================================================================== # --- named tuples -- cgit v1.2.1 From 100391f880ef2a2c5b124bba4b0722623f3edb3e Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Sun, 12 Nov 2017 23:41:30 +0100 Subject: sort imports by name --- psutil/_pslinux.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'psutil/_pslinux.py') diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 228dbd98..fc38fb14 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -41,10 +41,10 @@ from ._compat import b from ._compat import basestring from ._compat import long from ._compat import PY3 -from ._exceptions import NoSuchProcess -from ._exceptions import ZombieProcess from ._exceptions import AccessDenied +from ._exceptions import NoSuchProcess from ._exceptions import TimeoutExpired +from ._exceptions import ZombieProcess if sys.version_info >= (3, 4): import enum -- cgit v1.2.1 From 40573cbe58407a3f8dfcb0c3b71237444b10fc0a Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Mon, 13 Nov 2017 00:00:30 +0100 Subject: #1174: use TimeoutExpired in wait_pid() --- psutil/_pslinux.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) (limited to 'psutil/_pslinux.py') diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index fc38fb14..3fe62c5c 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -43,7 +43,6 @@ from ._compat import long from ._compat import PY3 from ._exceptions import AccessDenied from ._exceptions import NoSuchProcess -from ._exceptions import TimeoutExpired from ._exceptions import ZombieProcess if sys.version_info >= (3, 4): @@ -1534,10 +1533,7 @@ class Process(object): @wrap_exceptions def wait(self, timeout=None): - try: - return _psposix.wait_pid(self.pid, timeout) - except _psposix.TimeoutExpired: - raise TimeoutExpired(timeout, self.pid, self._name) + return _psposix.wait_pid(self.pid, timeout, self._name) @wrap_exceptions def create_time(self): -- cgit v1.2.1 From 7c6b6c2a6d89a1a270d6357be3b77fd8e0e2cbe7 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Tue, 28 Nov 2017 11:10:42 +0100 Subject: fix #1179 / linux / cmdline: handle processes erroneously overwriting /proc/pid/cmdline by using spaces instead of null bytes as args separator --- psutil/_pslinux.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'psutil/_pslinux.py') diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 3fe62c5c..a5a3fd89 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -1471,9 +1471,17 @@ class Process(object): if not data: # may happen in case of zombie process return [] - if data.endswith('\x00'): + # 'man proc' states that args are separated by null bytes '\0' + # and last char is supposed to be a null byte. Nevertheless + # some processes may change their cmdline after being started + # (via setproctitle() or similar), they are usually not + # compliant with this rule and use spaces instead. Google + # Chrome process is an example. See: + # https://github.com/giampaolo/psutil/issues/1179 + sep = '\x00' if data.endswith('\x00') else ' ' + if data.endswith(sep): data = data[:-1] - return [x for x in data.split('\x00')] + return [x for x in data.split(sep)] @wrap_exceptions def environ(self): -- cgit v1.2.1 From f0094db79ad9e4f2246997cb8c2046b71c465a29 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Fri, 1 Dec 2017 14:49:53 +0100 Subject: Speedup Process.children() (#1185) * update HISTORY * update doc * #1183: speedup Process.children() by 2.2x * fix windows err * #1083 / #1084: implement linux-specific ppid_map() function speending things up from 2x to 2.4x * add ESRCH to err handling * update doc --- psutil/_pslinux.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'psutil/_pslinux.py') diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index a5a3fd89..b57adb34 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -1356,6 +1356,30 @@ def pid_exists(pid): return pid in pids() +def ppid_map(): + """Obtain a {pid: ppid, ...} dict for all running processes in + one shot. Used to speed up Process.children(). + """ + ret = {} + procfs_path = get_procfs_path() + for pid in pids(): + try: + with open_binary("%s/%s/stat" % (procfs_path, pid)) as f: + data = f.read() + except EnvironmentError as err: + # Note: we should be able to access /stat for all processes + # so we won't bump into EPERM, which is good. + if err.errno not in (errno.ENOENT, errno.ESRCH, + errno.EPERM, errno.EACCES): + raise + else: + rpar = data.rfind(b')') + dset = data[rpar + 2:].split() + ppid = int(dset[1]) + ret[pid] = ppid + return ret + + def wrap_exceptions(fun): """Decorator which translates bare OSError and IOError exceptions into NoSuchProcess and AccessDenied. -- cgit v1.2.1