getruntime.c revision 33965
1207753Smm/* Return time used so far, in microseconds.
2207753Smm   Copyright (C) 1994 Free Software Foundation, Inc.
3207753Smm
4207753SmmThis file is part of the libiberty library.
5207753SmmLibiberty is free software; you can redistribute it and/or
6207753Smmmodify it under the terms of the GNU Library General Public
7207753SmmLicense as published by the Free Software Foundation; either
8207753Smmversion 2 of the License, or (at your option) any later version.
9263285Sdelphij
10207753SmmLibiberty is distributed in the hope that it will be useful,
11207753Smmbut WITHOUT ANY WARRANTY; without even the implied warranty of
12207753SmmMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13207753SmmLibrary General Public License for more details.
14223935Smm
15223935SmmYou should have received a copy of the GNU Library General Public
16223935SmmLicense along with libiberty; see the file COPYING.LIB.  If
17207753Smmnot, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18244601SmmBoston, MA 02111-1307, USA.  */
19207753Smm
20213700Smm#include "ansidecl.h"
21213700Smm#include "libiberty.h"
22207753Smm
23274261Sdelphij/* There are several ways to get elapsed execution time; unfortunately no
24292588Sdelphij   single way is available for all host systems, nor are there reliable
25244601Smm   ways to find out which way is correct for a given host. */
26207753Smm
27207753Smm#include <time.h>
28207753Smm
29207753Smm/* These should go away when libiberty uses autoconf. */
30207753Smm
31213700Smm#if defined(__sun__) && !defined(__svr4__)
32244601Smm#define HAVE_GETRUSAGE
33292588Sdelphij#endif
34207753Smm
35292588Sdelphij#ifdef HAVE_SYSCONF
36292588Sdelphij#define HAVE_TIMES
37223935Smm#endif
38219001Smm
39292588Sdelphij#ifdef HAVE_GETRUSAGE
40207753Smm#include <sys/time.h>
41292588Sdelphij#include <sys/resource.h>
42292588Sdelphij#endif
43207753Smm
44207753Smm#ifdef HAVE_TIMES
45292588Sdelphij#ifndef NO_SYS_PARAM_H
46207753Smm#include <sys/param.h>
47207753Smm#endif
48207753Smm#include <sys/times.h>
49207753Smm#endif
50292588Sdelphij
51292588Sdelphij/* This is a fallback; if wrong, it will likely make obviously wrong
52207753Smm   results. */
53207753Smm
54292588Sdelphij#ifndef CLOCKS_PER_SEC
55207753Smm#define CLOCKS_PER_SEC 1
56207753Smm#endif
57244601Smm
58223935Smmlong
59213700Smmget_run_time ()
60244601Smm{
61223935Smm#ifdef HAVE_GETRUSAGE
62207753Smm  struct rusage rusage;
63292588Sdelphij
64263285Sdelphij  getrusage (0, &rusage);
65207753Smm  return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
66207753Smm	  + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
67312518Sdelphij#else /* ! HAVE_GETRUSAGE */
68207753Smm#ifdef HAVE_TIMES
69207753Smm  struct tms tms;
70213700Smm
71213700Smm  times (&tms);
72244601Smm  return (tms.tms_utime + tms.tms_stime) * (1000000 / HZ);
73292588Sdelphij#else /* ! HAVE_TIMES */
74207753Smm  /* Fall back on clock and hope it's correctly implemented. */
75223935Smm  const long clocks_per_sec = CLOCKS_PER_SEC;
76207753Smm  if (clocks_per_sec <= 1000000)
77207753Smm    return clock () * (1000000 / clocks_per_sec);
78312518Sdelphij  else
79292588Sdelphij    return clock () / clocks_per_sec;
80263285Sdelphij#endif  /* HAVE_TIMES */
81292588Sdelphij#endif  /* HAVE_GETRUSAGE */
82207753Smm}
83207753Smm