1 /* Return time used so far, in microseconds.
2 Copyright (C) 1994, 1999, 2002 Free Software Foundation, Inc.
4 This file is part of the libiberty library.
5 Libiberty is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 Libiberty is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with libiberty; see the file COPYING.LIB. If
17 not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18 Boston, MA 02110-1301, USA. */
23 #include "libiberty.h"
25 /* On some systems (such as WindISS), you must include <sys/types.h>
26 to get the definition of "time_t" before you include <time.h>. */
27 #include <sys/types.h>
29 /* There are several ways to get elapsed execution time; unfortunately no
30 single way is available for all host systems, nor are there reliable
31 ways to find out which way is correct for a given host. */
33 #ifdef TIME_WITH_SYS_TIME
34 # include <sys/time.h>
38 # include <sys/time.h>
46 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
47 #include <sys/resource.h>
51 #ifdef HAVE_SYS_PARAM_H
52 #include <sys/param.h>
54 #include <sys/times.h>
61 /* This is a fallback; if wrong, it will likely make obviously wrong
64 #ifndef CLOCKS_PER_SEC
65 #define CLOCKS_PER_SEC 1
73 #define GNU_HZ sysconf(_SC_CLK_TCK)
79 #define GNU_HZ CLOCKS_PER_SEC
86 @deftypefn Replacement long get_run_time (void)
88 Returns the time used so far, in microseconds. If possible, this is
89 the time used by this process, else it is the elapsed time since the
99 #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
100 struct rusage rusage
;
102 getrusage (RUSAGE_SELF
, &rusage
);
103 return (rusage
.ru_utime
.tv_sec
* 1000000 + rusage
.ru_utime
.tv_usec
104 + rusage
.ru_stime
.tv_sec
* 1000000 + rusage
.ru_stime
.tv_usec
);
105 #else /* ! HAVE_GETRUSAGE */
110 return (tms
.tms_utime
+ tms
.tms_stime
) * (1000000 / GNU_HZ
);
111 #else /* ! HAVE_TIMES */
112 /* Fall back on clock and hope it's correctly implemented. */
113 const long clocks_per_sec
= CLOCKS_PER_SEC
;
114 if (clocks_per_sec
<= 1000000)
115 return clock () * (1000000 / clocks_per_sec
);
117 return clock () / clocks_per_sec
;
118 #endif /* HAVE_TIMES */
119 #endif /* HAVE_GETRUSAGE */