xref: /dflybsd-src/contrib/gcc-4.7/libiberty/getruntime.c (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino /* Return time used so far, in microseconds.
2*e4b17023SJohn Marino    Copyright (C) 1994, 1999, 2002 Free Software Foundation, Inc.
3*e4b17023SJohn Marino 
4*e4b17023SJohn Marino This file is part of the libiberty library.
5*e4b17023SJohn Marino Libiberty is free software; you can redistribute it and/or
6*e4b17023SJohn Marino modify it under the terms of the GNU Library General Public
7*e4b17023SJohn Marino License as published by the Free Software Foundation; either
8*e4b17023SJohn Marino version 2 of the License, or (at your option) any later version.
9*e4b17023SJohn Marino 
10*e4b17023SJohn Marino Libiberty is distributed in the hope that it will be useful,
11*e4b17023SJohn Marino but WITHOUT ANY WARRANTY; without even the implied warranty of
12*e4b17023SJohn Marino MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13*e4b17023SJohn Marino Library General Public License for more details.
14*e4b17023SJohn Marino 
15*e4b17023SJohn Marino You should have received a copy of the GNU Library General Public
16*e4b17023SJohn Marino License along with libiberty; see the file COPYING.LIB.  If
17*e4b17023SJohn Marino not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18*e4b17023SJohn Marino Boston, MA 02110-1301, USA.  */
19*e4b17023SJohn Marino 
20*e4b17023SJohn Marino #include "config.h"
21*e4b17023SJohn Marino 
22*e4b17023SJohn Marino #include "ansidecl.h"
23*e4b17023SJohn Marino #include "libiberty.h"
24*e4b17023SJohn Marino 
25*e4b17023SJohn Marino /* On some systems (such as WindISS), you must include <sys/types.h>
26*e4b17023SJohn Marino    to get the definition of "time_t" before you include <time.h>.  */
27*e4b17023SJohn Marino #include <sys/types.h>
28*e4b17023SJohn Marino 
29*e4b17023SJohn Marino /* There are several ways to get elapsed execution time; unfortunately no
30*e4b17023SJohn Marino    single way is available for all host systems, nor are there reliable
31*e4b17023SJohn Marino    ways to find out which way is correct for a given host. */
32*e4b17023SJohn Marino 
33*e4b17023SJohn Marino #ifdef TIME_WITH_SYS_TIME
34*e4b17023SJohn Marino # include <sys/time.h>
35*e4b17023SJohn Marino # include <time.h>
36*e4b17023SJohn Marino #else
37*e4b17023SJohn Marino # if HAVE_SYS_TIME_H
38*e4b17023SJohn Marino #  include <sys/time.h>
39*e4b17023SJohn Marino # else
40*e4b17023SJohn Marino #  ifdef HAVE_TIME_H
41*e4b17023SJohn Marino #   include <time.h>
42*e4b17023SJohn Marino #  endif
43*e4b17023SJohn Marino # endif
44*e4b17023SJohn Marino #endif
45*e4b17023SJohn Marino 
46*e4b17023SJohn Marino #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
47*e4b17023SJohn Marino #include <sys/resource.h>
48*e4b17023SJohn Marino #endif
49*e4b17023SJohn Marino 
50*e4b17023SJohn Marino #ifdef HAVE_TIMES
51*e4b17023SJohn Marino #ifdef HAVE_SYS_PARAM_H
52*e4b17023SJohn Marino #include <sys/param.h>
53*e4b17023SJohn Marino #endif
54*e4b17023SJohn Marino #include <sys/times.h>
55*e4b17023SJohn Marino #endif
56*e4b17023SJohn Marino 
57*e4b17023SJohn Marino #ifdef HAVE_UNISTD_H
58*e4b17023SJohn Marino #include <unistd.h>
59*e4b17023SJohn Marino #endif
60*e4b17023SJohn Marino 
61*e4b17023SJohn Marino /* This is a fallback; if wrong, it will likely make obviously wrong
62*e4b17023SJohn Marino    results. */
63*e4b17023SJohn Marino 
64*e4b17023SJohn Marino #ifndef CLOCKS_PER_SEC
65*e4b17023SJohn Marino #define CLOCKS_PER_SEC 1
66*e4b17023SJohn Marino #endif
67*e4b17023SJohn Marino 
68*e4b17023SJohn Marino #ifdef _SC_CLK_TCK
69*e4b17023SJohn Marino #define GNU_HZ  sysconf(_SC_CLK_TCK)
70*e4b17023SJohn Marino #else
71*e4b17023SJohn Marino #ifdef HZ
72*e4b17023SJohn Marino #define GNU_HZ  HZ
73*e4b17023SJohn Marino #else
74*e4b17023SJohn Marino #ifdef CLOCKS_PER_SEC
75*e4b17023SJohn Marino #define GNU_HZ  CLOCKS_PER_SEC
76*e4b17023SJohn Marino #endif
77*e4b17023SJohn Marino #endif
78*e4b17023SJohn Marino #endif
79*e4b17023SJohn Marino 
80*e4b17023SJohn Marino /*
81*e4b17023SJohn Marino 
82*e4b17023SJohn Marino @deftypefn Replacement long get_run_time (void)
83*e4b17023SJohn Marino 
84*e4b17023SJohn Marino Returns the time used so far, in microseconds.  If possible, this is
85*e4b17023SJohn Marino the time used by this process, else it is the elapsed time since the
86*e4b17023SJohn Marino process started.
87*e4b17023SJohn Marino 
88*e4b17023SJohn Marino @end deftypefn
89*e4b17023SJohn Marino 
90*e4b17023SJohn Marino */
91*e4b17023SJohn Marino 
92*e4b17023SJohn Marino long
get_run_time(void)93*e4b17023SJohn Marino get_run_time (void)
94*e4b17023SJohn Marino {
95*e4b17023SJohn Marino #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
96*e4b17023SJohn Marino   struct rusage rusage;
97*e4b17023SJohn Marino 
98*e4b17023SJohn Marino   getrusage (0, &rusage);
99*e4b17023SJohn Marino   return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
100*e4b17023SJohn Marino 	  + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
101*e4b17023SJohn Marino #else /* ! HAVE_GETRUSAGE */
102*e4b17023SJohn Marino #ifdef HAVE_TIMES
103*e4b17023SJohn Marino   struct tms tms;
104*e4b17023SJohn Marino 
105*e4b17023SJohn Marino   times (&tms);
106*e4b17023SJohn Marino   return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
107*e4b17023SJohn Marino #else /* ! HAVE_TIMES */
108*e4b17023SJohn Marino   /* Fall back on clock and hope it's correctly implemented. */
109*e4b17023SJohn Marino   const long clocks_per_sec = CLOCKS_PER_SEC;
110*e4b17023SJohn Marino   if (clocks_per_sec <= 1000000)
111*e4b17023SJohn Marino     return clock () * (1000000 / clocks_per_sec);
112*e4b17023SJohn Marino   else
113*e4b17023SJohn Marino     return clock () / clocks_per_sec;
114*e4b17023SJohn Marino #endif  /* HAVE_TIMES */
115*e4b17023SJohn Marino #endif  /* HAVE_GETRUSAGE */
116*e4b17023SJohn Marino }
117