1 /*-------------------------------------------------------------------------
4 * get information about resource utilisation
6 * Portions Copyright (c) 1996-2014, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/port/getrusage.c
13 *-------------------------------------------------------------------------
18 #include "rusagestub.h"
20 /* This code works on:
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.
32 getrusage(int who, struct rusage * rusage)
41 if (who != RUSAGE_SELF)
43 /* Only RUSAGE_SELF is supported in this implementation for now */
48 if (rusage == (struct rusage *) NULL)
53 memset(rusage, 0, sizeof(struct rusage));
54 if (GetProcessTimes(GetCurrentProcess(),
55 &starttime, &exittime, &kerneltime, &usertime) == 0)
57 _dosmaperr(GetLastError());
61 /* Convert FILETIMEs (0.1 us) to struct timeval */
62 memcpy(&li, &kerneltime, sizeof(FILETIME));
63 li.QuadPart /= 10L; /* Convert to microseconds */
64 rusage->ru_stime.tv_sec = li.QuadPart / 1000000L;
65 rusage->ru_stime.tv_usec = li.QuadPart % 1000000L;
67 memcpy(&li, &usertime, sizeof(FILETIME));
68 li.QuadPart /= 10L; /* Convert to microseconds */
69 rusage->ru_utime.tv_sec = li.QuadPart / 1000000L;
70 rusage->ru_utime.tv_usec = li.QuadPart % 1000000L;
71 #else /* all but WIN32 */
74 int tick_rate = CLK_TCK; /* ticks per second */
78 if (rusage == (struct rusage *) NULL)
85 /* errno set by times */
102 #define TICK_TO_SEC(T, RATE) ((T)/(RATE))
103 #define TICK_TO_USEC(T,RATE) (((T)%(RATE)*1000000)/RATE)
104 rusage->ru_utime.tv_sec = TICK_TO_SEC(u, tick_rate);
105 rusage->ru_utime.tv_usec = TICK_TO_USEC(u, tick_rate);
106 rusage->ru_stime.tv_sec = TICK_TO_SEC(s, tick_rate);
107 rusage->ru_stime.tv_usec = TICK_TO_USEC(u, tick_rate);