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