]> sjero.net Git - wget/commitdiff
[svn] Timer code update.
authorhniksic <devnull@localhost>
Sat, 8 Nov 2003 04:55:44 +0000 (20:55 -0800)
committerhniksic <devnull@localhost>
Sat, 8 Nov 2003 04:55:44 +0000 (20:55 -0800)
src/ChangeLog
src/convert.c
src/retr.c
src/utils.c
src/utils.h

index c305d9919d35d4c370ddd8395b57375b66efeedf..40415df857760fbd593152f2db5307a1ccee45f0 100644 (file)
@@ -1,3 +1,11 @@
+2003-11-08  Hrvoje Niksic  <hniksic@xemacs.org>
+
+       * retr.c (get_contents): Pass the timer to limit_bandwidth().
+
+       * utils.c (wtimer_update): New function instead of wget_elapsed;
+       just update the timer, but don't return anything.
+       (wtimer_read): Read and return the last known value of the timer.
+
 2003-11-08  Hrvoje Niksic  <hniksic@xemacs.org>
 
        * http.c (persistent_available_p): Instead of matching all the
index d3e85ae83d3077a644e2e5fa383f2a63277ef54e..ca9e36b8e2359f87b22a5e5110ea5405059eb57c 100644 (file)
@@ -166,7 +166,8 @@ convert_all_links (void)
       free_urlpos (urls);
     }
 
-  msecs = wtimer_elapsed (timer);
+  wtimer_update (timer);
+  msecs = wtimer_read (timer);
   wtimer_delete (timer);
   logprintf (LOG_VERBOSE, _("Converted %d files in %.2f seconds.\n"),
             file_count, (double)msecs / 1000);
index db8b1c18174fda2a39b5c08ef07fc7a276c7039a..5e4212f88e94b2049acb8f49e072de939cea8868 100644 (file)
@@ -84,13 +84,13 @@ limit_bandwidth_reset (void)
 }
 
 /* Limit the bandwidth by pausing the download for an amount of time.
-   BYTES is the number of bytes received from the network, and DELTA
-   is the number of milliseconds it took to receive them.  */
+   BYTES is the number of bytes received from the network, and TIMER
+   is the timer that started at the beginning of download.  */
 
 static void
-limit_bandwidth (long bytes, double *dltime, struct wget_timer *timer)
+limit_bandwidth (long bytes, struct wget_timer *timer)
 {
-  double delta_t = *dltime - limit_data.chunk_start;
+  double delta_t = wtimer_read (timer) - limit_data.chunk_start;
   double expected;
 
   limit_data.chunk_bytes += bytes;
@@ -113,23 +113,20 @@ limit_bandwidth (long bytes, double *dltime, struct wget_timer *timer)
       DEBUGP (("\nsleeping %.2f ms for %ld bytes, adjust %.2f ms\n",
               slp, limit_data.chunk_bytes, limit_data.sleep_adjust));
 
-      t0 = *dltime;
+      t0 = wtimer_read (timer);
       xsleep (slp / 1000);
-      t1 = wtimer_elapsed (timer);
+      wtimer_update (timer);
+      t1 = wtimer_read (timer);
 
       /* Due to scheduling, we probably slept slightly longer (or
         shorter) than desired.  Calculate the difference between the
         desired and the actual sleep, and adjust the next sleep by
         that amount.  */
       limit_data.sleep_adjust = slp - (t1 - t0);
-
-      /* Since we've called wtimer_elapsed, we might as well update
-        the caller's dltime. */
-      *dltime = t1;
     }
 
   limit_data.chunk_bytes = 0;
-  limit_data.chunk_start = *dltime;
+  limit_data.chunk_start = wtimer_read (timer);
 }
 
 #define MIN(i, j) ((i) <= (j) ? (i) : (j))
