xref: /netbsd-src/external/gpl3/binutils.old/dist/libiberty/clock.c (revision e992f068c547fd6e84b3f104dc2340adcc955732)
116dce513Schristos /* ANSI-compatible clock function.
2*e992f068Schristos    Copyright (C) 1994-2022 Free Software Foundation, Inc.
316dce513Schristos 
416dce513Schristos This file is part of the libiberty library.  This library is free
516dce513Schristos software; you can redistribute it and/or modify it under the
616dce513Schristos terms of the GNU General Public License as published by the
716dce513Schristos Free Software Foundation; either version 2, or (at your option)
816dce513Schristos any later version.
916dce513Schristos 
1016dce513Schristos This library is distributed in the hope that it will be useful,
1116dce513Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1216dce513Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1316dce513Schristos GNU General Public License for more details.
1416dce513Schristos 
1516dce513Schristos You should have received a copy of the GNU General Public License
1616dce513Schristos along with GNU CC; see the file COPYING.  If not, write to
1716dce513Schristos the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
1816dce513Schristos 
1916dce513Schristos As a special exception, if you link this library with files
2016dce513Schristos compiled with a GNU compiler to produce an executable, this does not cause
2116dce513Schristos the resulting executable to be covered by the GNU General Public License.
2216dce513Schristos This exception does not however invalidate any other reasons why
2316dce513Schristos the executable file might be covered by the GNU General Public License. */
2416dce513Schristos 
2516dce513Schristos /*
2616dce513Schristos 
2716dce513Schristos @deftypefn Supplemental long clock (void)
2816dce513Schristos 
2916dce513Schristos Returns an approximation of the CPU time used by the process as a
3016dce513Schristos @code{clock_t}; divide this number by @samp{CLOCKS_PER_SEC} to get the
3116dce513Schristos number of seconds used.
3216dce513Schristos 
3316dce513Schristos @end deftypefn
3416dce513Schristos 
3516dce513Schristos */
3616dce513Schristos 
3716dce513Schristos #include "config.h"
3816dce513Schristos 
3916dce513Schristos #ifdef HAVE_GETRUSAGE
4016dce513Schristos #include <sys/time.h>
4116dce513Schristos #include <sys/resource.h>
4216dce513Schristos #endif
4316dce513Schristos 
4416dce513Schristos #ifdef HAVE_TIMES
4516dce513Schristos #ifdef HAVE_SYS_PARAM_H
4616dce513Schristos #include <sys/param.h>
4716dce513Schristos #endif
4816dce513Schristos #include <sys/times.h>
4916dce513Schristos #endif
5016dce513Schristos 
5116dce513Schristos #ifdef HAVE_UNISTD_H
5216dce513Schristos #include <unistd.h>
5316dce513Schristos #endif
5416dce513Schristos 
5516dce513Schristos #ifdef _SC_CLK_TCK
5616dce513Schristos #define GNU_HZ  sysconf(_SC_CLK_TCK)
5716dce513Schristos #else
5816dce513Schristos #ifdef HZ
5916dce513Schristos #define GNU_HZ  HZ
6016dce513Schristos #else
6116dce513Schristos #ifdef CLOCKS_PER_SEC
6216dce513Schristos #define GNU_HZ  CLOCKS_PER_SEC
6316dce513Schristos #endif
6416dce513Schristos #endif
6516dce513Schristos #endif
6616dce513Schristos 
6716dce513Schristos /* FIXME: should be able to declare as clock_t. */
6816dce513Schristos 
6916dce513Schristos long
clock(void)7016dce513Schristos clock (void)
7116dce513Schristos {
7216dce513Schristos #ifdef HAVE_GETRUSAGE
7316dce513Schristos   struct rusage rusage;
7416dce513Schristos 
7516dce513Schristos   getrusage (0, &rusage);
7616dce513Schristos   return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
7716dce513Schristos 	  + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
7816dce513Schristos #else
7916dce513Schristos #ifdef HAVE_TIMES
8016dce513Schristos   struct tms tms;
8116dce513Schristos 
8216dce513Schristos   times (&tms);
8316dce513Schristos   return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
8416dce513Schristos #else
8516dce513Schristos #ifdef VMS
8616dce513Schristos   struct
8716dce513Schristos     {
8816dce513Schristos       int proc_user_time;
8916dce513Schristos       int proc_system_time;
9016dce513Schristos       int child_user_time;
9116dce513Schristos       int child_system_time;
9216dce513Schristos     } vms_times;
9316dce513Schristos 
9416dce513Schristos   times (&vms_times);
9516dce513Schristos   return (vms_times.proc_user_time + vms_times.proc_system_time) * 10000;
9616dce513Schristos #else
9716dce513Schristos   /* A fallback, if nothing else available. */
9816dce513Schristos   return 0;
9916dce513Schristos #endif /* VMS */
10016dce513Schristos #endif /* HAVE_TIMES */
10116dce513Schristos #endif /* HAVE_GETRUSAGE */
10216dce513Schristos }
10316dce513Schristos 
104