xref: /csrg-svn/lib/libc/gen/clock.c (revision 42620)
137124Sbostic /*
237124Sbostic  * Copyright (c) 1989 The Regents of the University of California.
337124Sbostic  * All rights reserved.
437124Sbostic  *
5*42620Sbostic  * %sccs.include.redist.c%
637124Sbostic  */
737124Sbostic 
837124Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*42620Sbostic static char sccsid[] = "@(#)clock.c	5.3 (Berkeley) 06/01/90";
1037124Sbostic #endif /* LIBC_SCCS and not lint */
1137124Sbostic 
1237124Sbostic #include <machine/machlimits.h>
1337124Sbostic #include <sys/types.h>
1437124Sbostic #include <sys/time.h>
1537124Sbostic #include <sys/resource.h>
1637124Sbostic 
1737124Sbostic clock_t
1837124Sbostic clock()
1937124Sbostic {
2037124Sbostic 	struct rusage rusage;
2137124Sbostic 	clock_t val;
2237124Sbostic 
2337124Sbostic 	if (getrusage(RUSAGE_SELF, &rusage))
2442195Skarels 		return ((clock_t) -1);
2542195Skarels 	val = (rusage.ru_utime.tv_sec + rusage.ru_stime.tv_sec) * CLK_TCK;
2642195Skarels 	/*
2742195Skarels 	 * Convert usec to clock ticks; could do (usec * CLK_TCK) / 1000000,
2842195Skarels 	 * but this would overflow if we switch to nanosec.
2942195Skarels 	 */
3042195Skarels 	val += (rusage.ru_utime.tv_usec + rusage.ru_stime.tv_usec) /
3142195Skarels 		(1000000 / CLK_TCK);
3242195Skarels 	return (val);
3337124Sbostic }
34