summaryrefslogtreecommitdiff
path: root/psutil/_psutil_windows.c
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 /psutil/_psutil_windows.c
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
Diffstat (limited to 'psutil/_psutil_windows.c')
-rw-r--r--psutil/_psutil_windows.c50
1 files changed, 5 insertions, 45 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,