004b5d1b1bb91de3e968f00e815fafbbc38a78f5
[rsync/rsync.git] / progress.c
1 #include "rsync.h"
2
3 static OFF_T  last_ofs;
4 static struct timeval print_time;
5 static struct timeval start_time;
6 static OFF_T  start_ofs;
7
8 static unsigned long msdiff(struct timeval *t1, struct timeval *t2)
9 {
10     return (t2->tv_sec - t1->tv_sec) * 1000
11         + (t2->tv_usec - t1->tv_usec) / 1000;
12 }
13
14
15 /**
16  * @param ofs Current position in file
17  * @param size Total size of file
18  * @param is_last True if this is the last time progress will be
19  * printed for this file, so we should output a newline.  (Not
20  * necessarily the same as all bytes being received.)
21  **/
22 static void rprint_progress(OFF_T ofs, OFF_T size, struct timeval *now,
23                             int is_last)
24 {
25     int           pct  = (ofs == size) ? 100 : (int)((100.0*ofs)/size);
26     unsigned long diff = msdiff(&start_time, now);
27     double        rate = diff ? (double) (ofs-start_ofs) * 1000.0 / diff / 1024.0 : 0;
28     const char    *units;
29     /* If we've finished transferring this file, show the time taken;
30      * otherwise show expected time to complete.  That's kind of
31      * inconsistent, but people can probably cope.  Hopefully we'll
32      * get more consistent and complete progress reporting soon. --
33      * mbp */
34     double        remain = is_last
35                         ? (double) diff / 1000.0
36                         : rate ? (double) (size-ofs) / rate / 1000.0 : 0.0;
37     int           remain_h, remain_m, remain_s;
38
39     if (rate > 1024*1024) {
40             rate /= 1024.0 * 1024.0;
41             units = "GB/s";
42     } else if (rate > 1024) {
43             rate /= 1024.0;
44             units = "MB/s";
45     } else {
46             units = "kB/s";
47     }
48
49     remain_s = (int) remain % 60;
50     remain_m = (int) (remain / 60.0) % 60;
51     remain_h = (int) (remain / 3600.0);
52     
53     rprintf(FINFO, "%12.0f %3d%% %7.2f%s %4d:%02d:%02d%s",
54             (double) ofs, pct, rate, units,
55             remain_h, remain_m, remain_s,
56             is_last ? "\n" : "\r");
57 }
58
59 void end_progress(OFF_T size)
60 {
61         extern int do_progress, am_server;
62
63         if (do_progress && !am_server) {
64                 struct timeval now;
65                 gettimeofday(&now, NULL);
66                 rprint_progress(size, size, &now, True);
67         }
68         last_ofs   = 0;
69         start_ofs  = 0;
70         print_time.tv_sec  = print_time.tv_usec  = 0;
71         start_time.tv_sec  = start_time.tv_usec  = 0;
72 }
73
74 void show_progress(OFF_T ofs, OFF_T size)
75 {
76         extern int do_progress, am_server;
77         struct timeval now;
78
79         gettimeofday(&now, NULL);
80
81         if (!start_time.tv_sec && !start_time.tv_usec) {
82                 start_time.tv_sec  = now.tv_sec;
83                 start_time.tv_usec = now.tv_usec;
84                 start_ofs          = ofs;
85         }
86
87         if (do_progress
88             && !am_server
89             && ofs > last_ofs + 1000
90             && msdiff(&print_time, &now) > 250) {
91                 rprint_progress(ofs, size, &now, False);
92                 last_ofs = ofs;
93                 print_time.tv_sec  = now.tv_sec;
94                 print_time.tv_usec = now.tv_usec;
95         }
96 }