git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 11/14] progress: allow pure-throughput progress meters

From
Jeff King <peff@peff.net>
Date
Nov 10, 2011, 07:53 UTC
Message-ID
<20111110075300.GK27950@sigill.intra.peff.net>
In-Reply-To
<20111110074330.GA27925@sigill.intra.peff.net>

The progress code assumes we are counting something (usually objects), even if we are measuring throughput. This works for fetching packfiles, since they show us the object count alongside the throughput, like:

  Receiving objects:   2% (301/11968), 22.00 MiB | 10.97 MiB/s

You can also tell the progress code you don't know how many items you have (by specifying a total of 0), and it looks like:

  Counting objects: 34957

However, if you're fetching a single large item, you want throughput but you might not have a meaningful count. You can say you are getting item 0 or 1 out of 1 total, but then the percent meter is misleading:

  Downloading:   0% (0/1), 22.00 MiB | 10.97 MiB/s
or
  Downloading: 100% (0/1), 22.00 MiB | 10.97 MiB/s

Neither of those is accurate. You are probably somewhere between zero and 100 percent through the operation, but you don't know how far.

Telling it you don't know how many items is even uglier:
  Downloading: 1, 22.00 MiB | 10.97 MiB/s

Instead, this patch will omit the count entirely if you are on the zero-th item of an unknown number of items. It looks like:

  Downloading: 22.00 MiB | 10.97 MiB/s
Signed-off-by: Jeff King <peff@peff.net>
---
This was the last amount of work to massage the progress code into doing
what I wanted. It might be nicer if it could show a percentage (if we
know the total size), but there's even more surgery required for that.
 progress.c |   20 ++++++++++++--------
 1 files changed, 12 insertions(+), 8 deletions(-)
diff --git a/progress.c b/progress.c
index 3971f49..92fc3c5 100644
--- a/progress.c
+++ b/progress.c
@@ -103,7 +103,10 @@ static int display(struct progress *progress, unsigned n, const char *done)
 			return 1;
 		}
 	} else if (progress_update) {
-		fprintf(stderr, "%s: %u%s%s", progress->title, n, tp, eol);
+		fprintf(stderr, "%s: ", progress->title);
+		if (n)
+			fprintf(stderr, "%u", n);
+		fprintf(stderr, "%s%s", tp, eol);
 		fflush(stderr);
 		progress_update = 0;
 		return 1;
@@ -113,23 +116,24 @@ static int display(struct progress *progress, unsigned n, const char *done)
 }
 
 static void throughput_string(struct throughput *tp, off_t total,
-			      unsigned int rate)
+			      unsigned int rate, struct progress *p)
 {
+	const char *delim = p->total || p->last_value > 0 ? ", " : "";
 	int l = sizeof(tp->display);
 	if (total > 1 << 30) {
-		l -= snprintf(tp->display, l, ", %u.%2.2u GiB",
+		l -= snprintf(tp->display, l, "%s%u.%2.2u GiB", delim,
 			      (int)(total >> 30),
 			      (int)(total & ((1 << 30) - 1)) / 10737419);
 	} else if (total > 1 << 20) {
 		int x = total + 5243;  /* for rounding */
-		l -= snprintf(tp->display, l, ", %u.%2.2u MiB",
+		l -= snprintf(tp->display, l, "%s%u.%2.2u MiB", delim,
 			      x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20);
 	} else if (total > 1 << 10) {
 		int x = total + 5;  /* for rounding */
-		l -= snprintf(tp->display, l, ", %u.%2.2u KiB",
+		l -= snprintf(tp->display, l, "%s%u.%2.2u KiB", delim,
 			      x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10);
 	} else {
-		l -= snprintf(tp->display, l, ", %u bytes", (int)total);
+		l -= snprintf(tp->display, l, "%s%u bytes", delim, (int)total);
 	}
 
 	if (rate > 1 << 10) {
@@ -197,7 +201,7 @@ void display_throughput(struct progress *progress, off_t total)
 		tp->last_misecs[tp->idx] = misecs;
 		tp->idx = (tp->idx + 1) % TP_IDX_MAX;
 
-		throughput_string(tp, total, rate);
+		throughput_string(tp, total, rate, progress);
 		if (progress->last_value != -1 && progress_update)
 			display(progress, progress->last_value, NULL);
 	}
@@ -255,7 +259,7 @@ void stop_progress_msg(struct progress **p_progress, const char *msg)
 		if (tp) {
 			unsigned int rate = !tp->avg_misecs ? 0 :
 					tp->avg_bytes / tp->avg_misecs;
-			throughput_string(tp, tp->curr_total, rate);
+			throughput_string(tp, tp->curr_total, rate, progress);
 		}
 		progress_update = 1;
 		sprintf(bufp, ", %s.\n", msg);
-- 
1.7.7.2.7.g9f96f
Previous: Jeff KingNext: Jeff King
Message 15 of 23 in “resumable network bundles”
  1. 0/14 resumable network bundlesJeff King, Nov 10, 2011
  2. Jeff KingNov 10, 2011
  3. 01/14 t/lib-httpd: check for NO_CURLJeff King, Nov 10, 2011
  4. 02/14 http: turn off curl signalsJeff King, Nov 10, 2011
  5. Daniel StenbergNov 10, 2011
  6. Jeff KingNov 11, 2011
  7. 03/14 http: refactor http_request functionJeff King, Nov 10, 2011
  8. 04/14 http: add a public function for arbitrary-callback requestJeff King, Nov 10, 2011
  9. 05/14 remote-curl: use http callback for requesting refsJeff King, Nov 10, 2011
  10. 06/14 transport: factor out bundle to ref list conversionJeff King, Nov 10, 2011
  11. 07/14 bundle: add is_bundle_buf helperJeff King, Nov 10, 2011
  12. 08/14 remote-curl: free "discovery" objectJeff King, Nov 10, 2011
  13. 09/14 remote-curl: auto-detect bundles when fetching refsJeff King, Nov 10, 2011
  14. 10/14 remote-curl: try base $URL after $URL/info/refsJeff King, Nov 10, 2011
  15. 11/14 progress: allow pure-throughput progress metersJeff King, Nov 10, 2011
  16. 12/14 remote-curl: show progress for bundle downloadsJeff King, Nov 10, 2011
  17. 13/14 remote-curl: resume interrupted bundle transfersJeff King, Nov 10, 2011
  18. 14/14 clone: give advice on how to resume a failed cloneJeff King, Nov 10, 2011
  19. Junio C HamanoNov 10, 2011
  20. Jeff KingNov 11, 2011
  21. David Michael BarrNov 11, 2011
  22. Tay Ray ChuanNov 12, 2011
  23. Jeff KingNov 12, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.