postgresql/src/port/getrusage.c

111 lines
2.6 KiB
C
Raw Normal View History

2003-11-12 00:52:45 +01:00
/*-------------------------------------------------------------------------
*
* getrusage.c
* get information about resource utilisation
2003-11-12 00:52:45 +01:00
*
* Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
2003-11-12 00:52:45 +01:00
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
2010-09-20 22:08:53 +02:00
* src/port/getrusage.c
2003-11-12 00:52:45 +01:00
*
*-------------------------------------------------------------------------
*/
#include "c.h"
#include "rusagestub.h"
/* This code works on:
* solaris_i386
* solaris_sparc
* hpux 9.*
* win32
* which currently is all the supported platforms that don't have a
* native version of getrusage(). So, if configure decides to compile
* this file at all, we just use this version unconditionally.
*/
int
2017-06-21 20:39:04 +02:00
getrusage(int who, struct rusage *rusage)
{
#ifdef WIN32
2005-10-15 04:49:52 +02:00
FILETIME starttime;
FILETIME exittime;
FILETIME kerneltime;
FILETIME usertime;
ULARGE_INTEGER li;
if (who != RUSAGE_SELF)
{
/* Only RUSAGE_SELF is supported in this implementation for now */
errno = EINVAL;
return -1;
}
2005-10-15 04:49:52 +02:00
if (rusage == (struct rusage *) NULL)
{
errno = EFAULT;
return -1;
}
memset(rusage, 0, sizeof(struct rusage));
if (GetProcessTimes(GetCurrentProcess(),
&starttime, &exittime, &kerneltime, &usertime) == 0)
{
_dosmaperr(GetLastError());
return -1;
}
/* Convert FILETIMEs (0.1 us) to struct timeval */
memcpy(&li, &kerneltime, sizeof(FILETIME));
2005-10-15 04:49:52 +02:00
li.QuadPart /= 10L; /* Convert to microseconds */
rusage->ru_stime.tv_sec = li.QuadPart / 1000000L;
rusage->ru_stime.tv_usec = li.QuadPart % 1000000L;
memcpy(&li, &usertime, sizeof(FILETIME));
2005-10-15 04:49:52 +02:00
li.QuadPart /= 10L; /* Convert to microseconds */
rusage->ru_utime.tv_sec = li.QuadPart / 1000000L;
rusage->ru_utime.tv_usec = li.QuadPart % 1000000L;
2005-10-15 04:49:52 +02:00
#else /* all but WIN32 */
struct tms tms;
int tick_rate = CLK_TCK; /* ticks per second */
clock_t u,
s;
if (rusage == (struct rusage *) NULL)
{
errno = EFAULT;
1998-09-01 05:29:17 +02:00
return -1;
}
if (times(&tms) < 0)
{
/* errno set by times */
1998-09-01 05:29:17 +02:00
return -1;
}
switch (who)
{
case RUSAGE_SELF:
u = tms.tms_utime;
s = tms.tms_stime;
break;
case RUSAGE_CHILDREN:
u = tms.tms_cutime;
s = tms.tms_cstime;
break;
default:
errno = EINVAL;
1998-09-01 05:29:17 +02:00
return -1;
}
#define TICK_TO_SEC(T, RATE) ((T)/(RATE))
#define TICK_TO_USEC(T,RATE) (((T)%(RATE)*1000000)/RATE)
rusage->ru_utime.tv_sec = TICK_TO_SEC(u, tick_rate);
rusage->ru_utime.tv_usec = TICK_TO_USEC(u, tick_rate);
rusage->ru_stime.tv_sec = TICK_TO_SEC(s, tick_rate);
rusage->ru_stime.tv_usec = TICK_TO_USEC(u, tick_rate);
Phase 2 of pgindent updates. Change pg_bsd_indent to follow upstream rules for placement of comments to the right of code, and remove pgindent hack that caused comments following #endif to not obey the general rule. Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using the published version of pg_bsd_indent, but a hacked-up version that tried to minimize the amount of movement of comments to the right of code. The situation of interest is where such a comment has to be moved to the right of its default placement at column 33 because there's code there. BSD indent has always moved right in units of tab stops in such cases --- but in the previous incarnation, indent was working in 8-space tab stops, while now it knows we use 4-space tabs. So the net result is that in about half the cases, such comments are placed one tab stop left of before. This is better all around: it leaves more room on the line for comment text, and it means that in such cases the comment uniformly starts at the next 4-space tab stop after the code, rather than sometimes one and sometimes two tabs after. Also, ensure that comments following #endif are indented the same as comments following other preprocessor commands such as #else. That inconsistency turns out to have been self-inflicted damage from a poorly-thought-through post-indent "fixup" in pgindent. This patch is much less interesting than the first round of indent changes, but also bulkier, so I thought it best to separate the effects. Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
#endif /* WIN32 */
1998-09-01 05:29:17 +02:00
return 0;
}