Skip to content

Commit

Permalink
#792 / cpu_stats: osx impl
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Mar 4, 2016
1 parent fb0154e commit fa00dfb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 19 deletions.
38 changes: 19 additions & 19 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
10 changes: 10 additions & 0 deletions psutil/_psosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@

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',
Expand Down Expand Up @@ -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()
Expand Down
32 changes: 32 additions & 0 deletions psutil/_psutil_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -1788,6 +1788,36 @@ psutil_users(PyObject *self, PyObject *args) {
}


/*
* 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.
*/
Expand Down Expand Up @@ -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}
};
Expand Down

0 comments on commit fa00dfb

Please sign in to comment.