Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Related Pages  

progress.c

Go to the documentation of this file.
00001 /*  -*- c-file-style: "linux" -*-
00002  * 
00003  * Copyright (C) 1996-2000 by Andrew Tridgell 
00004  * Copyright (C) Paul Mackerras 1996
00005  * Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
00006  * 
00007  * This program is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  * 
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020  */
00021 
00022 #include "rsync.h"
00023 
00024 static OFF_T  last_ofs;
00025 static struct timeval print_time;
00026 static struct timeval start_time;
00027 static OFF_T  start_ofs;
00028 
00029 static unsigned long msdiff(struct timeval *t1, struct timeval *t2)
00030 {
00031     return (t2->tv_sec - t1->tv_sec) * 1000
00032         + (t2->tv_usec - t1->tv_usec) / 1000;
00033 }
00034 
00035 
00036 /**
00037  * @param ofs Current position in file
00038  * @param size Total size of file
00039  * @param is_last True if this is the last time progress will be
00040  * printed for this file, so we should output a newline.  (Not
00041  * necessarily the same as all bytes being received.)
00042  **/
00043 static void rprint_progress(OFF_T ofs, OFF_T size, struct timeval *now,
00044                             int is_last)
00045 {
00046     int           pct  = (ofs == size) ? 100 : (int)((100.0*ofs)/size);
00047     unsigned long diff = msdiff(&start_time, now);
00048     double        rate = diff ? (double) (ofs-start_ofs) * 1000.0 / diff / 1024.0 : 0;
00049     const char    *units;
00050     /* If we've finished transferring this file, show the time taken;
00051      * otherwise show expected time to complete.  That's kind of
00052      * inconsistent, but people can probably cope.  Hopefully we'll
00053      * get more consistent and complete progress reporting soon. --
00054      * mbp */
00055     double        remain = is_last
00056                         ? (double) diff / 1000.0
00057                         : rate ? (double) (size-ofs) / rate / 1000.0 : 0.0;
00058     int           remain_h, remain_m, remain_s;
00059 
00060     if (rate > 1024*1024) {
00061             rate /= 1024.0 * 1024.0;
00062             units = "GB/s";
00063     } else if (rate > 1024) {
00064             rate /= 1024.0;
00065             units = "MB/s";
00066     } else {
00067             units = "kB/s";
00068     }
00069 
00070     remain_s = (int) remain % 60;
00071     remain_m = (int) (remain / 60.0) % 60;
00072     remain_h = (int) (remain / 3600.0);
00073     
00074     rprintf(FINFO, "%12.0f %3d%% %7.2f%s %4d:%02d:%02d%s",
00075             (double) ofs, pct, rate, units,
00076             remain_h, remain_m, remain_s,
00077             is_last ? "\n" : "\r");
00078 }
00079 
00080 void end_progress(OFF_T size)
00081 {
00082         extern int do_progress, am_server;
00083 
00084         if (do_progress && !am_server) {
00085                 struct timeval now;
00086                 gettimeofday(&now, NULL);
00087                 rprint_progress(size, size, &now, True);
00088         }
00089         last_ofs   = 0;
00090         start_ofs  = 0;
00091         print_time.tv_sec  = print_time.tv_usec  = 0;
00092         start_time.tv_sec  = start_time.tv_usec  = 0;
00093 }
00094 
00095 void show_progress(OFF_T ofs, OFF_T size)
00096 {
00097         extern int do_progress, am_server;
00098         struct timeval now;
00099 
00100         gettimeofday(&now, NULL);
00101 
00102         if (!start_time.tv_sec && !start_time.tv_usec) {
00103                 start_time.tv_sec  = now.tv_sec;
00104                 start_time.tv_usec = now.tv_usec;
00105                 start_ofs          = ofs;
00106         }
00107 
00108         if (do_progress
00109             && !am_server
00110             && ofs > last_ofs + 1000
00111             && msdiff(&print_time, &now) > 250) {
00112                 rprint_progress(ofs, size, &now, False);
00113                 last_ofs = ofs;
00114                 print_time.tv_sec  = now.tv_sec;
00115                 print_time.tv_usec = now.tv_usec;
00116         }
00117 }

Generated on Tue Apr 16 12:37:37 2002 for rsync by doxygen1.2.15