Tweaked the output a little.
[rsync/rsync.git] / progress.c
CommitLineData
0f78b815
WD
1/*
2 * Routines to output progress information during a file transfer.
603e6b05 3 *
0f78b815
WD
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
603e6b05 8 *
fcb69e5c
MP
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.
603e6b05 13 *
fcb69e5c
MP
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.
603e6b05 18 *
e7c67065
WD
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.
fcb69e5c
MP
22 */
23
b35d0d8e
MP
24#include "rsync.h"
25
fb6e0ea1 26extern struct stats stats;
5562deb1
WD
27extern int am_server;
28
304f127f
WD
29#define PROGRESS_HISTORY_SECS 5
30
4f5b0756 31#ifdef GETPGRP_VOID
8311f1c1
WD
32#define GETPGRP_ARG
33#else
34#define GETPGRP_ARG 0
35#endif
36
304f127f
WD
37struct 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;
b35d0d8e
MP
45
46static unsigned long msdiff(struct timeval *t1, struct timeval *t2)
47{
304f127f 48 return (t2->tv_sec - t1->tv_sec) * 1000L
603e6b05 49 + (t2->tv_usec - t1->tv_usec) / 1000;
b35d0d8e
MP
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 **/
60static void rprint_progress(OFF_T ofs, OFF_T size, struct timeval *now,
61 int is_last)
62{
fb6e0ea1 63 char eol[256];
603e6b05 64 const char *units;
304f127f
WD
65 int pct = ofs == size ? 100 : (int) (100.0 * ofs / size);
66 unsigned long diff;
67 double rate, remain;
603e6b05 68 int remain_h, remain_m, remain_s;
b35d0d8e 69
304f127f
WD
70 if (is_last) {
71 /* Compute stats based on the starting info. */
56efa564
WD
72 if (!ph_start.time.tv_sec
73 || !(diff = msdiff(&ph_start.time, now)))
304f127f
WD
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. */
56efa564
WD
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;
304f127f
WD
84 remain = rate ? (double) (size - ofs) / rate / 1000.0 : 0.0;
85 }
86
603e6b05
WD
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);
b35d0d8e 100
fb6e0ea1 101 if (is_last) {
91683c43 102 snprintf(eol, sizeof eol, " (xfer#%d, to-check=%d/%d)\n",
fb6e0ea1 103 stats.num_transferred_files,
3381ffa6 104 stats.num_files - stats.current_file_index - 1,
fb6e0ea1
WD
105 stats.num_files);
106 } else
c9bce0b8 107 strlcpy(eol, "\r", sizeof eol);
15ce4b24 108 rprintf(FCLIENT, "%12s %3d%% %7.2f%s %4d:%02d:%02d%s",
24172e4b 109 human_num(ofs), pct, rate, units,
fb6e0ea1 110 remain_h, remain_m, remain_s, eol);
b35d0d8e
MP
111}
112
113void end_progress(OFF_T size)
114{
1c8162a9
WD
115 if (!am_server) {
116 struct timeval now;
117 gettimeofday(&now, NULL);
118 rprint_progress(size, size, &now, True);
b35d0d8e 119 }
304f127f 120 memset(&ph_start, 0, sizeof ph_start);
b35d0d8e
MP
121}
122
123void show_progress(OFF_T ofs, OFF_T size)
124{
1c8162a9 125 struct timeval now;
4f5b0756 126#if defined HAVE_GETPGRP && defined HAVE_TCGETPGRP
d6a3e37b
WD
127 static pid_t pgrp = -1;
128 pid_t tc_pgrp;
129#endif
b35d0d8e 130
304f127f
WD
131 if (am_server)
132 return;
133
4f5b0756 134#if defined HAVE_GETPGRP && defined HAVE_TCGETPGRP
8311f1c1
WD
135 if (pgrp == -1)
136 pgrp = getpgrp(GETPGRP_ARG);
d6a3e37b
WD
137#endif
138
304f127f
WD
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;
1c8162a9
WD
158 }
159 else {
304f127f 160 if (msdiff(&ph_list[newest_hpos].time, &now) < 1000)
1c8162a9 161 return;
b35d0d8e 162
304f127f
WD
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;
b35d0d8e 168 }
304f127f 169
4f5b0756 170#if defined HAVE_GETPGRP && defined HAVE_TCGETPGRP
d6a3e37b
WD
171 tc_pgrp = tcgetpgrp(STDOUT_FILENO);
172 if (tc_pgrp != pgrp && tc_pgrp != -1)
173 return;
174#endif
175
304f127f 176 rprint_progress(ofs, size, &now, False);
b35d0d8e 177}