xref: /csrg-svn/lib/libc/gen/clock.c (revision 42195)
137124Sbostic /*
237124Sbostic  * Copyright (c) 1989 The Regents of the University of California.
337124Sbostic  * All rights reserved.
437124Sbostic  *
537124Sbostic  * Redistribution and use in source and binary forms are permitted
637124Sbostic  * provided that the above copyright notice and this paragraph are
737124Sbostic  * duplicated in all such forms and that any documentation,
837124Sbostic  * advertising materials, and other materials related to such
937124Sbostic  * distribution and use acknowledge that the software was developed
1037124Sbostic  * by the University of California, Berkeley.  The name of the
1137124Sbostic  * University may not be used to endorse or promote products derived
1237124Sbostic  * from this software without specific prior written permission.
1337124Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1437124Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1537124Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1637124Sbostic  */
1737124Sbostic 
1837124Sbostic #if defined(LIBC_SCCS) && !defined(lint)
19*42195Skarels static char sccsid[] = "@(#)clock.c	5.2 (Berkeley) 05/17/90";
2037124Sbostic #endif /* LIBC_SCCS and not lint */
2137124Sbostic 
2237124Sbostic #include <machine/machlimits.h>
2337124Sbostic #include <sys/types.h>
2437124Sbostic #include <sys/time.h>
2537124Sbostic #include <sys/resource.h>
2637124Sbostic 
2737124Sbostic clock_t
2837124Sbostic clock()
2937124Sbostic {
3037124Sbostic 	struct rusage rusage;
3137124Sbostic 	clock_t val;
3237124Sbostic 
3337124Sbostic 	if (getrusage(RUSAGE_SELF, &rusage))
34*42195Skarels 		return ((clock_t) -1);
35*42195Skarels 	val = (rusage.ru_utime.tv_sec + rusage.ru_stime.tv_sec) * CLK_TCK;
36*42195Skarels 	/*
37*42195Skarels 	 * Convert usec to clock ticks; could do (usec * CLK_TCK) / 1000000,
38*42195Skarels 	 * but this would overflow if we switch to nanosec.
39*42195Skarels 	 */
40*42195Skarels 	val += (rusage.ru_utime.tv_usec + rusage.ru_stime.tv_usec) /
41*42195Skarels 		(1000000 / CLK_TCK);
42*42195Skarels 	return (val);
4337124Sbostic }
44