getrusage.c 1.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*-------------------------------------------------------------------------
 *
 * getusage.c
 *	  64-bit versions of fseeko/ftello()
 *
 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
 *	  $Header: /cvsroot/pgsql/src/port/getrusage.c,v 1.3 2003/11/11 23:52:45 momjian Exp $
 *
 *-------------------------------------------------------------------------
 */
15

Bruce Momjian's avatar
Bruce Momjian committed
16
#include <stdio.h>
17
#include <errno.h>
18 19

#include "postgres.h"
20 21
#include "rusagestub.h"

22 23 24 25 26 27 28 29 30 31 32 33
/* 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.
 */

34
int
35
getrusage(int who, struct rusage * rusage)
36
{
37 38 39 40
#ifdef WIN32
	if (rusage)
		memset(rusage, 0, sizeof(rusage));
#else
41
	struct tms	tms;
42
	int			tick_rate = CLK_TCK;	/* ticks per second */
43 44
	clock_t		u,
				s;
45 46 47 48

	if (rusage == (struct rusage *) NULL)
	{
		errno = EFAULT;
49
		return -1;
50 51 52 53
	}
	if (times(&tms) < 0)
	{
		/* errno set by times */
54
		return -1;
55 56 57
	}
	switch (who)
	{
58 59 60 61 62 63 64 65 66 67
		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;
68
			return -1;
69
	}
70
#define TICK_TO_SEC(T, RATE)	((T)/(RATE))
71 72 73 74 75
#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);
76
#endif
77
	return 0;
78
}