1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * timing.c -- Program timing routines
|
---|
4 | * Copyright (C) 1994 Neil Sharman
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | *
|
---|
20 | * $Id: timing.c 439 1999-08-10 21:23:37Z sjboddie $
|
---|
21 | *
|
---|
22 | **************************************************************************/
|
---|
23 |
|
---|
24 | /*
|
---|
25 | $Log$
|
---|
26 | Revision 1.1 1999/08/10 21:17:05 sjboddie
|
---|
27 | renamed mg-1.3d directory mg
|
---|
28 |
|
---|
29 | Revision 1.1 1998/11/17 09:32:49 rjmcnab
|
---|
30 | *** empty log message ***
|
---|
31 |
|
---|
32 | * Revision 1.1 1994/08/22 00:24:53 tes
|
---|
33 | * Initial placement under CVS.
|
---|
34 | *
|
---|
35 | */
|
---|
36 |
|
---|
37 | static char *RCSID = "$Id: timing.c 439 1999-08-10 21:23:37Z sjboddie $";
|
---|
38 |
|
---|
39 | #include "sysfuncs.h"
|
---|
40 | #include "timing.h"
|
---|
41 |
|
---|
42 |
|
---|
43 | /*
|
---|
44 | * Return the time in seconds since 00:00:00 GMT, Jan. 1, 1970 to the
|
---|
45 | * best precision possible
|
---|
46 | *
|
---|
47 | */
|
---|
48 | double
|
---|
49 | RealTime (void)
|
---|
50 | {
|
---|
51 | return ((double) time (NULL));
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 |
|
---|
56 | /*
|
---|
57 | * Return the amount of system and user CPU used by the process to date to
|
---|
58 | * the best precision possible. If user is non-null then it is initialised to
|
---|
59 | * the user time. If sys is non-null then it is initialised to the system time.
|
---|
60 | *
|
---|
61 | */
|
---|
62 | double
|
---|
63 | CPUTime (double *user, double *sys)
|
---|
64 | {
|
---|
65 |
|
---|
66 | #if defined(HAVE_TIMES)
|
---|
67 |
|
---|
68 | struct tms buffer;
|
---|
69 | static double clk_tck = 0;
|
---|
70 | double u, s;
|
---|
71 |
|
---|
72 | times (&buffer);
|
---|
73 |
|
---|
74 | if (clk_tck == 0)
|
---|
75 | clk_tck = CLK_TCK;
|
---|
76 |
|
---|
77 | u = (double) buffer.tms_utime / clk_tck;
|
---|
78 | s = (double) buffer.tms_stime / clk_tck;
|
---|
79 | if (user)
|
---|
80 | *user = u;
|
---|
81 | if (sys)
|
---|
82 | *sys = s;
|
---|
83 | return u + s;
|
---|
84 |
|
---|
85 | #elif defined(HAVE_GETRUSAGE)
|
---|
86 |
|
---|
87 | struct rusage ruse;
|
---|
88 | getrusage (RUSAGE_SELF, &ruse);
|
---|
89 | if (user)
|
---|
90 | *user = (double) ruse.ru_utime.tv_sec +
|
---|
91 | (double) ruse.ru_utime.tv_usec / 1000000;
|
---|
92 | if (sys)
|
---|
93 | *sys = (double) ruse.ru_stime.tv_sec +
|
---|
94 | (double) ruse.ru_stime.tv_usec / 1000000;
|
---|
95 | return ((double) ruse.ru_utime.tv_sec + ruse.ru_stime.tv_sec +
|
---|
96 | ((double) ruse.ru_utime.tv_usec + ruse.ru_stime.tv_usec) / 1000000);
|
---|
97 |
|
---|
98 | #else
|
---|
99 |
|
---|
100 | /* [RPAP - Feb 97: WIN32 Port] */
|
---|
101 | #ifdef __WIN32__
|
---|
102 | if (user) *user = 0.0;
|
---|
103 | if (sys) *sys = 0.0;
|
---|
104 | return 0.0;
|
---|
105 | #else
|
---|
106 | -->Can NOT find a system time routine to use ! !!
|
---|
107 | #endif /* __WIN32__ */
|
---|
108 |
|
---|
109 | #endif
|
---|
110 |
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 |
|
---|
115 |
|
---|
116 | /*
|
---|
117 | * Get the Real and CPU time and store them in a ProgTime structure
|
---|
118 | */
|
---|
119 | void
|
---|
120 | GetTime (ProgTime * StartTime)
|
---|
121 | {
|
---|
122 | StartTime->RealTime = RealTime ();
|
---|
123 | StartTime->CPUTime = CPUTime (NULL, NULL);
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 |
|
---|
128 |
|
---|
129 | /*
|
---|
130 | * Display the Real and CPU time elapsed since the StartTime anf FinishTime
|
---|
131 | * structures were initialised. If FinishTime is NULL then FinishTime is
|
---|
132 | * Now.
|
---|
133 | */
|
---|
134 | char *
|
---|
135 | ElapsedTime (ProgTime * StartTime,
|
---|
136 | ProgTime * FinishTime)
|
---|
137 | {
|
---|
138 | static char buf[50];
|
---|
139 | double Real, CPU, hour, min, sec;
|
---|
140 | if (!FinishTime)
|
---|
141 | {
|
---|
142 | Real = RealTime () - StartTime->RealTime;
|
---|
143 | CPU = CPUTime (NULL, NULL) - StartTime->CPUTime;
|
---|
144 | }
|
---|
145 | else
|
---|
146 | {
|
---|
147 | Real = FinishTime->RealTime - StartTime->RealTime;
|
---|
148 | CPU = FinishTime->CPUTime - StartTime->CPUTime;
|
---|
149 | }
|
---|
150 | hour = floor (CPU / 3600);
|
---|
151 | min = floor ((CPU - hour * 3600) / 60);
|
---|
152 | sec = CPU - hour * 3600 - min * 60;
|
---|
153 | sprintf (buf, "%02.0f:%02.0f:%05.2f cpu, %02d:%02d:%02d elapsed.",
|
---|
154 | hour, min, sec,
|
---|
155 | ((int) ceil (Real)) / 3600,
|
---|
156 | (((int) ceil (Real)) % 3600) / 60, ((int) ceil (Real)) % 60);
|
---|
157 | return (buf);
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | #define MILLION 1000000
|
---|
162 |
|
---|
163 | /* ===============================================================================
|
---|
164 | * Function: cputime_string
|
---|
165 | * Description:
|
---|
166 | * Prints out given cpu time into a string buffer.
|
---|
167 | * Input: cpu-time in clock_t or timeval format
|
---|
168 | * Output: pointer to internal buffer
|
---|
169 | * =============================================================================== */
|
---|
170 |
|
---|
171 | #ifdef HAVE_TIMES
|
---|
172 | char *
|
---|
173 | cputime_string (clock_t clk)
|
---|
174 | {
|
---|
175 | static char buf[15];
|
---|
176 | double CPU, hour, min, sec;
|
---|
177 |
|
---|
178 | CPU = (double) clk / (double) CLK_TCK;
|
---|
179 | hour = floor (CPU / 3600);
|
---|
180 | min = floor ((CPU - hour * 3600) / 60);
|
---|
181 | sec = CPU - hour * 3600 - min * 60;
|
---|
182 | sprintf (buf, "%02.0f:%02.0f:%05.2f", hour, min, sec);
|
---|
183 | return (buf);
|
---|
184 | }
|
---|
185 | #else
|
---|
186 | char *
|
---|
187 | cputime_string (struct timeval *t)
|
---|
188 | {
|
---|
189 | static char buf[15];
|
---|
190 | double CPU, hour, min, sec;
|
---|
191 |
|
---|
192 | CPU = (double) t->tv_sec + (double) t->tv_usec / MILLION;
|
---|
193 | hour = floor (CPU / 3600);
|
---|
194 | min = floor ((CPU - hour * 3600) / 60);
|
---|
195 | sec = CPU - hour * 3600 - min * 60;
|
---|
196 | sprintf (buf, "%02.0f:%02.0f:%05.2f", hour, min, sec);
|
---|
197 | return (buf);
|
---|
198 | }
|
---|
199 | #endif
|
---|
200 |
|
---|
201 |
|
---|
202 |
|
---|
203 | /* ===============================================================================
|
---|
204 | * Function: time_normalise
|
---|
205 | * Description:
|
---|
206 | * If the micro-second component is negative or over a million,
|
---|
207 | * then must take the over/under flow and add it to the second component.
|
---|
208 | * Usage:
|
---|
209 | * Use this routine when you have just been doing straight addition and
|
---|
210 | * subtraction on the micro-second component. For this could lead to a
|
---|
211 | * negative number or over a million.
|
---|
212 | *
|
---|
213 | * =============================================================================== */
|
---|
214 |
|
---|
215 | #ifndef HAVE_TIMES
|
---|
216 |
|
---|
217 | #ifndef __WIN32__ /* [RPAP - Feb 97: WIN32 Port] */
|
---|
218 | static
|
---|
219 | #endif
|
---|
220 | void
|
---|
221 | time_normalise (struct timeval *t)
|
---|
222 | {
|
---|
223 | while (t->tv_usec < 0)
|
---|
224 | {
|
---|
225 | t->tv_usec += MILLION;
|
---|
226 | t->tv_sec--;
|
---|
227 | }
|
---|
228 | while (t->tv_usec > MILLION)
|
---|
229 | {
|
---|
230 | t->tv_usec -= MILLION;
|
---|
231 | t->tv_sec++;
|
---|
232 | }
|
---|
233 | }
|
---|
234 | #endif
|
---|