getruntime.c revision 33965
1/* Return time used so far, in microseconds. 2 Copyright (C) 1994 Free Software Foundation, Inc. 3 4This file is part of the libiberty library. 5Libiberty is free software; you can redistribute it and/or 6modify it under the terms of the GNU Library General Public 7License as published by the Free Software Foundation; either 8version 2 of the License, or (at your option) any later version. 9 10Libiberty is distributed in the hope that it will be useful, 11but WITHOUT ANY WARRANTY; without even the implied warranty of 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13Library General Public License for more details. 14 15You should have received a copy of the GNU Library General Public 16License along with libiberty; see the file COPYING.LIB. If 17not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 18Boston, MA 02111-1307, USA. */ 19 20#include "ansidecl.h" 21#include "libiberty.h" 22 23/* There are several ways to get elapsed execution time; unfortunately no 24 single way is available for all host systems, nor are there reliable 25 ways to find out which way is correct for a given host. */ 26 27#include <time.h> 28 29/* These should go away when libiberty uses autoconf. */ 30 31#if defined(__sun__) && !defined(__svr4__) 32#define HAVE_GETRUSAGE 33#endif 34 35#ifdef HAVE_SYSCONF 36#define HAVE_TIMES 37#endif 38 39#ifdef HAVE_GETRUSAGE 40#include <sys/time.h> 41#include <sys/resource.h> 42#endif 43 44#ifdef HAVE_TIMES 45#ifndef NO_SYS_PARAM_H 46#include <sys/param.h> 47#endif 48#include <sys/times.h> 49#endif 50 51/* This is a fallback; if wrong, it will likely make obviously wrong 52 results. */ 53 54#ifndef CLOCKS_PER_SEC 55#define CLOCKS_PER_SEC 1 56#endif 57 58long 59get_run_time () 60{ 61#ifdef HAVE_GETRUSAGE 62 struct rusage rusage; 63 64 getrusage (0, &rusage); 65 return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec 66 + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec); 67#else /* ! HAVE_GETRUSAGE */ 68#ifdef HAVE_TIMES 69 struct tms tms; 70 71 times (&tms); 72 return (tms.tms_utime + tms.tms_stime) * (1000000 / HZ); 73#else /* ! HAVE_TIMES */ 74 /* Fall back on clock and hope it's correctly implemented. */ 75 const long clocks_per_sec = CLOCKS_PER_SEC; 76 if (clocks_per_sec <= 1000000) 77 return clock () * (1000000 / clocks_per_sec); 78 else 79 return clock () / clocks_per_sec; 80#endif /* HAVE_TIMES */ 81#endif /* HAVE_GETRUSAGE */ 82} 83