[PATCH v3 01/18] progress: add close_progress() to display some statistics

Sascha Hauer sha at pengutronix.de
Fri Aug 19 00:21:55 PDT 2022


On Mon, Aug 15, 2022 at 10:42:05AM +0200, Enrico Scholz wrote:
> With a later patch, this will produce output like
> 
> | :/ cp -v /mnt/tftp/tmp /tmp/
> |         [########...#########] 29138411 bytes, 127413 bytes/s
> 
> Signed-off-by: Enrico Scholz <enrico.scholz at sigma-chemnitz.de>
> ---
>  include/progress.h  |  1 +
>  lib/show_progress.c | 25 +++++++++++++++++++++++++
>  2 files changed, 26 insertions(+)
> 
> diff --git a/include/progress.h b/include/progress.h
> index 7230bd3a9bd5..219d772201bf 100644
> --- a/include/progress.h
> +++ b/include/progress.h
> @@ -16,6 +16,7 @@ void init_progression_bar(loff_t max);
>   * spinner is printed.
>   */
>  void show_progress(loff_t now);
> +void close_progress(loff_t total);
>  
>  extern struct notifier_head progress_notifier;
>  
> diff --git a/lib/show_progress.c b/lib/show_progress.c
> index 1b624bcb9af8..5b42a2d74b8f 100644
> --- a/lib/show_progress.c
> +++ b/lib/show_progress.c
> @@ -24,6 +24,7 @@
>  static loff_t printed;
>  static loff_t progress_max;
>  static unsigned spin;
> +static uint64_t start_tm;
>  
>  void show_progress(loff_t now)
>  {
> @@ -47,11 +48,35 @@ void show_progress(loff_t now)
>  	}
>  }
>  
> +void close_progress(loff_t total)
> +{
> +	uint64_t now = get_time_ns();
> +	unsigned long speed;
> +	unsigned long delta;
> +
> +	if (now <= start_tm) {
> +		pr_crit("tm mismatch");
> +		return;
> +	}
> +
> +	if (total < 0)
> +		return;
> +
> +	/* convert to ms and add '1' to avoid div-by-zero */
> +	delta = div_u64(now - start_tm, 1000000) + 1;
> +
> +	/* speed is bytes/s */
> +	speed = div_u64(total * 1000, delta);
> +
> +	printf("] %llu bytes, %lu bytes/s", (unsigned long long)total, speed);
> +}

show_progress() doesn't necessarily show the progress in bytes, it could
also be percent, megabytes or anything else. We could maybe rename this
to close_progress_bytes(). Also it would be nice to print the sizes in
human readable form, maybe size_human_readable() could be used here.

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



More information about the barebox mailing list