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 13:35:39 -0800
commitd6633684407c33673dcdb90067010c8e36361747 (patch)
treebee5808d0107acf702ec92bf407164b5ab5146e3
parentb0cff82c6b91f99d2962f10d8d61d64853296e36 (diff)
downloadpsutil-d6633684407c33673dcdb90067010c8e36361747.tar.gz
refactoring: get rid of duplicated code; use one function to return (user, sys, create time)
-rw-r--r--psutil/_psutil_windows.c50
-rw-r--r--psutil/_pswindows.py5
2 files changed, 8 insertions, 47 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..0cdea9d1 100644
--- a/psutil/_pswindows.py
+++ b/psutil/_pswindows.py
@@ -929,7 +929,8 @@ class Process(object):
@wrap_exceptions
def create_time(self):
try:
- return cext.proc_create_time(self.pid)
+ user, kernel, 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']]
@@ -951,7 +952,7 @@ 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