@@ -166,7 +163,6 @@ get_contents (int fd, FILE *fp, long *len, long restval, long expected,
 
   void *progress = NULL;
   struct wget_timer *timer = wtimer_allocate ();
-  double dltime = 0;
 
   *len = restval;
 
@@ -222,7 +218,7 @@ get_contents (int fd, FILE *fp, long *len, long restval, long expected,
       /* Always flush the contents of the network packet.  This should
         not hinder performance: fast downloads will be received in
         16K chunks (which stdio would write out anyway), and slow
-        downloads won't be limited with disk performance.  */
+        downloads won't be limited by disk performance.  */
       fflush (fp);
       if (ferror (fp))
        {
@@ -230,13 +226,13 @@ get_contents (int fd, FILE *fp, long *len, long restval, long expected,
          goto out;
        }
 
-      dltime = wtimer_elapsed (timer);
+      wtimer_update (timer);
       if (opt.limit_rate)
-       limit_bandwidth (res, &dltime, timer);
+       limit_bandwidth (res, timer);
 
       *len += res;
       if (progress)
-       progress_update (progress, res, dltime);
+       progress_update (progress, res, wtimer_read (timer));
 #ifdef WINDOWS
       if (use_expected && expected > 0)
        ws_percenttitle (100.0 * (double)(*len) / (double)expected);
@@ -247,9 +243,9 @@ get_contents (int fd, FILE *fp, long *len, long restval, long expected,
 
  out:
   if (progress)
-    progress_finish (progress, dltime);
+    progress_finish (progress, wtimer_read (timer));
   if (elapsed)
-    *elapsed = dltime;
+    *elapsed = wtimer_read (timer);
   wtimer_delete (timer);
 
   return res;
index 5f99fa7ea7a6e642604453518037f021c3b7d7af..1559b2796ebc996d4ff73e4f4bb212ad81b6ea12 100644 (file)
@@ -1395,8 +1395,11 @@ wtimer_sys_set (wget_sys_time *wst)
 }
 
 /* Reset timer WT.  This establishes the starting point from which
-   wtimer_elapsed() will return the number of elapsed
-   milliseconds.  It is allowed to reset a previously used timer.  */
+   wtimer_elapsed() will return the number of elapsed milliseconds.
+   It is allowed to reset a previously used timer.
+
+   If a non-zero value is used as START, the timer's values will be
+   offset by START.  */
 
 void
 wtimer_reset (struct wget_timer *wt)
@@ -1427,13 +1430,16 @@ wtimer_sys_diff (wget_sys_time *wst1, wget_sys_time *wst2)
 #endif
 }
 
-/* Return the number of milliseconds elapsed since the timer was last
-   reset.  It is allowed to call this function more than once to get
-   increasingly higher elapsed values.  These timers handle clock
-   skew.  */
+/* Update the timer's elapsed interval.  This function causes the
+   timer to call gettimeofday (or time(), etc.) to update its idea of
+   current time.  To get the elapsed interval in milliseconds, use
+   wtimer_read.
 
-double
-wtimer_elapsed (struct wget_timer *wt)
+   This function handles clock skew, i.e. time that moves backwards is
+   ignored.  */
+
+void
+wtimer_update (struct wget_timer *wt)
 {
   wget_sys_time now;
   double elapsed;
@@ -1462,7 +1468,22 @@ wtimer_elapsed (struct wget_timer *wt)
     }
 
   wt->elapsed_last = elapsed;
-  return elapsed;
+}
+
+/* Return the elapsed time in milliseconds between the last call to
+   wtimer_reset and the last call to wtimer_update.
+
+   A typical use of the timer interface would be:
+
+       struct wtimer *timer = wtimer_new ();
+       ... do something that takes a while ...
+       wtimer_update ();
+       double msecs = wtimer_read ();  */
+
+double
+wtimer_read (const struct wget_timer *wt)
+{
+  return wt->elapsed_last;
 }
 
 /* Return the assessed granularity of the timer implementation, in
index 972d5502debaa68c7c3809ce98b09a53dc8e5c7e..28976ff522c87e471c6b72188c3c949ea686f019 100644 (file)
@@ -111,7 +111,8 @@ struct wget_timer *wtimer_allocate PARAMS ((void));
 struct wget_timer *wtimer_new PARAMS ((void));
 void wtimer_delete PARAMS ((struct wget_timer *));
 void wtimer_reset PARAMS ((struct wget_timer *));
-double wtimer_elapsed PARAMS ((struct wget_timer *));
+void wtimer_update PARAMS ((struct wget_timer *));
+double wtimer_read PARAMS ((const struct wget_timer *));
 double wtimer_granularity PARAMS ((void));
 
 char *html_quote_string PARAMS ((const char *));