1169695Skan/* ANSI-compatible clock function.
2169695Skan   Copyright (C) 1994, 1995, 1999 Free Software Foundation, Inc.
3169695Skan
4169695SkanThis file is part of the libiberty library.  This library is free
5169695Skansoftware; you can redistribute it and/or modify it under the
6169695Skanterms of the GNU General Public License as published by the
7169695SkanFree Software Foundation; either version 2, or (at your option)
8169695Skanany later version.
9169695Skan
10169695SkanThis library is distributed in the hope that it will be useful,
11169695Skanbut WITHOUT ANY WARRANTY; without even the implied warranty of
12169695SkanMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13169695SkanGNU General Public License for more details.
14169695Skan
15169695SkanYou should have received a copy of the GNU General Public License
16169695Skanalong with GNU CC; see the file COPYING.  If not, write to
17169695Skanthe Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
18169695Skan
19169695SkanAs a special exception, if you link this library with files
20169695Skancompiled with a GNU compiler to produce an executable, this does not cause
21169695Skanthe resulting executable to be covered by the GNU General Public License.
22169695SkanThis exception does not however invalidate any other reasons why
23169695Skanthe executable file might be covered by the GNU General Public License. */
24169695Skan
25169695Skan/*
26169695Skan
27169695Skan@deftypefn Supplemental long clock (void)
28169695Skan
29169695SkanReturns an approximation of the CPU time used by the process as a
30169695Skan@code{clock_t}; divide this number by @samp{CLOCKS_PER_SEC} to get the
31169695Skannumber of seconds used.
32169695Skan
33169695Skan@end deftypefn
34169695Skan
35169695Skan*/
36169695Skan
37169695Skan#include "config.h"
38169695Skan
39169695Skan#ifdef HAVE_GETRUSAGE
40169695Skan#include <sys/time.h>
41169695Skan#include <sys/resource.h>
42169695Skan#endif
43169695Skan
44169695Skan#ifdef HAVE_TIMES
45169695Skan#ifdef HAVE_SYS_PARAM_H
46169695Skan#include <sys/param.h>
47169695Skan#endif
48169695Skan#include <sys/times.h>
49169695Skan#endif
50169695Skan
51169695Skan#ifdef HAVE_UNISTD_H
52169695Skan#include <unistd.h>
53169695Skan#endif
54169695Skan
55169695Skan#ifdef _SC_CLK_TCK
56169695Skan#define GNU_HZ  sysconf(_SC_CLK_TCK)
57169695Skan#else
58169695Skan#ifdef HZ
59169695Skan#define GNU_HZ  HZ
60169695Skan#else
61169695Skan#ifdef CLOCKS_PER_SEC
62169695Skan#define GNU_HZ  CLOCKS_PER_SEC
63169695Skan#endif
64169695Skan#endif
65169695Skan#endif
66169695Skan
67169695Skan/* FIXME: should be able to declare as clock_t. */
68169695Skan
69169695Skanlong
70169695Skanclock (void)
71169695Skan{
72169695Skan#ifdef HAVE_GETRUSAGE
73169695Skan  struct rusage rusage;
74169695Skan
75169695Skan  getrusage (0, &rusage);
76169695Skan  return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
77169695Skan	  + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
78169695Skan#else
79169695Skan#ifdef HAVE_TIMES
80169695Skan  struct tms tms;
81169695Skan
82169695Skan  times (&tms);
83169695Skan  return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
84169695Skan#else
85169695Skan#ifdef VMS
86169695Skan  struct
87169695Skan    {
88169695Skan      int proc_user_time;
89169695Skan      int proc_system_time;
90169695Skan      int child_user_time;
91169695Skan      int child_system_time;
92169695Skan    } vms_times;
93169695Skan
94169695Skan  times (&vms_times);
95169695Skan  return (vms_times.proc_user_time + vms_times.proc_system_time) * 10000;
96169695Skan#else
97169695Skan  /* A fallback, if nothing else available. */
98169695Skan  return 0;
99169695Skan#endif /* VMS */
100169695Skan#endif /* HAVE_TIMES */
101169695Skan#endif /* HAVE_GETRUSAGE */
102169695Skan}
103169695Skan
104