1 /*-------------------------------------------------------------------------
4 * get information about resource utilisation
6 * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/port/getrusage.c,v 1.18 2010/01/02 16:58:13 momjian Exp $
13 *-------------------------------------------------------------------------
18 #include "rusagestub.h"
20 /* This code works on:
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.
34 getrusage(int who, struct rusage * rusage)
44 if (who != RUSAGE_SELF)
46 /* Only RUSAGE_SELF is supported in this implementation for now */
51 if (rusage == (struct rusage *) NULL)
56 memset(rusage, 0, sizeof(struct rusage));
57 if (GetProcessTimes(GetCurrentProcess(),
58 &starttime, &exittime, &kerneltime, &usertime) == 0)
60 _dosmaperr(GetLastError());
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 */
77 int tick_rate = CLK_TCK; /* ticks per second */
81 if (rusage == (struct rusage *) NULL)
88 /* errno set by times */
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);