getruntime.c revision 104835
1193323Sed/* Return time used so far, in microseconds. 2193323Sed Copyright (C) 1994, 1999, 2002 Free Software Foundation, Inc. 3193323Sed 4193323SedThis file is part of the libiberty library. 5193323SedLibiberty is free software; you can redistribute it and/or 6193323Sedmodify it under the terms of the GNU Library General Public 7193323SedLicense as published by the Free Software Foundation; either 8193323Sedversion 2 of the License, or (at your option) any later version. 9193323Sed 10193323SedLibiberty is distributed in the hope that it will be useful, 11193323Sedbut WITHOUT ANY WARRANTY; without even the implied warranty of 12193323SedMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13193323SedLibrary General Public License for more details. 14193323Sed 15193323SedYou should have received a copy of the GNU Library General Public 16193323SedLicense along with libiberty; see the file COPYING.LIB. If 17193323Sednot, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 18193323SedBoston, MA 02111-1307, USA. */ 19193323Sed 20193323Sed#include "config.h" 21193323Sed 22193323Sed#include "ansidecl.h" 23193323Sed#include "libiberty.h" 24193323Sed 25193323Sed/* On some systems (such as WindISS), you must include <sys/types.h> 26193323Sed to get the definition of "time_t" before you include <time.h>. */ 27193323Sed#include <sys/types.h> 28193323Sed 29249423Sdim/* There are several ways to get elapsed execution time; unfortunately no 30249423Sdim single way is available for all host systems, nor are there reliable 31249423Sdim ways to find out which way is correct for a given host. */ 32193323Sed 33193323Sed#ifdef TIME_WITH_SYS_TIME 34193323Sed# include <sys/time.h> 35249423Sdim# include <time.h> 36249423Sdim#else 37249423Sdim# if HAVE_SYS_TIME_H 38249423Sdim# include <sys/time.h> 39249423Sdim# else 40249423Sdim# ifdef HAVE_TIME_H 41249423Sdim# include <time.h> 42249423Sdim# endif 43193323Sed# endif 44224145Sdim#endif 45193323Sed 46198090Srdivacky#if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H) 47249423Sdim#include <sys/resource.h> 48249423Sdim#endif 49193323Sed 50226633Sdim#ifdef HAVE_TIMES 51193323Sed#ifdef HAVE_SYS_PARAM_H 52193323Sed#include <sys/param.h> 53224145Sdim#endif 54224145Sdim#include <sys/times.h> 55224145Sdim#endif 56224145Sdim 57224145Sdim#ifdef HAVE_UNISTD_H 58193323Sed#include <unistd.h> 59234353Sdim#endif 60234353Sdim 61234353Sdim/* This is a fallback; if wrong, it will likely make obviously wrong 62234353Sdim results. */ 63234353Sdim 64234353Sdim#ifndef CLOCKS_PER_SEC 65223017Sdim#define CLOCKS_PER_SEC 1 66193323Sed#endif 67198090Srdivacky 68193323Sed#ifdef _SC_CLK_TCK 69193323Sed#define GNU_HZ sysconf(_SC_CLK_TCK) 70195098Sed#else 71243830Sdim#ifdef HZ 72243830Sdim#define GNU_HZ HZ 73224145Sdim#else 74221345Sdim#ifdef CLOCKS_PER_SEC 75193323Sed#define GNU_HZ CLOCKS_PER_SEC 76193323Sed#endif 77193323Sed#endif 78198090Srdivacky#endif 79234353Sdim 80224145Sdim/* 81218893Sdim 82218893Sdim@deftypefn Replacement long get_run_time (void) 83193323Sed 84198090SrdivackyReturns the time used so far, in microseconds. If possible, this is 85193323Sedthe time used by this process, else it is the elapsed time since the 86198090Srdivackyprocess started. 87198090Srdivacky 88198090Srdivacky@end deftypefn 89198090Srdivacky 90198090Srdivacky*/ 91198090Srdivacky 92198090Srdivackylong 93198090Srdivackyget_run_time () 94198090Srdivacky{ 95198090Srdivacky#if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H) 96198090Srdivacky struct rusage rusage; 97193323Sed 98193323Sed getrusage (0, &rusage); 99224145Sdim return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec 100224145Sdim + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec); 101224145Sdim#else /* ! HAVE_GETRUSAGE */ 102224145Sdim#ifdef HAVE_TIMES 103221345Sdim struct tms tms; 104193323Sed 105224145Sdim times (&tms); 106224145Sdim return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ); 107224145Sdim#else /* ! HAVE_TIMES */ 108226633Sdim /* Fall back on clock and hope it's correctly implemented. */ 109226633Sdim const long clocks_per_sec = CLOCKS_PER_SEC; 110224145Sdim if (clocks_per_sec <= 1000000) 111224145Sdim return clock () * (1000000 / clocks_per_sec); 112226633Sdim else 113226633Sdim return clock () / clocks_per_sec; 114223017Sdim#endif /* HAVE_TIMES */ 115195098Sed#endif /* HAVE_GETRUSAGE */ 116193323Sed} 117193323Sed