Initial revision
[binutils.git] / libiberty / clock.c
blobdb2509c17f277aaffacd36533797fabcd1d1e037
1 /* ANSI-compatible clock function.
2 Copyright (C) 1994, 1995, 1999 Free Software Foundation, Inc.
4 This file is part of the libiberty library. This library is free
5 software; you can redistribute it and/or modify it under the
6 terms of the GNU General Public License as published by the
7 Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU CC; see the file COPYING. If not, write to
17 the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 As a special exception, if you link this library with files
20 compiled with a GNU compiler to produce an executable, this does not cause
21 the resulting executable to be covered by the GNU General Public License.
22 This exception does not however invalidate any other reasons why
23 the executable file might be covered by the GNU General Public License. */
25 #include "config.h"
27 #ifdef HAVE_GETRUSAGE
28 #include <sys/time.h>
29 #include <sys/resource.h>
30 #endif
32 #ifdef HAVE_TIMES
33 #ifdef HAVE_SYS_PARAM_H
34 #include <sys/param.h>
35 #endif
36 #include <sys/times.h>
37 #endif
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
43 #ifdef _SC_CLK_TCK
44 #define GNU_HZ sysconf(_SC_CLK_TCK)
45 #else
46 #ifdef HZ
47 #define GNU_HZ HZ
48 #else
49 #ifdef CLOCKS_PER_SEC
50 #define GNU_HZ CLOCKS_PER_SEC
51 #endif
52 #endif
53 #endif
55 /* FIXME: should be able to declare as clock_t. */
57 long
58 clock ()
60 #ifdef HAVE_GETRUSAGE
61 struct rusage rusage;
63 getrusage (0, &rusage);
64 return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
65 + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
66 #else
67 #ifdef HAVE_TIMES
68 struct tms tms;
70 times (&tms);
71 return (tms.tms_utime + tms.tms_stime) * (1000000 / GNU_HZ);
72 #else
73 #ifdef VMS
74 struct
76 int proc_user_time;
77 int proc_system_time;
78 int child_user_time;
79 int child_system_time;
80 } vms_times;
82 times (&vms_times);
83 return (vms_times.proc_user_time + vms_times.proc_system_time) * 10000;
84 #else
85 /* A fallback, if nothing else available. */
86 return 0;
87 #endif /* VMS */
88 #endif /* HAVE_TIMES */
89 #endif /* HAVE_GETRUSAGE */