Back to index

cell-binutils  2.17cvs20070401
clock.c
Go to the documentation of this file.
00001 /* ANSI-compatible clock function.
00002    Copyright (C) 1994, 1995, 1999 Free Software Foundation, Inc.
00003 
00004 This file is part of the libiberty library.  This library is free
00005 software; you can redistribute it and/or modify it under the
00006 terms of the GNU General Public License as published by the
00007 Free Software Foundation; either version 2, or (at your option)
00008 any later version.
00009 
00010 This library is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License
00016 along with GNU CC; see the file COPYING.  If not, write to
00017 the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
00018 
00019 As a special exception, if you link this library with files
00020 compiled with a GNU compiler to produce an executable, this does not cause
00021 the resulting executable to be covered by the GNU General Public License.
00022 This exception does not however invalidate any other reasons why
00023 the executable file might be covered by the GNU General Public License. */
00024 
00025 /*
00026 
00027 @deftypefn Supplemental long clock (void)
00028 
00029 Returns an approximation of the CPU time used by the process as a
00030 @code{clock_t}; divide this number by @samp{CLOCKS_PER_SEC} to get the
00031 number of seconds used.
00032 
00033 @end deftypefn
00034 
00035 */
00036 
00037 #include "config.h"
00038 
00039 #ifdef HAVE_GETRUSAGE
00040 #include <sys/time.h>
00041 #include <sys/resource.h>
00042 #endif
00043 
00044 #ifdef HAVE_TIMES
00045 #ifdef HAVE_SYS_PARAM_H
00046 #include <sys/param.h>
00047 #endif
00048 #include <sys/times.h>
00049 #endif
00050 
00051 #ifdef HAVE_UNISTD_H
00052 #include <unistd.h>
00053 #endif
00054 
00055 #ifdef _SC_CLK_TCK
00056 #define GNU_HZ  sysconf(_SC_CLK_TCK)
00057 #else
00058 #ifdef HZ
00059 #define GNU_HZ  HZ
00060 #else
00061 #ifdef CLOCKS_PER_SEC
00062 #define GNU_HZ  CLOCKS_PER_SEC
00063 #endif
00064 #endif
00065 #endif
00066 
00067 /* FIXME: should be able to declare as clock_t. */
00068 
00069 long
00070 clock (void)
00071 {
00072 #ifdef HAVE_GETRUSAGE
00073   struct rusage rusage;
00074 
00075   getrusage (0, &rusage);
00076   return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
00077          + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
00078 #else
00079 #ifdef HAVE_TIMES
00080   struct tms tms;
00081 
00082   times (&tms);
00083   return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
00084 #else
00085 #ifdef VMS
00086   struct
00087     {
00088       int proc_user_time;
00089       int proc_system_time;
00090       int child_user_time;
00091       int child_system_time;
00092     } vms_times;
00093 
00094   times (&vms_times);
00095   return (vms_times.proc_user_time + vms_times.proc_system_time) * 10000;
00096 #else
00097   /* A fallback, if nothing else available. */
00098   return 0;
00099 #endif /* VMS */
00100 #endif /* HAVE_TIMES */
00101 #endif /* HAVE_GETRUSAGE */
00102 }
00103