2003-11-12 00:52:45 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
2005-05-16 07:52:13 +02:00
|
|
|
* getrusage.c
|
|
|
|
* get information about resource utilisation
|
2003-11-12 00:52:45 +01:00
|
|
|
*
|
2020-01-01 18:21:45 +01:00
|
|
|
* Portions Copyright (c) 1996-2020, 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
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1997-03-25 08:54:53 +01:00
|
|
|
|
2004-06-24 20:53:48 +02:00
|
|
|
#include "c.h"
|
2005-07-28 06:03:14 +02:00
|
|
|
|
1997-03-25 08:54:53 +01:00
|
|
|
#include "rusagestub.h"
|
|
|
|
|
1998-07-13 18:39:07 +02:00
|
|
|
/* This code works on:
|
2012-05-02 06:29:17 +02:00
|
|
|
* solaris_i386
|
1998-07-13 18:39:07 +02:00
|
|
|
* solaris_sparc
|
|
|
|
* hpux 9.*
|
2004-09-02 19:55:16 +02:00
|
|
|
* win32
|
1998-07-13 18:39:07 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1997-03-25 08:54:53 +01:00
|
|
|
int
|
2017-06-21 20:39:04 +02:00
|
|
|
getrusage(int who, struct rusage *rusage)
|
1997-03-25 08:54:53 +01:00
|
|
|
{
|
2003-05-15 18:35:30 +02:00
|
|
|
#ifdef WIN32
|
2005-10-15 04:49:52 +02:00
|
|
|
FILETIME starttime;
|
|
|
|
FILETIME exittime;
|
|
|
|
FILETIME kerneltime;
|
|
|
|
FILETIME usertime;
|
2004-09-02 19:55:16 +02:00
|
|
|
ULARGE_INTEGER li;
|
|
|
|
|
2008-05-14 09:28:13 +02:00
|
|
|
if (who != RUSAGE_SELF)
|
|
|
|
{
|
|
|
|
/* Only RUSAGE_SELF is supported in this implementation for now */
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2009-06-11 16:49:15 +02:00
|
|
|
|
2005-10-15 04:49:52 +02:00
|
|
|
if (rusage == (struct rusage *) NULL)
|
2004-09-02 19:55:16 +02:00
|
|
|
{
|
|
|
|
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;
|
2004-09-02 19:55:16 +02:00
|
|
|
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;
|
2004-09-02 19:55:16 +02:00
|
|
|
rusage->ru_utime.tv_usec = li.QuadPart % 1000000L;
|
2005-10-15 04:49:52 +02:00
|
|
|
#else /* all but WIN32 */
|
2004-09-02 19:55:16 +02:00
|
|
|
|
1997-09-08 04:41:22 +02:00
|
|
|
struct tms tms;
|
1998-02-26 05:46:47 +01:00
|
|
|
int tick_rate = CLK_TCK; /* ticks per second */
|
1997-09-08 04:41:22 +02:00
|
|
|
clock_t u,
|
|
|
|
s;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
if (rusage == (struct rusage *) NULL)
|
|
|
|
{
|
|
|
|
errno = EFAULT;
|
1998-09-01 05:29:17 +02:00
|
|
|
return -1;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
if (times(&tms) < 0)
|
|
|
|
{
|
|
|
|
/* errno set by times */
|
1998-09-01 05:29:17 +02:00
|
|
|
return -1;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
switch (who)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
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;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1997-03-25 08:54:53 +01:00
|
|
|
#define TICK_TO_SEC(T, RATE) ((T)/(RATE))
|
1997-09-07 07:04:48 +02:00
|
|
|
#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 */
|
2004-09-02 19:55:16 +02:00
|
|
|
|
1998-09-01 05:29:17 +02:00
|
|
|
return 0;
|
1997-03-25 08:54:53 +01:00
|
|
|
}
|