diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2016-03-04 17:02:50 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2016-03-04 17:02:50 +0100 |
commit | fa00dfb961ef63426c7818899340866ced8d2418 (patch) | |
tree | af5222562d38d5016f4b3a33f3de7981a04f8fac | |
parent | fb0154ef164d0e5942ac85102ab660b8d2938fbb (diff) | |
download | psutil-fa00dfb961ef63426c7818899340866ced8d2418.tar.gz |
#792 / cpu_stats: osx impl
-rw-r--r-- | docs/index.rst | 38 | ||||
-rw-r--r-- | psutil/_psosx.py | 10 | ||||
-rw-r--r-- | psutil/_psutil_osx.c | 32 |
3 files changed, 61 insertions, 19 deletions
diff --git a/docs/index.rst b/docs/index.rst index 27488e32..fc062389 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -157,25 +157,25 @@ CPU ctx_switches, interrupts, soft_interrupts, syscalls, traps - +-----------------+--------------+--------------+--------------+-----------------+-------------+--------------+ - | Linux | OSX | Windows | SunOS | FreeBSD | OpenBSD | NetBSD | - +=================+==============+==============+==============+=================+=============+==============+ - | ctx_switches | | | | ctx_switches | | | - +-----------------+--------------+--------------+--------------+-----------------+-------------+--------------+ - | interrupts | | | | interrupts | | | - +-----------------+--------------+--------------+--------------+-----------------+-------------+--------------+ - | soft_interrupts | | | | soft_interrupts | | | - +-----------------+--------------+--------------+--------------+-----------------+-------------+--------------+ - | procs_running | | | | syscalls | | | - +-----------------+--------------+--------------+--------------+-----------------+-------------+--------------+ - | procs_blocked | | | | traps | | | - +-----------------+--------------+--------------+--------------+-----------------+-------------+--------------+ - | | | | | | | | - +-----------------+--------------+--------------+--------------+-----------------+-------------+--------------+ - | | | | | | | | - +-----------------+--------------+--------------+--------------+-----------------+-------------+--------------+ - | | | | | | | | - +-----------------+--------------+--------------+--------------+-----------------+-------------+--------------+ + +-----------------+-----------------+--------------+-----------------+-------------+--------------+--------------+ + | Linux | OSX | Windows | FreeBSD | OpenBSD | NetBSD | SunOS | + +=================+=================+==============+=================+=============+==============+==============+ + | ctx_switches | ctx_switches | | ctx_switches | | | | + +-----------------+-----------------+--------------+-----------------+-------------+--------------+--------------+ + | interrupts | interrupts | | interrupts | | | | + +-----------------+-----------------+--------------+-----------------+-------------+--------------+--------------+ + | soft_interrupts | soft_interrupts | | soft_interrupts | | | | + +-----------------+-----------------+--------------+-----------------+-------------+--------------+--------------+ + | procs_running | syscalls | | syscalls | | | | + +-----------------+-----------------+--------------+-----------------+-------------+--------------+--------------+ + | procs_blocked | traps | | traps | | | | + +-----------------+-----------------+--------------+-----------------+-------------+--------------+--------------+ + | | | | | | | | + +-----------------+-----------------+--------------+-----------------+-------------+--------------+--------------+ + | | | | | | | | + +-----------------+-----------------+--------------+-----------------+-------------+--------------+--------------+ + | | | | | | | | + +-----------------+-----------------+--------------+-----------------+-------------+--------------+--------------+ Example (Linux): diff --git a/psutil/_psosx.py b/psutil/_psosx.py index b8066775..db21d542 100644 --- a/psutil/_psosx.py +++ b/psutil/_psosx.py @@ -60,6 +60,9 @@ svmem = namedtuple( pmem = namedtuple('pmem', ['rss', 'vms', 'pfaults', 'pageins']) pfullmem = namedtuple('pfullmem', pmem._fields + ('uss', )) +scpustats = namedtuple( + 'scpustats', ['ctx_switches', 'interrupts', 'soft_interrupts', + 'syscalls', 'traps']) pmmap_grouped = namedtuple( 'pmmap_grouped', @@ -120,6 +123,13 @@ def cpu_count_physical(): return cext.cpu_count_phys() +def cpu_stats(): + ctx_switches, interrupts, soft_interrupts, syscalls, traps = \ + cext.cpu_stats() + return scpustats( + ctx_switches, interrupts, soft_interrupts, syscalls, traps) + + def boot_time(): """The system boot time expressed in seconds since the epoch.""" return cext.boot_time() diff --git a/psutil/_psutil_osx.c b/psutil/_psutil_osx.c index 12500dc8..7956beeb 100644 --- a/psutil/_psutil_osx.c +++ b/psutil/_psutil_osx.c @@ -1789,6 +1789,36 @@ error: /* + * Return CPU statistics. + */ +static PyObject * +psutil_cpu_stats(PyObject *self, PyObject *args) { + struct vmmeter vmstat; + kern_return_t ret; + mach_msg_type_number_t count = sizeof(vmstat) / sizeof(integer_t); + mach_port_t mport = mach_host_self(); + + ret = host_statistics(mport, HOST_VM_INFO, (host_info_t)&vmstat, &count); + if (ret != KERN_SUCCESS) { + PyErr_Format(PyExc_RuntimeError, + "host_statistics() failed: %s", mach_error_string(ret)); + return NULL; + } + mach_port_deallocate(mach_task_self(), mport); + + return Py_BuildValue( + "IIIii", + vmstat.v_swtch, // ctx switches + vmstat.v_intr, // interrupts + vmstat.v_soft, // software interrupts + vmstat.v_syscall, // syscalls + vmstat.v_trap // traps + ); +} + + + +/* * define the psutil C module methods and initialize the module. */ static PyMethodDef @@ -1867,6 +1897,8 @@ PsutilMethods[] = { "Return dict of tuples of disks I/O information."}, {"users", psutil_users, METH_VARARGS, "Return currently connected users as a list of tuples"}, + {"cpu_stats", psutil_cpu_stats, METH_VARARGS, + "Return CPU statistics"}, {NULL, NULL, 0, NULL} }; |