1998-09-01 05:29:17 +02:00
|
|
|
/* $Id: getrusage.c,v 1.10 1998/09/01 03:24:26 momjian Exp $ */
|
1997-03-25 08:54:53 +01:00
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include "rusagestub.h"
|
|
|
|
|
1998-07-13 18:39:07 +02:00
|
|
|
/* This code works on:
|
|
|
|
* univel
|
|
|
|
* solaris_i386
|
|
|
|
* sco
|
|
|
|
* solaris_sparc
|
|
|
|
* svr4
|
|
|
|
* hpux 9.*
|
|
|
|
* 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
|
1997-09-07 07:04:48 +02:00
|
|
|
getrusage(int who, struct rusage * rusage)
|
1997-03-25 08:54:53 +01: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);
|
1998-09-01 05:29:17 +02:00
|
|
|
return 0;
|
1997-03-25 08:54:53 +01:00
|
|
|
}
|