]> granicus.if.org Git - postgresql/blob - src/port/getrusage.c
Centralize single quote escaping in src/port/quotes.c
[postgresql] / src / port / getrusage.c
1 /*-------------------------------------------------------------------------
2  *
3  * getrusage.c
4  *        get information about resource utilisation
5  *
6  * Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  *        src/port/getrusage.c
12  *
13  *-------------------------------------------------------------------------
14  */
15
16 #include "c.h"
17
18 #include "rusagestub.h"
19
20 /* This code works on:
21  *              sco
22  *              solaris_i386
23  *              solaris_sparc
24  *              hpux 9.*
25  *              win32
26  * which currently is all the supported platforms that don't have a
27  * native version of getrusage().  So, if configure decides to compile
28  * this file at all, we just use this version unconditionally.
29  */
30
31 int
32 getrusage(int who, struct rusage * rusage)
33 {
34 #ifdef WIN32
35
36         FILETIME        starttime;
37         FILETIME        exittime;
38         FILETIME        kerneltime;
39         FILETIME        usertime;
40         ULARGE_INTEGER li;
41
42         if (who != RUSAGE_SELF)
43         {
44                 /* Only RUSAGE_SELF is supported in this implementation for now */
45                 errno = EINVAL;
46                 return -1;
47         }
48
49         if (rusage == (struct rusage *) NULL)
50         {
51                 errno = EFAULT;
52                 return -1;
53         }
54         memset(rusage, 0, sizeof(struct rusage));
55         if (GetProcessTimes(GetCurrentProcess(),
56                                                 &starttime, &exittime, &kerneltime, &usertime) == 0)
57         {
58                 _dosmaperr(GetLastError());
59                 return -1;
60         }
61
62         /* Convert FILETIMEs (0.1 us) to struct timeval */
63         memcpy(&li, &kerneltime, sizeof(FILETIME));
64         li.QuadPart /= 10L;                     /* Convert to microseconds */
65         rusage->ru_stime.tv_sec = li.QuadPart / 1000000L;
66         rusage->ru_stime.tv_usec = li.QuadPart % 1000000L;
67
68         memcpy(&li, &usertime, sizeof(FILETIME));
69         li.QuadPart /= 10L;                     /* Convert to microseconds */
70         rusage->ru_utime.tv_sec = li.QuadPart / 1000000L;
71         rusage->ru_utime.tv_usec = li.QuadPart % 1000000L;
72 #else                                                   /* all but WIN32 */
73
74         struct tms      tms;
75         int                     tick_rate = CLK_TCK;    /* ticks per second */
76         clock_t         u,
77                                 s;
78
79         if (rusage == (struct rusage *) NULL)
80         {
81                 errno = EFAULT;
82                 return -1;
83         }
84         if (times(&tms) < 0)
85         {
86                 /* errno set by times */
87                 return -1;
88         }
89         switch (who)
90         {
91                 case RUSAGE_SELF:
92                         u = tms.tms_utime;
93                         s = tms.tms_stime;
94                         break;
95                 case RUSAGE_CHILDREN:
96                         u = tms.tms_cutime;
97                         s = tms.tms_cstime;
98                         break;
99                 default:
100                         errno = EINVAL;
101                         return -1;
102         }
103 #define TICK_TO_SEC(T, RATE)    ((T)/(RATE))
104 #define TICK_TO_USEC(T,RATE)    (((T)%(RATE)*1000000)/RATE)
105         rusage->ru_utime.tv_sec = TICK_TO_SEC(u, tick_rate);
106         rusage->ru_utime.tv_usec = TICK_TO_USEC(u, tick_rate);
107         rusage->ru_stime.tv_sec = TICK_TO_SEC(s, tick_rate);
108         rusage->ru_stime.tv_usec = TICK_TO_USEC(u, tick_rate);
109 #endif   /* WIN32 */
110
111         return 0;
112 }