133965Sjdp/* Return time used so far, in microseconds.
2104834Sobrien   Copyright (C) 1994, 1999, 2002 Free Software Foundation, Inc.
333965Sjdp
433965SjdpThis file is part of the libiberty library.
533965SjdpLibiberty is free software; you can redistribute it and/or
633965Sjdpmodify it under the terms of the GNU Library General Public
733965SjdpLicense as published by the Free Software Foundation; either
833965Sjdpversion 2 of the License, or (at your option) any later version.
933965Sjdp
1033965SjdpLibiberty is distributed in the hope that it will be useful,
1133965Sjdpbut WITHOUT ANY WARRANTY; without even the implied warranty of
1233965SjdpMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1333965SjdpLibrary General Public License for more details.
1433965Sjdp
1533965SjdpYou should have received a copy of the GNU Library General Public
1633965SjdpLicense along with libiberty; see the file COPYING.LIB.  If
17218822Sdimnot, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18218822SdimBoston, MA 02110-1301, USA.  */
1933965Sjdp
2060484Sobrien#include "config.h"
2160484Sobrien
2233965Sjdp#include "ansidecl.h"
2333965Sjdp#include "libiberty.h"
2433965Sjdp
25104834Sobrien/* On some systems (such as WindISS), you must include <sys/types.h>
26104834Sobrien   to get the definition of "time_t" before you include <time.h>.  */
27104834Sobrien#include <sys/types.h>
28104834Sobrien
2933965Sjdp/* There are several ways to get elapsed execution time; unfortunately no
3033965Sjdp   single way is available for all host systems, nor are there reliable
3133965Sjdp   ways to find out which way is correct for a given host. */
3233965Sjdp
3377298Sobrien#ifdef TIME_WITH_SYS_TIME
3477298Sobrien# include <sys/time.h>
3577298Sobrien# include <time.h>
3677298Sobrien#else
3777298Sobrien# if HAVE_SYS_TIME_H
3877298Sobrien#  include <sys/time.h>
3977298Sobrien# else
4077298Sobrien#  ifdef HAVE_TIME_H
4177298Sobrien#   include <time.h>
4277298Sobrien#  endif
4377298Sobrien# endif
4477298Sobrien#endif
4533965Sjdp
4660484Sobrien#if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
4733965Sjdp#include <sys/resource.h>
4833965Sjdp#endif
4933965Sjdp
5033965Sjdp#ifdef HAVE_TIMES
5160484Sobrien#ifdef HAVE_SYS_PARAM_H
5233965Sjdp#include <sys/param.h>
5333965Sjdp#endif
5433965Sjdp#include <sys/times.h>
5533965Sjdp#endif
5633965Sjdp
5760484Sobrien#ifdef HAVE_UNISTD_H
5860484Sobrien#include <unistd.h>
5960484Sobrien#endif
6060484Sobrien
6133965Sjdp/* This is a fallback; if wrong, it will likely make obviously wrong
6233965Sjdp   results. */
6333965Sjdp
6433965Sjdp#ifndef CLOCKS_PER_SEC
6533965Sjdp#define CLOCKS_PER_SEC 1
6633965Sjdp#endif
6733965Sjdp
6860484Sobrien#ifdef _SC_CLK_TCK
6960484Sobrien#define GNU_HZ  sysconf(_SC_CLK_TCK)
7060484Sobrien#else
7160484Sobrien#ifdef HZ
7260484Sobrien#define GNU_HZ  HZ
7360484Sobrien#else
7460484Sobrien#ifdef CLOCKS_PER_SEC
7560484Sobrien#define GNU_HZ  CLOCKS_PER_SEC
7660484Sobrien#endif
7760484Sobrien#endif
7860484Sobrien#endif
7960484Sobrien
8089857Sobrien/*
8189857Sobrien
8289857Sobrien@deftypefn Replacement long get_run_time (void)
8389857Sobrien
8489857SobrienReturns the time used so far, in microseconds.  If possible, this is
8589857Sobrienthe time used by this process, else it is the elapsed time since the
8689857Sobrienprocess started.
8789857Sobrien
8889857Sobrien@end deftypefn
8989857Sobrien
9089857Sobrien*/
9189857Sobrien
9233965Sjdplong
93218822Sdimget_run_time (void)
9433965Sjdp{
9560484Sobrien#if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
9633965Sjdp  struct rusage rusage;
9733965Sjdp
9833965Sjdp  getrusage (0, &rusage);
9933965Sjdp  return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
10033965Sjdp	  + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
10133965Sjdp#else /* ! HAVE_GETRUSAGE */
10233965Sjdp#ifdef HAVE_TIMES
10333965Sjdp  struct tms tms;
10433965Sjdp
10533965Sjdp  times (&tms);
10660484Sobrien  return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
10733965Sjdp#else /* ! HAVE_TIMES */
10833965Sjdp  /* Fall back on clock and hope it's correctly implemented. */
10933965Sjdp  const long clocks_per_sec = CLOCKS_PER_SEC;
11033965Sjdp  if (clocks_per_sec <= 1000000)
11133965Sjdp    return clock () * (1000000 / clocks_per_sec);
11233965Sjdp  else
11333965Sjdp    return clock () / clocks_per_sec;
11433965Sjdp#endif  /* HAVE_TIMES */
11533965Sjdp#endif  /* HAVE_GETRUSAGE */
11633965Sjdp}
117