]> git.ipfire.org Git - thirdparty/gcc.git/blame - libiberty/getruntime.c
Fred Fish <fnf@be.com>
[thirdparty/gcc.git] / libiberty / getruntime.c
CommitLineData
6599da04 1/* Return time used so far, in microseconds.
b207e09c 2 Copyright (C) 1994, 1999 Free Software Foundation, Inc.
6599da04
JM
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
3affd5f0
JL
20#include "config.h"
21
6599da04
JM
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#include <time.h>
30
6a071f4a 31#if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
6599da04
JM
32#include <sys/time.h>
33#include <sys/resource.h>
34#endif
35
36#ifdef HAVE_TIMES
3affd5f0 37#ifdef HAVE_SYS_PARAM_H
6599da04
JM
38#include <sys/param.h>
39#endif
40#include <sys/times.h>
41#endif
42
43/* This is a fallback; if wrong, it will likely make obviously wrong
44 results. */
45
46#ifndef CLOCKS_PER_SEC
47#define CLOCKS_PER_SEC 1
48#endif
49
b207e09c
MK
50#if defined (HAVE_TIMES) && ! defined (HZ)
51#define HZ CLOCKS_PER_SEC
52#endif
53
6599da04
JM
54long
55get_run_time ()
56{
6a071f4a 57#if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
6599da04
JM
58 struct rusage rusage;
59
60 getrusage (0, &rusage);
61 return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
62 + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
63#else /* ! HAVE_GETRUSAGE */
64#ifdef HAVE_TIMES
65 struct tms tms;
66
67 times (&tms);
68 return (tms.tms_utime + tms.tms_stime) * (1000000 / HZ);
69#else /* ! HAVE_TIMES */
70 /* Fall back on clock and hope it's correctly implemented. */
71 const long clocks_per_sec = CLOCKS_PER_SEC;
72 if (clocks_per_sec <= 1000000)
73 return clock () * (1000000 / clocks_per_sec);
74 else
75 return clock () / clocks_per_sec;
76#endif /* HAVE_TIMES */
77#endif /* HAVE_GETRUSAGE */
78}