Back to index

cell-binutils  2.17cvs20070401
getruntime.c
Go to the documentation of this file.
00001 /* Return time used so far, in microseconds.
00002    Copyright (C) 1994, 1999, 2002 Free Software Foundation, Inc.
00003 
00004 This file is part of the libiberty library.
00005 Libiberty is free software; you can redistribute it and/or
00006 modify it under the terms of the GNU Library General Public
00007 License as published by the Free Software Foundation; either
00008 version 2 of the License, or (at your option) any later version.
00009 
00010 Libiberty is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 Library General Public License for more details.
00014 
00015 You should have received a copy of the GNU Library General Public
00016 License along with libiberty; see the file COPYING.LIB.  If
00017 not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
00018 Boston, MA 02110-1301, USA.  */
00019 
00020 #include "config.h"
00021 
00022 #include "ansidecl.h"
00023 #include "libiberty.h"
00024 
00025 /* On some systems (such as WindISS), you must include <sys/types.h>
00026    to get the definition of "time_t" before you include <time.h>.  */
00027 #include <sys/types.h>
00028 
00029 /* There are several ways to get elapsed execution time; unfortunately no
00030    single way is available for all host systems, nor are there reliable
00031    ways to find out which way is correct for a given host. */
00032 
00033 #ifdef TIME_WITH_SYS_TIME
00034 # include <sys/time.h>
00035 # include <time.h>
00036 #else
00037 # if HAVE_SYS_TIME_H
00038 #  include <sys/time.h>
00039 # else
00040 #  ifdef HAVE_TIME_H
00041 #   include <time.h>
00042 #  endif
00043 # endif
00044 #endif
00045 
00046 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
00047 #include <sys/resource.h>
00048 #endif
00049 
00050 #ifdef HAVE_TIMES
00051 #ifdef HAVE_SYS_PARAM_H
00052 #include <sys/param.h>
00053 #endif
00054 #include <sys/times.h>
00055 #endif
00056 
00057 #ifdef HAVE_UNISTD_H
00058 #include <unistd.h>
00059 #endif
00060 
00061 /* This is a fallback; if wrong, it will likely make obviously wrong
00062    results. */
00063 
00064 #ifndef CLOCKS_PER_SEC
00065 #define CLOCKS_PER_SEC 1
00066 #endif
00067 
00068 #ifdef _SC_CLK_TCK
00069 #define GNU_HZ  sysconf(_SC_CLK_TCK)
00070 #else
00071 #ifdef HZ
00072 #define GNU_HZ  HZ
00073 #else
00074 #ifdef CLOCKS_PER_SEC
00075 #define GNU_HZ  CLOCKS_PER_SEC
00076 #endif
00077 #endif
00078 #endif
00079 
00080 /*
00081 
00082 @deftypefn Replacement long get_run_time (void)
00083 
00084 Returns the time used so far, in microseconds.  If possible, this is
00085 the time used by this process, else it is the elapsed time since the
00086 process started.
00087 
00088 @end deftypefn
00089 
00090 */
00091 
00092 long
00093 get_run_time (void)
00094 {
00095 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
00096   struct rusage rusage;
00097 
00098   getrusage (0, &rusage);
00099   return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
00100          + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
00101 #else /* ! HAVE_GETRUSAGE */
00102 #ifdef HAVE_TIMES
00103   struct tms tms;
00104 
00105   times (&tms);
00106   return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
00107 #else /* ! HAVE_TIMES */
00108   /* Fall back on clock and hope it's correctly implemented. */
00109   const long clocks_per_sec = CLOCKS_PER_SEC;
00110   if (clocks_per_sec <= 1000000)
00111     return clock () * (1000000 / clocks_per_sec);
00112   else
00113     return clock () / clocks_per_sec;
00114 #endif  /* HAVE_TIMES */
00115 #endif  /* HAVE_GETRUSAGE */
00116 }