Skip to content

Commit

Permalink
#720 / sysinfo /windows: add interrupts
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Mar 4, 2016
1 parent a2502d1 commit 46e1f3c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 22 deletions.
27 changes: 17 additions & 10 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ Other system info
+---------------+---------+--------------+--------------+------------+-------------+--------------+
| procs_blocked | | syscalls | interrupts | max_procs | max_procs | max_procs |
+---------------+---------+--------------+--------------+------------+-------------+--------------+
| ctx_switches | | | num_threads | max_pid | max_threads | ctx_switches |
| ctx_switches | | interrupts | num_threads | max_pid | max_threads | ctx_switches |
+---------------+---------+--------------+--------------+------------+-------------+--------------+
| open_files | | | traps | open_files | open_files | interrupts |
+---------------+---------+--------------+--------------+------------+-------------+--------------+
Expand All @@ -565,18 +565,25 @@ Other system info
| max_procs | | | | | | |
+---------------+---------+--------------+--------------+------------+-------------+--------------+

- **procs_running** (Linux): current number of actively running processes
- **procs_blocked** (Linux): current number of processes waiting for I/O
- **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):
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): number of total system-wide interrupts since boot (cumulative, always increasing)
- **max_threads** (Linux): maximum number of threads which can be run, system-wide
- **max_files** (Linux, FreeBSD): maximum number of files which can be opened, system-wide
- **max_pid** (Linux, FreeBSD): the highest possible PID number
- **max_procs** (FreeBSD): maximum number of processes which can be run, system-wide
- **num_threads** (OpenBSD): total number of running threads, system-wide
- **open_files** (Linux, FreeBSD):
total number of opened file descriptors (regular files, sockets, etc.),
system-wide.
- **interrupts** (Linux, Windows):
number of total system-wide interrupts since boot (cumulative, always
increasing).
- **max_threads** (Linux): maximum number of threads which can be run,
system-wide.
- **max_files** (Linux, FreeBSD):
maximum number of files which can be opened, system-wide.
- **max_pid** (Linux, FreeBSD): the highest possible PID number.
- **max_procs** (FreeBSD): maximum number of processes which can be run,
system-wide.
- **num_threads** (OpenBSD): total number of running threads, system-wide.
- **traps** (SunOS): number of kernel traps.
- **syscalls** (Windows, SunOS): number of syscalls.

Expand Down
54 changes: 45 additions & 9 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -3119,8 +3119,11 @@ psutil_sysinfo(PyObject *self, PyObject *args) {
HINSTANCE hNtDll;

NTSTATUS status;
_SYSTEM_PERFORMANCE_INFORMATION *sppi = NULL;
_SYSTEM_PERFORMANCE_INFORMATION *spi = NULL;
_SYSTEM_INTERRUPT_INFORMATION *InterruptInformation = NULL;
SYSTEM_INFO si;
UINT i;
ULONG64 interrupts = 0;

// obtain NtQuerySystemInformation
hNtDll = LoadLibrary(TEXT("ntdll.dll"));
Expand All @@ -3140,32 +3143,65 @@ psutil_sysinfo(PyObject *self, PyObject *args) {

// allocates an array of _SYSTEM_PERFORMANCE_INFORMATION
// structures, one per processor
sppi = (_SYSTEM_PERFORMANCE_INFORMATION *) \
spi = (_SYSTEM_PERFORMANCE_INFORMATION *) \
malloc(si.dwNumberOfProcessors * \
sizeof(_SYSTEM_PERFORMANCE_INFORMATION));
if (sppi == NULL) {
if (spi == NULL) {
PyErr_NoMemory();
goto error;
}

// gets cpu time informations
// get syscalls / ctx switches
status = NtQuerySystemInformation(
SystemPerformanceInformation,
sppi,
spi,
si.dwNumberOfProcessors * sizeof(_SYSTEM_PERFORMANCE_INFORMATION),
NULL);
if (status != 0) {
PyErr_SetFromWindowsErr(0);
goto error;
}

free(sppi);
// allocates an array of SYSTEM_INTERRUPT_INFORMATION
// structures, one per processor
InterruptInformation = \
malloc(sizeof(_SYSTEM_INTERRUPT_INFORMATION) *
si.dwNumberOfProcessors);
if (InterruptInformation == NULL) {
PyErr_NoMemory();
goto error;
}

// get interrupts
status = NtQuerySystemInformation(
SystemInterruptInformation,
InterruptInformation,
si.dwNumberOfProcessors * sizeof(SYSTEM_INTERRUPT_INFORMATION),
NULL);
if (status != 0) {
PyErr_SetFromWindowsErr(0);
goto error;
}
for (i = 0; i < si.dwNumberOfProcessors; i++) {
interrupts += InterruptInformation[i].DpcCount;
}

// done
free(spi);
free(InterruptInformation);
FreeLibrary(hNtDll);
return Py_BuildValue("kk", sppi->ContextSwitches, sppi->SystemCalls);
return Py_BuildValue(
"kkk",
spi->ContextSwitches,
spi->SystemCalls,
(unsigned long)interrupts
);

error:
if (sppi)
free(sppi);
if (spi)
free(spi);
if (InterruptInformation)
free(InterruptInformation);
if (hNtDll)
FreeLibrary(hNtDll);
return NULL;
Expand Down
6 changes: 3 additions & 3 deletions psutil/_pswindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class Priority(enum.IntEnum):
'create_time', 'num_threads', 'io_rcount', 'io_wcount',
'io_rbytes', 'io_wbytes'])
ssysinfo = namedtuple(
'ssysinfo', ['ctx_switches', 'syscalls'])
'ssysinfo', ['ctx_switches', 'syscalls', 'interrupts'])

# set later from __init__.py
NoSuchProcess = None
Expand Down Expand Up @@ -272,8 +272,8 @@ def users():


def sysinfo():
ctx_switches, syscalls = cext.sysinfo()
return ssysinfo(ctx_switches, syscalls)
ctx_switches, syscalls, interrupts = cext.sysinfo()
return ssysinfo(ctx_switches, syscalls, interrupts)


pids = cext.pids
Expand Down
10 changes: 10 additions & 0 deletions psutil/arch/windows/ntextapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ typedef struct {
} _SYSTEM_PERFORMANCE_INFORMATION;


typedef struct {
ULONG ContextSwitches;
ULONG DpcCount;
ULONG DpcRate;
ULONG TimeIncrement;
ULONG DpcBypassCount;
ULONG ApcBypassCount;
} _SYSTEM_INTERRUPT_INFORMATION;


typedef enum _KTHREAD_STATE {
Initialized,
Ready,
Expand Down

0 comments on commit 46e1f3c

Please sign in to comment.