12a6b7db3Sskrll /* Return time used so far, in microseconds.
2*f22f0ef4Schristos    Copyright (C) 1994-2022 Free Software Foundation, Inc.
32a6b7db3Sskrll 
42a6b7db3Sskrll This file is part of the libiberty library.
52a6b7db3Sskrll Libiberty is free software; you can redistribute it and/or
62a6b7db3Sskrll modify it under the terms of the GNU Library General Public
72a6b7db3Sskrll License as published by the Free Software Foundation; either
82a6b7db3Sskrll version 2 of the License, or (at your option) any later version.
92a6b7db3Sskrll 
102a6b7db3Sskrll Libiberty is distributed in the hope that it will be useful,
112a6b7db3Sskrll but WITHOUT ANY WARRANTY; without even the implied warranty of
122a6b7db3Sskrll MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
132a6b7db3Sskrll Library General Public License for more details.
142a6b7db3Sskrll 
152a6b7db3Sskrll You should have received a copy of the GNU Library General Public
162a6b7db3Sskrll License along with libiberty; see the file COPYING.LIB.  If
172a6b7db3Sskrll not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
182a6b7db3Sskrll Boston, MA 02110-1301, USA.  */
192a6b7db3Sskrll 
202a6b7db3Sskrll #include "config.h"
212a6b7db3Sskrll 
222a6b7db3Sskrll #include "ansidecl.h"
232a6b7db3Sskrll #include "libiberty.h"
242a6b7db3Sskrll 
252a6b7db3Sskrll /* On some systems (such as WindISS), you must include <sys/types.h>
262a6b7db3Sskrll    to get the definition of "time_t" before you include <time.h>.  */
272a6b7db3Sskrll #include <sys/types.h>
282a6b7db3Sskrll 
292a6b7db3Sskrll /* There are several ways to get elapsed execution time; unfortunately no
302a6b7db3Sskrll    single way is available for all host systems, nor are there reliable
312a6b7db3Sskrll    ways to find out which way is correct for a given host. */
322a6b7db3Sskrll 
332a6b7db3Sskrll #ifdef TIME_WITH_SYS_TIME
342a6b7db3Sskrll # include <sys/time.h>
352a6b7db3Sskrll # include <time.h>
362a6b7db3Sskrll #else
372a6b7db3Sskrll # if HAVE_SYS_TIME_H
382a6b7db3Sskrll #  include <sys/time.h>
392a6b7db3Sskrll # else
402a6b7db3Sskrll #  ifdef HAVE_TIME_H
412a6b7db3Sskrll #   include <time.h>
422a6b7db3Sskrll #  endif
432a6b7db3Sskrll # endif
442a6b7db3Sskrll #endif
452a6b7db3Sskrll 
462a6b7db3Sskrll #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
472a6b7db3Sskrll #include <sys/resource.h>
482a6b7db3Sskrll #endif
492a6b7db3Sskrll 
502a6b7db3Sskrll #ifdef HAVE_TIMES
512a6b7db3Sskrll #ifdef HAVE_SYS_PARAM_H
522a6b7db3Sskrll #include <sys/param.h>
532a6b7db3Sskrll #endif
542a6b7db3Sskrll #include <sys/times.h>
552a6b7db3Sskrll #endif
562a6b7db3Sskrll 
572a6b7db3Sskrll #ifdef HAVE_UNISTD_H
582a6b7db3Sskrll #include <unistd.h>
592a6b7db3Sskrll #endif
602a6b7db3Sskrll 
612a6b7db3Sskrll /* This is a fallback; if wrong, it will likely make obviously wrong
622a6b7db3Sskrll    results. */
632a6b7db3Sskrll 
642a6b7db3Sskrll #ifndef CLOCKS_PER_SEC
652a6b7db3Sskrll #define CLOCKS_PER_SEC 1
662a6b7db3Sskrll #endif
672a6b7db3Sskrll 
6875f9f1baSchristos #ifndef RUSAGE_SELF
6975f9f1baSchristos #define RUSAGE_SELF 0
7075f9f1baSchristos #endif
7175f9f1baSchristos 
722a6b7db3Sskrll #ifdef _SC_CLK_TCK
732a6b7db3Sskrll #define GNU_HZ  sysconf(_SC_CLK_TCK)
742a6b7db3Sskrll #else
752a6b7db3Sskrll #ifdef HZ
762a6b7db3Sskrll #define GNU_HZ  HZ
772a6b7db3Sskrll #else
782a6b7db3Sskrll #ifdef CLOCKS_PER_SEC
792a6b7db3Sskrll #define GNU_HZ  CLOCKS_PER_SEC
802a6b7db3Sskrll #endif
812a6b7db3Sskrll #endif
822a6b7db3Sskrll #endif
832a6b7db3Sskrll 
842a6b7db3Sskrll /*
852a6b7db3Sskrll 
862a6b7db3Sskrll @deftypefn Replacement long get_run_time (void)
872a6b7db3Sskrll 
882a6b7db3Sskrll Returns the time used so far, in microseconds.  If possible, this is
892a6b7db3Sskrll the time used by this process, else it is the elapsed time since the
902a6b7db3Sskrll process started.
912a6b7db3Sskrll 
922a6b7db3Sskrll @end deftypefn
932a6b7db3Sskrll 
942a6b7db3Sskrll */
952a6b7db3Sskrll 
962a6b7db3Sskrll long
get_run_time(void)972a6b7db3Sskrll get_run_time (void)
982a6b7db3Sskrll {
992a6b7db3Sskrll #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
1002a6b7db3Sskrll   struct rusage rusage;
1012a6b7db3Sskrll 
10275f9f1baSchristos   getrusage (RUSAGE_SELF, &rusage);
1032a6b7db3Sskrll   return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
1042a6b7db3Sskrll 	  + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
1052a6b7db3Sskrll #else /* ! HAVE_GETRUSAGE */
1062a6b7db3Sskrll #ifdef HAVE_TIMES
1072a6b7db3Sskrll   struct tms tms;
1082a6b7db3Sskrll 
1092a6b7db3Sskrll   times (&tms);
1102a6b7db3Sskrll   return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
1112a6b7db3Sskrll #else /* ! HAVE_TIMES */
1122a6b7db3Sskrll   /* Fall back on clock and hope it's correctly implemented. */
1132a6b7db3Sskrll   const long clocks_per_sec = CLOCKS_PER_SEC;
1142a6b7db3Sskrll   if (clocks_per_sec <= 1000000)
1152a6b7db3Sskrll     return clock () * (1000000 / clocks_per_sec);
1162a6b7db3Sskrll   else
1172a6b7db3Sskrll     return clock () / clocks_per_sec;
1182a6b7db3Sskrll #endif  /* HAVE_TIMES */
1192a6b7db3Sskrll #endif  /* HAVE_GETRUSAGE */
1202a6b7db3Sskrll }
121