getruntime.c revision 89857
140682Snsouch/* Return time used so far, in microseconds. 240682Snsouch Copyright (C) 1994, 1999 Free Software Foundation, Inc. 340682Snsouch 440682SnsouchThis file is part of the libiberty library. 540682SnsouchLibiberty is free software; you can redistribute it and/or 640682Snsouchmodify it under the terms of the GNU Library General Public 740682SnsouchLicense as published by the Free Software Foundation; either 840682Snsouchversion 2 of the License, or (at your option) any later version. 940682Snsouch 1040682SnsouchLibiberty is distributed in the hope that it will be useful, 1140682Snsouchbut WITHOUT ANY WARRANTY; without even the implied warranty of 1240682SnsouchMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1340682SnsouchLibrary General Public License for more details. 1440682Snsouch 1540682SnsouchYou should have received a copy of the GNU Library General Public 1640682SnsouchLicense along with libiberty; see the file COPYING.LIB. If 1740682Snsouchnot, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 1840682SnsouchBoston, MA 02111-1307, USA. */ 1940682Snsouch 2040682Snsouch#include "config.h" 2140682Snsouch 2240682Snsouch#include "ansidecl.h" 2340682Snsouch#include "libiberty.h" 2440682Snsouch 2550476Speter/* There are several ways to get elapsed execution time; unfortunately no 2640682Snsouch single way is available for all host systems, nor are there reliable 27274822Sian ways to find out which way is correct for a given host. */ 2840682Snsouch 2979538Sru#ifdef TIME_WITH_SYS_TIME 3040682Snsouch# include <sys/time.h> 3140682Snsouch# include <time.h> 3275670Sru#else 3340682Snsouch# if HAVE_SYS_TIME_H 3456460Sasmodai# include <sys/time.h> 3556460Sasmodai# else 3640682Snsouch# ifdef HAVE_TIME_H 3756460Sasmodai# include <time.h> 3856460Sasmodai# endif 3956460Sasmodai# endif 4040682Snsouch#endif 4140682Snsouch 4240682Snsouch#if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H) 4340682Snsouch#include <sys/resource.h> 4440682Snsouch#endif 4540682Snsouch 4640682Snsouch#ifdef HAVE_TIMES 4757676Ssheldonh#ifdef HAVE_SYS_PARAM_H 4857676Ssheldonh#include <sys/param.h> 4957676Ssheldonh#endif 50141851Sru#include <sys/times.h> 5140682Snsouch#endif 5240682Snsouch 5340682Snsouch#ifdef HAVE_UNISTD_H 5457676Ssheldonh#include <unistd.h> 5557676Ssheldonh#endif 5640682Snsouch 5770466Sru/* This is a fallback; if wrong, it will likely make obviously wrong 5840682Snsouch results. */ 5957676Ssheldonh 6057676Ssheldonh#ifndef CLOCKS_PER_SEC 6168881Sben#define CLOCKS_PER_SEC 1 6240682Snsouch#endif 6357676Ssheldonh 6457676Ssheldonh#ifdef _SC_CLK_TCK 6568881Sben#define GNU_HZ sysconf(_SC_CLK_TCK) 6670466Sru#else 6757676Ssheldonh#ifdef HZ 6857676Ssheldonh#define GNU_HZ HZ 6940682Snsouch#else 7057676Ssheldonh#ifdef CLOCKS_PER_SEC 7157676Ssheldonh#define GNU_HZ CLOCKS_PER_SEC 7257676Ssheldonh#endif 7357676Ssheldonh#endif 7440682Snsouch#endif 7540682Snsouch 7640682Snsouch/* 7740682Snsouch 7840682Snsouch@deftypefn Replacement long get_run_time (void) 7940682Snsouch 8040682SnsouchReturns the time used so far, in microseconds. If possible, this is 8140682Snsouchthe time used by this process, else it is the elapsed time since the 8240682Snsouchprocess started. 8340682Snsouch 8440682Snsouch@end deftypefn 8540682Snsouch 8657676Ssheldonh*/ 8757676Ssheldonh 8840682Snsouchlong 8957676Ssheldonhget_run_time () 9057676Ssheldonh{ 9140682Snsouch#if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H) 9270466Sru struct rusage rusage; 9340682Snsouch 9468881Sben getrusage (0, &rusage); 9540682Snsouch return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec 9670466Sru + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec); 9740682Snsouch#else /* ! HAVE_GETRUSAGE */ 9840682Snsouch#ifdef HAVE_TIMES 9940682Snsouch struct tms tms; 10040682Snsouch 10140682Snsouch times (&tms); 10240682Snsouch return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ); 10340682Snsouch#else /* ! HAVE_TIMES */ 10440682Snsouch /* Fall back on clock and hope it's correctly implemented. */ 10540682Snsouch const long clocks_per_sec = CLOCKS_PER_SEC; 106274822Sian if (clocks_per_sec <= 1000000) 107274822Sian return clock () * (1000000 / clocks_per_sec); 108274822Sian else 109275991Sbrueffer return clock () / clocks_per_sec; 110275991Sbrueffer#endif /* HAVE_TIMES */ 111274822Sian#endif /* HAVE_GETRUSAGE */ 112274822Sian} 113275991Sbrueffer