Skip to content

Commit

Permalink
human-readable: also use it to format rate
Browse files Browse the repository at this point in the history
Let's also simplify the code for rate in progress, and benefit from
same functionality.
  • Loading branch information
eworm-de committed Apr 11, 2024
1 parent eaa5201 commit d60e961
Showing 1 changed file with 10 additions and 21 deletions.
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%15s %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

0 comments on commit d60e961

Please sign in to comment.