xref: /csrg-svn/lib/libc/gen/clock.c (revision 37124)
1*37124Sbostic /*
2*37124Sbostic  * Copyright (c) 1989 The Regents of the University of California.
3*37124Sbostic  * All rights reserved.
4*37124Sbostic  *
5*37124Sbostic  * Redistribution and use in source and binary forms are permitted
6*37124Sbostic  * provided that the above copyright notice and this paragraph are
7*37124Sbostic  * duplicated in all such forms and that any documentation,
8*37124Sbostic  * advertising materials, and other materials related to such
9*37124Sbostic  * distribution and use acknowledge that the software was developed
10*37124Sbostic  * by the University of California, Berkeley.  The name of the
11*37124Sbostic  * University may not be used to endorse or promote products derived
12*37124Sbostic  * from this software without specific prior written permission.
13*37124Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*37124Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*37124Sbostic  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16*37124Sbostic  */
17*37124Sbostic 
18*37124Sbostic #if defined(LIBC_SCCS) && !defined(lint)
19*37124Sbostic static char sccsid[] = "@(#)clock.c	5.1 (Berkeley) 03/09/89";
20*37124Sbostic #endif /* LIBC_SCCS and not lint */
21*37124Sbostic 
22*37124Sbostic #include <machine/machlimits.h>
23*37124Sbostic #include <sys/types.h>
24*37124Sbostic #include <sys/time.h>
25*37124Sbostic #include <sys/resource.h>
26*37124Sbostic 
27*37124Sbostic clock_t
28*37124Sbostic clock()
29*37124Sbostic {
30*37124Sbostic 	struct rusage rusage;
31*37124Sbostic 	clock_t val;
32*37124Sbostic 
33*37124Sbostic 	if (getrusage(RUSAGE_SELF, &rusage))
34*37124Sbostic 		return((clock_t)-1);
35*37124Sbostic 	val = rusage.ru_utime.tv_sec * CLK_TCK;
36*37124Sbostic 	val += rusage.ru_stime.tv_sec * CLK_TCK;
37*37124Sbostic 	val += rusage.ru_utime.tv_usec / (1000000/CLK_TCK);
38*37124Sbostic 	val += rusage.ru_stime.tv_usec / (1000000/CLK_TCK);
39*37124Sbostic 	val += (rusage.ru_utime.tv_usec % (1000000/CLK_TCK) +
40*37124Sbostic 	    rusage.ru_utime.tv_usec % (1000000/CLK_TCK)) / (1000000/CLK_TCK);
41*37124Sbostic 	return(val);
42*37124Sbostic }
43