14fee23f9Smrg /* Return time used so far, in microseconds.
2*b1e83836Smrg Copyright (C) 1994-2022 Free Software Foundation, Inc.
34fee23f9Smrg
44fee23f9Smrg This file is part of the libiberty library.
54fee23f9Smrg Libiberty is free software; you can redistribute it and/or
64fee23f9Smrg modify it under the terms of the GNU Library General Public
74fee23f9Smrg License as published by the Free Software Foundation; either
84fee23f9Smrg version 2 of the License, or (at your option) any later version.
94fee23f9Smrg
104fee23f9Smrg Libiberty is distributed in the hope that it will be useful,
114fee23f9Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
124fee23f9Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
134fee23f9Smrg Library General Public License for more details.
144fee23f9Smrg
154fee23f9Smrg You should have received a copy of the GNU Library General Public
164fee23f9Smrg License along with libiberty; see the file COPYING.LIB. If
174fee23f9Smrg not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
184fee23f9Smrg Boston, MA 02110-1301, USA. */
194fee23f9Smrg
204fee23f9Smrg #include "config.h"
214fee23f9Smrg
224fee23f9Smrg #include "ansidecl.h"
234fee23f9Smrg #include "libiberty.h"
244fee23f9Smrg
254fee23f9Smrg /* On some systems (such as WindISS), you must include <sys/types.h>
264fee23f9Smrg to get the definition of "time_t" before you include <time.h>. */
274fee23f9Smrg #include <sys/types.h>
284fee23f9Smrg
294fee23f9Smrg /* There are several ways to get elapsed execution time; unfortunately no
304fee23f9Smrg single way is available for all host systems, nor are there reliable
314fee23f9Smrg ways to find out which way is correct for a given host. */
324fee23f9Smrg
334fee23f9Smrg #ifdef TIME_WITH_SYS_TIME
344fee23f9Smrg # include <sys/time.h>
354fee23f9Smrg # include <time.h>
364fee23f9Smrg #else
374fee23f9Smrg # if HAVE_SYS_TIME_H
384fee23f9Smrg # include <sys/time.h>
394fee23f9Smrg # else
404fee23f9Smrg # ifdef HAVE_TIME_H
414fee23f9Smrg # include <time.h>
424fee23f9Smrg # endif
434fee23f9Smrg # endif
444fee23f9Smrg #endif
454fee23f9Smrg
464fee23f9Smrg #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
474fee23f9Smrg #include <sys/resource.h>
484fee23f9Smrg #endif
494fee23f9Smrg
504fee23f9Smrg #ifdef HAVE_TIMES
514fee23f9Smrg #ifdef HAVE_SYS_PARAM_H
524fee23f9Smrg #include <sys/param.h>
534fee23f9Smrg #endif
544fee23f9Smrg #include <sys/times.h>
554fee23f9Smrg #endif
564fee23f9Smrg
574fee23f9Smrg #ifdef HAVE_UNISTD_H
584fee23f9Smrg #include <unistd.h>
594fee23f9Smrg #endif
604fee23f9Smrg
614fee23f9Smrg /* This is a fallback; if wrong, it will likely make obviously wrong
624fee23f9Smrg results. */
634fee23f9Smrg
644fee23f9Smrg #ifndef CLOCKS_PER_SEC
654fee23f9Smrg #define CLOCKS_PER_SEC 1
664fee23f9Smrg #endif
674fee23f9Smrg
68f9a78e0eSmrg #ifndef RUSAGE_SELF
69f9a78e0eSmrg #define RUSAGE_SELF 0
70f9a78e0eSmrg #endif
71f9a78e0eSmrg
724fee23f9Smrg #ifdef _SC_CLK_TCK
734fee23f9Smrg #define GNU_HZ sysconf(_SC_CLK_TCK)
744fee23f9Smrg #else
754fee23f9Smrg #ifdef HZ
764fee23f9Smrg #define GNU_HZ HZ
774fee23f9Smrg #else
784fee23f9Smrg #ifdef CLOCKS_PER_SEC
794fee23f9Smrg #define GNU_HZ CLOCKS_PER_SEC
804fee23f9Smrg #endif
814fee23f9Smrg #endif
824fee23f9Smrg #endif
834fee23f9Smrg
844fee23f9Smrg /*
854fee23f9Smrg
864fee23f9Smrg @deftypefn Replacement long get_run_time (void)
874fee23f9Smrg
884fee23f9Smrg Returns the time used so far, in microseconds. If possible, this is
894fee23f9Smrg the time used by this process, else it is the elapsed time since the
904fee23f9Smrg process started.
914fee23f9Smrg
924fee23f9Smrg @end deftypefn
934fee23f9Smrg
944fee23f9Smrg */
954fee23f9Smrg
964fee23f9Smrg long
get_run_time(void)974fee23f9Smrg get_run_time (void)
984fee23f9Smrg {
994fee23f9Smrg #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
1004fee23f9Smrg struct rusage rusage;
1014fee23f9Smrg
102f9a78e0eSmrg getrusage (RUSAGE_SELF, &rusage);
1034fee23f9Smrg return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
1044fee23f9Smrg + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
1054fee23f9Smrg #else /* ! HAVE_GETRUSAGE */
1064fee23f9Smrg #ifdef HAVE_TIMES
1074fee23f9Smrg struct tms tms;
1084fee23f9Smrg
1094fee23f9Smrg times (&tms);
1104fee23f9Smrg return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
1114fee23f9Smrg #else /* ! HAVE_TIMES */
1124fee23f9Smrg /* Fall back on clock and hope it's correctly implemented. */
1134fee23f9Smrg const long clocks_per_sec = CLOCKS_PER_SEC;
1144fee23f9Smrg if (clocks_per_sec <= 1000000)
1154fee23f9Smrg return clock () * (1000000 / clocks_per_sec);
1164fee23f9Smrg else
1174fee23f9Smrg return clock () / clocks_per_sec;
1184fee23f9Smrg #endif /* HAVE_TIMES */
1194fee23f9Smrg #endif /* HAVE_GETRUSAGE */
1204fee23f9Smrg }
121