1164410Ssyrinx/* Return time used so far, in microseconds.
2164410Ssyrinx   Copyright (C) 1994, 1999, 2002 Free Software Foundation, Inc.
3164410Ssyrinx
4164410SsyrinxThis file is part of the libiberty library.
5164410SsyrinxLibiberty is free software; you can redistribute it and/or
6164410Ssyrinxmodify it under the terms of the GNU Library General Public
7164410SsyrinxLicense as published by the Free Software Foundation; either
8164410Ssyrinxversion 2 of the License, or (at your option) any later version.
9164410Ssyrinx
10164410SsyrinxLibiberty is distributed in the hope that it will be useful,
11164410Ssyrinxbut WITHOUT ANY WARRANTY; without even the implied warranty of
12164410SsyrinxMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13164410SsyrinxLibrary General Public License for more details.
14164410Ssyrinx
15164410SsyrinxYou should have received a copy of the GNU Library General Public
16164410SsyrinxLicense along with libiberty; see the file COPYING.LIB.  If
17164410Ssyrinxnot, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18164410SsyrinxBoston, MA 02110-1301, USA.  */
19164410Ssyrinx
20164410Ssyrinx#include "config.h"
21164410Ssyrinx
22164410Ssyrinx#include "ansidecl.h"
23164410Ssyrinx#include "libiberty.h"
24164410Ssyrinx
25164410Ssyrinx/* On some systems (such as WindISS), you must include <sys/types.h>
26164410Ssyrinx   to get the definition of "time_t" before you include <time.h>.  */
27164410Ssyrinx#include <sys/types.h>
28164410Ssyrinx
29164410Ssyrinx/* There are several ways to get elapsed execution time; unfortunately no
30164410Ssyrinx   single way is available for all host systems, nor are there reliable
31164410Ssyrinx   ways to find out which way is correct for a given host. */
32164410Ssyrinx
33164410Ssyrinx#ifdef TIME_WITH_SYS_TIME
34164410Ssyrinx# include <sys/time.h>
35164410Ssyrinx# include <time.h>
36164410Ssyrinx#else
37164410Ssyrinx# if HAVE_SYS_TIME_H
38164410Ssyrinx#  include <sys/time.h>
39164410Ssyrinx# else
40164410Ssyrinx#  ifdef HAVE_TIME_H
41164410Ssyrinx#   include <time.h>
42164410Ssyrinx#  endif
43164410Ssyrinx# endif
44164410Ssyrinx#endif
45164410Ssyrinx
46164410Ssyrinx#if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
47164410Ssyrinx#include <sys/resource.h>
48164410Ssyrinx#endif
49164410Ssyrinx
50164410Ssyrinx#ifdef HAVE_TIMES
51164410Ssyrinx#ifdef HAVE_SYS_PARAM_H
52164410Ssyrinx#include <sys/param.h>
53164410Ssyrinx#endif
54164410Ssyrinx#include <sys/times.h>
55164410Ssyrinx#endif
56164410Ssyrinx
57164410Ssyrinx#ifdef HAVE_UNISTD_H
58164410Ssyrinx#include <unistd.h>
59164410Ssyrinx#endif
60164410Ssyrinx
61164410Ssyrinx/* This is a fallback; if wrong, it will likely make obviously wrong
62164410Ssyrinx   results. */
63164410Ssyrinx
64164410Ssyrinx#ifndef CLOCKS_PER_SEC
65164410Ssyrinx#define CLOCKS_PER_SEC 1
66164410Ssyrinx#endif
67164410Ssyrinx
68164410Ssyrinx#ifdef _SC_CLK_TCK
69164410Ssyrinx#define GNU_HZ  sysconf(_SC_CLK_TCK)
70164410Ssyrinx#else
71164410Ssyrinx#ifdef HZ
72164410Ssyrinx#define GNU_HZ  HZ
73164410Ssyrinx#else
74164410Ssyrinx#ifdef CLOCKS_PER_SEC
75164410Ssyrinx#define GNU_HZ  CLOCKS_PER_SEC
76164410Ssyrinx#endif
77164410Ssyrinx#endif
78164410Ssyrinx#endif
79164410Ssyrinx
80164410Ssyrinx/*
81164410Ssyrinx
82164410Ssyrinx@deftypefn Replacement long get_run_time (void)
83164410Ssyrinx
84164410SsyrinxReturns the time used so far, in microseconds.  If possible, this is
85164410Ssyrinxthe time used by this process, else it is the elapsed time since the
86164410Ssyrinxprocess started.
87164410Ssyrinx
88164410Ssyrinx@end deftypefn
89164410Ssyrinx
90164410Ssyrinx*/
91164410Ssyrinx
92164410Ssyrinxlong
93164410Ssyrinxget_run_time (void)
94164410Ssyrinx{
95164410Ssyrinx#if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
96164410Ssyrinx  struct rusage rusage;
97164410Ssyrinx
98164410Ssyrinx  getrusage (0, &rusage);
99164410Ssyrinx  return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
100164410Ssyrinx	  + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
101164410Ssyrinx#else /* ! HAVE_GETRUSAGE */
102164410Ssyrinx#ifdef HAVE_TIMES
103164410Ssyrinx  struct tms tms;
104164410Ssyrinx
105164410Ssyrinx  times (&tms);
106164410Ssyrinx  return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
107164410Ssyrinx#else /* ! HAVE_TIMES */
108164410Ssyrinx  /* Fall back on clock and hope it's correctly implemented. */
109164410Ssyrinx  const long clocks_per_sec = CLOCKS_PER_SEC;
110164410Ssyrinx  if (clocks_per_sec <= 1000000)
111164410Ssyrinx    return clock () * (1000000 / clocks_per_sec);
112164410Ssyrinx  else
113164410Ssyrinx    return clock () / clocks_per_sec;
114164410Ssyrinx#endif  /* HAVE_TIMES */
115164410Ssyrinx#endif  /* HAVE_GETRUSAGE */
116164410Ssyrinx}
117164410Ssyrinx