xref: /openbsd/gnu/lib/libiberty/src/getruntime.c (revision 150b7e42)
100bf4279Sespie /* Return time used so far, in microseconds.
237c53322Sespie    Copyright (C) 1994, 1999, 2002 Free Software Foundation, Inc.
300bf4279Sespie 
400bf4279Sespie This file is part of the libiberty library.
500bf4279Sespie Libiberty is free software; you can redistribute it and/or
600bf4279Sespie modify it under the terms of the GNU Library General Public
700bf4279Sespie License as published by the Free Software Foundation; either
800bf4279Sespie version 2 of the License, or (at your option) any later version.
900bf4279Sespie 
1000bf4279Sespie Libiberty is distributed in the hope that it will be useful,
1100bf4279Sespie but WITHOUT ANY WARRANTY; without even the implied warranty of
1200bf4279Sespie MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1300bf4279Sespie Library General Public License for more details.
1400bf4279Sespie 
1500bf4279Sespie You should have received a copy of the GNU Library General Public
1600bf4279Sespie License along with libiberty; see the file COPYING.LIB.  If
17*150b7e42Smiod not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18*150b7e42Smiod Boston, MA 02110-1301, USA.  */
1900bf4279Sespie 
2000bf4279Sespie #include "config.h"
2100bf4279Sespie 
2200bf4279Sespie #include "ansidecl.h"
2300bf4279Sespie #include "libiberty.h"
2400bf4279Sespie 
2537c53322Sespie /* On some systems (such as WindISS), you must include <sys/types.h>
2637c53322Sespie    to get the definition of "time_t" before you include <time.h>.  */
2737c53322Sespie #include <sys/types.h>
2837c53322Sespie 
2900bf4279Sespie /* There are several ways to get elapsed execution time; unfortunately no
3000bf4279Sespie    single way is available for all host systems, nor are there reliable
3100bf4279Sespie    ways to find out which way is correct for a given host. */
3200bf4279Sespie 
33f5dd06f4Sespie #ifdef TIME_WITH_SYS_TIME
34f5dd06f4Sespie # include <sys/time.h>
3500bf4279Sespie # include <time.h>
36f5dd06f4Sespie #else
37f5dd06f4Sespie # if HAVE_SYS_TIME_H
38f5dd06f4Sespie #  include <sys/time.h>
39f5dd06f4Sespie # else
40f5dd06f4Sespie #  ifdef HAVE_TIME_H
41f5dd06f4Sespie #   include <time.h>
42f5dd06f4Sespie #  endif
43f5dd06f4Sespie # endif
44f5dd06f4Sespie #endif
4500bf4279Sespie 
4600bf4279Sespie #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
4700bf4279Sespie #include <sys/resource.h>
4800bf4279Sespie #endif
4900bf4279Sespie 
5000bf4279Sespie #ifdef HAVE_TIMES
5100bf4279Sespie #ifdef HAVE_SYS_PARAM_H
5200bf4279Sespie #include <sys/param.h>
5300bf4279Sespie #endif
5400bf4279Sespie #include <sys/times.h>
5500bf4279Sespie #endif
5600bf4279Sespie 
5700bf4279Sespie #ifdef HAVE_UNISTD_H
5800bf4279Sespie #include <unistd.h>
5900bf4279Sespie #endif
6000bf4279Sespie 
6100bf4279Sespie /* This is a fallback; if wrong, it will likely make obviously wrong
6200bf4279Sespie    results. */
6300bf4279Sespie 
6400bf4279Sespie #ifndef CLOCKS_PER_SEC
6500bf4279Sespie #define CLOCKS_PER_SEC 1
6600bf4279Sespie #endif
6700bf4279Sespie 
6800bf4279Sespie #ifdef _SC_CLK_TCK
6900bf4279Sespie #define GNU_HZ  sysconf(_SC_CLK_TCK)
7000bf4279Sespie #else
7100bf4279Sespie #ifdef HZ
7200bf4279Sespie #define GNU_HZ  HZ
7300bf4279Sespie #else
7400bf4279Sespie #ifdef CLOCKS_PER_SEC
7500bf4279Sespie #define GNU_HZ  CLOCKS_PER_SEC
7600bf4279Sespie #endif
7700bf4279Sespie #endif
7800bf4279Sespie #endif
7900bf4279Sespie 
8037c53322Sespie /*
8137c53322Sespie 
8237c53322Sespie @deftypefn Replacement long get_run_time (void)
8337c53322Sespie 
8437c53322Sespie Returns the time used so far, in microseconds.  If possible, this is
8537c53322Sespie the time used by this process, else it is the elapsed time since the
8637c53322Sespie process started.
8737c53322Sespie 
8837c53322Sespie @end deftypefn
8937c53322Sespie 
9037c53322Sespie */
9137c53322Sespie 
9200bf4279Sespie long
get_run_time(void)93*150b7e42Smiod get_run_time (void)
9400bf4279Sespie {
9500bf4279Sespie #if defined (HAVE_GETRUSAGE) && defined (HAVE_SYS_RESOURCE_H)
9600bf4279Sespie   struct rusage rusage;
9700bf4279Sespie 
9800bf4279Sespie   getrusage (0, &rusage);
9900bf4279Sespie   return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
10000bf4279Sespie 	  + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
10100bf4279Sespie #else /* ! HAVE_GETRUSAGE */
10200bf4279Sespie #ifdef HAVE_TIMES
10300bf4279Sespie   struct tms tms;
10400bf4279Sespie 
10500bf4279Sespie   times (&tms);
10600bf4279Sespie   return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
10700bf4279Sespie #else /* ! HAVE_TIMES */
10800bf4279Sespie   /* Fall back on clock and hope it's correctly implemented. */
10900bf4279Sespie   const long clocks_per_sec = CLOCKS_PER_SEC;
11000bf4279Sespie   if (clocks_per_sec <= 1000000)
11100bf4279Sespie     return clock () * (1000000 / clocks_per_sec);
11200bf4279Sespie   else
11300bf4279Sespie     return clock () / clocks_per_sec;
11400bf4279Sespie #endif  /* HAVE_TIMES */
11500bf4279Sespie #endif  /* HAVE_GETRUSAGE */
11600bf4279Sespie }
117