Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve human readable output #459

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions lib/compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,28 +181,34 @@ char *do_big_num(int64 num, int human_flag, const char *fract)

if (human_flag > 1) {
int mult = human_flag == 2 ? 1000 : 1024;
if (num >= mult || num <= -mult) {
double dnum = (double)num / mult;
char units;
if (num < 0)
dnum = -dnum;
if (dnum < mult)
units = 'K';
else if ((dnum /= mult) < mult)
units = 'M';
else if ((dnum /= mult) < mult)
units = 'G';
else if ((dnum /= mult) < mult)
units = 'T';
else {
dnum /= mult;
units = 'P';
}
if (num < 0)
dnum = -dnum;
snprintf(bufs[n], sizeof bufs[0], "%.2f%c", dnum, units);
return bufs[n];

const char* units = " KMGTPEZY";
int64 powi = 1;
int powj = 1, precision = 2;

for (;;) {
if (labs(num / mult) < powi)
break;

if (units[1] == '\0')
break;

powi *= mult;
++units;
}

if (powi == 1)
precision = 0;

for (; precision > 0; precision--) {
powj *= 10;
if (labs(num / powi) < powj)
break;
}

snprintf(bufs[n], sizeof bufs[0], "%.*f%c%s", precision,
(double) num / powi, *units, num > mult && mult == 1024 ? "i" : "");
return bufs[n];
}

s = bufs[n] + sizeof bufs[0] - 1;
Expand Down
31 changes: 10 additions & 21 deletions progress.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ static unsigned long msdiff(struct timeval *t1, struct timeval *t2)
static void rprint_progress(OFF_T ofs, OFF_T size, struct timeval *now, int is_last)
{
char rembuf[64], eol[128];
const char *units;
unsigned long diff;
double rate, remain;
int64 rate, remain;
int pct;

if (is_last) {
Expand All @@ -93,41 +92,31 @@ static void rprint_progress(OFF_T ofs, OFF_T size, struct timeval *now, int is_l
/* Compute stats based on the starting info. */
if (!ph_start.time.tv_sec || !(diff = msdiff(&ph_start.time, now)))
diff = 1;
rate = (double) (ofs - ph_start.ofs) * 1000.0 / diff / 1024.0;
rate = (ofs - ph_start.ofs) * 1000 / diff;
/* Switch to total time taken for our last update. */
remain = (double) diff / 1000.0;
remain = diff;
} else {
strlcpy(eol, " ", sizeof eol);
/* Compute stats based on recent progress. */
if (!(diff = msdiff(&ph_list[oldest_hpos].time, now)))
diff = 1;
rate = (double) (ofs - ph_list[oldest_hpos].ofs) * 1000.0 / diff / 1024.0;
remain = rate ? (double) (size - ofs) / rate / 1000.0 : 0.0;
rate = (ofs - ph_list[oldest_hpos].ofs) * 1000 / diff;
remain = rate ? (size - ofs) / rate : 0;
}

if (rate > 1024*1024) {
rate /= 1024.0 * 1024.0;
units = "GB/s";
} else if (rate > 1024) {
rate /= 1024.0;
units = "MB/s";
} else {
units = "kB/s";
}

if (remain < 0 || remain > 9999.0 * 3600.0)
if (remain < 0 || remain > (int64) 9999999 * 3600)
strlcpy(rembuf, " ??:??:??", sizeof rembuf);
else {
snprintf(rembuf, sizeof rembuf, "%4u:%02u:%02u",
(unsigned int) (remain / 3600.0),
(unsigned int) (remain / 60.0) % 60,
(unsigned int) (remain / 3600),
(unsigned int) (remain / 60) % 60,
(unsigned int) remain % 60);
}

output_needs_newline = 0;
pct = ofs == size ? 100 : (int) (100.0 * ofs / size);
rprintf(FCLIENT, "\r%15s %3d%% %7.2f%s %s%s",
human_num(ofs), pct, rate, units, rembuf, eol);
rprintf(FCLIENT, "\r%15sB %3d%% %7sB/s %s%s",
human_num(ofs), pct, human_num(rate), rembuf, eol);
if (!is_last && !quiet) {
output_needs_newline = 1;
rflush(FCLIENT);
Expand Down
8 changes: 5 additions & 3 deletions rsync.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -3309,9 +3309,11 @@ expand it.
digits) by specifying the `--no-human-readable` (`--no-h`) option.

The unit letters that are appended in levels 2 and 3 are: `K` (kilo), `M`
(mega), `G` (giga), `T` (tera), or `P` (peta). For example, a 1234567-byte
file would output as 1.23M in level-2 (assuming that a period is your local
decimal point).
(mega), `G` (giga), `T` (tera), `P` (peta), `E` (exa), `Z` (zetta) or `Y`
(yotta). For example, a 1234567-byte file would output as 1.23M in level-2
(assuming that a period is your local decimal point).
Additionally an `i` is appended in level-3 to indicate the binary base.
The same file would output as 1.17Mi in level-3.

Backward compatibility note: versions of rsync prior to 3.1.0 do not
support human-readable level 1, and they default to level 0. Thus,
Expand Down
Loading