| 1 | /* |
| 2 | * Routines to output progress information during a file transfer. |
| 3 | * |
| 4 | * Copyright (C) 1996-2000 Andrew Tridgell |
| 5 | * Copyright (C) 1996 Paul Mackerras |
| 6 | * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org> |
| 7 | * Copyright (C) 2003, 2004, 2005, 2006 Wayne Davison |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License along |
| 20 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 21 | * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. |
| 22 | */ |
| 23 | |
| 24 | #include "rsync.h" |
| 25 | |
| 26 | extern struct stats stats; |
| 27 | extern int am_server; |
| 28 | |
| 29 | #define PROGRESS_HISTORY_SECS 5 |
| 30 | |
| 31 | #ifdef GETPGRP_VOID |
| 32 | #define GETPGRP_ARG |
| 33 | #else |
| 34 | #define GETPGRP_ARG 0 |
| 35 | #endif |
| 36 | |
| 37 | struct progress_history { |
| 38 | struct timeval time; |
| 39 | OFF_T ofs; |
| 40 | }; |
| 41 | |
| 42 | static struct progress_history ph_start; |
| 43 | static struct progress_history ph_list[PROGRESS_HISTORY_SECS]; |
| 44 | static int newest_hpos, oldest_hpos; |
| 45 | |
| 46 | static unsigned long msdiff(struct timeval *t1, struct timeval *t2) |
| 47 | { |
| 48 | return (t2->tv_sec - t1->tv_sec) * 1000L |
| 49 | + (t2->tv_usec - t1->tv_usec) / 1000; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * @param ofs Current position in file |
| 55 | * @param size Total size of file |
| 56 | * @param is_last True if this is the last time progress will be |
| 57 | * printed for this file, so we should output a newline. (Not |
| 58 | * necessarily the same as all bytes being received.) |
| 59 | **/ |
| 60 | static void rprint_progress(OFF_T ofs, OFF_T size, struct timeval *now, |
| 61 | int is_last) |
| 62 | { |
| 63 | char eol[256]; |
| 64 | const char *units; |
| 65 | int pct = ofs == size ? 100 : (int) (100.0 * ofs / size); |
| 66 | unsigned long diff; |
| 67 | double rate, remain; |
| 68 | int remain_h, remain_m, remain_s; |
| 69 | |
| 70 | if (is_last) { |
| 71 | /* Compute stats based on the starting info. */ |
| 72 | if (!ph_start.time.tv_sec |
| 73 | || !(diff = msdiff(&ph_start.time, now))) |
| 74 | diff = 1; |
| 75 | rate = (double) (ofs - ph_start.ofs) * 1000.0 / diff / 1024.0; |
| 76 | /* Switch to total time taken for our last update. */ |
| 77 | remain = (double) diff / 1000.0; |
| 78 | } else { |
| 79 | /* Compute stats based on recent progress. */ |
| 80 | if (!(diff = msdiff(&ph_list[oldest_hpos].time, now))) |
| 81 | diff = 1; |
| 82 | rate = (double) (ofs - ph_list[oldest_hpos].ofs) * 1000.0 |
| 83 | / diff / 1024.0; |
| 84 | remain = rate ? (double) (size - ofs) / rate / 1000.0 : 0.0; |
| 85 | } |
| 86 | |
| 87 | if (rate > 1024*1024) { |
| 88 | rate /= 1024.0 * 1024.0; |
| 89 | units = "GB/s"; |
| 90 | } else if (rate > 1024) { |
| 91 | rate /= 1024.0; |
| 92 | units = "MB/s"; |
| 93 | } else { |
| 94 | units = "kB/s"; |
| 95 | } |
| 96 | |
| 97 | remain_s = (int) remain % 60; |
| 98 | remain_m = (int) (remain / 60.0) % 60; |
| 99 | remain_h = (int) (remain / 3600.0); |
| 100 | |
| 101 | if (is_last) { |
| 102 | snprintf(eol, sizeof eol, " (xfer#%d, to-check=%d/%d)\n", |
| 103 | stats.num_transferred_files, |
| 104 | stats.num_files - stats.current_file_index - 1, |
| 105 | stats.num_files); |
| 106 | } else |
| 107 | strlcpy(eol, "\r", sizeof eol); |
| 108 | rprintf(FCLIENT, "%12s %3d%% %7.2f%s %4d:%02d:%02d%s", |
| 109 | human_num(ofs), pct, rate, units, |
| 110 | remain_h, remain_m, remain_s, eol); |
| 111 | } |
| 112 | |
| 113 | void end_progress(OFF_T size) |
| 114 | { |
| 115 | if (!am_server) { |
| 116 | struct timeval now; |
| 117 | gettimeofday(&now, NULL); |
| 118 | rprint_progress(size, size, &now, True); |
| 119 | } |
| 120 | memset(&ph_start, 0, sizeof ph_start); |
| 121 | } |
| 122 | |
| 123 | void show_progress(OFF_T ofs, OFF_T size) |
| 124 | { |
| 125 | struct timeval now; |
| 126 | #if defined HAVE_GETPGRP && defined HAVE_TCGETPGRP |
| 127 | static pid_t pgrp = -1; |
| 128 | pid_t tc_pgrp; |
| 129 | #endif |
| 130 | |
| 131 | if (am_server) |
| 132 | return; |
| 133 | |
| 134 | #if defined HAVE_GETPGRP && defined HAVE_TCGETPGRP |
| 135 | if (pgrp == -1) |
| 136 | pgrp = getpgrp(GETPGRP_ARG); |
| 137 | #endif |
| 138 | |
| 139 | gettimeofday(&now, NULL); |
| 140 | |
| 141 | if (!ph_start.time.tv_sec) { |
| 142 | int i; |
| 143 | |
| 144 | /* Try to guess the real starting time when the sender started |
| 145 | * to send us data by using the time we last received some data |
| 146 | * in the last file (if it was recent enough). */ |
| 147 | if (msdiff(&ph_list[newest_hpos].time, &now) <= 1500) { |
| 148 | ph_start.time = ph_list[newest_hpos].time; |
| 149 | ph_start.ofs = 0; |
| 150 | } else { |
| 151 | ph_start.time.tv_sec = now.tv_sec; |
| 152 | ph_start.time.tv_usec = now.tv_usec; |
| 153 | ph_start.ofs = ofs; |
| 154 | } |
| 155 | |
| 156 | for (i = 0; i < PROGRESS_HISTORY_SECS; i++) |
| 157 | ph_list[i] = ph_start; |
| 158 | } |
| 159 | else { |
| 160 | if (msdiff(&ph_list[newest_hpos].time, &now) < 1000) |
| 161 | return; |
| 162 | |
| 163 | newest_hpos = oldest_hpos; |
| 164 | oldest_hpos = (oldest_hpos + 1) % PROGRESS_HISTORY_SECS; |
| 165 | ph_list[newest_hpos].time.tv_sec = now.tv_sec; |
| 166 | ph_list[newest_hpos].time.tv_usec = now.tv_usec; |
| 167 | ph_list[newest_hpos].ofs = ofs; |
| 168 | } |
| 169 | |
| 170 | #if defined HAVE_GETPGRP && defined HAVE_TCGETPGRP |
| 171 | tc_pgrp = tcgetpgrp(STDOUT_FILENO); |
| 172 | if (tc_pgrp != pgrp && tc_pgrp != -1) |
| 173 | return; |
| 174 | #endif |
| 175 | |
| 176 | rprint_progress(ofs, size, &now, False); |
| 177 | } |