Skip to content

Commit

Permalink
#799 / windows / cpu_times: add memory_info() support
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Aug 6, 2016
1 parent 6c55785 commit 242f522
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
9 changes: 7 additions & 2 deletions make.bat
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ if "%1" == "test-system" (
goto :eof
)

f "%1" == "test-platform" (
if "%1" == "test-platform" (
call :install
%PYTHON% psutil\tests\test_windows.py
goto :eof
Expand Down Expand Up @@ -229,13 +229,18 @@ if "%1" == "setup-dev-env-all" (
goto :eof
)


if "%1" == "flake8" (
:flake8
%PYTHON% -c "from flake8.main import main; main()"
goto :eof
)

if "%1" == "bench-oneshot" (
call :install
%PYTHON% scripts\internal\bench_oneshot.py
goto :eof
)

goto :help

:error
Expand Down
16 changes: 4 additions & 12 deletions psutil/_psutil_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,6 @@ psutil_proc_cpu_times(PyObject *self, PyObject *args) {

if (! GetProcessTimes(
(HANDLE)handle, &ftCreate, &ftExit, &ftKernel, &ftUser)) {
CloseHandle(hProcess);
if (GetLastError() == ERROR_ACCESS_DENIED) {
// usually means the process has died so we throw a NoSuchProcess
// here
Expand Down Expand Up @@ -711,7 +710,7 @@ psutil_proc_name(PyObject *self, PyObject *args) {
*/
static PyObject *
psutil_proc_memory_info(PyObject *self, PyObject *args) {
HANDLE hProcess;
unsigned long handle;
DWORD pid;
#if (_WIN32_WINNT >= 0x0501) // Windows XP with SP2
PROCESS_MEMORY_COUNTERS_EX cnt;
Expand All @@ -720,25 +719,18 @@ psutil_proc_memory_info(PyObject *self, PyObject *args) {
#endif
SIZE_T private = 0;

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

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

if (! GetProcessMemoryInfo(hProcess, (PPROCESS_MEMORY_COUNTERS)&cnt,
sizeof(cnt))) {
CloseHandle(hProcess);
if (! GetProcessMemoryInfo(
(HANDLE)handle, (PPROCESS_MEMORY_COUNTERS)&cnt, sizeof(cnt))) {
return PyErr_SetFromWindowsErr(0);
}

#if (_WIN32_WINNT >= 0x0501) // Windows XP with SP2
private = cnt.PrivateUsage;
#endif

CloseHandle(hProcess);

// PROCESS_MEMORY_COUNTERS values are defined as SIZE_T which on 64bits
// is an (unsigned long long) and on 32bits is an (unsigned int).
// "_WIN64" is defined if we're running a 64bit Python interpreter not
Expand Down
3 changes: 2 additions & 1 deletion psutil/_pswindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,8 @@ def ppid(self):

def _get_raw_meminfo(self):
try:
return cext.proc_memory_info(self.pid)
with self.handle_ctx() as handle:
return cext.proc_memory_info(self.pid, handle)
except OSError as err:
if err.errno in ACCESS_DENIED_SET:
# TODO: the C ext can probably be refactored in order
Expand Down
23 changes: 19 additions & 4 deletions scripts/internal/bench_oneshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,20 @@
"""

from __future__ import print_function
import os
import sys
import time

import psutil


ITERATIONS = 1000
if hasattr(time, 'perf_counter'):
timer = time.perf_counter
elif os.name == 'nt':
timer = time.clock
else:
timer = time.time

# The list of Process methods which gets collected in one shot and
# as such get advantage of the speedup.
Expand Down Expand Up @@ -66,6 +73,14 @@
'terminal',
'uids',
)
elif psutil.WINDOWS:
names = (
'cpu_percent',
'cpu_times',
'num_handles',
'memory_info',
'memory_percent',
)
else:
raise RuntimeError("platform %r not supported" % sys.platform)

Expand All @@ -88,18 +103,18 @@ def main():
print(" " + name)

# first "normal" run
t = time.time()
t = timer()
for x in range(ITERATIONS):
call(funs)
elapsed1 = time.time() - t
elapsed1 = timer() - t
print("normal: %.3f secs" % elapsed1)

# "one shot" run
t = time.time()
t = timer()
for x in range(ITERATIONS):
with p.oneshot():
call(funs)
elapsed2 = time.time() - t
elapsed2 = timer() - t
print("oneshot: %.3f secs" % elapsed2)

# done
Expand Down

0 comments on commit 242f522

Please sign in to comment.