00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00038
00039
00040
00041
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
00051
00052
00053
00054
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 }