2 Copyright (C) 2005 Free Software Foundation, Inc.
4 This file is part of GNU Wget.
6 GNU Wget 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.
11 GNU Wget 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.
16 You should have received a copy of the GNU General Public License
17 along with Wget; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 In addition, as a special exception, the Free Software Foundation
21 gives permission to link the code of its release of Wget with the
22 OpenSSL project's "OpenSSL" library (or with modified versions of it
23 that use the same license as the "OpenSSL" library), and distribute
24 the linked executables. You must obey the GNU General Public License
25 in all respects for all of the code used other than "OpenSSL". If you
26 modify this file, you may extend this exception to your version of the
27 file, but you are not obligated to do so. If you do not wish to do
28 so, delete this exception statement from your version. */
30 /* This file implements "portable timers" (ptimers), objects that
31 measure elapsed time using the primitives most appropriate for the
32 underlying operating system. The entry points are:
34 ptimer_new -- creates a timer.
35 ptimer_reset -- resets the timer's elapsed time to zero.
36 ptimer_measure -- measure and return the time elapsed since
37 creation or last reset.
38 ptimer_read -- reads the last measured elapsed value.
39 ptimer_destroy -- destroy the timer.
40 ptimer_granularity -- returns the approximate granularity of the timers.
42 Timers measure time in milliseconds, but the timings they return
43 are floating point numbers, so they can carry as much precision as
44 the underlying system timer supports. For example, to measure the
45 time it takes to run a loop, you can use something like:
47 ptimer *tmr = ptimer_new ();
50 double msecs = ptimer_measure ();
51 printf ("The loop took %.2f ms\n", msecs); */
63 #ifdef HAVE_SYS_TIME_H
64 # include <sys/time.h>
67 /* Cygwin currently (as of 2005-04-08, Cygwin 1.5.14) lacks clock_getres,
68 but still defines _POSIX_TIMERS! Because of that we simply use the
69 Windows timers under Cygwin. */
77 /* Depending on the OS, one and only one of PTIMER_POSIX,
78 PTIMER_GETTIMEOFDAY, or PTIMER_WINDOWS will be defined. */
81 #undef PTIMER_GETTIMEOFDAY
84 #if defined(WINDOWS) || defined(__CYGWIN__)
85 # define PTIMER_WINDOWS /* use Windows timers */
86 #elif _POSIX_TIMERS - 0 > 0
87 # define PTIMER_POSIX /* use POSIX timers (clock_gettime) */
89 # define PTIMER_GETTIMEOFDAY /* use gettimeofday */
93 /* Elapsed time measurement using POSIX timers: system time is held in
94 struct timespec, time is retrieved using clock_gettime, and
95 resolution using clock_getres.
97 This method is used on Unix systems that implement POSIX
100 typedef struct timespec ptimer_system_time;
102 #define IMPL_init posix_init
103 #define IMPL_measure posix_measure
104 #define IMPL_diff posix_diff
105 #define IMPL_resolution posix_resolution
107 /* clock_id to use for POSIX clocks. This tries to use
108 CLOCK_MONOTONIC where available, CLOCK_REALTIME otherwise. */
109 static int posix_clock_id;
111 /* Resolution of the clock, in milliseconds. */
112 static double posix_millisec_resolution;
114 /* Decide which clock_id to use. */
119 /* List of clocks we want to support: some systems support monotonic
120 clocks, Solaris has "high resolution" clock (sometimes
121 unavailable except to superuser), and all should support the
123 #define NO_SYSCONF_CHECK -1
124 static const struct {
128 #if defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK - 0 >= 0
129 { CLOCK_MONOTONIC, _SC_MONOTONIC_CLOCK },
132 { CLOCK_HIGHRES, NO_SYSCONF_CHECK },
134 { CLOCK_REALTIME, NO_SYSCONF_CHECK },
138 /* Determine the clock we can use. For a clock to be usable, it
139 must be confirmed with sysconf (where applicable) and with
140 clock_getres. If no clock is found, CLOCK_REALTIME is used. */
142 for (i = 0; i < countof (clocks); i++)
145 if (clocks[i].sysconf_name != NO_SYSCONF_CHECK)
146 if (sysconf (clocks[i].sysconf_name) < 0)
147 continue; /* sysconf claims this clock is unavailable */
148 if (clock_getres (clocks[i].id, &r) < 0)
149 continue; /* clock_getres doesn't work for this clock */
150 posix_clock_id = clocks[i].id;
151 posix_millisec_resolution = r.tv_sec * 1000.0 + r.tv_nsec / 1000000.0;
152 /* Guard against broken clock_getres returning nonsensical
154 if (posix_millisec_resolution == 0)
155 posix_millisec_resolution = 1;
158 if (i == countof (clocks))
160 /* If no clock was found, it means that clock_getres failed for
161 the realtime clock. */
162 logprintf (LOG_NOTQUIET, _("Cannot get REALTIME clock frequency: %s\n"),
164 /* Use CLOCK_REALTIME, but invent a plausible resolution. */
165 posix_clock_id = CLOCK_REALTIME;
166 posix_millisec_resolution = 1;
171 posix_measure (ptimer_system_time *pst)
173 clock_gettime (posix_clock_id, pst);
177 posix_diff (ptimer_system_time *pst1, ptimer_system_time *pst2)
179 return ((pst1->tv_sec - pst2->tv_sec) * 1000.0
180 + (pst1->tv_nsec - pst2->tv_nsec) / 1000000.0);
184 posix_resolution (void)
186 return posix_millisec_resolution;
188 #endif /* PTIMER_POSIX */
190 #ifdef PTIMER_GETTIMEOFDAY
191 /* Elapsed time measurement using gettimeofday: system time is held in
192 struct timeval, retrieved using gettimeofday, and resolution is
195 This method is used Unix systems without POSIX timers. */
197 typedef struct timeval ptimer_system_time;
199 #define IMPL_measure gettimeofday_measure
200 #define IMPL_diff gettimeofday_diff
201 #define IMPL_resolution gettimeofday_resolution
204 gettimeofday_measure (ptimer_system_time *pst)
206 gettimeofday (pst, NULL);
210 gettimeofday_diff (ptimer_system_time *pst1, ptimer_system_time *pst2)
212 return ((pst1->tv_sec - pst2->tv_sec) * 1000.0
213 + (pst1->tv_usec - pst2->tv_usec) / 1000.0);
217 gettimeofday_resolution (void)
219 /* Granularity of gettimeofday varies wildly between architectures.
220 However, it appears that on modern machines it tends to be better
221 than 1ms. Assume 100 usecs. */
224 #endif /* PTIMER_GETTIMEOFDAY */
226 #ifdef PTIMER_WINDOWS
227 /* Elapsed time measurement on Windows: where high-resolution timers
228 are available, time is stored in a LARGE_INTEGER and retrieved
229 using QueryPerformanceCounter. Otherwise, it is stored in a DWORD
230 and retrieved using GetTickCount.
232 This method is used on Windows. */
235 DWORD lores; /* In case GetTickCount is used */
236 LARGE_INTEGER hires; /* In case high-resolution timer is used */
237 } ptimer_system_time;
239 #define IMPL_init windows_init
240 #define IMPL_measure windows_measure
241 #define IMPL_diff windows_diff
242 #define IMPL_resolution windows_resolution
244 /* Whether high-resolution timers are used. Set by ptimer_initialize_once
245 the first time ptimer_new is called. */
246 static bool windows_hires_timers;
248 /* Frequency of high-resolution timers -- number of updates per
249 millisecond. Calculated the first time ptimer_new is called
250 provided that high-resolution timers are available. */
251 static double windows_hires_msfreq;
258 QueryPerformanceFrequency (&freq);
259 if (freq.QuadPart != 0)
261 windows_hires_timers = true;
262 windows_hires_msfreq = (double) freq.QuadPart / 1000.0;
267 windows_measure (ptimer_system_time *pst)
269 if (windows_hires_timers)
270 QueryPerformanceCounter (&pst->hires);
272 /* Where hires counters are not available, use GetTickCount rather
273 GetSystemTime, because it is unaffected by clock skew and
274 simpler to use. Note that overflows don't affect us because we
275 never use absolute values of the ticker, only the
277 pst->lores = GetTickCount ();
281 windows_diff (ptimer_system_time *pst1, ptimer_system_time *pst2)
283 if (windows_hires_timers)
284 return (pst1->hires.QuadPart - pst2->hires.QuadPart) / windows_hires_msfreq;
286 return pst1->lores - pst2->lores;
290 windows_resolution (void)
292 if (windows_hires_timers)
293 return 1.0 / windows_hires_msfreq;
295 return 10; /* according to MSDN */
297 #endif /* PTIMER_WINDOWS */
299 /* The code below this point is independent of timer implementation. */
302 /* The starting point in time which, subtracted from the current
303 time, yields elapsed time. */
304 ptimer_system_time start;
306 /* The most recent elapsed time, calculated by ptimer_measure().
307 Measured in milliseconds. */
310 /* Approximately, the time elapsed between the true start of the
311 measurement and the time represented by START. This is used for
312 adjustment when clock skew is detected. */
313 double elapsed_pre_start;
316 /* Allocate a new timer and reset it. Return the new timer. */
321 struct ptimer *pt = xnew0 (struct ptimer);
323 static bool init_done;
334 /* Free the resources associated with the timer. Its further use is
338 ptimer_destroy (struct ptimer *pt)
343 /* Reset timer PT. This establishes the starting point from which
344 ptimer_read() will return the number of elapsed milliseconds.
345 It is allowed to reset a previously used timer. */
348 ptimer_reset (struct ptimer *pt)
350 /* Set the start time to the current time. */
351 IMPL_measure (&pt->start);
352 pt->elapsed_last = 0;
353 pt->elapsed_pre_start = 0;
356 /* Measure the elapsed time since timer creation/reset and return it
357 to the caller. The value remains stored for further reads by
360 This function causes the timer to call gettimeofday (or time(),
361 etc.) to update its idea of current time. To get the elapsed
362 interval in milliseconds, use ptimer_read.
364 This function handles clock skew, i.e. time that moves backwards is
368 ptimer_measure (struct ptimer *pt)
370 ptimer_system_time now;
374 elapsed = pt->elapsed_pre_start + IMPL_diff (&now, &pt->start);
376 /* Ideally we'd just return the difference between NOW and
377 pt->start. However, the system timer can be set back, and we
378 could return a value smaller than when we were last called, even
379 a negative value. Both of these would confuse the callers, which
380 expect us to return monotonically nondecreasing values.
382 Therefore: if ELAPSED is smaller than its previous known value,
383 we reset pt->start to the current time and effectively start
384 measuring from this point. But since we don't want the elapsed
385 value to start from zero, we set elapsed_pre_start to the last
386 elapsed time and increment all future calculations by that
389 This cannot happen with Windows and POSIX monotonic/highres
390 timers, but the check is not expensive. */
392 if (elapsed < pt->elapsed_last)
395 pt->elapsed_pre_start = pt->elapsed_last;
396 elapsed = pt->elapsed_last;
399 pt->elapsed_last = elapsed;
403 /* Return the elapsed time in milliseconds between the last call to
404 ptimer_reset and the last call to ptimer_update. */
407 ptimer_read (const struct ptimer *pt)
409 return pt->elapsed_last;
412 /* Return the assessed resolution of the timer implementation, in
413 milliseconds. This is used by code that tries to substitute a
414 better value for timers that have returned zero. */
417 ptimer_resolution (void)
419 return IMPL_resolution ();