xref: /netbsd-src/external/gpl3/binutils/dist/libiberty/clock.c (revision cb63e24e8d6aae7ddac1859a9015f48b1d8bd90e)
12a6b7db3Sskrll /* ANSI-compatible clock function.
2*cb63e24eSchristos    Copyright (C) 1994-2024 Free Software Foundation, Inc.
32a6b7db3Sskrll 
42a6b7db3Sskrll This file is part of the libiberty library.  This library is free
52a6b7db3Sskrll software; you can redistribute it and/or modify it under the
62a6b7db3Sskrll terms of the GNU General Public License as published by the
72a6b7db3Sskrll Free Software Foundation; either version 2, or (at your option)
82a6b7db3Sskrll any later version.
92a6b7db3Sskrll 
102a6b7db3Sskrll This library 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
132a6b7db3Sskrll GNU General Public License for more details.
142a6b7db3Sskrll 
152a6b7db3Sskrll You should have received a copy of the GNU General Public License
162a6b7db3Sskrll along with GNU CC; see the file COPYING.  If not, write to
172a6b7db3Sskrll the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
182a6b7db3Sskrll 
192a6b7db3Sskrll As a special exception, if you link this library with files
202a6b7db3Sskrll compiled with a GNU compiler to produce an executable, this does not cause
212a6b7db3Sskrll the resulting executable to be covered by the GNU General Public License.
222a6b7db3Sskrll This exception does not however invalidate any other reasons why
232a6b7db3Sskrll the executable file might be covered by the GNU General Public License. */
242a6b7db3Sskrll 
252a6b7db3Sskrll /*
262a6b7db3Sskrll 
272a6b7db3Sskrll @deftypefn Supplemental long clock (void)
282a6b7db3Sskrll 
292a6b7db3Sskrll Returns an approximation of the CPU time used by the process as a
302a6b7db3Sskrll @code{clock_t}; divide this number by @samp{CLOCKS_PER_SEC} to get the
312a6b7db3Sskrll number of seconds used.
322a6b7db3Sskrll 
332a6b7db3Sskrll @end deftypefn
342a6b7db3Sskrll 
352a6b7db3Sskrll */
362a6b7db3Sskrll 
372a6b7db3Sskrll #include "config.h"
382a6b7db3Sskrll 
392a6b7db3Sskrll #ifdef HAVE_GETRUSAGE
402a6b7db3Sskrll #include <sys/time.h>
412a6b7db3Sskrll #include <sys/resource.h>
422a6b7db3Sskrll #endif
432a6b7db3Sskrll 
442a6b7db3Sskrll #ifdef HAVE_TIMES
452a6b7db3Sskrll #ifdef HAVE_SYS_PARAM_H
462a6b7db3Sskrll #include <sys/param.h>
472a6b7db3Sskrll #endif
482a6b7db3Sskrll #include <sys/times.h>
492a6b7db3Sskrll #endif
502a6b7db3Sskrll 
512a6b7db3Sskrll #ifdef HAVE_UNISTD_H
522a6b7db3Sskrll #include <unistd.h>
532a6b7db3Sskrll #endif
542a6b7db3Sskrll 
552a6b7db3Sskrll #ifdef _SC_CLK_TCK
562a6b7db3Sskrll #define GNU_HZ  sysconf(_SC_CLK_TCK)
572a6b7db3Sskrll #else
582a6b7db3Sskrll #ifdef HZ
592a6b7db3Sskrll #define GNU_HZ  HZ
602a6b7db3Sskrll #else
612a6b7db3Sskrll #ifdef CLOCKS_PER_SEC
622a6b7db3Sskrll #define GNU_HZ  CLOCKS_PER_SEC
632a6b7db3Sskrll #endif
642a6b7db3Sskrll #endif
652a6b7db3Sskrll #endif
662a6b7db3Sskrll 
672a6b7db3Sskrll /* FIXME: should be able to declare as clock_t. */
682a6b7db3Sskrll 
692a6b7db3Sskrll long
clock(void)702a6b7db3Sskrll clock (void)
712a6b7db3Sskrll {
722a6b7db3Sskrll #ifdef HAVE_GETRUSAGE
732a6b7db3Sskrll   struct rusage rusage;
742a6b7db3Sskrll 
752a6b7db3Sskrll   getrusage (0, &rusage);
762a6b7db3Sskrll   return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
772a6b7db3Sskrll 	  + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
782a6b7db3Sskrll #else
792a6b7db3Sskrll #ifdef HAVE_TIMES
802a6b7db3Sskrll   struct tms tms;
812a6b7db3Sskrll 
822a6b7db3Sskrll   times (&tms);
832a6b7db3Sskrll   return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
842a6b7db3Sskrll #else
852a6b7db3Sskrll #ifdef VMS
862a6b7db3Sskrll   struct
872a6b7db3Sskrll     {
882a6b7db3Sskrll       int proc_user_time;
892a6b7db3Sskrll       int proc_system_time;
902a6b7db3Sskrll       int child_user_time;
912a6b7db3Sskrll       int child_system_time;
922a6b7db3Sskrll     } vms_times;
932a6b7db3Sskrll 
942a6b7db3Sskrll   times (&vms_times);
952a6b7db3Sskrll   return (vms_times.proc_user_time + vms_times.proc_system_time) * 10000;
962a6b7db3Sskrll #else
972a6b7db3Sskrll   /* A fallback, if nothing else available. */
982a6b7db3Sskrll   return 0;
992a6b7db3Sskrll #endif /* VMS */
1002a6b7db3Sskrll #endif /* HAVE_TIMES */
1012a6b7db3Sskrll #endif /* HAVE_GETRUSAGE */
1022a6b7db3Sskrll }
1032a6b7db3Sskrll 
104