getruntime.c revision 218822
150397Sobrien/* Return time used so far, in microseconds. 250397Sobrien Copyright (C) 1994, 1999, 2002 Free Software Foundation, Inc. 350397Sobrien 450397SobrienThis file is part of the libiberty library. 550397SobrienLibiberty is free software; you can redistribute it and/or 650397Sobrienmodify it under the terms of the GNU Library General Public 750397SobrienLicense as published by the Free Software Foundation; either 850397Sobrienversion 2 of the License, or (at your option) any later version. 950397Sobrien 1050397SobrienLibiberty is distributed in the hope that it will be useful, 1150397Sobrienbut WITHOUT ANY WARRANTY; without even the implied warranty of 1250397SobrienMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1350397SobrienLibrary General Public License for more details. 1450397Sobrien 1550397SobrienYou should have received a copy of the GNU Library General Public 1650397SobrienLicense along with libiberty; see the file COPYING.LIB. If 1750397Sobriennot, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, 1850397SobrienBoston, MA 02110-1301, USA. */ 1950397Sobrien 2050397Sobrien#include "config.h" 2152284Sobrien 2252284Sobrien#include "ansidecl.h" 2350397Sobrien#include "libiberty.h" 2450397Sobrien 2550397Sobrien/* On some systems (such as WindISS), you must include <sys/types.h> 2650397Sobrien to get the definition of "time_t" before you include <time.h>. */ 2750397Sobrien#include <sys/types.h> 2850397Sobrien 2952284Sobrien/* There are several ways to get elapsed execution time; unfortunately no 3050397Sobrien single way is available for all host systems, nor are there reliable 3150397Sobrien ways to find out which way is correct for a given host. */ 3250397Sobrien 3350397Sobrien#ifdef TIME_WITH_SYS_TIME 3450397Sobrien# include <sys/time.h> 3550397Sobrien# include <time.h> 3650397Sobrien#else 3750397Sobrien# if HAVE_SYS_TIME_H 3850397Sobrien# include <sys/time.h> 3950397Sobrien# else 4050397Sobrien# ifdef HAVE_TIME_H 4150397Sobrien# include <time.h> 4250397Sobrien# endif 4350397Sobrien# endif 4450397Sobrien#endif 4550397Sobrien 4650397Sobrien#if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H) 4750397Sobrien#include <sys/resource.h> 4850397Sobrien#endif 4950397Sobrien 5052284Sobrien#ifdef HAVE_TIMES 5152284Sobrien#ifdef HAVE_SYS_PARAM_H 5250397Sobrien#include <sys/param.h> 5350397Sobrien#endif 5450397Sobrien#include <sys/times.h> 5550397Sobrien#endif 5650397Sobrien 5750397Sobrien#ifdef HAVE_UNISTD_H 5850397Sobrien#include <unistd.h> 5950397Sobrien#endif 6050397Sobrien 6150397Sobrien/* This is a fallback; if wrong, it will likely make obviously wrong 6250397Sobrien results. */ 6350397Sobrien 6450397Sobrien#ifndef CLOCKS_PER_SEC 6550397Sobrien#define CLOCKS_PER_SEC 1 6650397Sobrien#endif 6750397Sobrien 6850397Sobrien#ifdef _SC_CLK_TCK 6950397Sobrien#define GNU_HZ sysconf(_SC_CLK_TCK) 7050397Sobrien#else 7150397Sobrien#ifdef HZ 7250397Sobrien#define GNU_HZ HZ 7350397Sobrien#else 7450397Sobrien#ifdef CLOCKS_PER_SEC 7550397Sobrien#define GNU_HZ CLOCKS_PER_SEC 7650397Sobrien#endif 7750397Sobrien#endif 7850397Sobrien#endif 7952284Sobrien 8052284Sobrien/* 8152284Sobrien 8252284Sobrien@deftypefn Replacement long get_run_time (void) 8352284Sobrien 8452284SobrienReturns the time used so far, in microseconds. If possible, this is 8552284Sobrienthe time used by this process, else it is the elapsed time since the 8652284Sobrienprocess started. 8752284Sobrien 8852284Sobrien@end deftypefn 8952284Sobrien 9052284Sobrien*/ 9152284Sobrien 9252284Sobrienlong 9352284Sobrienget_run_time (void) 9452284Sobrien{ 9552284Sobrien#if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H) 9652284Sobrien struct rusage rusage; 9752284Sobrien 9852284Sobrien getrusage (0, &rusage); 9952284Sobrien return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec 10052284Sobrien + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec); 10152284Sobrien#else /* ! HAVE_GETRUSAGE */ 10252284Sobrien#ifdef HAVE_TIMES 10352284Sobrien struct tms tms; 10452284Sobrien 10552284Sobrien times (&tms); 10652284Sobrien return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ); 10752284Sobrien#else /* ! HAVE_TIMES */ 10852284Sobrien /* Fall back on clock and hope it's correctly implemented. */ 10952284Sobrien const long clocks_per_sec = CLOCKS_PER_SEC; 11052284Sobrien if (clocks_per_sec <= 1000000) 11152284Sobrien return clock () * (1000000 / clocks_per_sec); 11252284Sobrien else 11352284Sobrien return clock () / clocks_per_sec; 11450397Sobrien#endif /* HAVE_TIMES */ 11550397Sobrien#endif /* HAVE_GETRUSAGE */ 11650397Sobrien} 11750397Sobrien