xref: /netbsd-src/external/gpl3/gcc.old/dist/libiberty/getruntime.c (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
11debfc3dSmrg /* Return time used so far, in microseconds.
2*8feb0f0bSmrg    Copyright (C) 1994-2020 Free Software Foundation, Inc.
31debfc3dSmrg 
41debfc3dSmrg This file is part of the libiberty library.
51debfc3dSmrg Libiberty is free software; you can redistribute it and/or
61debfc3dSmrg modify it under the terms of the GNU Library General Public
71debfc3dSmrg License as published by the Free Software Foundation; either
81debfc3dSmrg version 2 of the License, or (at your option) any later version.
91debfc3dSmrg 
101debfc3dSmrg Libiberty is distributed in the hope that it will be useful,
111debfc3dSmrg but WITHOUT ANY WARRANTY; without even the implied warranty of
121debfc3dSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
131debfc3dSmrg Library General Public License for more details.
141debfc3dSmrg 
151debfc3dSmrg You should have received a copy of the GNU Library General Public
161debfc3dSmrg License along with libiberty; see the file COPYING.LIB.  If
171debfc3dSmrg not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
181debfc3dSmrg Boston, MA 02110-1301, USA.  */
191debfc3dSmrg 
201debfc3dSmrg #include "config.h"
211debfc3dSmrg 
221debfc3dSmrg #include "ansidecl.h"
231debfc3dSmrg #include "libiberty.h"
241debfc3dSmrg 
251debfc3dSmrg /* On some systems (such as WindISS), you must include <sys/types.h>
261debfc3dSmrg    to get the definition of "time_t" before you include <time.h>.  */
271debfc3dSmrg #include <sys/types.h>
281debfc3dSmrg 
291debfc3dSmrg /* There are several ways to get elapsed execution time; unfortunately no
301debfc3dSmrg    single way is available for all host systems, nor are there reliable
311debfc3dSmrg    ways to find out which way is correct for a given host. */
321debfc3dSmrg 
331debfc3dSmrg #ifdef TIME_WITH_SYS_TIME
341debfc3dSmrg # include <sys/time.h>
351debfc3dSmrg # include <time.h>
361debfc3dSmrg #else
371debfc3dSmrg # if HAVE_SYS_TIME_H
381debfc3dSmrg #  include <sys/time.h>
391debfc3dSmrg # else
401debfc3dSmrg #  ifdef HAVE_TIME_H
411debfc3dSmrg #   include <time.h>
421debfc3dSmrg #  endif
431debfc3dSmrg # endif
441debfc3dSmrg #endif
451debfc3dSmrg 
461debfc3dSmrg #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
471debfc3dSmrg #include <sys/resource.h>
481debfc3dSmrg #endif
491debfc3dSmrg 
501debfc3dSmrg #ifdef HAVE_TIMES
511debfc3dSmrg #ifdef HAVE_SYS_PARAM_H
521debfc3dSmrg #include <sys/param.h>
531debfc3dSmrg #endif
541debfc3dSmrg #include <sys/times.h>
551debfc3dSmrg #endif
561debfc3dSmrg 
571debfc3dSmrg #ifdef HAVE_UNISTD_H
581debfc3dSmrg #include <unistd.h>
591debfc3dSmrg #endif
601debfc3dSmrg 
611debfc3dSmrg /* This is a fallback; if wrong, it will likely make obviously wrong
621debfc3dSmrg    results. */
631debfc3dSmrg 
641debfc3dSmrg #ifndef CLOCKS_PER_SEC
651debfc3dSmrg #define CLOCKS_PER_SEC 1
661debfc3dSmrg #endif
671debfc3dSmrg 
681debfc3dSmrg #ifndef RUSAGE_SELF
691debfc3dSmrg #define RUSAGE_SELF 0
701debfc3dSmrg #endif
711debfc3dSmrg 
721debfc3dSmrg #ifdef _SC_CLK_TCK
731debfc3dSmrg #define GNU_HZ  sysconf(_SC_CLK_TCK)
741debfc3dSmrg #else
751debfc3dSmrg #ifdef HZ
761debfc3dSmrg #define GNU_HZ  HZ
771debfc3dSmrg #else
781debfc3dSmrg #ifdef CLOCKS_PER_SEC
791debfc3dSmrg #define GNU_HZ  CLOCKS_PER_SEC
801debfc3dSmrg #endif
811debfc3dSmrg #endif
821debfc3dSmrg #endif
831debfc3dSmrg 
841debfc3dSmrg /*
851debfc3dSmrg 
861debfc3dSmrg @deftypefn Replacement long get_run_time (void)
871debfc3dSmrg 
881debfc3dSmrg Returns the time used so far, in microseconds.  If possible, this is
891debfc3dSmrg the time used by this process, else it is the elapsed time since the
901debfc3dSmrg process started.
911debfc3dSmrg 
921debfc3dSmrg @end deftypefn
931debfc3dSmrg 
941debfc3dSmrg */
951debfc3dSmrg 
961debfc3dSmrg long
get_run_time(void)971debfc3dSmrg get_run_time (void)
981debfc3dSmrg {
991debfc3dSmrg #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
1001debfc3dSmrg   struct rusage rusage;
1011debfc3dSmrg 
1021debfc3dSmrg   getrusage (RUSAGE_SELF, &rusage);
1031debfc3dSmrg   return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
1041debfc3dSmrg 	  + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
1051debfc3dSmrg #else /* ! HAVE_GETRUSAGE */
1061debfc3dSmrg #ifdef HAVE_TIMES
1071debfc3dSmrg   struct tms tms;
1081debfc3dSmrg 
1091debfc3dSmrg   times (&tms);
1101debfc3dSmrg   return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
1111debfc3dSmrg #else /* ! HAVE_TIMES */
1121debfc3dSmrg   /* Fall back on clock and hope it's correctly implemented. */
1131debfc3dSmrg   const long clocks_per_sec = CLOCKS_PER_SEC;
1141debfc3dSmrg   if (clocks_per_sec <= 1000000)
1151debfc3dSmrg     return clock () * (1000000 / clocks_per_sec);
1161debfc3dSmrg   else
1171debfc3dSmrg     return clock () / clocks_per_sec;
1181debfc3dSmrg #endif  /* HAVE_TIMES */
1191debfc3dSmrg #endif  /* HAVE_GETRUSAGE */
1201debfc3dSmrg }
121