xref: /csrg-svn/lib/libc/gen/clock.c (revision 45143)
137124Sbostic /*
237124Sbostic  * Copyright (c) 1989 The Regents of the University of California.
337124Sbostic  * All rights reserved.
437124Sbostic  *
542620Sbostic  * %sccs.include.redist.c%
637124Sbostic  */
737124Sbostic 
837124Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*45143Sbostic static char sccsid[] = "@(#)clock.c	5.4 (Berkeley) 08/27/90";
1037124Sbostic #endif /* LIBC_SCCS and not lint */
1137124Sbostic 
12*45143Sbostic #include <sys/param.h>
1337124Sbostic #include <sys/time.h>
1437124Sbostic #include <sys/resource.h>
1537124Sbostic 
16*45143Sbostic /*
17*45143Sbostic  * Convert usec to clock ticks; could do (usec * CLK_TCK) / 1000000,
18*45143Sbostic  * but this would overflow if we switch to nanosec.
19*45143Sbostic  */
20*45143Sbostic #define	CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
21*45143Sbostic 
2237124Sbostic clock_t
2337124Sbostic clock()
2437124Sbostic {
25*45143Sbostic 	struct rusage ru;
2637124Sbostic 
27*45143Sbostic 	if (getrusage(RUSAGE_SELF, &ru))
2842195Skarels 		return ((clock_t) -1);
29*45143Sbostic 	return((clock_t)((CONVTCK(ru.ru_utime) + CONVTCK(ru.ru_stime))));
3037124Sbostic }
31