OSDN Git Service

Initial revision
[pf3gnuchains/gcc-fork.git] / libiberty / clock.c
1 /* ANSI-compatible clock function.
2    Copyright (C) 1994, 1995 Free Software Foundation, Inc.
3
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.
9
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.
14
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.
18
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. */
24
25 #ifdef HAVE_GETRUSAGE
26 #include <sys/time.h>
27 #include <sys/resource.h>
28 #endif
29
30 #ifdef HAVE_TIMES
31 #ifndef NO_SYS_PARAM_H
32 #include <sys/param.h>
33 #endif
34 #include <sys/times.h>
35 #endif
36
37 /* FIXME: should be able to declare as clock_t. */
38
39 long
40 clock ()
41 {
42 #ifdef HAVE_GETRUSAGE
43   struct rusage rusage;
44
45   getrusage (0, &rusage);
46   return (rusage.ru_utime.tv_sec * 1000000 + rusage.ru_utime.tv_usec
47           + rusage.ru_stime.tv_sec * 1000000 + rusage.ru_stime.tv_usec);
48 #else
49 #ifdef HAVE_TIMES
50   struct tms tms;
51
52   times (&tms);
53   return (tms.tms_utime + tms.tms_stime) * (1000000 / HZ);
54 #else
55 #ifdef VMS
56   struct
57     {
58       int proc_user_time;
59       int proc_system_time;
60       int child_user_time;
61       int child_system_time;
62     } vms_times;
63
64   times (&vms_times);
65   return (vms_times.proc_user_time + vms_times.proc_system_time) * 10000;
66 #else
67   /* A fallback, if nothing else available. */
68   return 0;
69 #endif /* VMS */
70 #endif /* HAVE_TIMES */
71 #endif /* HAVE_GETRUSAGE */
72 }
73