From 4fddf40436143853ae1bd16107906379dc49d167 Mon Sep 17 00:00:00 2001 From: Andrew Wood Date: Sun, 16 Jul 2023 21:00:08 +0100 Subject: [PATCH] Rename --eta-window to --average-rate-window, since that is what it does, and correct the behaviour of the final update so the final rate shown is the average rate across the whole transfer. --- doc/NEWS.md | 2 +- doc/quickref.1.in | 6 +++--- src/include/options.h | 2 +- src/include/pv.h | 2 +- src/main/help.c | 4 ++-- src/main/main.c | 2 +- src/main/options.c | 6 +++--- src/pv/display.c | 16 ++++++++++++++++ src/pv/state.c | 2 +- 9 files changed, 29 insertions(+), 13 deletions(-) diff --git a/doc/NEWS.md b/doc/NEWS.md index 17f4088..5e77bbb 100644 --- a/doc/NEWS.md +++ b/doc/NEWS.md @@ -5,7 +5,7 @@ UNRELEASED * fix: add burst rate limit to transfer, so rate limits are not broken by bursty traffic (pull request [#62](https://github.com/a-j-wood/pv/pull/62) supplied by [Volodymyr Bychkovyak](https://github.com/vbychkoviak)) * feature: the "`--size`" option now accepts "`@filename`" to use the size of another file (pull request [#57](https://github.com/a-j-wood/pv/pull/57) supplied by [Dave Beckett](https://github.com/dajobe)) * feature: the "`--watchfd`" option is now available on OS X (pull request [#60](https://github.com/a-j-wood/pv/pull/60) supplied by [christoph-zededa](https://github.com/christoph-zededa)) - * feature: new "`--eta-window`" option, to set the window over which the rate is checked, when calculating the ETA (pull request [#65](https://github.com/a-j-wood/pv/pull/65) supplied by [lemonsqueeze](https://github.com/lemonsqueeze)) + * feature: new "`--average-rate-window`" option, to set the window over which the average rate is calculated, also used for ETA (modified from pull request [#65](https://github.com/a-j-wood/pv/pull/65) supplied by [lemonsqueeze](https://github.com/lemonsqueeze)) * feature: the "`--watchfd`" option will now show relative filenames, if they are under the current directory (pull request [#66](https://github.com/a-j-wood/pv/pull/66) supplied by [ikasty](https://github.com/ikasty)) * docs: moved all open issues into GitHub and updated the TODO list * docs: renamed README to README.md and altered it to Markdown format diff --git a/doc/quickref.1.in b/doc/quickref.1.in index 8257494..ff978e4 100644 --- a/doc/quickref.1.in +++ b/doc/quickref.1.in @@ -143,7 +143,7 @@ transfer. .TP .B \-a, \-\-average\-rate Turn the average rate counter on. This will display the current average -rate of data transfer (default: last 30s, see --eta-window). +rate of data transfer (default: last 30s, see --average-rate-window). .TP .B \-b, \-\-bytes Turn the total byte counter on. This will display the total amount of @@ -276,10 +276,10 @@ Wait seconds between updates. The default is to update every second. Note that this can be a decimal such as 0.1. .TP -.B \-m SEC, \-\-eta-window SEC +.B \-m SEC, \-\-average-rate-window SEC Compute current average rate over a .B SEC -seconds window for ETA calculation (default 30s). +seconds window for average rate and ETA calculations (default 30s). .TP .B \-w WIDTH, \-\-width WIDTH Assume the terminal is diff --git a/src/include/options.h b/src/include/options.h index 9de9e68..08b7bc5 100644 --- a/src/include/options.h +++ b/src/include/options.h @@ -42,7 +42,7 @@ struct opts_s { /* structure describing run-time options */ double delay_start; /* delay before first display */ unsigned int watch_pid; /* process to watch fds of */ int watch_fd; /* fd to watch */ - unsigned int eta_window; /* time window in seconds for eta calculations */ + unsigned int average_rate_window; /* time window in seconds for average rate calculations */ unsigned int width; /* screen width */ unsigned int height; /* screen height */ char *name; /* process name, if any */ diff --git a/src/include/pv.h b/src/include/pv.h index 26130e9..853d769 100644 --- a/src/include/pv.h +++ b/src/include/pv.h @@ -93,7 +93,7 @@ extern void pv_state_name_set(pvstate_t, const char *); extern void pv_state_format_string_set(pvstate_t, const char *); extern void pv_state_watch_pid_set(pvstate_t, unsigned int); extern void pv_state_watch_fd_set(pvstate_t, int); -extern void pv_state_eta_window_set(pvstate_t, int); +extern void pv_state_average_rate_window_set(pvstate_t, int); extern void pv_state_inputfiles(pvstate_t, int, const char **); diff --git a/src/main/help.c b/src/main/help.c index 103e49c..ee7f198 100644 --- a/src/main/help.c +++ b/src/main/help.c @@ -40,9 +40,9 @@ void display_help(void) N_("show data transfer rate counter")}, {"-a", "--average-rate", 0, N_("show data transfer average rate counter")}, - {"-m", "--eta-window", N_("SEC"), + {"-m", "--average-rate-window", N_("SEC"), N_ - ("compute ETA based on rate over past SEC seconds (default 30s)")}, + ("compute average rate over past SEC seconds (default 30s)")}, {"-b", "--bytes", 0, N_("show number of bytes transferred")}, {"-T", "--buffer-percent", 0, diff --git a/src/main/main.c b/src/main/main.c index 5e0f75f..328110b 100644 --- a/src/main/main.c +++ b/src/main/main.c @@ -206,7 +206,7 @@ int main(int argc, char **argv) pv_state_format_string_set(state, opts->format); pv_state_watch_pid_set(state, opts->watch_pid); pv_state_watch_fd_set(state, opts->watch_fd); - pv_state_eta_window_set(state, opts->eta_window); + pv_state_average_rate_window_set(state, opts->average_rate_window); pv_state_set_format(state, opts->progress, opts->timer, opts->eta, opts->fineta, opts->rate, opts->average_rate, diff --git a/src/main/options.c b/src/main/options.c index aad3fe1..7994a14 100644 --- a/src/main/options.c +++ b/src/main/options.c @@ -82,7 +82,7 @@ opts_t opts_parse(int argc, char **argv) {"remote", 1, NULL, (int) 'R'}, {"pidfile", 1, NULL, (int) 'P'}, {"watchfd", 1, NULL, (int) 'd'}, - {"eta-window", 1, NULL, (int) 'm'}, + {"average-rate-window", 1, NULL, (int) 'm'}, {NULL, 0, NULL, 0} }; int option_index = 0; @@ -124,7 +124,7 @@ opts_t opts_parse(int argc, char **argv) opts->delay_start = 0; opts->watch_pid = 0; opts->watch_fd = -1; - opts->eta_window = 30; + opts->average_rate_window = 30; do { #ifdef HAVE_GETOPT_LONG @@ -341,7 +341,7 @@ opts_t opts_parse(int argc, char **argv) &(opts->watch_fd)); break; case 'm': - opts->eta_window = pv_getnum_ui(optarg); + opts->average_rate_window = pv_getnum_ui(optarg); break; default: #ifdef HAVE_GETOPT_LONG diff --git a/src/pv/display.c b/src/pv/display.c index 9e3a576..3214108 100644 --- a/src/pv/display.c +++ b/src/pv/display.c @@ -542,6 +542,22 @@ static char *pv__format(pvstate_t state, update_history_avg_rate(state, total_bytes, elapsed_sec, rate); average_rate = state->current_avg_rate; + /* + * If this is the final update at the end of the transfer, we + * recalculate the rate - and the average rate - across the whole + * period of the transfer. + */ + if (bytes_since_last < 0) { + /* Sanity check to avoid division by zero */ + if (elapsed_sec < 0.000001) + elapsed_sec = 0.000001; + average_rate = + (((long double) total_bytes) - + ((long double) state->initial_offset)) / + (long double) elapsed_sec; + rate = average_rate; + } + if (state->size <= 0) { /* * If we don't know the total size of the incoming data, diff --git a/src/pv/state.c b/src/pv/state.c index d002c41..80e21d9 100644 --- a/src/pv/state.c +++ b/src/pv/state.c @@ -250,7 +250,7 @@ void pv_state_watch_fd_set(pvstate_t state, int val) state->watch_fd = val; }; -void pv_state_eta_window_set(pvstate_t state, int val) +void pv_state_average_rate_window_set(pvstate_t state, int val) { if (val < 1) val = 1;