Skip to content

Commit 9d81ecb

Browse files
jeffhostetlergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 041f5ea commit 9d81ecb

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

progress.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ struct progress {
3434
uint64_t total;
3535
unsigned last_percent;
3636
unsigned delay;
37+
unsigned sparse;
3738
struct throughput *throughput;
3839
uint64_t start_ns;
3940
};
@@ -194,7 +195,7 @@ int display_progress(struct progress *progress, uint64_t n)
194195
}
195196

196197
static struct progress *start_progress_delay(const char *title, uint64_t total,
197-
unsigned delay)
198+
unsigned delay, unsigned sparse)
198199
{
199200
struct progress *progress = malloc(sizeof(*progress));
200201
if (!progress) {
@@ -208,6 +209,7 @@ static struct progress *start_progress_delay(const char *title, uint64_t total,
208209
progress->last_value = -1;
209210
progress->last_percent = -1;
210211
progress->delay = delay;
212+
progress->sparse = sparse;
211213
progress->throughput = NULL;
212214
progress->start_ns = getnanotime();
213215
set_progress_signal();
@@ -216,16 +218,46 @@ static struct progress *start_progress_delay(const char *title, uint64_t total,
216218

217219
struct progress *start_delayed_progress(const char *title, uint64_t total)
218220
{
219-
return start_progress_delay(title, total, 2);
221+
return start_progress_delay(title, total, 2, 0);
220222
}
221223

222224
struct progress *start_progress(const char *title, uint64_t total)
223225
{
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);
225255
}
226256

227257
void stop_progress(struct progress **p_progress)
228258
{
259+
finish_if_sparse(*p_progress);
260+
229261
stop_progress_msg(p_progress, _("done"));
230262
}
231263

progress.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ struct progress;
66
void display_throughput(struct progress *progress, uint64_t total);
77
int display_progress(struct progress *progress, uint64_t n);
88
struct progress *start_progress(const char *title, uint64_t total);
9+
struct progress *start_sparse_progress(const char *title, uint64_t total);
910
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);
1013
void stop_progress(struct progress **progress);
1114
void stop_progress_msg(struct progress **progress, const char *msg);
1215

0 commit comments

Comments
 (0)