133965Sjdp/* ANSI-compatible clock function.
260484Sobrien   Copyright (C) 1994, 1995, 1999 Free Software Foundation, Inc.
333965Sjdp
433965SjdpThis file is part of the libiberty library.  This library is free
533965Sjdpsoftware; you can redistribute it and/or modify it under the
633965Sjdpterms of the GNU General Public License as published by the
733965SjdpFree Software Foundation; either version 2, or (at your option)
833965Sjdpany later version.
933965Sjdp
1033965SjdpThis library 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
1333965SjdpGNU General Public License for more details.
1433965Sjdp
1533965SjdpYou should have received a copy of the GNU General Public License
1633965Sjdpalong with GNU CC; see the file COPYING.  If not, write to
17218822Sdimthe Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
1833965Sjdp
1933965SjdpAs a special exception, if you link this library with files
2033965Sjdpcompiled with a GNU compiler to produce an executable, this does not cause
2133965Sjdpthe resulting executable to be covered by the GNU General Public License.
2233965SjdpThis exception does not however invalidate any other reasons why
2333965Sjdpthe executable file might be covered by the GNU General Public License. */
2433965Sjdp
2589857Sobrien/*
2689857Sobrien
2789857Sobrien@deftypefn Supplemental long clock (void)
2889857Sobrien
2989857SobrienReturns an approximation of the CPU time used by the process as a
3089857Sobrien@code{clock_t}; divide this number by @samp{CLOCKS_PER_SEC} to get the
3189857Sobriennumber of seconds used.
3289857Sobrien
3389857Sobrien@end deftypefn
3489857Sobrien
3589857Sobrien*/
3689857Sobrien
3760484Sobrien#include "config.h"
3860484Sobrien
3933965Sjdp#ifdef HAVE_GETRUSAGE
4033965Sjdp#include <sys/time.h>
4133965Sjdp#include <sys/resource.h>
4233965Sjdp#endif
4333965Sjdp
4433965Sjdp#ifdef HAVE_TIMES
4560484Sobrien#ifdef HAVE_SYS_PARAM_H
4633965Sjdp#include <sys/param.h>
4733965Sjdp#endif
4833965Sjdp#include <sys/times.h>
4933965Sjdp#endif
5033965Sjdp
5160484Sobrien#ifdef HAVE_UNISTD_H
5260484Sobrien#include <unistd.h>
5360484Sobrien#endif
5460484Sobrien
5560484Sobrien#ifdef _SC_CLK_TCK
5660484Sobrien#define GNU_HZ  sysconf(_SC_CLK_TCK)
5760484Sobrien#else
5860484Sobrien#ifdef HZ
5960484Sobrien#define GNU_HZ  HZ
6060484Sobrien#else
6160484Sobrien#ifdef CLOCKS_PER_SEC
6260484Sobrien#define GNU_HZ  CLOCKS_PER_SEC
6360484Sobrien#endif
6460484Sobrien#endif
6560484Sobrien#endif
6660484Sobrien
6733965Sjdp/* FIXME: should be able to declare as clock_t. */
6833965Sjdp
6933965Sjdplong
70218822Sdimclock (void)
7133965Sjdp{
7233965Sjdp#ifdef HAVE_GETRUSAGE
7333965Sjdp  struct rusage rusage;
7433965Sjdp
7533965Sjdp  getrusage (0, &rusage);
7633965Sjdp  return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
7733965Sjdp	  + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
7833965Sjdp#else
7933965Sjdp#ifdef HAVE_TIMES
8033965Sjdp  struct tms tms;
8133965Sjdp
8233965Sjdp  times (&tms);
8360484Sobrien  return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
8433965Sjdp#else
8533965Sjdp#ifdef VMS
8633965Sjdp  struct
8733965Sjdp    {
8833965Sjdp      int proc_user_time;
8933965Sjdp      int proc_system_time;
9033965Sjdp      int child_user_time;
9133965Sjdp      int child_system_time;
9233965Sjdp    } vms_times;
9333965Sjdp
9433965Sjdp  times (&vms_times);
9533965Sjdp  return (vms_times.proc_user_time + vms_times.proc_system_time) * 10000;
9633965Sjdp#else
9733965Sjdp  /* A fallback, if nothing else available. */
9833965Sjdp  return 0;
9933965Sjdp#endif /* VMS */
10033965Sjdp#endif /* HAVE_TIMES */
10133965Sjdp#endif /* HAVE_GETRUSAGE */
10233965Sjdp}
10333965Sjdp
104