Commit 1e4c33b1 authored by Tom Lane's avatar Tom Lane

Fix getrusage() emulation on Windows. Magnus Hagander

parent 3cc21343
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/port/getrusage.c,v 1.6 2004/08/29 04:13:12 momjian Exp $ * $PostgreSQL: pgsql/src/port/getrusage.c,v 1.7 2004/09/02 17:55:16 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -26,6 +26,7 @@ ...@@ -26,6 +26,7 @@
* solaris_sparc * solaris_sparc
* svr4 * svr4
* hpux 9.* * hpux 9.*
* win32
* which currently is all the supported platforms that don't have a * which currently is all the supported platforms that don't have a
* native version of getrusage(). So, if configure decides to compile * native version of getrusage(). So, if configure decides to compile
* this file at all, we just use this version unconditionally. * this file at all, we just use this version unconditionally.
...@@ -35,9 +36,39 @@ int ...@@ -35,9 +36,39 @@ int
getrusage(int who, struct rusage * rusage) getrusage(int who, struct rusage * rusage)
{ {
#ifdef WIN32 #ifdef WIN32
if (rusage)
memset(rusage, 0, sizeof(rusage)); FILETIME starttime;
#else FILETIME exittime;
FILETIME kerneltime;
FILETIME usertime;
ULARGE_INTEGER li;
if (rusage == (struct rusage *)NULL)
{
errno = EFAULT;
return -1;
}
memset(rusage, 0, sizeof(struct rusage));
if (GetProcessTimes(GetCurrentProcess(),
&starttime, &exittime, &kerneltime, &usertime) == 0)
{
_dosmaperr(GetLastError());
return -1;
}
/* Convert FILETIMEs (0.1 us) to struct timeval */
memcpy(&li, &kerneltime, sizeof(FILETIME));
li.QuadPart /= 10L; /* Convert to microseconds */
rusage->ru_stime.tv_sec = li.QuadPart / 1000000L;
rusage->ru_stime.tv_usec = li.QuadPart % 1000000L;
memcpy(&li, &usertime, sizeof(FILETIME));
li.QuadPart /= 10L; /* Convert to microseconds */
rusage->ru_utime.tv_sec = li.QuadPart / 1000000L;
rusage->ru_utime.tv_usec = li.QuadPart % 1000000L;
#else /* all but WIN32 */
struct tms tms; struct tms tms;
int tick_rate = CLK_TCK; /* ticks per second */ int tick_rate = CLK_TCK; /* ticks per second */
clock_t u, clock_t u,
...@@ -73,6 +104,8 @@ getrusage(int who, struct rusage * rusage) ...@@ -73,6 +104,8 @@ getrusage(int who, struct rusage * rusage)
rusage->ru_utime.tv_usec = TICK_TO_USEC(u, tick_rate); rusage->ru_utime.tv_usec = TICK_TO_USEC(u, tick_rate);
rusage->ru_stime.tv_sec = TICK_TO_SEC(s, tick_rate); rusage->ru_stime.tv_sec = TICK_TO_SEC(s, tick_rate);
rusage->ru_stime.tv_usec = TICK_TO_USEC(u, tick_rate); rusage->ru_stime.tv_usec = TICK_TO_USEC(u, tick_rate);
#endif
#endif /* WIN32 */
return 0; return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment