Fix xslt_process() to ensure that it inserts a NULL terminator after the
[PostgreSQL.git] / src / port / getrusage.c
blob8e18412f8664df054e2c80a138c892a0dca7710d
1 /*-------------------------------------------------------------------------
3 * getrusage.c
4 * get information about resource utilisation
6 * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * IDENTIFICATION
11 * $PostgreSQL$
13 *-------------------------------------------------------------------------
16 #include "c.h"
18 #include "rusagestub.h"
20 /* This code works on:
21 * univel
22 * solaris_i386
23 * sco
24 * solaris_sparc
25 * svr4
26 * hpux 9.*
27 * win32
28 * which currently is all the supported platforms that don't have a
29 * native version of getrusage(). So, if configure decides to compile
30 * this file at all, we just use this version unconditionally.
33 int
34 getrusage(int who, struct rusage * rusage)
36 #ifdef WIN32
38 FILETIME starttime;
39 FILETIME exittime;
40 FILETIME kerneltime;
41 FILETIME usertime;
42 ULARGE_INTEGER li;
44 if (who != RUSAGE_SELF)
46 /* Only RUSAGE_SELF is supported in this implementation for now */
47 errno = EINVAL;
48 return -1;
51 if (rusage == (struct rusage *) NULL)
53 errno = EFAULT;
54 return -1;
56 memset(rusage, 0, sizeof(struct rusage));
57 if (GetProcessTimes(GetCurrentProcess(),
58 &starttime, &exittime, &kerneltime, &usertime) == 0)
60 _dosmaperr(GetLastError());
61 return -1;
64 /* Convert FILETIMEs (0.1 us) to struct timeval */
65 memcpy(&li, &kerneltime, sizeof(FILETIME));
66 li.QuadPart /= 10L; /* Convert to microseconds */
67 rusage->ru_stime.tv_sec = li.QuadPart / 1000000L;
68 rusage->ru_stime.tv_usec = li.QuadPart % 1000000L;
70 memcpy(&li, &usertime, sizeof(FILETIME));
71 li.QuadPart /= 10L; /* Convert to microseconds */
72 rusage->ru_utime.tv_sec = li.QuadPart / 1000000L;
73 rusage->ru_utime.tv_usec = li.QuadPart % 1000000L;
74 #else /* all but WIN32 */
76 struct tms tms;
77 int tick_rate = CLK_TCK; /* ticks per second */
78 clock_t u,
81 if (rusage == (struct rusage *) NULL)
83 errno = EFAULT;
84 return -1;
86 if (times(&tms) < 0)
88 /* errno set by times */
89 return -1;
91 switch (who)
93 case RUSAGE_SELF:
94 u = tms.tms_utime;
95 s = tms.tms_stime;
96 break;
97 case RUSAGE_CHILDREN:
98 u = tms.tms_cutime;
99 s = tms.tms_cstime;
100 break;
101 default:
102 errno = EINVAL;
103 return -1;
105 #define TICK_TO_SEC(T, RATE) ((T)/(RATE))
106 #define TICK_TO_USEC(T,RATE) (((T)%(RATE)*1000000)/RATE)
107 rusage->ru_utime.tv_sec = TICK_TO_SEC(u, tick_rate);
108 rusage->ru_utime.tv_usec = TICK_TO_USEC(u, tick_rate);
109 rusage->ru_stime.tv_sec = TICK_TO_SEC(s, tick_rate);
110 rusage->ru_stime.tv_usec = TICK_TO_USEC(u, tick_rate);
111 #endif /* WIN32 */
113 return 0;