From a26fa599482e8ee346ebe27202d4ee1a5b03ada2 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Fri, 4 Mar 2016 16:42:47 +0100 Subject: [PATCH] #720: sysinfo osx --- docs/index.rst | 48 ++++++++++++++++++++++---------------------- psutil/_psosx.py | 4 ++++ psutil/_psutil_osx.c | 29 ++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 24 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 131505340..106d70f72 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -543,37 +543,37 @@ Other system info ctx_switches, interrupts, nthreads, traps, syscalls - +---------------+---------+--------------+--------------+------------+-------------+--------------+ - | Linux | OSX | Windows | SunOS | FreeBSD | OpenBSD | NetBSD | - +===============+=========+==============+==============+============+=============+==============+ - | procs_running | | ctx_switches | ctx_switches | max_files | max_files | max_files | - +---------------+---------+--------------+--------------+------------+-------------+--------------+ - | procs_blocked | | interrupts | interrupts | max_procs | max_procs | max_procs | - +---------------+---------+--------------+--------------+------------+-------------+--------------+ - | ctx_switches | | dpcs | num_threads | max_pid | max_threads | ctx_switches | - +---------------+---------+--------------+--------------+------------+-------------+--------------+ - | open_files | | syscalls | traps | open_files | open_files | interrupts | - +---------------+---------+--------------+--------------+------------+-------------+--------------+ - | interrupts | | | syscalls | | num_threads | | - +--------+------+---------+--------------+--------------+------------+-------------+--------------+ - | max_threads | | | | | | | - +---------------+---------+--------------+--------------+------------+-------------+--------------+ - | max_files | | | | | | | - +--------+------+---------+--------------+--------------+------------+-------------+--------------+ - | max_pid | | | | | | | - +---------------+---------+--------------+--------------+------------+-------------+--------------+ - | max_procs | | | | | | | - +---------------+---------+--------------+--------------+------------+-------------+--------------+ + +---------------+--------------+--------------+--------------+------------+-------------+--------------+ + | Linux | OSX | Windows | SunOS | FreeBSD | OpenBSD | NetBSD | + +===============+==============+==============+==============+============+=============+==============+ + | procs_running | ctx_switches | ctx_switches | ctx_switches | max_files | max_files | max_files | + +---------------+--------------+--------------+--------------+------------+-------------+--------------+ + | procs_blocked | interrupts | interrupts | interrupts | max_procs | max_procs | max_procs | + +---------------+--------------+--------------+--------------+------------+-------------+--------------+ + | ctx_switches | syscalls | dpcs | num_threads | max_pid | max_threads | ctx_switches | + +---------------+--------------+--------------+--------------+------------+-------------+--------------+ + | open_files | | syscalls | traps | open_files | open_files | interrupts | + +---------------+--------------+--------------+--------------+------------+-------------+--------------+ + | interrupts | | | syscalls | | num_threads | | + +---------------+--------------+--------------+--------------+------------+-------------+--------------+ + | max_threads | | | | | | | + +---------------+--------------+--------------+--------------+------------+-------------+--------------+ + | max_files | | | | | | | + +---------------+--------------+--------------+--------------+------------+-------------+--------------+ + | max_pid | | | | | | | + +---------------+--------------+--------------+--------------+------------+-------------+--------------+ + | max_procs | | | | | | | + +---------------+--------------+--------------+--------------+------------+-------------+--------------+ - **procs_running** (Linux): current number of actively running processes. - **procs_blocked** (Linux): current number of processes waiting for I/O. - - **ctx_switches** (Linux, Windows, NetBSD, SunOS): + - **ctx_switches** (Linux, Windows, NetBSD, SunOS, OSX): number of system-wide context switches (voluntary and involuntary) since boot (cumulative, always increasing) - **open_files** (Linux, FreeBSD): total number of opened file descriptors (regular files, sockets, etc.), system-wide. - - **interrupts** (Linux, Windows, NetBSD, SunOS): + - **interrupts** (Linux, OSX, Windows, NetBSD, SunOS): number of total system-wide interrupts since boot (cumulative, always increasing). - **max_threads** (Linux): maximum number of threads which can be run, @@ -585,7 +585,7 @@ Other system info system-wide. - **num_threads** (OpenBSD): total number of running threads, system-wide. - **traps** (SunOS): number of kernel traps. - - **syscalls** (Windows, SunOS): number of syscalls. + - **syscalls** (Windows, OSX, SunOS): number of syscalls. - **pdcs** (Windows): number of deferred procedure calls. Example (Linux): diff --git a/psutil/_psosx.py b/psutil/_psosx.py index b8066775b..b32acc2fd 100644 --- a/psutil/_psosx.py +++ b/psutil/_psosx.py @@ -154,6 +154,10 @@ def users(): return retlist +def sysinfo(): + return cext.sysinfo() + + def net_connections(kind='inet'): # Note: on OSX this will fail with AccessDenied unless # the process is owned by root. diff --git a/psutil/_psutil_osx.c b/psutil/_psutil_osx.c index 12500dc83..e49a7d424 100644 --- a/psutil/_psutil_osx.c +++ b/psutil/_psutil_osx.c @@ -1788,6 +1788,33 @@ psutil_users(PyObject *self, PyObject *args) { } +/* + * Return various system info. + */ +static PyObject * +psutil_sysinfo(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( + "III", + vmstat.v_swtch, // ctx switches + vmstat.v_intr, // interrupts + vmstat.v_syscall // syscalls + ); +} + + /* * define the psutil C module methods and initialize the module. */ @@ -1867,6 +1894,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"}, + {"sysinfo", psutil_sysinfo, METH_VARARGS, + "Return various system info"}, {NULL, NULL, 0, NULL} };