Skip to content

Commit

Permalink
#799 / win: pass handle also to memory_maps() and username() functions
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Oct 28, 2016
1 parent 3efb6bf commit 4a06a54
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 196 deletions.
86 changes: 43 additions & 43 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -808,49 +808,49 @@ Process class
The last column (speedup) shows an approximation of the speedup you can get
if you call all the methods together (best case scenario).

+------------------------------+------------------------------+------------------------------+------------------------------+--------------------------+
| Linux | Windows | OSX | BSD | SunOS |
+==============================+==============================+==============================+==============================+==========================+
| :meth:`~Process.cpu_percent` | :meth:`~Process.cpu_percent` | :meth:`~Process.cpu_percent` | :meth:`~Process.cpu_percent` | :meth:`name` |
+------------------------------+------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`~Process.cpu_times` | :meth:`~Process.cpu_times` | :meth:`~Process.cpu_times` | :meth:`~Process.cpu_times` | :meth:`cmdline` |
+------------------------------+------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`create_time` | :meth:`io_counters()` | :meth:`memory_info` | :meth:`create_time` | :meth:`create_time` |
+------------------------------+------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`name` | :meth:`ionice` | :meth:`memory_percent` | :meth:`gids` | |
+------------------------------+------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`ppid` | :meth:`memory_info` | :meth:`num_ctx_switches` | :meth:`io_counters` | :meth:`memory_info` |
+------------------------------+------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`status` | :meth:`nice` | :meth:`num_threads` | :meth:`name` | :meth:`memory_percent` |
+------------------------------+------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`terminal` | :meth:`num_ctx_switches` | | :meth:`memory_info` | :meth:`nice` |
+------------------------------+------------------------------+------------------------------+------------------------------+--------------------------+
| | :meth:`num_handles` | :meth:`create_time` | :meth:`memory_percent` | :meth:`num_threads` |
+------------------------------+------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`gids` | :meth:`num_threads` | :meth:`gids` | :meth:`num_ctx_switches` | :meth:`ppid` |
+------------------------------+------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`num_ctx_switches` | | :meth:`name` | :meth:`ppid` | :meth:`status` |
+------------------------------+------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`num_threads` | | :meth:`ppid` | :meth:`status` | :meth:`terminal` |
+------------------------------+------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`uids` | | :meth:`status` | :meth:`terminal` | |
+------------------------------+------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`username` | | :meth:`terminal` | :meth:`uids` | :meth:`gids` |
+------------------------------+------------------------------+------------------------------+------------------------------+--------------------------+
| | | :meth:`uids` | :meth:`username` | :meth:`uids` |
+------------------------------+------------------------------+------------------------------+------------------------------+--------------------------+
| | | :meth:`username` | | :meth:`username` |
+------------------------------+------------------------------+------------------------------+------------------------------+--------------------------+
| | | | | |
+------------------------------+------------------------------+------------------------------+------------------------------+--------------------------+
| *speedup: +2.5x* | *speedup: +1.8x* | *speedup: +1.9x* | *speedup: +2.0x* | |
+------------------------------+------------------------------+------------------------------+------------------------------+--------------------------+

.. versionadded:: 5.0.0

.. attribute:: pid

The process PID. This is the only (read-only) attribute of the class.
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
| Linux | Windows | OSX | BSD | SunOS |
+==============================+===============================+==============================+==============================+==========================+
| :meth:`~Process.cpu_percent` | :meth:`~Process.cpu_percent` | :meth:`~Process.cpu_percent` | :meth:`~Process.cpu_percent` | :meth:`name` |
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`~Process.cpu_times` | :meth:`~Process.cpu_times` | :meth:`~Process.cpu_times` | :meth:`~Process.cpu_times` | :meth:`cmdline` |
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`create_time` | :meth:`io_counters()` | :meth:`memory_info` | :meth:`create_time` | :meth:`create_time` |
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`name` | :meth:`ionice` | :meth:`memory_percent` | :meth:`gids` | |
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`ppid` | :meth:`memory_info` | :meth:`num_ctx_switches` | :meth:`io_counters` | :meth:`memory_info` |
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`status` | :meth:`nice` | :meth:`num_threads` | :meth:`name` | :meth:`memory_percent` |
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`terminal` | :meth:`memory_maps` | | :meth:`memory_info` | :meth:`nice` |
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
| | :meth:`num_ctx_switches` | :meth:`create_time` | :meth:`memory_percent` | :meth:`num_threads` |
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`gids` | :meth:`num_handles` | :meth:`gids` | :meth:`num_ctx_switches` | :meth:`ppid` |
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`num_ctx_switches` | :meth:`num_threads` | :meth:`name` | :meth:`ppid` | :meth:`status` |
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`num_threads` | :meth:`username` | :meth:`ppid` | :meth:`status` | :meth:`terminal` |
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`uids` | | :meth:`status` | :meth:`terminal` | |
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
| :meth:`username` | | :meth:`terminal` | :meth:`uids` | :meth:`gids` |
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
| | | :meth:`uids` | :meth:`username` | :meth:`uids` |
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
| | | :meth:`username` | | :meth:`username` |
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
| | | | | |
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+
| *speedup: +2.5x* | *speedup: from +1.8x to +6.5* | *speedup: +1.9x* | *speedup: +2.0x* | |
+------------------------------+-------------------------------+------------------------------+------------------------------+--------------------------+

