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