summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2020-02-13 13:35:39 -0800
committerGiampaolo Rodola <g.rodola@gmail.com>2020-02-13 23:10:25 +0100
commit7ce6c3c1626bd7101f69031e36b980639ca591d6 (patch)
tree9164e0cc77da4186bdfa00f27705b4a69b40b49b
parentb0cff82c6b91f99d2962f10d8d61d64853296e36 (diff)
downloadpsutil-7ce6c3c1626bd7101f69031e36b980639ca591d6.tar.gz
refactoring: get rid of duplicated code; use one function to return (user, sys, create time)
rename method remove unnecessary wrap_exceptions deco bind cpu_times() and create_time() with oneshot() rename var remove cache for proc_times(): unnecessary because create_time() is already cached
-rw-r--r--psutil/_psutil_windows.c50
-rw-r--r--psutil/_pswindows.py28
2 files changed, 20 insertions, 58 deletions
diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c
index 64592103..109ed6c5 100644
--- a/psutil/_psutil_windows.c
+++ b/psutil/_psutil_windows.c
@@ -265,7 +265,7 @@ psutil_proc_wait(PyObject *self, PyObject *args) {
* Return a Python tuple (user_time, kernel_time)
*/
static PyObject *
-psutil_proc_cpu_times(PyObject *self, PyObject *args) {
+psutil_proc_times(PyObject *self, PyObject *args) {
DWORD pid;
HANDLE hProcess;
FILETIME ftCreate, ftExit, ftKernel, ftUser;
@@ -302,54 +302,17 @@ psutil_proc_cpu_times(PyObject *self, PyObject *args) {
* below from Python's Modules/posixmodule.c
*/
return Py_BuildValue(
- "(dd)",
+ "(ddd)",
(double)(ftUser.dwHighDateTime * HI_T + \
ftUser.dwLowDateTime * LO_T),
(double)(ftKernel.dwHighDateTime * HI_T + \
- ftKernel.dwLowDateTime * LO_T)
+ ftKernel.dwLowDateTime * LO_T),
+ psutil_FiletimeToUnixTime(ftCreate)
);
}
/*
- * Return a Python float indicating the process create time expressed in
- * seconds since the epoch.
- */
-static PyObject *
-psutil_proc_create_time(PyObject *self, PyObject *args) {
- DWORD pid;
- HANDLE hProcess;
- FILETIME ftCreate, ftExit, ftKernel, ftUser;
-
- if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid))
- return NULL;
-
- // special case for PIDs 0 and 4, return system boot time
- if (0 == pid || 4 == pid)
- return psutil_boot_time(NULL, NULL);
-
- hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION);
- if (hProcess == NULL)
- return NULL;
- if (! GetProcessTimes(hProcess, &ftCreate, &ftExit, &ftKernel, &ftUser)) {
- if (GetLastError() == ERROR_ACCESS_DENIED) {
- // usually means the process has died so we throw a
- // NoSuchProcess here
- NoSuchProcess("GetProcessTimes");
- }
- else {
- PyErr_SetFromWindowsErr(0);
- }
- CloseHandle(hProcess);
- return NULL;
- }
-
- CloseHandle(hProcess);
- return Py_BuildValue("d", psutil_FiletimeToUnixTime(ftCreate));
-}
-
-
-/*
* Return process cmdline as a Python list of cmdline arguments.
*/
static PyObject *
@@ -1573,11 +1536,8 @@ PsutilMethods[] = {
"Return path of the process executable"},
{"proc_kill", psutil_proc_kill, METH_VARARGS,
"Kill the process identified by the given PID"},
- {"proc_cpu_times", psutil_proc_cpu_times, METH_VARARGS,
+ {"proc_times", psutil_proc_times, METH_VARARGS,
"Return tuple of user/kern time for the given PID"},
- {"proc_create_time", psutil_proc_create_time, METH_VARARGS,
- "Return a float indicating the process create time expressed in "
- "seconds since the epoch"},
{"proc_memory_info", psutil_proc_memory_info, METH_VARARGS,
"Return a tuple of process memory information"},
{"proc_memory_uss", psutil_proc_memory_uss, METH_VARARGS,
diff --git a/psutil/_pswindows.py b/psutil/_pswindows.py
index d8abf2e6..99d5d714 100644
--- a/psutil/_pswindows.py
+++ b/psutil/_pswindows.py
@@ -721,16 +721,15 @@ class Process(object):
# --- oneshot() stuff
def oneshot_enter(self):
- self.oneshot_info.cache_activate(self)
+ self._proc_info.cache_activate(self)
self.exe.cache_activate(self)
def oneshot_exit(self):
- self.oneshot_info.cache_deactivate(self)
+ self._proc_info.cache_deactivate(self)
self.exe.cache_deactivate(self)
- @wrap_exceptions
@memoize_when_activated
- def oneshot_info(self):
+ def _proc_info(self):
"""Return multiple information about this process as a
raw tuple.
"""
@@ -812,7 +811,7 @@ class Process(object):
if is_permission_err(err):
# TODO: the C ext can probably be refactored in order
# to get this from cext.proc_info()
- info = self.oneshot_info()
+ info = self._proc_info()
return (
info[pinfo_map['num_page_faults']],
info[pinfo_map['peak_wset']],
@@ -928,16 +927,19 @@ class Process(object):
@wrap_exceptions
def create_time(self):
+ # Note: proc_times() not put under oneshot() 'cause create_time()
+ # is already cached by the main Process class.
try:
- return cext.proc_create_time(self.pid)
+ user, system, created = cext.proc_times(self.pid)
+ return created
except OSError as err:
if is_permission_err(err):
- return self.oneshot_info()[pinfo_map['create_time']]
+ return self._proc_info()[pinfo_map['create_time']]
raise
@wrap_exceptions
def num_threads(self):
- return self.oneshot_info()[pinfo_map['num_threads']]
+ return self._proc_info()[pinfo_map['num_threads']]
@wrap_exceptions
def threads(self):
@@ -951,11 +953,11 @@ class Process(object):
@wrap_exceptions
def cpu_times(self):
try:
- user, system = cext.proc_cpu_times(self.pid)
+ user, system, created = cext.proc_times(self.pid)
except OSError as err:
if not is_permission_err(err):
raise
- info = self.oneshot_info()
+ info = self._proc_info()
user = info[pinfo_map['user_time']]
system = info[pinfo_map['kernel_time']]
# Children user/system times are not retrievable (set to 0).
@@ -1036,7 +1038,7 @@ class Process(object):
except OSError as err:
if not is_permission_err(err):
raise
- info = self.oneshot_info()
+ info = self._proc_info()
ret = (
info[pinfo_map['io_rcount']],
info[pinfo_map['io_wcount']],
@@ -1093,11 +1095,11 @@ class Process(object):
return cext.proc_num_handles(self.pid)
except OSError as err:
if is_permission_err(err):
- return self.oneshot_info()[pinfo_map['num_handles']]
+ return self._proc_info()[pinfo_map['num_handles']]
raise
@wrap_exceptions
def num_ctx_switches(self):
- ctx_switches = self.oneshot_info()[pinfo_map['ctx_switches']]
+ ctx_switches = self._proc_info()[pinfo_map['ctx_switches']]
# only voluntary ctx switches are supported
return _common.pctxsw(ctx_switches, 0)