xref: /netbsd-src/external/gpl3/gcc.old/dist/libiberty/clock.c (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
11debfc3dSmrg /* ANSI-compatible clock function.
2*8feb0f0bSmrg    Copyright (C) 1994-2020 Free Software Foundation, Inc.
31debfc3dSmrg 
41debfc3dSmrg This file is part of the libiberty library.  This library is free
51debfc3dSmrg software; you can redistribute it and/or modify it under the
61debfc3dSmrg terms of the GNU General Public License as published by the
71debfc3dSmrg Free Software Foundation; either version 2, or (at your option)
81debfc3dSmrg any later version.
91debfc3dSmrg 
101debfc3dSmrg This library is distributed in the hope that it will be useful,
111debfc3dSmrg but WITHOUT ANY WARRANTY; without even the implied warranty of
121debfc3dSmrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
131debfc3dSmrg GNU General Public License for more details.
141debfc3dSmrg 
151debfc3dSmrg You should have received a copy of the GNU General Public License
161debfc3dSmrg along with GNU CC; see the file COPYING.  If not, write to
171debfc3dSmrg the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
181debfc3dSmrg 
191debfc3dSmrg As a special exception, if you link this library with files
201debfc3dSmrg compiled with a GNU compiler to produce an executable, this does not cause
211debfc3dSmrg the resulting executable to be covered by the GNU General Public License.
221debfc3dSmrg This exception does not however invalidate any other reasons why
231debfc3dSmrg the executable file might be covered by the GNU General Public License. */
241debfc3dSmrg 
251debfc3dSmrg /*
261debfc3dSmrg 
271debfc3dSmrg @deftypefn Supplemental long clock (void)
281debfc3dSmrg 
291debfc3dSmrg Returns an approximation of the CPU time used by the process as a
301debfc3dSmrg @code{clock_t}; divide this number by @samp{CLOCKS_PER_SEC} to get the
311debfc3dSmrg number of seconds used.
321debfc3dSmrg 
331debfc3dSmrg @end deftypefn
341debfc3dSmrg 
351debfc3dSmrg */
361debfc3dSmrg 
371debfc3dSmrg #include "config.h"
381debfc3dSmrg 
391debfc3dSmrg #ifdef HAVE_GETRUSAGE
401debfc3dSmrg #include <sys/time.h>
411debfc3dSmrg #include <sys/resource.h>
421debfc3dSmrg #endif
431debfc3dSmrg 
441debfc3dSmrg #ifdef HAVE_TIMES
451debfc3dSmrg #ifdef HAVE_SYS_PARAM_H
461debfc3dSmrg #include <sys/param.h>
471debfc3dSmrg #endif
481debfc3dSmrg #include <sys/times.h>
491debfc3dSmrg #endif
501debfc3dSmrg 
511debfc3dSmrg #ifdef HAVE_UNISTD_H
521debfc3dSmrg #include <unistd.h>
531debfc3dSmrg #endif
541debfc3dSmrg 
551debfc3dSmrg #ifdef _SC_CLK_TCK
561debfc3dSmrg #define GNU_HZ  sysconf(_SC_CLK_TCK)
571debfc3dSmrg #else
581debfc3dSmrg #ifdef HZ
591debfc3dSmrg #define GNU_HZ  HZ
601debfc3dSmrg #else
611debfc3dSmrg #ifdef CLOCKS_PER_SEC
621debfc3dSmrg #define GNU_HZ  CLOCKS_PER_SEC
631debfc3dSmrg #endif
641debfc3dSmrg #endif
651debfc3dSmrg #endif
661debfc3dSmrg 
671debfc3dSmrg /* FIXME: should be able to declare as clock_t. */
681debfc3dSmrg 
691debfc3dSmrg long
clock(void)701debfc3dSmrg clock (void)
711debfc3dSmrg {
721debfc3dSmrg #ifdef HAVE_GETRUSAGE
731debfc3dSmrg   struct rusage rusage;
741debfc3dSmrg 
751debfc3dSmrg   getrusage (0, &rusage);
761debfc3dSmrg   return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
771debfc3dSmrg 	  + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
781debfc3dSmrg #else
791debfc3dSmrg #ifdef HAVE_TIMES
801debfc3dSmrg   struct tms tms;
811debfc3dSmrg 
821debfc3dSmrg   times (&tms);
831debfc3dSmrg   return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
841debfc3dSmrg #else
851debfc3dSmrg #ifdef VMS
861debfc3dSmrg   struct
871debfc3dSmrg     {
881debfc3dSmrg       int proc_user_time;
891debfc3dSmrg       int proc_system_time;
901debfc3dSmrg       int child_user_time;
911debfc3dSmrg       int child_system_time;
921debfc3dSmrg     } vms_times;
931debfc3dSmrg 
941debfc3dSmrg   times (&vms_times);
951debfc3dSmrg   return (vms_times.proc_user_time + vms_times.proc_system_time) * 10000;
961debfc3dSmrg #else
971debfc3dSmrg   /* A fallback, if nothing else available. */
981debfc3dSmrg   return 0;
991debfc3dSmrg #endif /* VMS */
1001debfc3dSmrg #endif /* HAVE_TIMES */
1011debfc3dSmrg #endif /* HAVE_GETRUSAGE */
1021debfc3dSmrg }
1031debfc3dSmrg 
104