xref: /original-bsd/lib/libc/gen/clock.c (revision 540a81df)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)clock.c	5.3 (Berkeley) 06/01/90";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <machine/machlimits.h>
13 #include <sys/types.h>
14 #include <sys/time.h>
15 #include <sys/resource.h>
16 
17 clock_t
18 clock()
19 {
20 	struct rusage rusage;
21 	clock_t val;
22 
23 	if (getrusage(RUSAGE_SELF, &rusage))
24 		return ((clock_t) -1);
25 	val = (rusage.ru_utime.tv_sec + rusage.ru_stime.tv_sec) * CLK_TCK;
26 	/*
27 	 * Convert usec to clock ticks; could do (usec * CLK_TCK) / 1000000,
28 	 * but this would overflow if we switch to nanosec.
29 	 */
30 	val += (rusage.ru_utime.tv_usec + rusage.ru_stime.tv_usec) /
31 		(1000000 / CLK_TCK);
32 	return (val);
33 }
34