4 static struct timeval print_time;
5 static struct timeval start_time;
6 static OFF_T start_ofs;
8 static unsigned long msdiff(struct timeval *t1, struct timeval *t2)
10 return (t2->tv_sec - t1->tv_sec) * 1000
11 + (t2->tv_usec - t1->tv_usec) / 1000;
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.)
22 static void rprint_progress(OFF_T ofs, OFF_T size, struct timeval *now,
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;
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. --
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;
39 if (rate > 1024*1024) {
40 rate /= 1024.0 * 1024.0;
42 } else if (rate > 1024) {
49 remain_s = (int) remain % 60;
50 remain_m = (int) (remain / 60.0) % 60;
51 remain_h = (int) (remain / 3600.0);
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");
59 void end_progress(OFF_T size)
61 extern int do_progress, am_server;
63 if (do_progress && !am_server) {
65 gettimeofday(&now, NULL);
66 rprint_progress(size, size, &now, True);
70 print_time.tv_sec = print_time.tv_usec = 0;
71 start_time.tv_sec = start_time.tv_usec = 0;
74 void show_progress(OFF_T ofs, OFF_T size)
76 extern int do_progress, am_server;
79 gettimeofday(&now, NULL);
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;
89 && ofs > last_ofs + 1000
90 && msdiff(&print_time, &now) > 250) {
91 rprint_progress(ofs, size, &now, False);
93 print_time.tv_sec = now.tv_sec;
94 print_time.tv_usec = now.tv_usec;