blob: fcd2a3d2ebab95650c768a567569e15965330c86 [file] [log] [blame]
Wayne Davison0f78b812006-04-25 20:23:34 +00001/*
2 * Routines to output progress information during a file transfer.
Wayne Davison603e6b02003-12-12 17:13:22 +00003 *
Wayne Davison0f78b812006-04-25 20:23:34 +00004 * 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
Wayne Davison603e6b02003-12-12 17:13:22 +00008 *
Martin Poolfcb69e52002-04-08 05:28:31 +00009 * 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.
Wayne Davison603e6b02003-12-12 17:13:22 +000013 *
Martin Poolfcb69e52002-04-08 05:28:31 +000014 * 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.
Wayne Davison603e6b02003-12-12 17:13:22 +000018 *
Wayne Davisone7c67062006-04-25 23:51:12 +000019 * 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.
Martin Poolfcb69e52002-04-08 05:28:31 +000022 */
23
Martin Poolb35d0d82002-04-08 04:10:20 +000024#include "rsync.h"
25
Wayne Davisonfb6e0ea2004-01-13 18:25:03 +000026extern struct stats stats;
Wayne Davison5562deb2004-01-13 05:13:57 +000027extern int am_server;
28
Wayne Davison304f1272004-02-28 02:00:57 +000029#define PROGRESS_HISTORY_SECS 5
30
Wayne Davison4f5b0752005-02-14 00:53:43 +000031#ifdef GETPGRP_VOID
Wayne Davison8311f1c2005-01-28 23:00:58 +000032#define GETPGRP_ARG
33#else
34#define GETPGRP_ARG 0
35#endif
36
Wayne Davison304f1272004-02-28 02:00:57 +000037struct progress_history {
38 struct timeval time;
39 OFF_T ofs;
40};
41
42static struct progress_history ph_start;
43static struct progress_history ph_list[PROGRESS_HISTORY_SECS];
44static int newest_hpos, oldest_hpos;
Martin Poolb35d0d82002-04-08 04:10:20 +000045
46static unsigned long msdiff(struct timeval *t1, struct timeval *t2)
47{
Wayne Davison304f1272004-02-28 02:00:57 +000048 return (t2->tv_sec - t1->tv_sec) * 1000L
Wayne Davison603e6b02003-12-12 17:13:22 +000049 + (t2->tv_usec - t1->tv_usec) / 1000;
Martin Poolb35d0d82002-04-08 04:10:20 +000050}
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 **/
60static void rprint_progress(OFF_T ofs, OFF_T size, struct timeval *now,
61 int is_last)
62{
Wayne Davisonfb6e0ea2004-01-13 18:25:03 +000063 char eol[256];
Wayne Davison603e6b02003-12-12 17:13:22 +000064 const char *units;
Wayne Davison304f1272004-02-28 02:00:57 +000065 int pct = ofs == size ? 100 : (int) (100.0 * ofs / size);
66 unsigned long diff;
67 double rate, remain;
Wayne Davison603e6b02003-12-12 17:13:22 +000068 int remain_h, remain_m, remain_s;
Martin Poolb35d0d82002-04-08 04:10:20 +000069
Wayne Davison304f1272004-02-28 02:00:57 +000070 if (is_last) {
71 /* Compute stats based on the starting info. */
Wayne Davison56efa562005-03-05 17:49:46 +000072 if (!ph_start.time.tv_sec
73 || !(diff = msdiff(&ph_start.time, now)))
Wayne Davison304f1272004-02-28 02:00:57 +000074 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. */
Wayne Davison56efa562005-03-05 17:49:46 +000080 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;
Wayne Davison304f1272004-02-28 02:00:57 +000084 remain = rate ? (double) (size - ofs) / rate / 1000.0 : 0.0;
85 }
86
Wayne Davison603e6b02003-12-12 17:13:22 +000087 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 }
Martin Poolb35d0d82002-04-08 04:10:20 +000096
Wayne Davison603e6b02003-12-12 17:13:22 +000097 remain_s = (int) remain % 60;
98 remain_m = (int) (remain / 60.0) % 60;
99 remain_h = (int) (remain / 3600.0);
100
Wayne Davisonfb6e0ea2004-01-13 18:25:03 +0000101 if (is_last) {
Wayne Davison91683c42005-11-03 19:45:59 +0000102 snprintf(eol, sizeof eol, " (xfer#%d, to-check=%d/%d)\n",
Wayne Davisonfb6e0ea2004-01-13 18:25:03 +0000103 stats.num_transferred_files,
Wayne Davison3381ffa2005-11-03 19:55:30 +0000104 stats.num_files - stats.current_file_index - 1,
Wayne Davisonfb6e0ea2004-01-13 18:25:03 +0000105 stats.num_files);
106 } else
Wayne Davisonc9bce0b2006-10-13 23:17:33 +0000107 strlcpy(eol, "\r", sizeof eol);
Wayne Davison15ce4b22006-05-09 18:31:19 +0000108 rprintf(FCLIENT, "%12s %3d%% %7.2f%s %4d:%02d:%02d%s",
Wayne Davison24172e42006-01-14 17:10:52 +0000109 human_num(ofs), pct, rate, units,
Wayne Davisonfb6e0ea2004-01-13 18:25:03 +0000110 remain_h, remain_m, remain_s, eol);
Martin Poolb35d0d82002-04-08 04:10:20 +0000111}
112
113void end_progress(OFF_T size)
114{
Wayne Davison1c8162a2003-07-08 16:54:53 +0000115 if (!am_server) {
116 struct timeval now;
117 gettimeofday(&now, NULL);
118 rprint_progress(size, size, &now, True);
Martin Poolb35d0d82002-04-08 04:10:20 +0000119 }
Wayne Davison304f1272004-02-28 02:00:57 +0000120 memset(&ph_start, 0, sizeof ph_start);
Martin Poolb35d0d82002-04-08 04:10:20 +0000121}
122
123void show_progress(OFF_T ofs, OFF_T size)
124{
Wayne Davison1c8162a2003-07-08 16:54:53 +0000125 struct timeval now;
Wayne Davison4f5b0752005-02-14 00:53:43 +0000126#if defined HAVE_GETPGRP && defined HAVE_TCGETPGRP
Wayne Davisond6a3e372005-01-25 17:16:13 +0000127 static pid_t pgrp = -1;
128 pid_t tc_pgrp;
129#endif
Martin Poolb35d0d82002-04-08 04:10:20 +0000130
Wayne Davison304f1272004-02-28 02:00:57 +0000131 if (am_server)
132 return;
133
Wayne Davison4f5b0752005-02-14 00:53:43 +0000134#if defined HAVE_GETPGRP && defined HAVE_TCGETPGRP
Wayne Davison8311f1c2005-01-28 23:00:58 +0000135 if (pgrp == -1)
136 pgrp = getpgrp(GETPGRP_ARG);
Wayne Davisond6a3e372005-01-25 17:16:13 +0000137#endif
138
Wayne Davison304f1272004-02-28 02:00:57 +0000139 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;
Wayne Davison1c8162a2003-07-08 16:54:53 +0000158 }
159 else {
Wayne Davison304f1272004-02-28 02:00:57 +0000160 if (msdiff(&ph_list[newest_hpos].time, &now) < 1000)
Wayne Davison1c8162a2003-07-08 16:54:53 +0000161 return;
Wayne Davison304f1272004-02-28 02:00:57 +0000162
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;
Wayne Davison1c8162a2003-07-08 16:54:53 +0000168 }
Martin Poolb35d0d82002-04-08 04:10:20 +0000169
Wayne Davison4f5b0752005-02-14 00:53:43 +0000170#if defined HAVE_GETPGRP && defined HAVE_TCGETPGRP
Wayne Davisond6a3e372005-01-25 17:16:13 +0000171 tc_pgrp = tcgetpgrp(STDOUT_FILENO);
172 if (tc_pgrp != pgrp && tc_pgrp != -1)
173 return;
174#endif
175
Wayne Davison304f1272004-02-28 02:00:57 +0000176 rprint_progress(ofs, size, &now, False);
Martin Poolb35d0d82002-04-08 04:10:20 +0000177}