clock.c revision 165903
129088Smarkm/* 229088Smarkm * Copyright (c) 1989, 1993 329088Smarkm * The Regents of the University of California. All rights reserved. 429088Smarkm * 529088Smarkm * Redistribution and use in source and binary forms, with or without 629088Smarkm * modification, are permitted provided that the following conditions 729088Smarkm * are met: 829088Smarkm * 1. Redistributions of source code must retain the above copyright 929088Smarkm * notice, this list of conditions and the following disclaimer. 1029088Smarkm * 2. Redistributions in binary form must reproduce the above copyright 1129088Smarkm * notice, this list of conditions and the following disclaimer in the 1229088Smarkm * documentation and/or other materials provided with the distribution. 1329088Smarkm * 4. Neither the name of the University nor the names of its contributors 1429088Smarkm * may be used to endorse or promote products derived from this software 1529088Smarkm * without specific prior written permission. 1629088Smarkm * 1729088Smarkm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1829088Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1929088Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2029088Smarkm * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2129088Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2229088Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2329088Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2429088Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2529088Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2629088Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2729088Smarkm * SUCH DAMAGE. 2829088Smarkm */ 2929088Smarkm 3029088Smarkm#if defined(LIBC_SCCS) && !defined(lint) 3129088Smarkmstatic char sccsid[] = "@(#)clock.c 8.1 (Berkeley) 6/4/93"; 3229088Smarkm#endif /* LIBC_SCCS and not lint */ 3329088Smarkm#include <sys/cdefs.h> 34114630Sobrien__FBSDID("$FreeBSD: head/lib/libc/gen/clock.c 165903 2007-01-09 00:28:16Z imp $"); 3529088Smarkm 3629181Smarkm#include <sys/param.h> 3763248Speter#include <sys/time.h> 38114630Sobrien#include <sys/resource.h> 39114630Sobrien 40114630Sobrien/* 4129088Smarkm * Convert usec to clock ticks; could do (usec * CLOCKS_PER_SEC) / 1000000, 4229088Smarkm * but this would overflow if we switch to nanosec. 4329088Smarkm */ 4429088Smarkm#define CONVTCK(r) ((r).tv_sec * CLOCKS_PER_SEC \ 4529088Smarkm + (r).tv_usec / (1000000 / CLOCKS_PER_SEC)) 4629088Smarkm 47103954Smarkmclock_t 4887139Smarkmclock() 4929088Smarkm{ 50114630Sobrien struct rusage ru; 5187139Smarkm 5287139Smarkm if (getrusage(RUSAGE_SELF, &ru)) 5329088Smarkm return ((clock_t) -1); 54114630Sobrien return((clock_t)((CONVTCK(ru.ru_utime) + CONVTCK(ru.ru_stime)))); 5587139Smarkm} 5629088Smarkm