xref: /netbsd-src/external/gpl3/gdb/dist/libiberty/clock.c (revision 5173eb0a33e5d83890ba976253e703be4c92557c)
198b9484cSchristos /* ANSI-compatible clock function.
2*5173eb0aSchristos    Copyright (C) 1994-2024 Free Software Foundation, Inc.
398b9484cSchristos 
498b9484cSchristos This file is part of the libiberty library.  This library is free
598b9484cSchristos software; you can redistribute it and/or modify it under the
698b9484cSchristos terms of the GNU General Public License as published by the
798b9484cSchristos Free Software Foundation; either version 2, or (at your option)
898b9484cSchristos any later version.
998b9484cSchristos 
1098b9484cSchristos This library 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
1398b9484cSchristos GNU General Public License for more details.
1498b9484cSchristos 
1598b9484cSchristos You should have received a copy of the GNU General Public License
1698b9484cSchristos along with GNU CC; see the file COPYING.  If not, write to
1798b9484cSchristos the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
1898b9484cSchristos 
1998b9484cSchristos As a special exception, if you link this library with files
2098b9484cSchristos compiled with a GNU compiler to produce an executable, this does not cause
2198b9484cSchristos the resulting executable to be covered by the GNU General Public License.
2298b9484cSchristos This exception does not however invalidate any other reasons why
2398b9484cSchristos the executable file might be covered by the GNU General Public License. */
2498b9484cSchristos 
2598b9484cSchristos /*
2698b9484cSchristos 
2798b9484cSchristos @deftypefn Supplemental long clock (void)
2898b9484cSchristos 
2998b9484cSchristos Returns an approximation of the CPU time used by the process as a
3098b9484cSchristos @code{clock_t}; divide this number by @samp{CLOCKS_PER_SEC} to get the
3198b9484cSchristos number of seconds used.
3298b9484cSchristos 
3398b9484cSchristos @end deftypefn
3498b9484cSchristos 
3598b9484cSchristos */
3698b9484cSchristos 
3798b9484cSchristos #include "config.h"
3898b9484cSchristos 
3998b9484cSchristos #ifdef HAVE_GETRUSAGE
4098b9484cSchristos #include <sys/time.h>
4198b9484cSchristos #include <sys/resource.h>
4298b9484cSchristos #endif
4398b9484cSchristos 
4498b9484cSchristos #ifdef HAVE_TIMES
4598b9484cSchristos #ifdef HAVE_SYS_PARAM_H
4698b9484cSchristos #include <sys/param.h>
4798b9484cSchristos #endif
4898b9484cSchristos #include <sys/times.h>
4998b9484cSchristos #endif
5098b9484cSchristos 
5198b9484cSchristos #ifdef HAVE_UNISTD_H
5298b9484cSchristos #include <unistd.h>
5398b9484cSchristos #endif
5498b9484cSchristos 
5598b9484cSchristos #ifdef _SC_CLK_TCK
5698b9484cSchristos #define GNU_HZ  sysconf(_SC_CLK_TCK)
5798b9484cSchristos #else
5898b9484cSchristos #ifdef HZ
5998b9484cSchristos #define GNU_HZ  HZ
6098b9484cSchristos #else
6198b9484cSchristos #ifdef CLOCKS_PER_SEC
6298b9484cSchristos #define GNU_HZ  CLOCKS_PER_SEC
6398b9484cSchristos #endif
6498b9484cSchristos #endif
6598b9484cSchristos #endif
6698b9484cSchristos 
6798b9484cSchristos /* FIXME: should be able to declare as clock_t. */
6898b9484cSchristos 
6998b9484cSchristos long
7098b9484cSchristos clock (void)
7198b9484cSchristos {
7298b9484cSchristos #ifdef HAVE_GETRUSAGE
7398b9484cSchristos   struct rusage rusage;
7498b9484cSchristos 
7598b9484cSchristos   getrusage (0, &rusage);
7698b9484cSchristos   return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
7798b9484cSchristos 	  + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
7898b9484cSchristos #else
7998b9484cSchristos #ifdef HAVE_TIMES
8098b9484cSchristos   struct tms tms;
8198b9484cSchristos 
8298b9484cSchristos   times (&tms);
8398b9484cSchristos   return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
8498b9484cSchristos #else
8598b9484cSchristos #ifdef VMS
8698b9484cSchristos   struct
8798b9484cSchristos     {
8898b9484cSchristos       int proc_user_time;
8998b9484cSchristos       int proc_system_time;
9098b9484cSchristos       int child_user_time;
9198b9484cSchristos       int child_system_time;
9298b9484cSchristos     } vms_times;
9398b9484cSchristos 
9498b9484cSchristos   times (&vms_times);
9598b9484cSchristos   return (vms_times.proc_user_time + vms_times.proc_system_time) * 10000;
9698b9484cSchristos #else
9798b9484cSchristos   /* A fallback, if nothing else available. */
9898b9484cSchristos   return 0;
9998b9484cSchristos #endif /* VMS */
10098b9484cSchristos #endif /* HAVE_TIMES */
10198b9484cSchristos #endif /* HAVE_GETRUSAGE */
10298b9484cSchristos }
10398b9484cSchristos 
104