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