100bf4279Sespie /* ANSI-compatible clock function.
200bf4279Sespie Copyright (C) 1994, 1995, 1999 Free Software Foundation, Inc.
300bf4279Sespie
400bf4279Sespie This file is part of the libiberty library. This library is free
500bf4279Sespie software; you can redistribute it and/or modify it under the
600bf4279Sespie terms of the GNU General Public License as published by the
700bf4279Sespie Free Software Foundation; either version 2, or (at your option)
800bf4279Sespie any later version.
900bf4279Sespie
1000bf4279Sespie This library is distributed in the hope that it will be useful,
1100bf4279Sespie but WITHOUT ANY WARRANTY; without even the implied warranty of
1200bf4279Sespie MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1300bf4279Sespie GNU General Public License for more details.
1400bf4279Sespie
1500bf4279Sespie You should have received a copy of the GNU General Public License
1600bf4279Sespie along with GNU CC; see the file COPYING. If not, write to
17*20fce977Smiod the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
1800bf4279Sespie
1900bf4279Sespie As a special exception, if you link this library with files
2000bf4279Sespie compiled with a GNU compiler to produce an executable, this does not cause
2100bf4279Sespie the resulting executable to be covered by the GNU General Public License.
2200bf4279Sespie This exception does not however invalidate any other reasons why
2300bf4279Sespie the executable file might be covered by the GNU General Public License. */
2400bf4279Sespie
259588ddcfSespie /*
269588ddcfSespie
279588ddcfSespie @deftypefn Supplemental long clock (void)
289588ddcfSespie
299588ddcfSespie Returns an approximation of the CPU time used by the process as a
309588ddcfSespie @code{clock_t}; divide this number by @samp{CLOCKS_PER_SEC} to get the
319588ddcfSespie number of seconds used.
329588ddcfSespie
339588ddcfSespie @end deftypefn
349588ddcfSespie
359588ddcfSespie */
369588ddcfSespie
3700bf4279Sespie #include "config.h"
3800bf4279Sespie
3900bf4279Sespie #ifdef HAVE_GETRUSAGE
4000bf4279Sespie #include <sys/time.h>
4100bf4279Sespie #include <sys/resource.h>
4200bf4279Sespie #endif
4300bf4279Sespie
4400bf4279Sespie #ifdef HAVE_TIMES
4500bf4279Sespie #ifdef HAVE_SYS_PARAM_H
4600bf4279Sespie #include <sys/param.h>
4700bf4279Sespie #endif
4800bf4279Sespie #include <sys/times.h>
4900bf4279Sespie #endif
5000bf4279Sespie
5100bf4279Sespie #ifdef HAVE_UNISTD_H
5200bf4279Sespie #include <unistd.h>
5300bf4279Sespie #endif
5400bf4279Sespie
5500bf4279Sespie #ifdef _SC_CLK_TCK
5600bf4279Sespie #define GNU_HZ sysconf(_SC_CLK_TCK)
5700bf4279Sespie #else
5800bf4279Sespie #ifdef HZ
5900bf4279Sespie #define GNU_HZ HZ
6000bf4279Sespie #else
6100bf4279Sespie #ifdef CLOCKS_PER_SEC
6200bf4279Sespie #define GNU_HZ CLOCKS_PER_SEC
6300bf4279Sespie #endif
6400bf4279Sespie #endif
6500bf4279Sespie #endif
6600bf4279Sespie
6700bf4279Sespie /* FIXME: should be able to declare as clock_t. */
6800bf4279Sespie
6900bf4279Sespie long
clock(void)70*20fce977Smiod clock (void)
7100bf4279Sespie {
7200bf4279Sespie #ifdef HAVE_GETRUSAGE
7300bf4279Sespie struct rusage rusage;
7400bf4279Sespie
7500bf4279Sespie getrusage (0, &rusage);
7600bf4279Sespie return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
7700bf4279Sespie + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
7800bf4279Sespie #else
7900bf4279Sespie #ifdef HAVE_TIMES
8000bf4279Sespie struct tms tms;
8100bf4279Sespie
8200bf4279Sespie times (&tms);
8300bf4279Sespie return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
8400bf4279Sespie #else
8500bf4279Sespie #ifdef VMS
8600bf4279Sespie struct
8700bf4279Sespie {
8800bf4279Sespie int proc_user_time;
8900bf4279Sespie int proc_system_time;
9000bf4279Sespie int child_user_time;
9100bf4279Sespie int child_system_time;
9200bf4279Sespie } vms_times;
9300bf4279Sespie
9400bf4279Sespie times (&vms_times);
9500bf4279Sespie return (vms_times.proc_user_time + vms_times.proc_system_time) * 10000;
9600bf4279Sespie #else
9700bf4279Sespie /* A fallback, if nothing else available. */
9800bf4279Sespie return 0;
9900bf4279Sespie #endif /* VMS */
10000bf4279Sespie #endif /* HAVE_TIMES */
10100bf4279Sespie #endif /* HAVE_GETRUSAGE */
10200bf4279Sespie }
10300bf4279Sespie
104