198b9484cSchristos /* Return time used so far, in microseconds. 2*5173eb0aSchristos Copyright (C) 1994-2024 Free Software Foundation, Inc. 398b9484cSchristos 498b9484cSchristos This file is part of the libiberty library. 598b9484cSchristos Libiberty is free software; you can redistribute it and/or 698b9484cSchristos modify it under the terms of the GNU Library General Public 798b9484cSchristos License as published by the Free Software Foundation; either 898b9484cSchristos version 2 of the License, or (at your option) any later version. 998b9484cSchristos 1098b9484cSchristos Libiberty is distributed in the hope that it will be useful, 1198b9484cSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of 1298b9484cSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1398b9484cSchristos Library General Public License for more details. 1498b9484cSchristos 1598b9484cSchristos You should have received a copy of the GNU Library General Public 1698b9484cSchristos License along with libiberty; see the file COPYING.LIB. If 1798b9484cSchristos not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, 1898b9484cSchristos Boston, MA 02110-1301, USA. */ 1998b9484cSchristos 2098b9484cSchristos #include "config.h" 2198b9484cSchristos 2298b9484cSchristos #include "ansidecl.h" 2398b9484cSchristos #include "libiberty.h" 2498b9484cSchristos 2598b9484cSchristos /* On some systems (such as WindISS), you must include <sys/types.h> 2698b9484cSchristos to get the definition of "time_t" before you include <time.h>. */ 2798b9484cSchristos #include <sys/types.h> 2898b9484cSchristos 2998b9484cSchristos /* There are several ways to get elapsed execution time; unfortunately no 3098b9484cSchristos single way is available for all host systems, nor are there reliable 3198b9484cSchristos ways to find out which way is correct for a given host. */ 3298b9484cSchristos 3398b9484cSchristos #ifdef TIME_WITH_SYS_TIME 3498b9484cSchristos # include <sys/time.h> 3598b9484cSchristos # include <time.h> 3698b9484cSchristos #else 3798b9484cSchristos # if HAVE_SYS_TIME_H 3898b9484cSchristos # include <sys/time.h> 3998b9484cSchristos # else 4098b9484cSchristos # ifdef HAVE_TIME_H 4198b9484cSchristos # include <time.h> 4298b9484cSchristos # endif 4398b9484cSchristos # endif 4498b9484cSchristos #endif 4598b9484cSchristos 4698b9484cSchristos #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H) 4798b9484cSchristos #include <sys/resource.h> 4898b9484cSchristos #endif 4998b9484cSchristos 5098b9484cSchristos #ifdef HAVE_TIMES 5198b9484cSchristos #ifdef HAVE_SYS_PARAM_H 5298b9484cSchristos #include <sys/param.h> 5398b9484cSchristos #endif 5498b9484cSchristos #include <sys/times.h> 5598b9484cSchristos #endif 5698b9484cSchristos 5798b9484cSchristos #ifdef HAVE_UNISTD_H 5898b9484cSchristos #include <unistd.h> 5998b9484cSchristos #endif 6098b9484cSchristos 6198b9484cSchristos /* This is a fallback; if wrong, it will likely make obviously wrong 6298b9484cSchristos results. */ 6398b9484cSchristos 6498b9484cSchristos #ifndef CLOCKS_PER_SEC 6598b9484cSchristos #define CLOCKS_PER_SEC 1 6698b9484cSchristos #endif 6798b9484cSchristos 68ba340e45Schristos #ifndef RUSAGE_SELF 69ba340e45Schristos #define RUSAGE_SELF 0 70ba340e45Schristos #endif 71ba340e45Schristos 7298b9484cSchristos #ifdef _SC_CLK_TCK 7398b9484cSchristos #define GNU_HZ sysconf(_SC_CLK_TCK) 7498b9484cSchristos #else 7598b9484cSchristos #ifdef HZ 7698b9484cSchristos #define GNU_HZ HZ 7798b9484cSchristos #else 7898b9484cSchristos #ifdef CLOCKS_PER_SEC 7998b9484cSchristos #define GNU_HZ CLOCKS_PER_SEC 8098b9484cSchristos #endif 8198b9484cSchristos #endif 8298b9484cSchristos #endif 8398b9484cSchristos 8498b9484cSchristos /* 8598b9484cSchristos 8698b9484cSchristos @deftypefn Replacement long get_run_time (void) 8798b9484cSchristos 8898b9484cSchristos Returns the time used so far, in microseconds. If possible, this is 8998b9484cSchristos the time used by this process, else it is the elapsed time since the 9098b9484cSchristos process started. 9198b9484cSchristos 9298b9484cSchristos @end deftypefn 9398b9484cSchristos 9498b9484cSchristos */ 9598b9484cSchristos 9698b9484cSchristos long 9798b9484cSchristos get_run_time (void) 9898b9484cSchristos { 9998b9484cSchristos #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H) 10098b9484cSchristos struct rusage rusage; 10198b9484cSchristos 102ba340e45Schristos getrusage (RUSAGE_SELF, &rusage); 10398b9484cSchristos return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec 10498b9484cSchristos + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec); 10598b9484cSchristos #else /* ! HAVE_GETRUSAGE */ 10698b9484cSchristos #ifdef HAVE_TIMES 10798b9484cSchristos struct tms tms; 10898b9484cSchristos 10998b9484cSchristos times (&tms); 11098b9484cSchristos return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ); 11198b9484cSchristos #else /* ! HAVE_TIMES */ 11298b9484cSchristos /* Fall back on clock and hope it's correctly implemented. */ 11398b9484cSchristos const long clocks_per_sec = CLOCKS_PER_SEC; 11498b9484cSchristos if (clocks_per_sec <= 1000000) 11598b9484cSchristos return clock () * (1000000 / clocks_per_sec); 11698b9484cSchristos else 11798b9484cSchristos return clock () / clocks_per_sec; 11898b9484cSchristos #endif /* HAVE_TIMES */ 11998b9484cSchristos #endif /* HAVE_GETRUSAGE */ 12098b9484cSchristos } 121