Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1e4c33b

Browse files
committed
Fix getrusage() emulation on Windows. Magnus Hagander
1 parent 3cc2134 commit 1e4c33b

File tree

1 file changed

+38
-5
lines changed

1 file changed

+38
-5
lines changed

src/port/getrusage.c

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/port/getrusage.c,v 1.6 2004/08/29 04:13:12 momjian Exp $
11+
* $PostgreSQL: pgsql/src/port/getrusage.c,v 1.7 2004/09/02 17:55:16 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -26,6 +26,7 @@
2626
* solaris_sparc
2727
* svr4
2828
* hpux 9.*
29+
* win32
2930
* which currently is all the supported platforms that don't have a
3031
* native version of getrusage(). So, if configure decides to compile
3132
* this file at all, we just use this version unconditionally.
@@ -35,9 +36,39 @@ int
3536
getrusage(int who, struct rusage * rusage)
3637
{
3738
#ifdef WIN32
38-
if (rusage)
39-
memset(rusage, 0, sizeof(rusage));
40-
#else
39+
40+
FILETIME starttime;
41+
FILETIME exittime;
42+
FILETIME kerneltime;
43+
FILETIME usertime;
44+
ULARGE_INTEGER li;
45+
46+
if (rusage == (struct rusage *)NULL)
47+
{
48+
errno = EFAULT;
49+
return -1;
50+
}
51+
memset(rusage, 0, sizeof(struct rusage));
52+
if (GetProcessTimes(GetCurrentProcess(),
53+
&starttime, &exittime, &kerneltime, &usertime) == 0)
54+
{
55+
_dosmaperr(GetLastError());
56+
return -1;
57+
}
58+
59+
/* Convert FILETIMEs (0.1 us) to struct timeval */
60+
memcpy(&li, &kerneltime, sizeof(FILETIME));
61+
li.QuadPart /= 10L; /* Convert to microseconds */
62+
rusage->ru_stime.tv_sec = li.QuadPart / 1000000L;
63+
rusage->ru_stime.tv_usec = li.QuadPart % 1000000L;
64+
65+
memcpy(&li, &usertime, sizeof(FILETIME));
66+
li.QuadPart /= 10L; /* Convert to microseconds */
67+
rusage->ru_utime.tv_sec = li.QuadPart / 1000000L;
68+
rusage->ru_utime.tv_usec = li.QuadPart % 1000000L;
69+
70+
#else /* all but WIN32 */
71+
4172
struct tms tms;
4273
int tick_rate = CLK_TCK; /* ticks per second */
4374
clock_t u,
@@ -73,6 +104,8 @@ getrusage(int who, struct rusage * rusage)
73104
rusage->ru_utime.tv_usec = TICK_TO_USEC(u, tick_rate);
74105
rusage->ru_stime.tv_sec = TICK_TO_SEC(s, tick_rate);
75106
rusage->ru_stime.tv_usec = TICK_TO_USEC(u, tick_rate);
76-
#endif
107+
108+
#endif /* WIN32 */
109+
77110
return 0;
78111
}

0 commit comments

Comments
 (0)