Skip to content

Commit

Permalink
minor cosmetic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Nov 12, 2015
1 parent b28c702 commit fc1e59d
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 70 deletions.
3 changes: 2 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,8 @@ Process class
`here <https://github.com/giampaolo/psutil/pull/597>`_).

.. 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 <https://github.com/giampaolo/psutil/pull/595>`_).

.. versionchanged:: 3.1.0 no longer hangs on Windows.
Expand Down
61 changes: 32 additions & 29 deletions psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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("</groups>")
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("</groups>")
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():
Expand Down
22 changes: 11 additions & 11 deletions psutil/_psutil_bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
}

Expand Down
7 changes: 4 additions & 3 deletions psutil/_psutil_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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)
*/
Expand Down Expand Up @@ -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));
}


Expand Down
25 changes: 12 additions & 13 deletions psutil/_psutil_sunos.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
}


Expand Down Expand Up @@ -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));
}


Expand Down Expand Up @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions psutil/arch/bsd/freebsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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;
Expand Down
15 changes: 8 additions & 7 deletions psutil/arch/bsd/openbsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit fc1e59d

Please sign in to comment.