progress: add sparse mode to force 100% complete message
Add new start_sparse_progress() and start_delayed_sparse_progress() constructors and "sparse" flag to struct progress. Teach stop_progress() to force a 100% complete progress message before printing the final "done" message when "sparse" is set. Calling display_progress() for every item in a large set can be expensive. If callers try to filter this for performance reasons, such as emitting every k-th item, progress would not reach 100% unless they made a final call to display_progress() with the item count before calling stop_progress(). Now this is automatic when "sparse" is set. Signed-off-by: Jeff Hostetler <jeffhost@microsoft.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff Hostetler committed
Mar 21, 2019 at 12:36 UTC
9d81ecb52b5e6611f66c968884dde42928350b18
2 files changed
+38
-3
progress.c
+35
-3
@@ -34,6 +34,7 @@ struct progress {
34
uint64_t total;
35
unsigned last_percent;
36
unsigned delay;
37
+ unsigned sparse;
38
struct throughput *throughput;
39
uint64_t start_ns;
40
};
@@ -194,7 +195,7 @@ int display_progress(struct progress *progress, uint64_t n)
195
}
196
197
static struct progress *start_progress_delay(const char *title, uint64_t total,
197
- unsigned delay)
198
+ unsigned delay, unsigned sparse)
199
{
200
struct progress *progress = malloc(sizeof(*progress));
201
if (!progress) {
@@ -208,6 +209,7 @@ static struct progress *start_progress_delay(const char *title, uint64_t total,
209
progress->last_value = -1;
210
progress->last_percent = -1;
211
progress->delay = delay;
212
+ progress->sparse = sparse;
213
progress->throughput = NULL;
214
progress->start_ns = getnanotime();
215
set_progress_signal();
@@ -216,16 +218,46 @@ static struct progress *start_progress_delay(const char *title, uint64_t total,
218
219
struct progress *start_delayed_progress(const char *title, uint64_t total)
220
{
219
- return start_progress_delay(title, total, 2);
221
+ return start_progress_delay(title, total, 2, 0);
222
}
223
224
struct progress *start_progress(const char *title, uint64_t total)
225
{
224
- return start_progress_delay(title, total, 0);
226
+ return start_progress_delay(title, total, 0, 0);
227
+}
228
+
229
+/*
230
+ * Here "sparse" means that the caller might use some sampling criteria to
231
+ * decide when to call display_progress() rather than calling it for every
232
+ * integer value in[0 .. total). In particular, the caller might not call
233
+ * display_progress() for the last value in the range.
234
+ *
235
+ * When "sparse" is set, stop_progress() will automatically force the done
236
+ * message to show 100%.
237
+ */
238
+struct progress *start_sparse_progress(const char *title, uint64_t total)
239
+{
240
+ return start_progress_delay(title, total, 0, 1);
241
+}
242
+
243
+struct progress *start_delayed_sparse_progress(const char *title,
244
+ uint64_t total)
245
+{
246
+ return start_progress_delay(title, total, 2, 1);
247
+}
248
+
249
+static void finish_if_sparse(struct progress *progress)
250
+{
251
+ if (progress &&
252
+ progress->sparse &&
253
+ progress->last_value != progress->total)
254
+ display_progress(progress, progress->total);
255
}
256
257
void stop_progress(struct progress **p_progress)
258
{
259
+ finish_if_sparse(*p_progress);
260
+
261
stop_progress_msg(p_progress, _("done"));
262
}
263
progress.h
+3
@@ -6,7 +6,10 @@ struct progress;
6
void display_throughput(struct progress *progress, uint64_t total);
7
int display_progress(struct progress *progress, uint64_t n);
8
struct progress *start_progress(const char *title, uint64_t total);
9
+struct progress *start_sparse_progress(const char *title, uint64_t total);
10
struct progress *start_delayed_progress(const char *title, uint64_t total);
11
+struct progress *start_delayed_sparse_progress(const char *title,
12
+ uint64_t total);
13
void stop_progress(struct progress **progress);
14
void stop_progress_msg(struct progress **progress, const char *msg);
15