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