xref: /original-bsd/lib/libc/gen/clock.c (revision bac379f5)
1fa1ffadaSbostic /*
2*bac379f5Sbostic  * Copyright (c) 1989, 1993
3*bac379f5Sbostic  *	The Regents of the University of California.  All rights reserved.
4fa1ffadaSbostic  *
5be25273bSbostic  * %sccs.include.redist.c%
6fa1ffadaSbostic  */
7fa1ffadaSbostic 
8fa1ffadaSbostic #if defined(LIBC_SCCS) && !defined(lint)
9*bac379f5Sbostic static char sccsid[] = "@(#)clock.c	8.1 (Berkeley) 06/04/93";
10fa1ffadaSbostic #endif /* LIBC_SCCS and not lint */
11fa1ffadaSbostic 
128f534921Sbostic #include <sys/param.h>
13fa1ffadaSbostic #include <sys/time.h>
14fa1ffadaSbostic #include <sys/resource.h>
15fa1ffadaSbostic 
165f54487bSkarels /*
175f54487bSkarels  * Convert usec to clock ticks; could do (usec * CLK_TCK) / 1000000,
185f54487bSkarels  * but this would overflow if we switch to nanosec.
195f54487bSkarels  */
208f534921Sbostic #define	CONVTCK(r)	(r.tv_sec * CLK_TCK + r.tv_usec / (1000000 / CLK_TCK))
218f534921Sbostic 
228f534921Sbostic clock_t
clock()238f534921Sbostic clock()
248f534921Sbostic {
258f534921Sbostic 	struct rusage ru;
268f534921Sbostic 
278f534921Sbostic 	if (getrusage(RUSAGE_SELF, &ru))
288f534921Sbostic 		return ((clock_t) -1);
298f534921Sbostic 	return((clock_t)((CONVTCK(ru.ru_utime) + CONVTCK(ru.ru_stime))));
30fa1ffadaSbostic }
31