blob: c83cb60bf17eb490ccc3abc97a1b6f6679196e07 [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
Elijah Newren77f091e2023-04-11 00:42:00 -070012#include "git-compat-util.h"
Elijah Newrenca4eed72023-04-11 00:41:59 -070013#include "pager.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -040014#include "progress.h"
Elijah Newrend1cbe1e2023-04-22 20:17:20 +000015#include "repository.h"
Antoine Pelisse079b5462013-04-10 21:03:23 +020016#include "strbuf.h"
Karsten Blees83d26fa2014-07-12 02:08:11 +020017#include "trace.h"
Elijah Newren74ea5c92023-04-11 03:00:38 +000018#include "trace2.h"
SZEDER Gábor545dc342019-04-12 21:45:15 +020019#include "utf8.h"
Calvin Wanb1bda752023-09-29 14:20:51 -070020#include "parse.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -040021
Nicolas Pitrecf84d512007-10-30 14:57:34 -040022#define TP_IDX_MAX 8
23
24struct throughput {
Nicolas Pitre53ed7b52007-11-06 16:30:28 -050025 off_t curr_total;
Nicolas Pitre218558a2007-11-04 22:15:41 -050026 off_t prev_total;
Karsten Blees83d26fa2014-07-12 02:08:11 +020027 uint64_t prev_ns;
Nicolas Pitre218558a2007-11-04 22:15:41 -050028 unsigned int avg_bytes;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040029 unsigned int avg_misecs;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -050030 unsigned int last_bytes[TP_IDX_MAX];
Nicolas Pitrecf84d512007-10-30 14:57:34 -040031 unsigned int last_misecs[TP_IDX_MAX];
32 unsigned int idx;
Jeff King31319772015-09-24 17:05:57 -040033 struct strbuf display;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040034};
35
Nicolas Pitredc6a0752007-10-30 14:57:32 -040036struct progress {
37 const char *title;
Elijah Newrend6861d02017-11-13 12:15:58 -080038 uint64_t last_value;
39 uint64_t total;
Nicolas Pitredc6a0752007-10-30 14:57:32 -040040 unsigned last_percent;
41 unsigned delay;
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -070042 unsigned sparse;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040043 struct throughput *throughput;
René Scharfe0fae1e02017-07-08 18:43:42 +020044 uint64_t start_ns;
SZEDER Gábord53ba842019-04-05 02:45:37 +020045 struct strbuf counters_sb;
SZEDER Gábor545dc342019-04-12 21:45:15 +020046 int title_len;
47 int split;
Nicolas Pitredc6a0752007-10-30 14:57:32 -040048};
49
Nicolas Pitre96a02f82007-04-18 14:27:45 -040050static volatile sig_atomic_t progress_update;
51
SZEDER Gábor2bb74b52019-09-16 22:54:12 +020052/*
53 * These are only intended for testing the progress output, i.e. exclusively
54 * for 'test-tool progress'.
55 */
56int progress_testing;
57uint64_t progress_test_ns = 0;
SZEDER Gábor2bb74b52019-09-16 22:54:12 +020058void progress_test_force_update(void)
59{
60 progress_update = 1;
61}
62
63
Jeff King9ec03b52023-02-24 01:39:20 -050064static void progress_interval(int signum UNUSED)
Nicolas Pitre96a02f82007-04-18 14:27:45 -040065{
66 progress_update = 1;
67}
68
69static void set_progress_signal(void)
70{
71 struct sigaction sa;
72 struct itimerval v;
73
SZEDER Gábor2bb74b52019-09-16 22:54:12 +020074 if (progress_testing)
75 return;
76
Nicolas Pitre180a9f22007-04-20 15:05:27 -040077 progress_update = 0;
78
Nicolas Pitre96a02f82007-04-18 14:27:45 -040079 memset(&sa, 0, sizeof(sa));
80 sa.sa_handler = progress_interval;
81 sigemptyset(&sa.sa_mask);
82 sa.sa_flags = SA_RESTART;
83 sigaction(SIGALRM, &sa, NULL);
84
85 v.it_interval.tv_sec = 1;
86 v.it_interval.tv_usec = 0;
87 v.it_value = v.it_interval;
88 setitimer(ITIMER_REAL, &v, NULL);
89}
90
91static void clear_progress_signal(void)
92{
93 struct itimerval v = {{0,},};
SZEDER Gábor2bb74b52019-09-16 22:54:12 +020094
95 if (progress_testing)
96 return;
97
Nicolas Pitre96a02f82007-04-18 14:27:45 -040098 setitimer(ITIMER_REAL, &v, NULL);
99 signal(SIGALRM, SIG_IGN);
100 progress_update = 0;
101}
102
Luke Mewburn85cb8902015-04-13 23:30:51 +1000103static int is_foreground_fd(int fd)
104{
Jeff Kinga4fb76c2015-05-19 01:24:57 -0400105 int tpgrp = tcgetpgrp(fd);
106 return tpgrp < 0 || tpgrp == getpgid(0);
Luke Mewburn85cb8902015-04-13 23:30:51 +1000107}
108
SZEDER Gábor9219d122019-04-05 02:45:36 +0200109static void display(struct progress *progress, uint64_t n, const char *done)
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400110{
SZEDER Gábord53ba842019-04-05 02:45:37 +0200111 const char *tp;
112 struct strbuf *counters_sb = &progress->counters_sb;
113 int show_update = 0;
SZEDER Gáborbbf47562019-09-16 22:54:11 +0200114 int last_count_len = counters_sb->len;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400115
Lars Schneider9c5951c2017-12-04 17:07:00 -0500116 if (progress->delay && (!progress_update || --progress->delay))
SZEDER Gábor9219d122019-04-05 02:45:36 +0200117 return;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400118
119 progress->last_value = n;
Jeff King31319772015-09-24 17:05:57 -0400120 tp = (progress->throughput) ? progress->throughput->display.buf : "";
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400121 if (progress->total) {
122 unsigned percent = n * 100 / progress->total;
123 if (percent != progress->last_percent || progress_update) {
124 progress->last_percent = percent;
SZEDER Gábord53ba842019-04-05 02:45:37 +0200125
126 strbuf_reset(counters_sb);
127 strbuf_addf(counters_sb,
128 "%3u%% (%"PRIuMAX"/%"PRIuMAX")%s", percent,
129 (uintmax_t)n, (uintmax_t)progress->total,
130 tp);
131 show_update = 1;
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400132 }
133 } else if (progress_update) {
SZEDER Gábord53ba842019-04-05 02:45:37 +0200134 strbuf_reset(counters_sb);
135 strbuf_addf(counters_sb, "%"PRIuMAX"%s", (uintmax_t)n, tp);
136 show_update = 1;
137 }
138
139 if (show_update) {
Luke Mewburn85cb8902015-04-13 23:30:51 +1000140 if (is_foreground_fd(fileno(stderr)) || done) {
SZEDER Gábor9f1fd842019-04-12 21:45:14 +0200141 const char *eol = done ? done : "\r";
SZEDER Gáborbbf47562019-09-16 22:54:11 +0200142 size_t clear_len = counters_sb->len < last_count_len ?
143 last_count_len - counters_sb->len + 1 :
144 0;
145 /* The "+ 2" accounts for the ": ". */
146 size_t progress_line_len = progress->title_len +
147 counters_sb->len + 2;
148 int cols = term_columns();
SZEDER Gábor545dc342019-04-12 21:45:15 +0200149
150 if (progress->split) {
SZEDER Gáborbbf47562019-09-16 22:54:11 +0200151 fprintf(stderr, " %s%*s", counters_sb->buf,
152 (int) clear_len, eol);
153 } else if (!done && cols < progress_line_len) {
154 clear_len = progress->title_len + 1 < cols ?
155 cols - progress->title_len - 1 : 0;
156 fprintf(stderr, "%s:%*s\n %s%s",
157 progress->title, (int) clear_len, "",
158 counters_sb->buf, eol);
SZEDER Gábor545dc342019-04-12 21:45:15 +0200159 progress->split = 1;
160 } else {
SZEDER Gáborbbf47562019-09-16 22:54:11 +0200161 fprintf(stderr, "%s: %s%*s", progress->title,
162 counters_sb->buf, (int) clear_len, eol);
SZEDER Gábor545dc342019-04-12 21:45:15 +0200163 }
Luke Mewburn85cb8902015-04-13 23:30:51 +1000164 fflush(stderr);
165 }
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400166 progress_update = 0;
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400167 }
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400168}
169
Elijah Newrend6861d02017-11-13 12:15:58 -0800170static void throughput_string(struct strbuf *buf, uint64_t total,
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500171 unsigned int rate)
172{
Jeff King31319772015-09-24 17:05:57 -0400173 strbuf_reset(buf);
Antoine Pelisse079b5462013-04-10 21:03:23 +0200174 strbuf_addstr(buf, ", ");
175 strbuf_humanise_bytes(buf, total);
176 strbuf_addstr(buf, " | ");
Dimitriy Ryazantcev8f354a12019-07-02 21:22:48 +0300177 strbuf_humanise_rate(buf, rate * 1024);
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500178}
179
SZEDER Gábor2bb74b52019-09-16 22:54:12 +0200180static uint64_t progress_getnanotime(struct progress *progress)
181{
182 if (progress_testing)
183 return progress->start_ns + progress_test_ns;
184 else
185 return getnanotime();
186}
187
Elijah Newrend6861d02017-11-13 12:15:58 -0800188void display_throughput(struct progress *progress, uint64_t total)
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400189{
190 struct throughput *tp;
Karsten Blees83d26fa2014-07-12 02:08:11 +0200191 uint64_t now_ns;
192 unsigned int misecs, count, rate;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400193
194 if (!progress)
195 return;
196 tp = progress->throughput;
197
SZEDER Gábor2bb74b52019-09-16 22:54:12 +0200198 now_ns = progress_getnanotime(progress);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400199
200 if (!tp) {
René Scharfeca56dad2021-03-13 17:17:22 +0100201 progress->throughput = CALLOC_ARRAY(tp, 1);
Jeff King999b9512019-04-11 09:49:57 -0400202 tp->prev_total = tp->curr_total = total;
203 tp->prev_ns = now_ns;
204 strbuf_init(&tp->display, 0);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400205 return;
206 }
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500207 tp->curr_total = total;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400208
Karsten Blees83d26fa2014-07-12 02:08:11 +0200209 /* only update throughput every 0.5 s */
210 if (now_ns - tp->prev_ns <= 500000000)
211 return;
212
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400213 /*
Karsten Blees83d26fa2014-07-12 02:08:11 +0200214 * We have x = bytes and y = nanosecs. We want z = KiB/s:
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400215 *
Karsten Blees83d26fa2014-07-12 02:08:11 +0200216 * z = (x / 1024) / (y / 1000000000)
217 * z = x / y * 1000000000 / 1024
218 * z = x / (y * 1024 / 1000000000)
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400219 * z = x / y'
220 *
221 * To simplify things we'll keep track of misecs, or 1024th of a sec
222 * obtained with:
223 *
Karsten Blees83d26fa2014-07-12 02:08:11 +0200224 * y' = y * 1024 / 1000000000
225 * y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
226 * y' = y / 2^32 * 4398
227 * y' = (y * 4398) >> 32
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400228 */
Karsten Blees83d26fa2014-07-12 02:08:11 +0200229 misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400230
Karsten Blees83d26fa2014-07-12 02:08:11 +0200231 count = total - tp->prev_total;
232 tp->prev_total = total;
233 tp->prev_ns = now_ns;
234 tp->avg_bytes += count;
235 tp->avg_misecs += misecs;
236 rate = tp->avg_bytes / tp->avg_misecs;
237 tp->avg_bytes -= tp->last_bytes[tp->idx];
238 tp->avg_misecs -= tp->last_misecs[tp->idx];
239 tp->last_bytes[tp->idx] = count;
240 tp->last_misecs[tp->idx] = misecs;
241 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500242
Jeff King31319772015-09-24 17:05:57 -0400243 throughput_string(&tp->display, total, rate);
Karsten Blees83d26fa2014-07-12 02:08:11 +0200244 if (progress->last_value != -1 && progress_update)
245 display(progress, progress->last_value, NULL);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400246}
247
SZEDER Gábor9219d122019-04-05 02:45:36 +0200248void display_progress(struct progress *progress, uint64_t n)
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400249{
SZEDER Gábor9219d122019-04-05 02:45:36 +0200250 if (progress)
251 display(progress, n, NULL);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400252}
253
Elijah Newrend6861d02017-11-13 12:15:58 -0800254static struct progress *start_progress_delay(const char *title, uint64_t total,
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -0700255 unsigned delay, unsigned sparse)
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400256{
Jeff King999b9512019-04-11 09:49:57 -0400257 struct progress *progress = xmalloc(sizeof(*progress));
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400258 progress->title = title;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400259 progress->total = total;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400260 progress->last_value = -1;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400261 progress->last_percent = -1;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400262 progress->delay = delay;
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -0700263 progress->sparse = sparse;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400264 progress->throughput = NULL;
René Scharfe0fae1e02017-07-08 18:43:42 +0200265 progress->start_ns = getnanotime();
SZEDER Gábord53ba842019-04-05 02:45:37 +0200266 strbuf_init(&progress->counters_sb, 0);
SZEDER Gábor545dc342019-04-12 21:45:15 +0200267 progress->title_len = utf8_strwidth(title);
268 progress->split = 0;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400269 set_progress_signal();
Emily Shaffer98a13642020-05-12 14:44:20 -0700270 trace2_region_enter("progress", title, the_repository);
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400271 return progress;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400272}
273
Derrick Stolee44a46932019-11-25 21:28:22 +0000274static int get_default_delay(void)
275{
276 static int delay_in_secs = -1;
277
278 if (delay_in_secs < 0)
279 delay_in_secs = git_env_ulong("GIT_PROGRESS_DELAY", 2);
280
281 return delay_in_secs;
282}
283
Elijah Newrend6861d02017-11-13 12:15:58 -0800284struct progress *start_delayed_progress(const char *title, uint64_t total)
Junio C Hamano8aade102017-08-19 10:39:41 -0700285{
Derrick Stolee44a46932019-11-25 21:28:22 +0000286 return start_progress_delay(title, total, get_default_delay(), 0);
Junio C Hamano8aade102017-08-19 10:39:41 -0700287}
288
Elijah Newrend6861d02017-11-13 12:15:58 -0800289struct progress *start_progress(const char *title, uint64_t total)
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400290{
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -0700291 return start_progress_delay(title, total, 0, 0);
292}
293
294/*
295 * Here "sparse" means that the caller might use some sampling criteria to
296 * decide when to call display_progress() rather than calling it for every
297 * integer value in[0 .. total). In particular, the caller might not call
298 * display_progress() for the last value in the range.
299 *
300 * When "sparse" is set, stop_progress() will automatically force the done
301 * message to show 100%.
302 */
303struct progress *start_sparse_progress(const char *title, uint64_t total)
304{
305 return start_progress_delay(title, total, 0, 1);
306}
307
308struct progress *start_delayed_sparse_progress(const char *title,
309 uint64_t total)
310{
Derrick Stolee44a46932019-11-25 21:28:22 +0000311 return start_progress_delay(title, total, get_default_delay(), 1);
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -0700312}
313
314static void finish_if_sparse(struct progress *progress)
315{
Ævar Arnfjörð Bjarmason74900a62022-02-03 22:40:18 +0100316 if (progress->sparse &&
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -0700317 progress->last_value != progress->total)
318 display_progress(progress, progress->total);
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400319}
320
Ævar Arnfjörð Bjarmasonaccf1eb2022-02-03 22:40:17 +0100321static void force_last_update(struct progress *progress, const char *msg)
322{
323 char *buf;
324 struct throughput *tp = progress->throughput;
325
326 if (tp) {
327 uint64_t now_ns = progress_getnanotime(progress);
328 unsigned int misecs, rate;
329 misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
330 rate = tp->curr_total / (misecs ? misecs : 1);
331 throughput_string(&tp->display, tp->curr_total, rate);
332 }
333 progress_update = 1;
334 buf = xstrfmt(", %s.\n", msg);
335 display(progress, progress->last_value, buf);
336 free(buf);
337}
338
339static void log_trace2(struct progress *progress)
340{
341 trace2_data_intmax("progress", the_repository, "total_objects",
342 progress->total);
343
344 if (progress->throughput)
345 trace2_data_intmax("progress", the_repository, "total_bytes",
346 progress->throughput->curr_total);
347
348 trace2_region_leave("progress", progress->title, the_repository);
349}
350
Nicolas Pitrea984a062007-11-08 15:45:41 -0500351void stop_progress_msg(struct progress **p_progress, const char *msg)
352{
Martin Ågrenac900fd2020-08-10 21:47:48 +0200353 struct progress *progress;
354
355 if (!p_progress)
356 BUG("don't provide NULL to stop_progress_msg");
357
358 progress = *p_progress;
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400359 if (!progress)
360 return;
361 *p_progress = NULL;
Boyd Lynn Gerberd4c44442008-06-08 09:26:15 -0600362
Ævar Arnfjörð Bjarmason74900a62022-02-03 22:40:18 +0100363 finish_if_sparse(progress);
Ævar Arnfjörð Bjarmasonaccf1eb2022-02-03 22:40:17 +0100364 if (progress->last_value != -1)
365 force_last_update(progress, msg);
Ævar Arnfjörð Bjarmason74900a62022-02-03 22:40:18 +0100366 log_trace2(progress);
Ævar Arnfjörð Bjarmasonaccf1eb2022-02-03 22:40:17 +0100367
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400368 clear_progress_signal();
SZEDER Gábord53ba842019-04-05 02:45:37 +0200369 strbuf_release(&progress->counters_sb);
Jeff King31319772015-09-24 17:05:57 -0400370 if (progress->throughput)
371 strbuf_release(&progress->throughput->display);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400372 free(progress->throughput);
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400373 free(progress);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400374}