From fc1e59d08c968898c2ede425a621b62ccf44681c Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Thu, 12 Nov 2015 23:04:39 +0100 Subject: [PATCH] minor cosmetic changes --- docs/index.rst | 3 +- psutil/_psbsd.py | 61 ++++++++++++++++++++------------------- psutil/_psutil_bsd.c | 22 +++++++------- psutil/_psutil_osx.c | 7 +++-- psutil/_psutil_sunos.c | 25 ++++++++-------- psutil/arch/bsd/freebsd.c | 12 ++++---- psutil/arch/bsd/openbsd.c | 15 +++++----- 7 files changed, 75 insertions(+), 70 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 9561b45c1..026df8865 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -1092,7 +1092,8 @@ Process class `here `_). .. warning:: - on FreeBSD this method can return files with a 'null' path (see + on FreeBSD and OpenBSD this method can return files with a 'null' path + due to a kernel bug (see `issue 595 `_). .. versionchanged:: 3.1.0 no longer hangs on Windows. diff --git a/psutil/_psbsd.py b/psutil/_psbsd.py index 580392459..a772e2646 100644 --- a/psutil/_psbsd.py +++ b/psutil/_psbsd.py @@ -2,7 +2,7 @@ # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. -"""FreeBSD platform implementation.""" +"""FreeBSD and OpenBSD platforms implementation.""" import errno import functools @@ -156,34 +156,37 @@ def cpu_count_logical(): return cext.cpu_count_logical() -def cpu_count_physical(): - """Return the number of physical CPUs in the system.""" - # From the C module we'll get an XML string similar to this: - # http://manpages.ubuntu.com/manpages/precise/man4/smp.4freebsd.html - # We may get None in case "sysctl kern.sched.topology_spec" - # is not supported on this BSD version, in which case we'll mimic - # os.cpu_count() and return None. - if sys.platform.startswith("openbsd"): - return cext.cpu_count_logical() - ret = None - s = cext.cpu_count_phys() - if s is not None: - # get rid of padding chars appended at the end of the string - index = s.rfind("") - if index != -1: - s = s[:index + 9] - root = ET.fromstring(s) - try: - ret = len(root.findall('group/children/group/cpu')) or None - finally: - # needed otherwise it will memleak - root.clear() - if not ret: - # If logical CPUs are 1 it's obvious we'll have only 1 - # physical CPU. - if cpu_count_logical() == 1: - return 1 - return ret +if OPENBSD: + def cpu_count_physical(): + # OpenBSD does not implement this. + return 1 if cpu_count_logical() == 1 else None +else: + def cpu_count_physical(): + """Return the number of physical CPUs in the system.""" + # From the C module we'll get an XML string similar to this: + # http://manpages.ubuntu.com/manpages/precise/man4/smp.4freebsd.html + # We may get None in case "sysctl kern.sched.topology_spec" + # is not supported on this BSD version, in which case we'll mimic + # os.cpu_count() and return None. + ret = None + s = cext.cpu_count_phys() + if s is not None: + # get rid of padding chars appended at the end of the string + index = s.rfind("") + if index != -1: + s = s[:index + 9] + root = ET.fromstring(s) + try: + ret = len(root.findall('group/children/group/cpu')) or None + finally: + # needed otherwise it will memleak + root.clear() + if not ret: + # If logical CPUs are 1 it's obvious we'll have only 1 + # physical CPU. + if cpu_count_logical() == 1: + return 1 + return ret def boot_time(): diff --git a/psutil/_psutil_bsd.c b/psutil/_psutil_bsd.c index 2fd177fd2..278504f81 100644 --- a/psutil/_psutil_bsd.c +++ b/psutil/_psutil_bsd.c @@ -86,17 +86,17 @@ #endif -#define TV2DOUBLE(t) ((t).tv_sec + (t).tv_usec / 1000000.0) +// convert a timeval struct to a double +#define PSUTIL_TV2DOUBLE(t) ((t).tv_sec + (t).tv_usec / 1000000.0) #ifdef __FreeBSD__ - // convert a timeval struct to a double // convert a bintime struct to milliseconds - #define BT2MSEC(bt) (bt.sec * 1000 + (((uint64_t) 1000000000 * (uint32_t) \ - (bt.frac >> 32) ) >> 32 ) / 1000000) + #define PSUTIL_BT2MSEC(bt) (bt.sec * 1000 + (((uint64_t) 1000000000 * \ + (uint32_t) (bt.frac >> 32) ) >> 32 ) / 1000000) #endif #ifdef __OpenBSD__ - #define KPT2DOUBLE(t) (t ## _sec + t ## _usec / 1000000.0) + #define PSUTIL_KPT2DOUBLE(t) (t ## _sec + t ## _usec / 1000000.0) #endif @@ -351,11 +351,11 @@ psutil_proc_cpu_times(PyObject *self, PyObject *args) { return NULL; // convert from microseconds to seconds #ifdef __FreeBSD__ - user_t = TV2DOUBLE(kp.ki_rusage.ru_utime); - sys_t = TV2DOUBLE(kp.ki_rusage.ru_stime); + user_t = PSUTIL_TV2DOUBLE(kp.ki_rusage.ru_utime); + sys_t = PSUTIL_TV2DOUBLE(kp.ki_rusage.ru_stime); #elif __OpenBSD__ - user_t = KPT2DOUBLE(kp.p_uutime); - sys_t = KPT2DOUBLE(kp.p_ustime); + user_t = PSUTIL_KPT2DOUBLE(kp.p_uutime); + sys_t = PSUTIL_KPT2DOUBLE(kp.p_ustime); #endif return Py_BuildValue("(dd)", user_t, sys_t); } @@ -395,9 +395,9 @@ psutil_proc_create_time(PyObject *self, PyObject *args) { if (psutil_kinfo_proc(pid, &kp) == -1) return NULL; #ifdef __FreeBSD__ - return Py_BuildValue("d", TV2DOUBLE(kp.ki_start)); + return Py_BuildValue("d", PSUTIL_TV2DOUBLE(kp.ki_start)); #elif __OpenBSD__ - return Py_BuildValue("d", KPT2DOUBLE(kp.p_ustart)); + return Py_BuildValue("d", PSUTIL_KPT2DOUBLE(kp.p_ustart)); #endif } diff --git a/psutil/_psutil_osx.c b/psutil/_psutil_osx.c index 2830ee385..bea74f7d1 100644 --- a/psutil/_psutil_osx.c +++ b/psutil/_psutil_osx.c @@ -44,6 +44,9 @@ #include "arch/osx/process_info.h" +#define PSUTIL_TV2DOUBLE(t) ((t).tv_sec + (t).tv_usec / 1000000.0) + + /* * A wrapper around host_statistics() invoked with HOST_VM_INFO. */ @@ -439,8 +442,6 @@ psutil_cpu_count_phys(PyObject *self, PyObject *args) { } -#define TV2DOUBLE(t) ((t).tv_sec + (t).tv_usec / 1000000.0) - /* * Return a Python tuple (user_time, kernel_time) */ @@ -471,7 +472,7 @@ psutil_proc_create_time(PyObject *self, PyObject *args) { return NULL; if (psutil_get_kinfo_proc(pid, &kp) == -1) return NULL; - return Py_BuildValue("d", TV2DOUBLE(kp.kp_proc.p_starttime)); + return Py_BuildValue("d", PSUTIL_TV2DOUBLE(kp.kp_proc.p_starttime)); } diff --git a/psutil/_psutil_sunos.c b/psutil/_psutil_sunos.c index 0a235d222..3be1f545a 100644 --- a/psutil/_psutil_sunos.c +++ b/psutil/_psutil_sunos.c @@ -45,7 +45,13 @@ #include "_psutil_sunos.h" -#define TV2DOUBLE(t) (((t).tv_nsec * 0.000000001) + (t).tv_sec) +#define PSUTIL_TV2DOUBLE(t) (((t).tv_nsec * 0.000000001) + (t).tv_sec) +#ifndef EXPER_IP_AND_ALL_IRES +#define EXPER_IP_AND_ALL_IRES (1024+4) +#endif +// a signaler for connections without an actual status +static int PSUTIL_CONN_NONE = 128; + /* * Read a file content and fills a C structure with it. @@ -94,7 +100,7 @@ psutil_proc_basic_info(PyObject *self, PyObject *args) { info.pr_ppid, // parent pid info.pr_rssize, // rss info.pr_size, // vms - TV2DOUBLE(info.pr_start), // create time + PSUTIL_TV2DOUBLE(info.pr_start), // create time info.pr_lwp.pr_nice, // nice info.pr_nlwp, // no. of threads info.pr_lwp.pr_state, // status code @@ -137,8 +143,8 @@ psutil_proc_cpu_times(PyObject *self, PyObject *args) { return NULL; // results are more precise than os.times() return Py_BuildValue("dd", - TV2DOUBLE(info.pr_utime), - TV2DOUBLE(info.pr_stime)); + PSUTIL_TV2DOUBLE(info.pr_utime), + PSUTIL_TV2DOUBLE(info.pr_stime)); } @@ -232,8 +238,8 @@ psutil_proc_query_thread(PyObject *self, PyObject *args) { if (! psutil_file_to_struct(path, (void *)&info, sizeof(info))) return NULL; return Py_BuildValue("dd", - TV2DOUBLE(info.pr_utime), - TV2DOUBLE(info.pr_stime)); + PSUTIL_TV2DOUBLE(info.pr_utime), + PSUTIL_TV2DOUBLE(info.pr_stime)); } @@ -783,13 +789,6 @@ psutil_net_io_counters(PyObject *self, PyObject *args) { } -#ifndef EXPER_IP_AND_ALL_IRES -#define EXPER_IP_AND_ALL_IRES (1024+4) -#endif - -// a signaler for connections without an actual status -static int PSUTIL_CONN_NONE = 128; - /* * Return TCP and UDP connections opened by process. * UNIX sockets are excluded. diff --git a/psutil/arch/bsd/freebsd.c b/psutil/arch/bsd/freebsd.c index 4000c2c21..cf5a51c7b 100644 --- a/psutil/arch/bsd/freebsd.c +++ b/psutil/arch/bsd/freebsd.c @@ -29,8 +29,8 @@ #include "../../_psutil_common.h" -#define TV2DOUBLE(t) ((t).tv_sec + (t).tv_usec / 1000000.0) -#define BT2MSEC(bt) (bt.sec * 1000 + (((uint64_t) 1000000000 * (uint32_t) \ +#define PSUTIL_TV2DOUBLE(t) ((t).tv_sec + (t).tv_usec / 1000000.0) +#define PSUTIL_BT2MSEC(bt) (bt.sec * 1000 + (((uint64_t) 1000000000 * (uint32_t) \ (bt.frac >> 32) ) >> 32 ) / 1000000) #ifndef _PATH_DEVNULL #define _PATH_DEVNULL "/dev/null" @@ -417,8 +417,8 @@ psutil_proc_threads(PyObject *self, PyObject *args) { kipp = &kip[i]; py_tuple = Py_BuildValue("Idd", kipp->ki_tid, - TV2DOUBLE(kipp->ki_rusage.ru_utime), - TV2DOUBLE(kipp->ki_rusage.ru_stime)); + PSUTIL_TV2DOUBLE(kipp->ki_rusage.ru_utime), + PSUTIL_TV2DOUBLE(kipp->ki_rusage.ru_stime)); if (py_tuple == NULL) goto error; if (PyList_Append(py_retlist, py_tuple)) @@ -747,8 +747,8 @@ psutil_disk_io_counters(PyObject *self, PyObject *args) { current.operations[DEVSTAT_WRITE], // no writes current.bytes[DEVSTAT_READ], // bytes read current.bytes[DEVSTAT_WRITE], // bytes written - (long long) BT2MSEC(current.duration[DEVSTAT_READ]), // r time - (long long) BT2MSEC(current.duration[DEVSTAT_WRITE]) // w time + (long long) PSUTIL_BT2MSEC(current.duration[DEVSTAT_READ]), // r time + (long long) PSUTIL_BT2MSEC(current.duration[DEVSTAT_WRITE]) // w time ); // finished transactions if (!py_disk_info) goto error; diff --git a/psutil/arch/bsd/openbsd.c b/psutil/arch/bsd/openbsd.c index 969247b30..fa9e5e940 100644 --- a/psutil/arch/bsd/openbsd.c +++ b/psutil/arch/bsd/openbsd.c @@ -34,12 +34,11 @@ #include "openbsd.h" +#define PSUTIL_KPT2DOUBLE(t) (t ## _sec + t ## _usec / 1000000.0) +#define PSUTIL_TV2DOUBLE(t) ((t).tv_sec + (t).tv_usec / 1000000.0) // a signaler for connections without an actual status int PSUTIL_CONN_NONE = 128; -#define KPT2DOUBLE(t) (t ## _sec + t ## _usec / 1000000.0) -#define TV2DOUBLE(t) ((t).tv_sec + (t).tv_usec / 1000000.0) - // ============================================================================ // Utility functions @@ -298,8 +297,8 @@ psutil_proc_threads(PyObject *self, PyObject *args) { py_tuple = Py_BuildValue( "Idd", kp[i].p_tid, - KPT2DOUBLE(kp[i].p_uutime), - KPT2DOUBLE(kp[i].p_ustime)); + PSUTIL_KPT2DOUBLE(kp[i].p_uutime), + PSUTIL_KPT2DOUBLE(kp[i].p_ustime)); if (py_tuple == NULL) goto error; if (PyList_Append(py_retlist, py_tuple)) @@ -740,8 +739,10 @@ psutil_disk_io_counters(PyObject *self, PyObject *args) { stats[i].ds_wxfer, stats[i].ds_rbytes, stats[i].ds_wbytes, - (long long) TV2DOUBLE(stats[i].ds_time) / 2, /* assume half read - half writes.. */ - (long long) TV2DOUBLE(stats[i].ds_time) / 2); + // assume half read - half writes. + // TODO: why? + (long long) PSUTIL_TV2DOUBLE(stats[i].ds_time) / 2, + (long long) PSUTIL_TV2DOUBLE(stats[i].ds_time) / 2); if (!py_disk_info) goto error; if (PyDict_SetItemString(py_retdict, stats[i].ds_name, py_disk_info))