getruntime.c revision 77298
133965Sjdp/* Return time used so far, in microseconds. 260484Sobrien Copyright (C) 1994, 1999 Free Software Foundation, Inc. 333965Sjdp 433965SjdpThis file is part of the libiberty library. 533965SjdpLibiberty is free software; you can redistribute it and/or 633965Sjdpmodify it under the terms of the GNU Library General Public 733965SjdpLicense as published by the Free Software Foundation; either 833965Sjdpversion 2 of the License, or (at your option) any later version. 933965Sjdp 1033965SjdpLibiberty is distributed in the hope that it will be useful, 1133965Sjdpbut WITHOUT ANY WARRANTY; without even the implied warranty of 1233965SjdpMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1333965SjdpLibrary General Public License for more details. 1433965Sjdp 1533965SjdpYou should have received a copy of the GNU Library General Public 1633965SjdpLicense along with libiberty; see the file COPYING.LIB. If 1733965Sjdpnot, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 1833965SjdpBoston, MA 02111-1307, USA. */ 1933965Sjdp 2060484Sobrien#include "config.h" 2160484Sobrien 2233965Sjdp#include "ansidecl.h" 2333965Sjdp#include "libiberty.h" 2433965Sjdp 2533965Sjdp/* There are several ways to get elapsed execution time; unfortunately no 2633965Sjdp single way is available for all host systems, nor are there reliable 2733965Sjdp ways to find out which way is correct for a given host. */ 2833965Sjdp 2977298Sobrien#ifdef TIME_WITH_SYS_TIME 3077298Sobrien# include <sys/time.h> 3177298Sobrien# include <time.h> 3277298Sobrien#else 3377298Sobrien# if HAVE_SYS_TIME_H 3477298Sobrien# include <sys/time.h> 3577298Sobrien# else 3677298Sobrien# ifdef HAVE_TIME_H 3777298Sobrien# include <time.h> 3877298Sobrien# endif 3977298Sobrien# endif 4077298Sobrien#endif 4133965Sjdp 4260484Sobrien#if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H) 4333965Sjdp#include <sys/resource.h> 4433965Sjdp#endif 4533965Sjdp 4633965Sjdp#ifdef HAVE_TIMES 4760484Sobrien#ifdef HAVE_SYS_PARAM_H 4833965Sjdp#include <sys/param.h> 4933965Sjdp#endif 5033965Sjdp#include <sys/times.h> 5133965Sjdp#endif 5233965Sjdp 5360484Sobrien#ifdef HAVE_UNISTD_H 5460484Sobrien#include <unistd.h> 5560484Sobrien#endif 5660484Sobrien 5733965Sjdp/* This is a fallback; if wrong, it will likely make obviously wrong 5833965Sjdp results. */ 5933965Sjdp 6033965Sjdp#ifndef CLOCKS_PER_SEC 6133965Sjdp#define CLOCKS_PER_SEC 1 6233965Sjdp#endif 6333965Sjdp 6460484Sobrien#ifdef _SC_CLK_TCK 6560484Sobrien#define GNU_HZ sysconf(_SC_CLK_TCK) 6660484Sobrien#else 6760484Sobrien#ifdef HZ 6860484Sobrien#define GNU_HZ HZ 6960484Sobrien#else 7060484Sobrien#ifdef CLOCKS_PER_SEC 7160484Sobrien#define GNU_HZ CLOCKS_PER_SEC 7260484Sobrien#endif 7360484Sobrien#endif 7460484Sobrien#endif 7560484Sobrien 7633965Sjdplong 7733965Sjdpget_run_time () 7833965Sjdp{ 7960484Sobrien#if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H) 8033965Sjdp struct rusage rusage; 8133965Sjdp 8233965Sjdp getrusage (0, &rusage); 8333965Sjdp return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec 8433965Sjdp + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec); 8533965Sjdp#else /* ! HAVE_GETRUSAGE */ 8633965Sjdp#ifdef HAVE_TIMES 8733965Sjdp struct tms tms; 8833965Sjdp 8933965Sjdp times (&tms); 9060484Sobrien return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ); 9133965Sjdp#else /* ! HAVE_TIMES */ 9233965Sjdp /* Fall back on clock and hope it's correctly implemented. */ 9333965Sjdp const long clocks_per_sec = CLOCKS_PER_SEC; 9433965Sjdp if (clocks_per_sec <= 1000000) 9533965Sjdp return clock () * (1000000 / clocks_per_sec); 9633965Sjdp else 9733965Sjdp return clock () / clocks_per_sec; 9833965Sjdp#endif /* HAVE_TIMES */ 9933965Sjdp#endif /* HAVE_GETRUSAGE */ 10033965Sjdp} 101