blob: 44c784d75f106c066c85b7a7a6569744332a6be2 [file] [log] [blame]
Nicolas Pitre74b67922007-10-30 15:41:13 -04001/*
2 * Simple text-based progress display module for GIT
3 *
Nicolas Pitre03aa8ff2009-09-14 02:41:16 -04004 * Copyright (c) 2007 by Nicolas Pitre <nico@fluxnic.net>
Nicolas Pitre74b67922007-10-30 15:41:13 -04005 *
6 * This code is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
Đoàn Trần Công Danh3cacb9a2020-04-27 21:22:37 +070011#define GIT_TEST_PROGRESS_ONLY
SZEDER Gábor545dc342019-04-12 21:45:15 +020012#include "cache.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -040013#include "progress.h"
Antoine Pelisse079b5462013-04-10 21:03:23 +020014#include "strbuf.h"
Karsten Blees83d26fa2014-07-12 02:08:11 +020015#include "trace.h"
SZEDER Gábor545dc342019-04-12 21:45:15 +020016#include "utf8.h"
Derrick Stolee44a46932019-11-25 21:28:22 +000017#include "config.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -040018
Nicolas Pitrecf84d512007-10-30 14:57:34 -040019#define TP_IDX_MAX 8
20
21struct throughput {
Nicolas Pitre53ed7b52007-11-06 16:30:28 -050022 off_t curr_total;
Nicolas Pitre218558a2007-11-04 22:15:41 -050023 off_t prev_total;
Karsten Blees83d26fa2014-07-12 02:08:11 +020024 uint64_t prev_ns;
Nicolas Pitre218558a2007-11-04 22:15:41 -050025 unsigned int avg_bytes;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040026 unsigned int avg_misecs;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -050027 unsigned int last_bytes[TP_IDX_MAX];
Nicolas Pitrecf84d512007-10-30 14:57:34 -040028 unsigned int last_misecs[TP_IDX_MAX];
29 unsigned int idx;
Jeff King31319772015-09-24 17:05:57 -040030 struct strbuf display;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040031};
32
Nicolas Pitredc6a0752007-10-30 14:57:32 -040033struct progress {
34 const char *title;
Elijah Newrend6861d02017-11-13 12:15:58 -080035 uint64_t last_value;
36 uint64_t total;
Nicolas Pitredc6a0752007-10-30 14:57:32 -040037 unsigned last_percent;
38 unsigned delay;
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -070039 unsigned sparse;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040040 struct throughput *throughput;
René Scharfe0fae1e02017-07-08 18:43:42 +020041 uint64_t start_ns;
SZEDER Gábord53ba842019-04-05 02:45:37 +020042 struct strbuf counters_sb;
SZEDER Gábor545dc342019-04-12 21:45:15 +020043 int title_len;
44 int split;
Nicolas Pitredc6a0752007-10-30 14:57:32 -040045};
46
Nicolas Pitre96a02f82007-04-18 14:27:45 -040047static volatile sig_atomic_t progress_update;
48
SZEDER Gábor2bb74b52019-09-16 22:54:12 +020049/*
50 * These are only intended for testing the progress output, i.e. exclusively
51 * for 'test-tool progress'.
52 */
53int progress_testing;
54uint64_t progress_test_ns = 0;
SZEDER Gábor2bb74b52019-09-16 22:54:12 +020055void progress_test_force_update(void)
56{
57 progress_update = 1;
58}
59
60
Jeff King9ec03b52023-02-24 01:39:20 -050061static void progress_interval(int signum UNUSED)
Nicolas Pitre96a02f82007-04-18 14:27:45 -040062{
63 progress_update = 1;
64}
65
66static void set_progress_signal(void)
67{
68 struct sigaction sa;
69 struct itimerval v;
70
SZEDER Gábor2bb74b52019-09-16 22:54:12 +020071 if (progress_testing)
72 return;
73
Nicolas Pitre180a9f22007-04-20 15:05:27 -040074 progress_update = 0;
75
Nicolas Pitre96a02f82007-04-18 14:27:45 -040076 memset(&sa, 0, sizeof(sa));
77 sa.sa_handler = progress_interval;
78 sigemptyset(&sa.sa_mask);
79 sa.sa_flags = SA_RESTART;
80 sigaction(SIGALRM, &sa, NULL);
81
82 v.it_interval.tv_sec = 1;
83 v.it_interval.tv_usec = 0;
84 v.it_value = v.it_interval;
85 setitimer(ITIMER_REAL, &v, NULL);
86}
87
88static void clear_progress_signal(void)
89{
90 struct itimerval v = {{0,},};
SZEDER Gábor2bb74b52019-09-16 22:54:12 +020091
92 if (progress_testing)
93 return;
94
Nicolas Pitre96a02f82007-04-18 14:27:45 -040095 setitimer(ITIMER_REAL, &v, NULL);
96 signal(SIGALRM, SIG_IGN);
97 progress_update = 0;
98}
99
Luke Mewburn85cb8902015-04-13 23:30:51 +1000100static int is_foreground_fd(int fd)
101{
Jeff Kinga4fb76c2015-05-19 01:24:57 -0400102 int tpgrp = tcgetpgrp(fd);
103 return tpgrp < 0 || tpgrp == getpgid(0);
Luke Mewburn85cb8902015-04-13 23:30:51 +1000104}
105
SZEDER Gábor9219d122019-04-05 02:45:36 +0200106static void display(struct progress *progress, uint64_t n, const char *done)
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400107{
SZEDER Gábord53ba842019-04-05 02:45:37 +0200108 const char *tp;
109 struct strbuf *counters_sb = &progress->counters_sb;
110 int show_update = 0;
SZEDER Gáborbbf47562019-09-16 22:54:11 +0200111 int last_count_len = counters_sb->len;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400112
Lars Schneider9c5951c2017-12-04 17:07:00 -0500113 if (progress->delay && (!progress_update || --progress->delay))
SZEDER Gábor9219d122019-04-05 02:45:36 +0200114 return;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400115
116 progress->last_value = n;
Jeff King31319772015-09-24 17:05:57 -0400117 tp = (progress->throughput) ? progress->throughput->display.buf : "";
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400118 if (progress->total) {
119 unsigned percent = n * 100 / progress->total;
120 if (percent != progress->last_percent || progress_update) {
121 progress->last_percent = percent;
SZEDER Gábord53ba842019-04-05 02:45:37 +0200122
123 strbuf_reset(counters_sb);
124 strbuf_addf(counters_sb,
125 "%3u%% (%"PRIuMAX"/%"PRIuMAX")%s", percent,
126 (uintmax_t)n, (uintmax_t)progress->total,
127 tp);
128 show_update = 1;
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400129 }
130 } else if (progress_update) {
SZEDER Gábord53ba842019-04-05 02:45:37 +0200131 strbuf_reset(counters_sb);
132 strbuf_addf(counters_sb, "%"PRIuMAX"%s", (uintmax_t)n, tp);
133 show_update = 1;
134 }
135
136 if (show_update) {
Luke Mewburn85cb8902015-04-13 23:30:51 +1000137 if (is_foreground_fd(fileno(stderr)) || done) {
SZEDER Gábor9f1fd842019-04-12 21:45:14 +0200138 const char *eol = done ? done : "\r";
SZEDER Gáborbbf47562019-09-16 22:54:11 +0200139 size_t clear_len = counters_sb->len < last_count_len ?
140 last_count_len - counters_sb->len + 1 :
141 0;
142 /* The "+ 2" accounts for the ": ". */
143 size_t progress_line_len = progress->title_len +
144 counters_sb->len + 2;
145 int cols = term_columns();
SZEDER Gábor545dc342019-04-12 21:45:15 +0200146
147 if (progress->split) {
SZEDER Gáborbbf47562019-09-16 22:54:11 +0200148 fprintf(stderr, " %s%*s", counters_sb->buf,
149 (int) clear_len, eol);
150 } else if (!done && cols < progress_line_len) {
151 clear_len = progress->title_len + 1 < cols ?
152 cols - progress->title_len - 1 : 0;
153 fprintf(stderr, "%s:%*s\n %s%s",
154 progress->title, (int) clear_len, "",
155 counters_sb->buf, eol);
SZEDER Gábor545dc342019-04-12 21:45:15 +0200156 progress->split = 1;
157 } else {
SZEDER Gáborbbf47562019-09-16 22:54:11 +0200158 fprintf(stderr, "%s: %s%*s", progress->title,
159 counters_sb->buf, (int) clear_len, eol);
SZEDER Gábor545dc342019-04-12 21:45:15 +0200160 }
Luke Mewburn85cb8902015-04-13 23:30:51 +1000161 fflush(stderr);
162 }
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400163 progress_update = 0;
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400164 }
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400165}
166
Elijah Newrend6861d02017-11-13 12:15:58 -0800167static void throughput_string(struct strbuf *buf, uint64_t total,
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500168 unsigned int rate)
169{
Jeff King31319772015-09-24 17:05:57 -0400170 strbuf_reset(buf);
Antoine Pelisse079b5462013-04-10 21:03:23 +0200171 strbuf_addstr(buf, ", ");
172 strbuf_humanise_bytes(buf, total);
173 strbuf_addstr(buf, " | ");
Dimitriy Ryazantcev8f354a12019-07-02 21:22:48 +0300174 strbuf_humanise_rate(buf, rate * 1024);
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500175}
176
SZEDER Gábor2bb74b52019-09-16 22:54:12 +0200177static uint64_t progress_getnanotime(struct progress *progress)
178{
179 if (progress_testing)
180 return progress->start_ns + progress_test_ns;
181 else
182 return getnanotime();
183}
184
Elijah Newrend6861d02017-11-13 12:15:58 -0800185void display_throughput(struct progress *progress, uint64_t total)
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400186{
187 struct throughput *tp;
Karsten Blees83d26fa2014-07-12 02:08:11 +0200188 uint64_t now_ns;
189 unsigned int misecs, count, rate;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400190
191 if (!progress)
192 return;
193 tp = progress->throughput;
194
SZEDER Gábor2bb74b52019-09-16 22:54:12 +0200195 now_ns = progress_getnanotime(progress);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400196
197 if (!tp) {
René Scharfeca56dad2021-03-13 17:17:22 +0100198 progress->throughput = CALLOC_ARRAY(tp, 1);
Jeff King999b9512019-04-11 09:49:57 -0400199 tp->prev_total = tp->curr_total = total;
200 tp->prev_ns = now_ns;
201 strbuf_init(&tp->display, 0);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400202 return;
203 }
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500204 tp->curr_total = total;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400205
Karsten Blees83d26fa2014-07-12 02:08:11 +0200206 /* only update throughput every 0.5 s */
207 if (now_ns - tp->prev_ns <= 500000000)
208 return;
209
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400210 /*
Karsten Blees83d26fa2014-07-12 02:08:11 +0200211 * We have x = bytes and y = nanosecs. We want z = KiB/s:
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400212 *
Karsten Blees83d26fa2014-07-12 02:08:11 +0200213 * z = (x / 1024) / (y / 1000000000)
214 * z = x / y * 1000000000 / 1024
215 * z = x / (y * 1024 / 1000000000)
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400216 * z = x / y'
217 *
218 * To simplify things we'll keep track of misecs, or 1024th of a sec
219 * obtained with:
220 *
Karsten Blees83d26fa2014-07-12 02:08:11 +0200221 * y' = y * 1024 / 1000000000
222 * y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
223 * y' = y / 2^32 * 4398
224 * y' = (y * 4398) >> 32
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400225 */
Karsten Blees83d26fa2014-07-12 02:08:11 +0200226 misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400227
Karsten Blees83d26fa2014-07-12 02:08:11 +0200228 count = total - tp->prev_total;
229 tp->prev_total = total;
230 tp->prev_ns = now_ns;
231 tp->avg_bytes += count;
232 tp->avg_misecs += misecs;
233 rate = tp->avg_bytes / tp->avg_misecs;
234 tp->avg_bytes -= tp->last_bytes[tp->idx];
235 tp->avg_misecs -= tp->last_misecs[tp->idx];
236 tp->last_bytes[tp->idx] = count;
237 tp->last_misecs[tp->idx] = misecs;
238 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500239
Jeff King31319772015-09-24 17:05:57 -0400240 throughput_string(&tp->display, total, rate);
Karsten Blees83d26fa2014-07-12 02:08:11 +0200241 if (progress->last_value != -1 && progress_update)
242 display(progress, progress->last_value, NULL);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400243}
244
SZEDER Gábor9219d122019-04-05 02:45:36 +0200245void display_progress(struct progress *progress, uint64_t n)
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400246{
SZEDER Gábor9219d122019-04-05 02:45:36 +0200247 if (progress)
248 display(progress, n, NULL);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400249}
250
Elijah Newrend6861d02017-11-13 12:15:58 -0800251static struct progress *start_progress_delay(const char *title, uint64_t total,
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -0700252 unsigned delay, unsigned sparse)
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400253{
Jeff King999b9512019-04-11 09:49:57 -0400254 struct progress *progress = xmalloc(sizeof(*progress));
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400255 progress->title = title;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400256 progress->total = total;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400257 progress->last_value = -1;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400258 progress->last_percent = -1;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400259 progress->delay = delay;
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -0700260 progress->sparse = sparse;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400261 progress->throughput = NULL;
René Scharfe0fae1e02017-07-08 18:43:42 +0200262 progress->start_ns = getnanotime();
SZEDER Gábord53ba842019-04-05 02:45:37 +0200263 strbuf_init(&progress->counters_sb, 0);
SZEDER Gábor545dc342019-04-12 21:45:15 +0200264 progress->title_len = utf8_strwidth(title);
265 progress->split = 0;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400266 set_progress_signal();
Emily Shaffer98a13642020-05-12 14:44:20 -0700267 trace2_region_enter("progress", title, the_repository);
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400268 return progress;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400269}
270
Derrick Stolee44a46932019-11-25 21:28:22 +0000271static int get_default_delay(void)
272{
273 static int delay_in_secs = -1;
274
275 if (delay_in_secs < 0)
276 delay_in_secs = git_env_ulong("GIT_PROGRESS_DELAY", 2);
277
278 return delay_in_secs;
279}
280
Elijah Newrend6861d02017-11-13 12:15:58 -0800281struct progress *start_delayed_progress(const char *title, uint64_t total)
Junio C Hamano8aade102017-08-19 10:39:41 -0700282{
Derrick Stolee44a46932019-11-25 21:28:22 +0000283 return start_progress_delay(title, total, get_default_delay(), 0);
Junio C Hamano8aade102017-08-19 10:39:41 -0700284}
285
Elijah Newrend6861d02017-11-13 12:15:58 -0800286struct progress *start_progress(const char *title, uint64_t total)
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400287{
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -0700288 return start_progress_delay(title, total, 0, 0);
289}
290
291/*
292 * Here "sparse" means that the caller might use some sampling criteria to
293 * decide when to call display_progress() rather than calling it for every
294 * integer value in[0 .. total). In particular, the caller might not call
295 * display_progress() for the last value in the range.
296 *
297 * When "sparse" is set, stop_progress() will automatically force the done
298 * message to show 100%.
299 */
300struct progress *start_sparse_progress(const char *title, uint64_t total)
301{
302 return start_progress_delay(title, total, 0, 1);
303}
304
305struct progress *start_delayed_sparse_progress(const char *title,
306 uint64_t total)
307{
Derrick Stolee44a46932019-11-25 21:28:22 +0000308 return start_progress_delay(title, total, get_default_delay(), 1);
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -0700309}
310
311static void finish_if_sparse(struct progress *progress)
312{
Ævar Arnfjörð Bjarmason74900a62022-02-03 22:40:18 +0100313 if (progress->sparse &&
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -0700314 progress->last_value != progress->total)
315 display_progress(progress, progress->total);
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400316}
317
Ævar Arnfjörð Bjarmasonaccf1eb2022-02-03 22:40:17 +0100318static void force_last_update(struct progress *progress, const char *msg)
319{
320 char *buf;
321 struct throughput *tp = progress->throughput;
322
323 if (tp) {
324 uint64_t now_ns = progress_getnanotime(progress);
325 unsigned int misecs, rate;
326 misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
327 rate = tp->curr_total / (misecs ? misecs : 1);
328 throughput_string(&tp->display, tp->curr_total, rate);
329 }
330 progress_update = 1;
331 buf = xstrfmt(", %s.\n", msg);
332 display(progress, progress->last_value, buf);
333 free(buf);
334}
335
336static void log_trace2(struct progress *progress)
337{
338 trace2_data_intmax("progress", the_repository, "total_objects",
339 progress->total);
340
341 if (progress->throughput)
342 trace2_data_intmax("progress", the_repository, "total_bytes",
343 progress->throughput->curr_total);
344
345 trace2_region_leave("progress", progress->title, the_repository);
346}
347
Nicolas Pitrea984a062007-11-08 15:45:41 -0500348void stop_progress_msg(struct progress **p_progress, const char *msg)
349{
Martin Ågrenac900fd2020-08-10 21:47:48 +0200350 struct progress *progress;
351
352 if (!p_progress)
353 BUG("don't provide NULL to stop_progress_msg");
354
355 progress = *p_progress;
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400356 if (!progress)
357 return;
358 *p_progress = NULL;
Boyd Lynn Gerberd4c44442008-06-08 09:26:15 -0600359
Ævar Arnfjörð Bjarmason74900a62022-02-03 22:40:18 +0100360 finish_if_sparse(progress);
Ævar Arnfjörð Bjarmasonaccf1eb2022-02-03 22:40:17 +0100361 if (progress->last_value != -1)
362 force_last_update(progress, msg);
Ævar Arnfjörð Bjarmason74900a62022-02-03 22:40:18 +0100363 log_trace2(progress);
Ævar Arnfjörð Bjarmasonaccf1eb2022-02-03 22:40:17 +0100364
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400365 clear_progress_signal();
SZEDER Gábord53ba842019-04-05 02:45:37 +0200366 strbuf_release(&progress->counters_sb);
Jeff King31319772015-09-24 17:05:57 -0400367 if (progress->throughput)
368 strbuf_release(&progress->throughput->display);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400369 free(progress->throughput);
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400370 free(progress);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400371}