xref: /netbsd-src/external/gpl3/binutils.old/dist/libiberty/getruntime.c (revision e992f068c547fd6e84b3f104dc2340adcc955732)
116dce513Schristos /* Return time used so far, in microseconds.
2*e992f068Schristos    Copyright (C) 1994-2022 Free Software Foundation, Inc.
316dce513Schristos 
416dce513Schristos This file is part of the libiberty library.
516dce513Schristos Libiberty is free software; you can redistribute it and/or
616dce513Schristos modify it under the terms of the GNU Library General Public
716dce513Schristos License as published by the Free Software Foundation; either
816dce513Schristos version 2 of the License, or (at your option) any later version.
916dce513Schristos 
1016dce513Schristos Libiberty 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 GNU
1316dce513Schristos Library General Public License for more details.
1416dce513Schristos 
1516dce513Schristos You should have received a copy of the GNU Library General Public
1616dce513Schristos License along with libiberty; see the file COPYING.LIB.  If
1716dce513Schristos not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
1816dce513Schristos Boston, MA 02110-1301, USA.  */
1916dce513Schristos 
2016dce513Schristos #include "config.h"
2116dce513Schristos 
2216dce513Schristos #include "ansidecl.h"
2316dce513Schristos #include "libiberty.h"
2416dce513Schristos 
2516dce513Schristos /* On some systems (such as WindISS), you must include <sys/types.h>
2616dce513Schristos    to get the definition of "time_t" before you include <time.h>.  */
2716dce513Schristos #include <sys/types.h>
2816dce513Schristos 
2916dce513Schristos /* There are several ways to get elapsed execution time; unfortunately no
3016dce513Schristos    single way is available for all host systems, nor are there reliable
3116dce513Schristos    ways to find out which way is correct for a given host. */
3216dce513Schristos 
3316dce513Schristos #ifdef TIME_WITH_SYS_TIME
3416dce513Schristos # include <sys/time.h>
3516dce513Schristos # include <time.h>
3616dce513Schristos #else
3716dce513Schristos # if HAVE_SYS_TIME_H
3816dce513Schristos #  include <sys/time.h>
3916dce513Schristos # else
4016dce513Schristos #  ifdef HAVE_TIME_H
4116dce513Schristos #   include <time.h>
4216dce513Schristos #  endif
4316dce513Schristos # endif
4416dce513Schristos #endif
4516dce513Schristos 
4616dce513Schristos #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
4716dce513Schristos #include <sys/resource.h>
4816dce513Schristos #endif
4916dce513Schristos 
5016dce513Schristos #ifdef HAVE_TIMES
5116dce513Schristos #ifdef HAVE_SYS_PARAM_H
5216dce513Schristos #include <sys/param.h>
5316dce513Schristos #endif
5416dce513Schristos #include <sys/times.h>
5516dce513Schristos #endif
5616dce513Schristos 
5716dce513Schristos #ifdef HAVE_UNISTD_H
5816dce513Schristos #include <unistd.h>
5916dce513Schristos #endif
6016dce513Schristos 
6116dce513Schristos /* This is a fallback; if wrong, it will likely make obviously wrong
6216dce513Schristos    results. */
6316dce513Schristos 
6416dce513Schristos #ifndef CLOCKS_PER_SEC
6516dce513Schristos #define CLOCKS_PER_SEC 1
6616dce513Schristos #endif
6716dce513Schristos 
6816dce513Schristos #ifndef RUSAGE_SELF
6916dce513Schristos #define RUSAGE_SELF 0
7016dce513Schristos #endif
7116dce513Schristos 
7216dce513Schristos #ifdef _SC_CLK_TCK
7316dce513Schristos #define GNU_HZ  sysconf(_SC_CLK_TCK)
7416dce513Schristos #else
7516dce513Schristos #ifdef HZ
7616dce513Schristos #define GNU_HZ  HZ
7716dce513Schristos #else
7816dce513Schristos #ifdef CLOCKS_PER_SEC
7916dce513Schristos #define GNU_HZ  CLOCKS_PER_SEC
8016dce513Schristos #endif
8116dce513Schristos #endif
8216dce513Schristos #endif
8316dce513Schristos 
8416dce513Schristos /*
8516dce513Schristos 
8616dce513Schristos @deftypefn Replacement long get_run_time (void)
8716dce513Schristos 
8816dce513Schristos Returns the time used so far, in microseconds.  If possible, this is
8916dce513Schristos the time used by this process, else it is the elapsed time since the
9016dce513Schristos process started.
9116dce513Schristos 
9216dce513Schristos @end deftypefn
9316dce513Schristos 
9416dce513Schristos */
9516dce513Schristos 
9616dce513Schristos long
get_run_time(void)9716dce513Schristos get_run_time (void)
9816dce513Schristos {
9916dce513Schristos #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
10016dce513Schristos   struct rusage rusage;
10116dce513Schristos 
10216dce513Schristos   getrusage (RUSAGE_SELF, &rusage);
10316dce513Schristos   return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
10416dce513Schristos 	  + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
10516dce513Schristos #else /* ! HAVE_GETRUSAGE */
10616dce513Schristos #ifdef HAVE_TIMES
10716dce513Schristos   struct tms tms;
10816dce513Schristos 
10916dce513Schristos   times (&tms);
11016dce513Schristos   return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
11116dce513Schristos #else /* ! HAVE_TIMES */
11216dce513Schristos   /* Fall back on clock and hope it's correctly implemented. */
11316dce513Schristos   const long clocks_per_sec = CLOCKS_PER_SEC;
11416dce513Schristos   if (clocks_per_sec <= 1000000)
11516dce513Schristos     return clock () * (1000000 / clocks_per_sec);
11616dce513Schristos   else
11716dce513Schristos     return clock () / clocks_per_sec;
11816dce513Schristos #endif  /* HAVE_TIMES */
11916dce513Schristos #endif  /* HAVE_GETRUSAGE */
12016dce513Schristos }
121