From 9181e1363f09f80ba822175886450b751e408f14 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Mon, 6 Apr 2020 09:03:52 +0100 Subject: time: WIN32 port Port the time applet to WIN32. This requires the implemntation of a replacement for wait3(2). Only elapsed, user and system times are supported, not the memory and i/o statistics reported by GNU time. --- win32/process.c | 41 ++++++++++++++++++++++++++++++++++------- win32/sys/resource.h | 11 +++++++++++ 2 files changed, 45 insertions(+), 7 deletions(-) (limited to 'win32') diff --git a/win32/process.c b/win32/process.c index 67ce3c100..28df33c23 100644 --- a/win32/process.c +++ b/win32/process.c @@ -3,21 +3,48 @@ #include #include "lazyload.h" -int waitpid(pid_t pid, int *status, int options) +pid_t waitpid(pid_t pid, int *status, int options) +#if ENABLE_TIME +{ + return mingw_wait3(pid, status, options, NULL); +} +#endif + +#if ENABLE_TIME +pid_t mingw_wait3(pid_t pid, int *status, int options, struct rusage *rusage) +#endif { HANDLE proc; - intptr_t ret; + DWORD code; /* Windows does not understand parent-child */ if (pid > 0 && options == 0) { if ( (proc=OpenProcess(SYNCHRONIZE|PROCESS_QUERY_INFORMATION, FALSE, pid)) != NULL ) { - ret = _cwait(status, (intptr_t)proc, 0); - CloseHandle(proc); - if (ret == -1) { - return -1; + WaitForSingleObject(proc, INFINITE); + GetExitCodeProcess(proc, &code); +#if ENABLE_TIME + if (rusage != NULL) { + FILETIME crTime, exTime, keTime, usTime; + + memset(rusage, 0, sizeof(*rusage)); + if (GetProcessTimes(proc, &crTime, &exTime, &keTime, &usTime)) { + uint64_t kernel_usec = + (((uint64_t)keTime.dwHighDateTime << 32) + | (uint64_t)keTime.dwLowDateTime)/10; + uint64_t user_usec = + (((uint64_t)usTime.dwHighDateTime << 32) + | (uint64_t)usTime.dwLowDateTime)/10; + + rusage->ru_utime.tv_sec = user_usec / 1000000U; + rusage->ru_utime.tv_usec = user_usec % 1000000U; + rusage->ru_stime.tv_sec = kernel_usec / 1000000U; + rusage->ru_stime.tv_usec = kernel_usec % 1000000U; + } } - *status <<= 8; +#endif + CloseHandle(proc); + *status = code << 8; return pid; } } diff --git a/win32/sys/resource.h b/win32/sys/resource.h index e69de29bb..3220d8112 100644 --- a/win32/sys/resource.h +++ b/win32/sys/resource.h @@ -0,0 +1,11 @@ +#ifndef _SYS_RESOURCE_H +#define _SYS_RESOURCE_H 1 + +#include + +struct rusage { + struct timeval ru_utime; + struct timeval ru_stime; +}; + +#endif -- cgit v1.2.3-55-g6feb