Skip to content

Commit 041769d

Browse files
eworm-desteadytao
andauthored
human-readable: standardise size and progress-rate formatting (#459)
* human-readable: do not add trailing space in output Co-authored-by: Zen Dodd <mail@steadytao.com> * human-readable: use dynamic precision length Let's lower precision for huge numbers. The output used to be: 3.45M -> 46.73M -> 523.11M -> 1.24G -> ... With this change the code always gives the three most significant digits: 3.45M -> 46.7M -> 523M -> 1.24G -> ... Co-authored-by: Zen Dodd <mail@steadytao.com> * human-readable: also handle num < mult with the same code ... just make sure no precision is added. Co-authored-by: Zen Dodd <mail@steadytao.com> * human-readable: add an "i" to unit to indicate binary (1024) base Co-authored-by: Zen Dodd <mail@steadytao.com> * human-readable: also use it to format rate in progress Let's also simplify the code for rate in progress, and benefit from same functionality. Co-authored-by: Zen Dodd <mail@steadytao.com> * human-readable: append suffix "B" for byte to ofs... ... and also fix the formatting in man page. Co-authored-by: Zen Dodd <mail@steadytao.com> * human-readable: unbreak for do_big_dnum() with fract Co-authored-by: Christian Hesse <mail@eworm.de> * testsuite: cover human-readable output --------- Co-authored-by: Zen Dodd <mail@steadytao.com>
1 parent 750012c commit 041769d

4 files changed

Lines changed: 63 additions & 37 deletions

File tree

‎lib/compat.c‎

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -184,23 +184,33 @@ char *do_big_num(int64 num, int human_flag, const char *fract)
184184
if (human_flag > 1) {
185185
unsigned int mult = human_flag == 2 ? 1000 : 1024;
186186

187-
if (abs_num >= mult) {
188-
const char* units = " KMGTPE";
189-
uint64_t powi = 1;
187+
char units[] = "\0KMGTPE";
188+
char *unit = units;
189+
uint64_t powi = 1;
190190

191-
for (;;) {
192-
if (abs_num / mult < powi)
193-
break;
191+
for (;;) {
192+
if (abs_num / mult < powi)
193+
break;
194194

195-
if (units[1] == '\0')
196-
break;
195+
if (unit[1] == '\0')
196+
break;
197+
198+
powi *= mult;
199+
++unit;
200+
}
201+
unit[1] = '\0';
202+
203+
if (powi > 1) {
204+
unsigned int powj = 1, precision = 2;
197205

198-
powi *= mult;
199-
++units;
206+
for (; precision > 0; precision--) {
207+
powj *= 10;
208+
if (abs_num / powi < powj)
209+
break;
200210
}
201211

202-
snprintf(bufs[n], sizeof bufs[0], "%.2f%c",
203-
(double) num / powi, *units);
212+
snprintf(bufs[n], sizeof bufs[0], "%.*f%s%s", precision,
213+
(double) num / powi, unit, *unit && mult == 1024 ? "i" : "");
204214
return bufs[n];
205215
}
206216
}

‎progress.c‎

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ static unsigned long msdiff(struct timeval *t1, struct timeval *t2)
6969
static void rprint_progress(OFF_T ofs, OFF_T size, struct timeval *now, int is_last)
7070
{
7171
char rembuf[64], eol[128];
72-
const char *units;
7372
unsigned long diff;
7473
double rate, remain;
7574
int pct;
@@ -93,26 +92,16 @@ static void rprint_progress(OFF_T ofs, OFF_T size, struct timeval *now, int is_l
9392
/* Compute stats based on the starting info. */
9493
if (!ph_start.time.tv_sec || !(diff = msdiff(&ph_start.time, now)))
9594
diff = 1;
96-
rate = (double) (ofs - ph_start.ofs) * 1000.0 / diff / 1024.0;
95+
rate = (double) (ofs - ph_start.ofs) * 1000.0 / diff;
9796
/* Switch to total time taken for our last update. */
9897
remain = (double) diff / 1000.0;
9998
} else {
10099
strlcpy(eol, " ", sizeof eol);
101100
/* Compute stats based on recent progress. */
102101
if (!(diff = msdiff(&ph_list[oldest_hpos].time, now)))
103102
diff = 1;
104-
rate = (double) (ofs - ph_list[oldest_hpos].ofs) * 1000.0 / diff / 1024.0;
105-
remain = rate ? (double) (size - ofs) / rate / 1000.0 : 0.0;
106-
}
107-
108-
if (rate > 1024*1024) {
109-
rate /= 1024.0 * 1024.0;
110-
units = "GB/s";
111-
} else if (rate > 1024) {
112-
rate /= 1024.0;
113-
units = "MB/s";
114-
} else {
115-
units = "kB/s";
103+
rate = (double) (ofs - ph_list[oldest_hpos].ofs) * 1000.0 / diff;
104+
remain = rate ? (double) (size - ofs) / rate : 0.0;
116105
}
117106

118107
if (remain < 0 || remain > 9999.0 * 3600.0)
@@ -127,8 +116,8 @@ static void rprint_progress(OFF_T ofs, OFF_T size, struct timeval *now, int is_l
127116
output_needs_newline = 0;
128117
pct = ofs == size ? 100 : (int) (100.0 * ofs / size);
129118
rput_progress();
130-
rprintf(FCLIENT, "%15s %3d%% %7.2f%s %s%s",
131-
human_num(ofs), pct, rate, units, rembuf, eol);
119+
rprintf(FCLIENT, "%15sB %3d%% %7sB/s %s%s", human_num(ofs), pct,
120+
rate >= INT64_MAX ? ">MAX " : human_num((int64)rate), rembuf, eol);
132121
if (!is_last && !quiet) {
133122
output_needs_newline = 1;
134123
rflush(FCLIENT);

‎rsync.1.md‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3616,7 +3616,11 @@ sign) if you want the local shell to expand it.
36163616
The unit letters that are appended in levels 2 and 3 are: `K` (kilo), `M`
36173617
(mega), `G` (giga), `T` (tera), `P` (peta) or `E` (exa). For example, a
36183618
1234567-byte file would output as 1.23M in level-2 (assuming that a
3619-
period is your local decimal point).
3619+
period is your local decimal point). Dynamic precision is applied, so the
3620+
three most-significant digits are shown (for example: 3.45M -> 46.7M ->
3621+
523M -> 1.24G -> ...).
3622+
Additionally an `i` is appended in level-3 to indicate the binary base.
3623+
The same file would output as 1.17Mi in level-3.
36203624

36213625
Backward compatibility note: versions of rsync prior to 3.1.0 (September 2013) do not
36223626
support human-readable level 1, and they default to level 0. Thus,
@@ -3784,10 +3788,10 @@ sign) if you want the local shell to expand it.
37843788
While rsync is transferring a regular file, it updates a progress line that
37853789
looks like this:
37863790

3787-
> 782448 63% 110.64kB/s 0:00:04
3791+
> 782,448B 63% 113,295B/s 0:00:04
37883792
3789-
In this example, the receiver has reconstructed 782448 bytes or 63% of the
3790-
sender's file, which is being reconstructed at a rate of 110.64 kilobytes
3793+
In this example, the receiver has reconstructed 782,448 bytes or 63% of the
3794+
sender's file, which is being reconstructed at a rate of 113,295 bytes
37913795
per second, and the transfer will finish in 4 seconds if the current rate
37923796
is maintained until the end.
37933797

@@ -3801,11 +3805,11 @@ sign) if you want the local shell to expand it.
38013805
When the file transfer finishes, rsync replaces the progress line with a
38023806
summary line that looks like this:
38033807

3804-
> 1,238,099 100% 146.38kB/s 0:00:08 (xfr#5, to-chk=169/396)
3808+
> 1,238,099B 100% 149,893B/s 0:00:08 (xfr#5, to-chk=169/396)
38053809
38063810
In this example, the file was 1,238,099 bytes long in total, the average
3807-
rate of transfer for the whole file was 146.38 kilobytes per second over
3808-
the 8 seconds that it took to complete, it was the 5th transfer of a
3811+
rate of transfer for the whole file was 149,893 bytes per second over the
3812+
8 seconds that it took to complete, it was the 5th transfer of a
38093813
regular file during the current rsync session, and there are 169 more files
38103814
for the receiver to check (to see if they are up-to-date or not) remaining
38113815
out of the 396 total files in the file-list.

‎testsuite/output-options_test.py‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,9 @@ def out(*args, want_rc=0, env=None, text=True):
9595
# --- --progress shows a percentage ------------------------------------------
9696
rmtree(TODIR)
9797
p = out('-a', '--progress', f'{src}/', f'{TODIR}/')
98-
if '100%' not in p.stdout:
99-
test_fail(f"--progress did not show a percentage:\n{p.stdout}")
98+
progress_re = r'[\d.,]+B\s+100%\s+[\d.,]+B/s\s+\d+:\d\d:\d\d'
99+
if not re.search(progress_re, p.stdout):
100+
test_fail(f"--progress did not show the expected final shape:\n{p.stdout}")
100101

101102
# --quiet must suppress the progress carriage return as well as its text.
102103
rmtree(TODIR)
@@ -121,6 +122,28 @@ def out(*args, want_rc=0, env=None, text=True):
121122
if re.search(suffix_re, plain):
122123
test_fail(f"--stats without -h unexpectedly used a unit suffix:\n{plain}")
123124

125+
rmtree(TODIR)
126+
human_binary = out('-a', '-hh', '--stats', f'{src}/', f'{TODIR}/').stdout
127+
if 'Total file size: 48.8Ki bytes' not in human_binary:
128+
test_fail(f"-hh did not use dynamic binary-unit precision:\n{human_binary}")
129+
130+
rmtree(src)
131+
rmtree(TODIR)
132+
makepath(src)
133+
make_data_file(src / 'threshold', 1024)
134+
threshold = out('-a', '-hh', '--stats', f'{src}/', f'{TODIR}/').stdout
135+
if 'Total file size: 1.00Ki bytes' not in threshold:
136+
test_fail(f"-hh did not show an exact-threshold binary unit:\n{threshold}")
137+
138+
rmtree(src)
139+
rmtree(TODIR)
140+
makepath(src)
141+
make_data_file(src / 'below-threshold', 1000)
142+
below_threshold = out('-a', '-hh', '--stats', f'{src}/', f'{TODIR}/').stdout
143+
if 'Total file size: 1,000 bytes' not in below_threshold:
144+
test_fail("-hh should preserve raw byte formatting below the binary-unit "
145+
f"threshold:\n{below_threshold}")
146+
124147
# --- -8 / --8-bit-output leaves high-bit filename bytes unescaped ------------
125148
# rsync escapes non-printable name bytes as \#NNN; -8 prints 8-bit bytes raw.
126149
# This needs a filename containing a high-bit byte and a C locale (where such a

0 commit comments

Comments
 (0)