PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
rusagestub.h File Reference
#include <sys/time.h>
#include <sys/times.h>
#include <limits.h>
Include dependency graph for rusagestub.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  rusage
 

Macros

#define RUSAGE_SELF   0
 
#define RUSAGE_CHILDREN   (-1)
 

Functions

int getrusage (int who, struct rusage *rusage)
 

Macro Definition Documentation

#define RUSAGE_CHILDREN   (-1)

Definition at line 24 of file rusagestub.h.

Referenced by getrusage().

#define RUSAGE_SELF   0

Definition at line 23 of file rusagestub.h.

Referenced by getrusage(), pg_rusage_init(), ResetUsage(), and ShowUsage().

Function Documentation

int getrusage ( int  who,
struct rusage rusage 
)

Definition at line 31 of file getrusage.c.

References _dosmaperr(), NULL, rusage::ru_stime, rusage::ru_utime, RUSAGE_CHILDREN, RUSAGE_SELF, TICK_TO_SEC, TICK_TO_USEC, and times.

Referenced by pg_rusage_init(), ResetUsage(), and ShowUsage().

32 {
33 #ifdef WIN32
34  FILETIME starttime;
35  FILETIME exittime;
36  FILETIME kerneltime;
37  FILETIME usertime;
38  ULARGE_INTEGER li;
39 
40  if (who != RUSAGE_SELF)
41  {
42  /* Only RUSAGE_SELF is supported in this implementation for now */
43  errno = EINVAL;
44  return -1;
45  }
46 
47  if (rusage == (struct rusage *) NULL)
48  {
49  errno = EFAULT;
50  return -1;
51  }
52  memset(rusage, 0, sizeof(struct rusage));
53  if (GetProcessTimes(GetCurrentProcess(),
54  &starttime, &exittime, &kerneltime, &usertime) == 0)
55  {
56  _dosmaperr(GetLastError());
57  return -1;
58  }
59 
60  /* Convert FILETIMEs (0.1 us) to struct timeval */
61  memcpy(&li, &kerneltime, sizeof(FILETIME));
62  li.QuadPart /= 10L; /* Convert to microseconds */
63  rusage->ru_stime.tv_sec = li.QuadPart / 1000000L;
64  rusage->ru_stime.tv_usec = li.QuadPart % 1000000L;
65 
66  memcpy(&li, &usertime, sizeof(FILETIME));
67  li.QuadPart /= 10L; /* Convert to microseconds */
68  rusage->ru_utime.tv_sec = li.QuadPart / 1000000L;
69  rusage->ru_utime.tv_usec = li.QuadPart % 1000000L;
70 #else /* all but WIN32 */
71 
72  struct tms tms;
73  int tick_rate = CLK_TCK; /* ticks per second */
74  clock_t u,
75  s;
76 
77  if (rusage == (struct rusage *) NULL)
78  {
79  errno = EFAULT;
80  return -1;
81  }
82  if (times(&tms) < 0)
83  {
84  /* errno set by times */
85  return -1;
86  }
87  switch (who)
88  {
89  case RUSAGE_SELF:
90  u = tms.tms_utime;
91  s = tms.tms_stime;
92  break;
93  case RUSAGE_CHILDREN:
94  u = tms.tms_cutime;
95  s = tms.tms_cstime;
96  break;
97  default:
98  errno = EINVAL;
99  return -1;
100  }
101 #define TICK_TO_SEC(T, RATE) ((T)/(RATE))
102 #define TICK_TO_USEC(T,RATE) (((T)%(RATE)*1000000)/RATE)
103  rusage->ru_utime.tv_sec = TICK_TO_SEC(u, tick_rate);
104  rusage->ru_utime.tv_usec = TICK_TO_USEC(u, tick_rate);
105  rusage->ru_stime.tv_sec = TICK_TO_SEC(s, tick_rate);
106  rusage->ru_stime.tv_usec = TICK_TO_USEC(u, tick_rate);
107 #endif /* WIN32 */
108 
109  return 0;
110 }
#define RUSAGE_CHILDREN
Definition: rusagestub.h:24
struct timeval ru_stime
Definition: rusagestub.h:29
static char * times[]
#define TICK_TO_USEC(T, RATE)
struct timeval ru_utime
Definition: rusagestub.h:28
#define RUSAGE_SELF
Definition: rusagestub.h:23
#define NULL
Definition: c.h:229
void _dosmaperr(unsigned long)
Definition: win32error.c:171
#define TICK_TO_SEC(T, RATE)