clock.c revision 288097
1226586Sdim/* 2226586Sdim * Copyright (c) 1989, 1993 3226586Sdim * The Regents of the University of California. All rights reserved. 4226586Sdim * 5226586Sdim * Redistribution and use in source and binary forms, with or without 6226586Sdim * modification, are permitted provided that the following conditions 7226586Sdim * are met: 8226586Sdim * 1. Redistributions of source code must retain the above copyright 9226586Sdim * notice, this list of conditions and the following disclaimer. 10226586Sdim * 2. Redistributions in binary form must reproduce the above copyright 11226586Sdim * notice, this list of conditions and the following disclaimer in the 12226586Sdim * documentation and/or other materials provided with the distribution. 13226586Sdim * 4. Neither the name of the University nor the names of its contributors 14226586Sdim * may be used to endorse or promote products derived from this software 15226586Sdim * without specific prior written permission. 16226586Sdim * 17226586Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18226586Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19226586Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20226586Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21226586Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22226586Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23252723Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24252723Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25226586Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26226586Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27226586Sdim * SUCH DAMAGE. 28226586Sdim */ 29226586Sdim 30226586Sdim#if defined(LIBC_SCCS) && !defined(lint) 31226586Sdimstatic char sccsid[] = "@(#)clock.c 8.1 (Berkeley) 6/4/93"; 32226586Sdim#endif /* LIBC_SCCS and not lint */ 33226586Sdim#include <sys/cdefs.h> 34226586Sdim__FBSDID("$FreeBSD: head/lib/libc/gen/clock.c 288097 2015-09-22 07:31:40Z rodrigc $"); 35226586Sdim 36226586Sdim#include <sys/param.h> 37226586Sdim#include <sys/time.h> 38226586Sdim#include <sys/resource.h> 39226586Sdim 40226586Sdim/* 41226586Sdim * Convert usec to clock ticks; could do (usec * CLOCKS_PER_SEC) / 1000000, 42226586Sdim * but this would overflow if we switch to nanosec. 43226586Sdim */ 44226586Sdim#define CONVTCK(r) ((r).tv_sec * CLOCKS_PER_SEC \ 45226586Sdim + (r).tv_usec / (1000000 / CLOCKS_PER_SEC)) 46226586Sdim 47252723Sdimclock_t 48226586Sdimclock(void) 49226586Sdim{ 50226586Sdim struct rusage ru; 51252723Sdim 52226586Sdim if (getrusage(RUSAGE_SELF, &ru)) 53226586Sdim return ((clock_t) -1); 54226586Sdim return((clock_t)((CONVTCK(ru.ru_utime) + CONVTCK(ru.ru_stime)))); 55226586Sdim} 56226586Sdim