1/* ANSI-compatible clock function. 2 Copyright (C) 1994-2020 Free Software Foundation, Inc. 3 4This file is part of the libiberty library. This library is free 5software; you can redistribute it and/or modify it under the 6terms of the GNU General Public License as published by the 7Free Software Foundation; either version 2, or (at your option) 8any later version. 9 10This library is distributed in the hope that it will be useful, 11but WITHOUT ANY WARRANTY; without even the implied warranty of 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13GNU General Public License for more details. 14 15You should have received a copy of the GNU General Public License 16along with GNU CC; see the file COPYING. If not, write to 17the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. 18 19As a special exception, if you link this library with files 20compiled with a GNU compiler to produce an executable, this does not cause 21the resulting executable to be covered by the GNU General Public License. 22This exception does not however invalidate any other reasons why 23the executable file might be covered by the GNU General Public License. */ 24 25/* 26 27@deftypefn Supplemental long clock (void) 28 29Returns an approximation of the CPU time used by the process as a 30@code{clock_t}; divide this number by @samp{CLOCKS_PER_SEC} to get the 31number of seconds used. 32 33@end deftypefn 34 35*/ 36 37#include "config.h" 38 39#ifdef HAVE_GETRUSAGE 40#include <sys/time.h> 41#include <sys/resource.h> 42#endif 43 44#ifdef HAVE_TIMES 45#ifdef HAVE_SYS_PARAM_H 46#include <sys/param.h> 47#endif 48#include <sys/times.h> 49#endif 50 51#ifdef HAVE_UNISTD_H 52#include <unistd.h> 53#endif 54 55#ifdef _SC_CLK_TCK 56#define GNU_HZ sysconf(_SC_CLK_TCK) 57#else 58#ifdef HZ 59#define GNU_HZ HZ 60#else 61#ifdef CLOCKS_PER_SEC 62#define GNU_HZ CLOCKS_PER_SEC 63#endif 64#endif 65#endif 66 67/* FIXME: should be able to declare as clock_t. */ 68 69long 70clock (void) 71{ 72#ifdef HAVE_GETRUSAGE 73 struct rusage rusage; 74 75 getrusage (0, &rusage); 76 return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec 77 + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec); 78#else 79#ifdef HAVE_TIMES 80 struct tms tms; 81 82 times (&tms); 83 return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ); 84#else 85#ifdef VMS 86 struct 87 { 88 int proc_user_time; 89 int proc_system_time; 90 int child_user_time; 91 int child_system_time; 92 } vms_times; 93 94 times (&vms_times); 95 return (vms_times.proc_user_time + vms_times.proc_system_time) * 10000; 96#else 97 /* A fallback, if nothing else available. */ 98 return 0; 99#endif /* VMS */ 100#endif /* HAVE_TIMES */ 101#endif /* HAVE_GETRUSAGE */ 102} 103 104