Current-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: progress(1) a bit drunk?



On Sunday 20 July 2008 21:58:33 David Holland wrote:
> On Sun, Jul 20, 2008 at 08:42:25PM +0100, Roy Marples wrote:
>  > > If the time jumps backward it will confuse progressbar.c, but to get
>  > > the results you were seeing with the rate it'd need to have jumped
>  > > back to before the transfer started. (Plus, if you're losing time
>  > > that's probably not it.)
>  >
>  > Maybe the code should use the clock_gettime(CLOCK_MONOTONIC, &timespec)
>  > function instead then, so any clock changes don't affect the progress
>  > bar.
>
> Patches are welcome :-)


And in this case, trivial too :)
A quick test shows that I didn't break it ;)

Thanks

Roy
? .gdbinit
? ftp
? ftp.cat1
Index: progressbar.c
===================================================================
RCS file: /cvsroot/src/usr.bin/ftp/progressbar.c,v
retrieving revision 1.19
diff -u -p -r1.19 progressbar.c
--- progressbar.c       28 Apr 2008 20:24:13 -0000      1.19
+++ progressbar.c       21 Jul 2008 06:40:31 -0000
@@ -102,6 +102,20 @@ static const char * const suffixes[] = {
 };
 #define NSUFFIXES      (sizeof(suffixes) / sizeof(suffixes[0]))
 
+/* Retuns a monotonic clock in a timeval so timersub and friends can use it */
+static int
+getmonotonicclock(struct timeval *tv)
+{
+       struct timespec ts;
+
+       if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1)
+               return -1;
+       tv->tv_sec = ts.tv_sec;
+       tv->tv_usec = ts.tv_nsec / 1000;
+       return 0;
+}
+
+
 /*
  * Display a transfer progress bar if progress is non-zero.
  * SIGALRM is hijacked for use by this function.
@@ -154,12 +168,12 @@ progressmeter(int flag)
 #endif
 
        if (flag == -1) {
-               (void)gettimeofday(&start, NULL);
+               (void)getmonotonicclock(&start);
                lastupdate = start;
                lastsize = restart_point;
        }
 
-       (void)gettimeofday(&now, NULL);
+       (void)getmonotonicclock(&now);
        cursize = bytes + restart_point;
        timersub(&now, &lastupdate, &wait);
        if (cursize > lastsize) {
@@ -311,7 +325,7 @@ ptransfer(int siginfo)
        if (!verbose && !progress && !siginfo)
                return;
 
-       (void)gettimeofday(&now, NULL);
+       (void)getmonotonicclock(&now);
        timersub(&now, &start, &td);
        elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
        bytespersec = 0;


Home | Main Index | Thread Index | Old Index