getruntime.c revision 60484
1227569Sphilip/* Return time used so far, in microseconds.
2227569Sphilip   Copyright (C) 1994, 1999 Free Software Foundation, Inc.
3227569Sphilip
4227569SphilipThis file is part of the libiberty library.
5227569SphilipLibiberty is free software; you can redistribute it and/or
6227569Sphilipmodify it under the terms of the GNU Library General Public
7227569SphilipLicense as published by the Free Software Foundation; either
8227569Sphilipversion 2 of the License, or (at your option) any later version.
9227569Sphilip
10227569SphilipLibiberty is distributed in the hope that it will be useful,
11227569Sphilipbut WITHOUT ANY WARRANTY; without even the implied warranty of
12227569SphilipMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13227569SphilipLibrary General Public License for more details.
14227569Sphilip
15227569SphilipYou should have received a copy of the GNU Library General Public
16227569SphilipLicense along with libiberty; see the file COPYING.LIB.  If
17227569Sphilipnot, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18227569SphilipBoston, MA 02111-1307, USA.  */
19227569Sphilip
20227569Sphilip#include "config.h"
21227569Sphilip
22227569Sphilip#include "ansidecl.h"
23227569Sphilip#include "libiberty.h"
24227569Sphilip
25227569Sphilip/* There are several ways to get elapsed execution time; unfortunately no
26228078Sphilip   single way is available for all host systems, nor are there reliable
27228078Sphilip   ways to find out which way is correct for a given host. */
28228078Sphilip
29227569Sphilip#include <time.h>
30227569Sphilip
31227569Sphilip#if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
32227569Sphilip#include <sys/time.h>
33227569Sphilip#include <sys/resource.h>
34227569Sphilip#endif
35227569Sphilip
36227569Sphilip#ifdef HAVE_TIMES
37227569Sphilip#ifdef HAVE_SYS_PARAM_H
38227569Sphilip#include <sys/param.h>
39227569Sphilip#endif
40227569Sphilip#include <sys/times.h>
41227569Sphilip#endif
42227569Sphilip
43227569Sphilip#ifdef HAVE_UNISTD_H
44227569Sphilip#include <unistd.h>
45227569Sphilip#endif
46227569Sphilip
47227569Sphilip/* This is a fallback; if wrong, it will likely make obviously wrong
48227569Sphilip   results. */
49227569Sphilip
50227569Sphilip#ifndef CLOCKS_PER_SEC
51227569Sphilip#define CLOCKS_PER_SEC 1
52227569Sphilip#endif
53227569Sphilip
54227569Sphilip#ifdef _SC_CLK_TCK
55227569Sphilip#define GNU_HZ  sysconf(_SC_CLK_TCK)
56227569Sphilip#else
57227569Sphilip#ifdef HZ
58227569Sphilip#define GNU_HZ  HZ
59227569Sphilip#else
60227569Sphilip#ifdef CLOCKS_PER_SEC
61227569Sphilip#define GNU_HZ  CLOCKS_PER_SEC
62227569Sphilip#endif
63227569Sphilip#endif
64227569Sphilip#endif
65227569Sphilip
66227569Sphiliplong
67227569Sphilipget_run_time ()
68227569Sphilip{
69227569Sphilip#if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
70227569Sphilip  struct rusage rusage;
71227569Sphilip
72227569Sphilip  getrusage (0, &rusage);
73227569Sphilip  return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
74227569Sphilip	  + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
75227569Sphilip#else /* ! HAVE_GETRUSAGE */
76227569Sphilip#ifdef HAVE_TIMES
77227569Sphilip  struct tms tms;
78227569Sphilip
79227569Sphilip  times (&tms);
80227569Sphilip  return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
81227569Sphilip#else /* ! HAVE_TIMES */
82227569Sphilip  /* Fall back on clock and hope it's correctly implemented. */
83227569Sphilip  const long clocks_per_sec = CLOCKS_PER_SEC;
84227569Sphilip  if (clocks_per_sec <= 1000000)
85227569Sphilip    return clock () * (1000000 / clocks_per_sec);
86227569Sphilip  else
87227569Sphilip    return clock () / clocks_per_sec;
88227569Sphilip#endif  /* HAVE_TIMES */
89227569Sphilip#endif  /* HAVE_GETRUSAGE */
90227569Sphilip}
91227569Sphilip