.. versionadded:: 5.0.0

.. attribute:: pid

The process PID. This is the only (read-only) attribute of the class.

.. method:: ppid()

Expand Down
27 changes: 7 additions & 20 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -1302,22 +1302,14 @@ psutil_proc_username(PyObject *self, PyObject *args) {
ULONG domainNameSize;
SID_NAME_USE nameUse;
PTSTR fullName;
unsigned long handle;
PyObject *py_unicode;

if (! PyArg_ParseTuple(args, "l", &pid))
return NULL;

processHandle = psutil_handle_from_pid_waccess(
pid, PROCESS_QUERY_INFORMATION);
if (processHandle == NULL)
if (! PyArg_ParseTuple(args, "lk", &pid, &handle))
return NULL;

if (!OpenProcessToken(processHandle, TOKEN_QUERY, &tokenHandle)) {
CloseHandle(processHandle);
if (!OpenProcessToken((HANDLE)handle, TOKEN_QUERY, &tokenHandle))
return PyErr_SetFromWindowsErr(0);
}

CloseHandle(processHandle);

// Get the user SID.

Expand Down Expand Up @@ -2817,29 +2809,27 @@ psutil_proc_memory_maps(PyObject *self, PyObject *args) {
CHAR mappedFileName[MAX_PATH];
SYSTEM_INFO system_info;
LPVOID maxAddr;
unsigned long handle;
PyObject *py_retlist = PyList_New(0);
PyObject *py_tuple = NULL;

if (py_retlist == NULL)
return NULL;
if (! PyArg_ParseTuple(args, "l", &pid))
goto error;
hProcess = psutil_handle_from_pid(pid);
if (NULL == hProcess)
if (! PyArg_ParseTuple(args, "lk", &pid, &handle))
goto error;

GetSystemInfo(&system_info);
maxAddr = system_info.lpMaximumApplicationAddress;
baseAddress = NULL;
previousAllocationBase = NULL;

while (VirtualQueryEx(hProcess, baseAddress, &basicInfo,
while (VirtualQueryEx((HANDLE)handle, baseAddress, &basicInfo,
sizeof(MEMORY_BASIC_INFORMATION)))
{
py_tuple = NULL;
if (baseAddress > maxAddr)
break;
if (GetMappedFileNameA(hProcess, baseAddress, mappedFileName,
if (GetMappedFileNameA((HANDLE)handle, baseAddress, mappedFileName,
sizeof(mappedFileName)))
{
#ifdef _WIN64
Expand All @@ -2865,14 +2855,11 @@ psutil_proc_memory_maps(PyObject *self, PyObject *args) {
baseAddress = (PCHAR)baseAddress + basicInfo.RegionSize;
}

CloseHandle(hProcess);
return py_retlist;

error:
Py_XDECREF(py_tuple);
Py_DECREF(py_retlist);
if (hProcess != NULL)
CloseHandle(hProcess);
return NULL;
}

Expand Down
6 changes: 4 additions & 2 deletions psutil/_pswindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,8 @@ def memory_full_info(self):

def memory_maps(self):
try:
raw = cext.proc_memory_maps(self.pid)
with self.handle_ctx() as handle:
raw = cext.proc_memory_maps(self.pid, handle)
except OSError as err:
# XXX - can't use wrap_exceptions decorator as we're
# returning a generator; probably needs refactoring.
Expand Down Expand Up @@ -759,7 +760,8 @@ def wait(self, timeout=None):
def username(self):
if self.pid in (0, 4):
return 'NT AUTHORITY\\SYSTEM'
return cext.proc_username(self.pid)
with self.handle_ctx() as handle:
return cext.proc_username(self.pid, handle)

@wrap_exceptions
def create_time(self):
Expand Down
Loading

0 comments on commit 4a06a54

Please sign in to comment.