2 Get the time spent in the process.
4 Copyright (C) 1992, 1993 Ian Lance Taylor
6 This file is part of the Taylor UUCP package.
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 The author of the program may be contacted at ian@airs.com or
23 c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
32 #include <sys/param.h>
39 /* Prefer gettimeofday to ftime to times. */
41 #if HAVE_GETTIMEOFDAY || HAVE_FTIME
51 #if HAVE_TIME_H && (TIME_WITH_SYS_TIME || ! HAVE_GETTIMEOFDAY)
60 #include <sys/timeb.h>
66 #include <sys/times.h>
69 #if TIMES_DECLARATION_OK
70 /* We use a macro to protect this because times really returns clock_t
71 and on some systems, such as Ultrix 4.0, clock_t is int. We don't
72 leave it out entirely because on some systems, such as System III,
73 the declaration is necessary for correct compilation. */
77 #endif /* TIMES_DECLARATION_OK */
80 #define HAVE_SC_CLK_TCK 1
82 #define HAVE_SC_CLK_TCK 0
85 /* TIMES_TICK may have been set in policy.h, or we may be able to get
86 it using sysconf. If neither is the case, try to find a useful
87 definition from the system header files. */
88 #if TIMES_TICK == 0 && (! HAVE_SYSCONF || ! HAVE_SC_CLK_TCK)
91 #define TIMES_TICK CLK_TCK
92 #else /* ! defined (CLK_TCK) */
96 #endif /* defined (HZ) */
97 #endif /* ! defined (CLK_TCK) */
98 #endif /* TIMES_TICK == 0 && (! HAVE_SYSCONF || ! HAVE_SC_CLK_TCK) */
100 #endif /* HAVE_TIMES */
103 extern time_t time ();
107 extern long sysconf ();
111 /* Get the time in seconds and microseconds; this need only work
112 within the process when called from the system independent code.
113 It is also called by ixsysdep_time. */
116 ixsysdep_process_time (pimicros)
119 #if HAVE_GETTIMEOFDAY
120 struct timeval stime;
123 (void) gettimeofday (&stime, &stz);
124 if (pimicros != NULL)
125 *pimicros = (long) stime.tv_usec;
126 return (long) stime.tv_sec;
127 #endif /* HAVE_GETTIMEOFDAY */
135 static struct timeb slast;
137 (void) ftime (&stime);
139 /* On some systems, such as SCO 3.2.2, ftime can go backwards in
140 time. If we detect this, we switch to using time. */
142 && (stime.time < slast.time
143 || (stime.time == slast.time &&
144 stime.millitm < slast.millitm)))
149 if (pimicros != NULL)
150 *pimicros = (long) stime.millitm * (long) 1000;
151 return (long) stime.time;
155 if (pimicros != NULL)
157 return (long) time ((time_t *) NULL);
158 #endif /* HAVE_FTIME */
168 #if HAVE_SYSCONF && HAVE_SC_CLK_TCK
169 itick = (int) sysconf (_SC_CLK_TCK);
170 #else /* ! HAVE_SYSCONF || ! HAVE_SC_CLK_TCK */
175 itick = (int) strtol (z, (char **) NULL, 10);
177 /* If we really couldn't get anything, just use 60. */
180 #endif /* ! HAVE_SYSCONF || ! HAVE_SC_CLK_TCK */
181 #else /* TIMES_TICK != 0 */
183 #endif /* TIMES_TICK == 0 */
186 i = (long) times (&s);
187 if (pimicros != NULL)
188 *pimicros = (i % (long) itick) * ((long) 1000000 / (long) itick);
189 return i / (long) itick;
190 #endif /* HAVE_TIMES */
192 #if ! HAVE_GETTIMEOFDAY && ! HAVE_FTIME && ! HAVE_TIMES
193 if (pimicros != NULL)
195 return (long) time ((time_t *) NULL);
196 #endif /* ! HAVE_GETTIMEOFDAY && ! HAVE_FTIME && ! HAVE_TIMES */