xref: /dflybsd-src/contrib/gdb-7/libiberty/getruntime.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /* Return time used so far, in microseconds.
286d7f5d3SJohn Marino    Copyright (C) 1994, 1999, 2002 Free Software Foundation, Inc.
386d7f5d3SJohn Marino 
486d7f5d3SJohn Marino This file is part of the libiberty library.
586d7f5d3SJohn Marino Libiberty is free software; you can redistribute it and/or
686d7f5d3SJohn Marino modify it under the terms of the GNU Library General Public
786d7f5d3SJohn Marino License as published by the Free Software Foundation; either
886d7f5d3SJohn Marino version 2 of the License, or (at your option) any later version.
986d7f5d3SJohn Marino 
1086d7f5d3SJohn Marino Libiberty is distributed in the hope that it will be useful,
1186d7f5d3SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
1286d7f5d3SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1386d7f5d3SJohn Marino Library General Public License for more details.
1486d7f5d3SJohn Marino 
1586d7f5d3SJohn Marino You should have received a copy of the GNU Library General Public
1686d7f5d3SJohn Marino License along with libiberty; see the file COPYING.LIB.  If
1786d7f5d3SJohn Marino not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
1886d7f5d3SJohn Marino Boston, MA 02110-1301, USA.  */
1986d7f5d3SJohn Marino 
2086d7f5d3SJohn Marino #include "config.h"
2186d7f5d3SJohn Marino 
2286d7f5d3SJohn Marino #include "ansidecl.h"
2386d7f5d3SJohn Marino #include "libiberty.h"
2486d7f5d3SJohn Marino 
2586d7f5d3SJohn Marino /* On some systems (such as WindISS), you must include <sys/types.h>
2686d7f5d3SJohn Marino    to get the definition of "time_t" before you include <time.h>.  */
2786d7f5d3SJohn Marino #include <sys/types.h>
2886d7f5d3SJohn Marino 
2986d7f5d3SJohn Marino /* There are several ways to get elapsed execution time; unfortunately no
3086d7f5d3SJohn Marino    single way is available for all host systems, nor are there reliable
3186d7f5d3SJohn Marino    ways to find out which way is correct for a given host. */
3286d7f5d3SJohn Marino 
3386d7f5d3SJohn Marino #ifdef TIME_WITH_SYS_TIME
3486d7f5d3SJohn Marino # include <sys/time.h>
3586d7f5d3SJohn Marino # include <time.h>
3686d7f5d3SJohn Marino #else
3786d7f5d3SJohn Marino # if HAVE_SYS_TIME_H
3886d7f5d3SJohn Marino #  include <sys/time.h>
3986d7f5d3SJohn Marino # else
4086d7f5d3SJohn Marino #  ifdef HAVE_TIME_H
4186d7f5d3SJohn Marino #   include <time.h>
4286d7f5d3SJohn Marino #  endif
4386d7f5d3SJohn Marino # endif
4486d7f5d3SJohn Marino #endif
4586d7f5d3SJohn Marino 
4686d7f5d3SJohn Marino #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
4786d7f5d3SJohn Marino #include <sys/resource.h>
4886d7f5d3SJohn Marino #endif
4986d7f5d3SJohn Marino 
5086d7f5d3SJohn Marino #ifdef HAVE_TIMES
5186d7f5d3SJohn Marino #ifdef HAVE_SYS_PARAM_H
5286d7f5d3SJohn Marino #include <sys/param.h>
5386d7f5d3SJohn Marino #endif
5486d7f5d3SJohn Marino #include <sys/times.h>
5586d7f5d3SJohn Marino #endif
5686d7f5d3SJohn Marino 
5786d7f5d3SJohn Marino #ifdef HAVE_UNISTD_H
5886d7f5d3SJohn Marino #include <unistd.h>
5986d7f5d3SJohn Marino #endif
6086d7f5d3SJohn Marino 
6186d7f5d3SJohn Marino /* This is a fallback; if wrong, it will likely make obviously wrong
6286d7f5d3SJohn Marino    results. */
6386d7f5d3SJohn Marino 
6486d7f5d3SJohn Marino #ifndef CLOCKS_PER_SEC
6586d7f5d3SJohn Marino #define CLOCKS_PER_SEC 1
6686d7f5d3SJohn Marino #endif
6786d7f5d3SJohn Marino 
6886d7f5d3SJohn Marino #ifdef _SC_CLK_TCK
6986d7f5d3SJohn Marino #define GNU_HZ  sysconf(_SC_CLK_TCK)
7086d7f5d3SJohn Marino #else
7186d7f5d3SJohn Marino #ifdef HZ
7286d7f5d3SJohn Marino #define GNU_HZ  HZ
7386d7f5d3SJohn Marino #else
7486d7f5d3SJohn Marino #ifdef CLOCKS_PER_SEC
7586d7f5d3SJohn Marino #define GNU_HZ  CLOCKS_PER_SEC
7686d7f5d3SJohn Marino #endif
7786d7f5d3SJohn Marino #endif
7886d7f5d3SJohn Marino #endif
7986d7f5d3SJohn Marino 
8086d7f5d3SJohn Marino /*
8186d7f5d3SJohn Marino 
8286d7f5d3SJohn Marino @deftypefn Replacement long get_run_time (void)
8386d7f5d3SJohn Marino 
8486d7f5d3SJohn Marino Returns the time used so far, in microseconds.  If possible, this is
8586d7f5d3SJohn Marino the time used by this process, else it is the elapsed time since the
8686d7f5d3SJohn Marino process started.
8786d7f5d3SJohn Marino 
8886d7f5d3SJohn Marino @end deftypefn
8986d7f5d3SJohn Marino 
9086d7f5d3SJohn Marino */
9186d7f5d3SJohn Marino 
9286d7f5d3SJohn Marino long
get_run_time(void)9386d7f5d3SJohn Marino get_run_time (void)
9486d7f5d3SJohn Marino {
9586d7f5d3SJohn Marino #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
9686d7f5d3SJohn Marino   struct rusage rusage;
9786d7f5d3SJohn Marino 
9886d7f5d3SJohn Marino   getrusage (0, &rusage);
9986d7f5d3SJohn Marino   return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
10086d7f5d3SJohn Marino 	  + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
10186d7f5d3SJohn Marino #else /* ! HAVE_GETRUSAGE */
10286d7f5d3SJohn Marino #ifdef HAVE_TIMES
10386d7f5d3SJohn Marino   struct tms tms;
10486d7f5d3SJohn Marino 
10586d7f5d3SJohn Marino   times (&tms);
10686d7f5d3SJohn Marino   return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
10786d7f5d3SJohn Marino #else /* ! HAVE_TIMES */
10886d7f5d3SJohn Marino   /* Fall back on clock and hope it's correctly implemented. */
10986d7f5d3SJohn Marino   const long clocks_per_sec = CLOCKS_PER_SEC;
11086d7f5d3SJohn Marino   if (clocks_per_sec <= 1000000)
11186d7f5d3SJohn Marino     return clock () * (1000000 / clocks_per_sec);
11286d7f5d3SJohn Marino   else
11386d7f5d3SJohn Marino     return clock () / clocks_per_sec;
11486d7f5d3SJohn Marino #endif  /* HAVE_TIMES */
11586d7f5d3SJohn Marino #endif  /* HAVE_GETRUSAGE */
11686d7f5d3SJohn Marino }
117