blob: 0cdd875d37f166bedbbeb5f0e889046674ed58be [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"
Nguyễn Thái Ngọc Duy754dbc42014-02-21 19:50:18 +070013#include "gettext.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -040014#include "progress.h"
Antoine Pelisse079b5462013-04-10 21:03:23 +020015#include "strbuf.h"
Karsten Blees83d26fa2014-07-12 02:08:11 +020016#include "trace.h"
SZEDER Gábor545dc342019-04-12 21:45:15 +020017#include "utf8.h"
Derrick Stolee44a46932019-11-25 21:28:22 +000018#include "config.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -040019
Nicolas Pitrecf84d512007-10-30 14:57:34 -040020#define TP_IDX_MAX 8
21
22struct throughput {
Nicolas Pitre53ed7b52007-11-06 16:30:28 -050023 off_t curr_total;
Nicolas Pitre218558a2007-11-04 22:15:41 -050024 off_t prev_total;
Karsten Blees83d26fa2014-07-12 02:08:11 +020025 uint64_t prev_ns;
Nicolas Pitre218558a2007-11-04 22:15:41 -050026 unsigned int avg_bytes;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040027 unsigned int avg_misecs;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -050028 unsigned int last_bytes[TP_IDX_MAX];
Nicolas Pitrecf84d512007-10-30 14:57:34 -040029 unsigned int last_misecs[TP_IDX_MAX];
30 unsigned int idx;
Jeff King31319772015-09-24 17:05:57 -040031 struct strbuf display;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040032};
33
Nicolas Pitredc6a0752007-10-30 14:57:32 -040034struct progress {
35 const char *title;
Elijah Newrend6861d02017-11-13 12:15:58 -080036 uint64_t last_value;
37 uint64_t total;
Nicolas Pitredc6a0752007-10-30 14:57:32 -040038 unsigned last_percent;
39 unsigned delay;
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -070040 unsigned sparse;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040041 struct throughput *throughput;
René Scharfe0fae1e02017-07-08 18:43:42 +020042 uint64_t start_ns;
SZEDER Gábord53ba842019-04-05 02:45:37 +020043 struct strbuf counters_sb;
SZEDER Gábor545dc342019-04-12 21:45:15 +020044 int title_len;
45 int split;
Nicolas Pitredc6a0752007-10-30 14:57:32 -040046};
47
Nicolas Pitre96a02f82007-04-18 14:27:45 -040048static volatile sig_atomic_t progress_update;
49
SZEDER Gábor2bb74b52019-09-16 22:54:12 +020050/*
51 * These are only intended for testing the progress output, i.e. exclusively
52 * for 'test-tool progress'.
53 */
54int progress_testing;
55uint64_t progress_test_ns = 0;
SZEDER Gábor2bb74b52019-09-16 22:54:12 +020056void progress_test_force_update(void)
57{
58 progress_update = 1;
59}
60
61
Nicolas Pitre96a02f82007-04-18 14:27:45 -040062static void progress_interval(int signum)
63{
64 progress_update = 1;
65}
66
67static void set_progress_signal(void)
68{
69 struct sigaction sa;
70 struct itimerval v;
71
SZEDER Gábor2bb74b52019-09-16 22:54:12 +020072 if (progress_testing)
73 return;
74
Nicolas Pitre180a9f22007-04-20 15:05:27 -040075 progress_update = 0;
76
Nicolas Pitre96a02f82007-04-18 14:27:45 -040077 memset(&sa, 0, sizeof(sa));
78 sa.sa_handler = progress_interval;
79 sigemptyset(&sa.sa_mask);
80 sa.sa_flags = SA_RESTART;
81 sigaction(SIGALRM, &sa, NULL);
82
83 v.it_interval.tv_sec = 1;
84 v.it_interval.tv_usec = 0;
85 v.it_value = v.it_interval;
86 setitimer(ITIMER_REAL, &v, NULL);
87}
88
89static void clear_progress_signal(void)
90{
91 struct itimerval v = {{0,},};
SZEDER Gábor2bb74b52019-09-16 22:54:12 +020092
93 if (progress_testing)
94 return;
95
Nicolas Pitre96a02f82007-04-18 14:27:45 -040096 setitimer(ITIMER_REAL, &v, NULL);
97 signal(SIGALRM, SIG_IGN);
98 progress_update = 0;
99}
100
Luke Mewburn85cb8902015-04-13 23:30:51 +1000101static int is_foreground_fd(int fd)
102{
Jeff Kinga4fb76c2015-05-19 01:24:57 -0400103 int tpgrp = tcgetpgrp(fd);
104 return tpgrp < 0 || tpgrp == getpgid(0);
Luke Mewburn85cb8902015-04-13 23:30:51 +1000105}
106
SZEDER Gábor9219d122019-04-05 02:45:36 +0200107static void display(struct progress *progress, uint64_t n, const char *done)
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400108{
SZEDER Gábord53ba842019-04-05 02:45:37 +0200109 const char *tp;
110 struct strbuf *counters_sb = &progress->counters_sb;
111 int show_update = 0;
SZEDER Gáborbbf47562019-09-16 22:54:11 +0200112 int last_count_len = counters_sb->len;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400113
Lars Schneider9c5951c2017-12-04 17:07:00 -0500114 if (progress->delay && (!progress_update || --progress->delay))
SZEDER Gábor9219d122019-04-05 02:45:36 +0200115 return;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400116
117 progress->last_value = n;
Jeff King31319772015-09-24 17:05:57 -0400118 tp = (progress->throughput) ? progress->throughput->display.buf : "";
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400119 if (progress->total) {
120 unsigned percent = n * 100 / progress->total;
121 if (percent != progress->last_percent || progress_update) {
122 progress->last_percent = percent;
SZEDER Gábord53ba842019-04-05 02:45:37 +0200123
124 strbuf_reset(counters_sb);
125 strbuf_addf(counters_sb,
126 "%3u%% (%"PRIuMAX"/%"PRIuMAX")%s", percent,
127 (uintmax_t)n, (uintmax_t)progress->total,
128 tp);
129 show_update = 1;
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400130 }
131 } else if (progress_update) {
SZEDER Gábord53ba842019-04-05 02:45:37 +0200132 strbuf_reset(counters_sb);
133 strbuf_addf(counters_sb, "%"PRIuMAX"%s", (uintmax_t)n, tp);
134 show_update = 1;
135 }
136
137 if (show_update) {
Luke Mewburn85cb8902015-04-13 23:30:51 +1000138 if (is_foreground_fd(fileno(stderr)) || done) {
SZEDER Gábor9f1fd842019-04-12 21:45:14 +0200139 const char *eol = done ? done : "\r";
SZEDER Gáborbbf47562019-09-16 22:54:11 +0200140 size_t clear_len = counters_sb->len < last_count_len ?
141 last_count_len - counters_sb->len + 1 :
142 0;
143 /* The "+ 2" accounts for the ": ". */
144 size_t progress_line_len = progress->title_len +
145 counters_sb->len + 2;
146 int cols = term_columns();
SZEDER Gábor545dc342019-04-12 21:45:15 +0200147
148 if (progress->split) {
SZEDER Gáborbbf47562019-09-16 22:54:11 +0200149 fprintf(stderr, " %s%*s", counters_sb->buf,
150 (int) clear_len, eol);
151 } else if (!done && cols < progress_line_len) {
152 clear_len = progress->title_len + 1 < cols ?
153 cols - progress->title_len - 1 : 0;
154 fprintf(stderr, "%s:%*s\n %s%s",
155 progress->title, (int) clear_len, "",
156 counters_sb->buf, eol);
SZEDER Gábor545dc342019-04-12 21:45:15 +0200157 progress->split = 1;
158 } else {
SZEDER Gáborbbf47562019-09-16 22:54:11 +0200159 fprintf(stderr, "%s: %s%*s", progress->title,
160 counters_sb->buf, (int) clear_len, eol);
SZEDER Gábor545dc342019-04-12 21:45:15 +0200161 }
Luke Mewburn85cb8902015-04-13 23:30:51 +1000162 fflush(stderr);
163 }
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400164 progress_update = 0;
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400165 }
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400166}
167
Elijah Newrend6861d02017-11-13 12:15:58 -0800168static void throughput_string(struct strbuf *buf, uint64_t total,
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500169 unsigned int rate)
170{
Jeff King31319772015-09-24 17:05:57 -0400171 strbuf_reset(buf);
Antoine Pelisse079b5462013-04-10 21:03:23 +0200172 strbuf_addstr(buf, ", ");
173 strbuf_humanise_bytes(buf, total);
174 strbuf_addstr(buf, " | ");
Dimitriy Ryazantcev8f354a12019-07-02 21:22:48 +0300175 strbuf_humanise_rate(buf, rate * 1024);
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500176}
177
SZEDER Gábor2bb74b52019-09-16 22:54:12 +0200178static uint64_t progress_getnanotime(struct progress *progress)
179{
180 if (progress_testing)
181 return progress->start_ns + progress_test_ns;
182 else
183 return getnanotime();
184}
185
Elijah Newrend6861d02017-11-13 12:15:58 -0800186void display_throughput(struct progress *progress, uint64_t total)
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400187{
188 struct throughput *tp;
Karsten Blees83d26fa2014-07-12 02:08:11 +0200189 uint64_t now_ns;
190 unsigned int misecs, count, rate;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400191
192 if (!progress)
193 return;
194 tp = progress->throughput;
195
SZEDER Gábor2bb74b52019-09-16 22:54:12 +0200196 now_ns = progress_getnanotime(progress);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400197
198 if (!tp) {
René Scharfeca56dad2021-03-13 17:17:22 +0100199 progress->throughput = CALLOC_ARRAY(tp, 1);
Jeff King999b9512019-04-11 09:49:57 -0400200 tp->prev_total = tp->curr_total = total;
201 tp->prev_ns = now_ns;
202 strbuf_init(&tp->display, 0);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400203 return;
204 }
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500205 tp->curr_total = total;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400206
Karsten Blees83d26fa2014-07-12 02:08:11 +0200207 /* only update throughput every 0.5 s */
208 if (now_ns - tp->prev_ns <= 500000000)
209 return;
210
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400211 /*
Karsten Blees83d26fa2014-07-12 02:08:11 +0200212 * We have x = bytes and y = nanosecs. We want z = KiB/s:
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400213 *
Karsten Blees83d26fa2014-07-12 02:08:11 +0200214 * z = (x / 1024) / (y / 1000000000)
215 * z = x / y * 1000000000 / 1024
216 * z = x / (y * 1024 / 1000000000)
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400217 * z = x / y'
218 *
219 * To simplify things we'll keep track of misecs, or 1024th of a sec
220 * obtained with:
221 *
Karsten Blees83d26fa2014-07-12 02:08:11 +0200222 * y' = y * 1024 / 1000000000
223 * y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
224 * y' = y / 2^32 * 4398
225 * y' = (y * 4398) >> 32
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400226 */
Karsten Blees83d26fa2014-07-12 02:08:11 +0200227 misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400228
Karsten Blees83d26fa2014-07-12 02:08:11 +0200229 count = total - tp->prev_total;
230 tp->prev_total = total;
231 tp->prev_ns = now_ns;
232 tp->avg_bytes += count;
233 tp->avg_misecs += misecs;
234 rate = tp->avg_bytes / tp->avg_misecs;
235 tp->avg_bytes -= tp->last_bytes[tp->idx];
236 tp->avg_misecs -= tp->last_misecs[tp->idx];
237 tp->last_bytes[tp->idx] = count;
238 tp->last_misecs[tp->idx] = misecs;
239 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500240
Jeff King31319772015-09-24 17:05:57 -0400241 throughput_string(&tp->display, total, rate);
Karsten Blees83d26fa2014-07-12 02:08:11 +0200242 if (progress->last_value != -1 && progress_update)
243 display(progress, progress->last_value, NULL);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400244}
245
SZEDER Gábor9219d122019-04-05 02:45:36 +0200246void display_progress(struct progress *progress, uint64_t n)
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400247{
SZEDER Gábor9219d122019-04-05 02:45:36 +0200248 if (progress)
249 display(progress, n, NULL);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400250}
251
Elijah Newrend6861d02017-11-13 12:15:58 -0800252static struct progress *start_progress_delay(const char *title, uint64_t total,
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -0700253 unsigned delay, unsigned sparse)
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400254{
Jeff King999b9512019-04-11 09:49:57 -0400255 struct progress *progress = xmalloc(sizeof(*progress));
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400256 progress->title = title;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400257 progress->total = total;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400258 progress->last_value = -1;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400259 progress->last_percent = -1;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400260 progress->delay = delay;
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -0700261 progress->sparse = sparse;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400262 progress->throughput = NULL;
René Scharfe0fae1e02017-07-08 18:43:42 +0200263 progress->start_ns = getnanotime();
SZEDER Gábord53ba842019-04-05 02:45:37 +0200264 strbuf_init(&progress->counters_sb, 0);
SZEDER Gábor545dc342019-04-12 21:45:15 +0200265 progress->title_len = utf8_strwidth(title);
266 progress->split = 0;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400267 set_progress_signal();
Emily Shaffer98a13642020-05-12 14:44:20 -0700268 trace2_region_enter("progress", title, the_repository);
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400269 return progress;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400270}
271
Derrick Stolee44a46932019-11-25 21:28:22 +0000272static int get_default_delay(void)
273{
274 static int delay_in_secs = -1;
275
276 if (delay_in_secs < 0)
277 delay_in_secs = git_env_ulong("GIT_PROGRESS_DELAY", 2);
278
279 return delay_in_secs;
280}
281
Elijah Newrend6861d02017-11-13 12:15:58 -0800282struct progress *start_delayed_progress(const char *title, uint64_t total)
Junio C Hamano8aade102017-08-19 10:39:41 -0700283{
Derrick Stolee44a46932019-11-25 21:28:22 +0000284 return start_progress_delay(title, total, get_default_delay(), 0);
Junio C Hamano8aade102017-08-19 10:39:41 -0700285}
286
Elijah Newrend6861d02017-11-13 12:15:58 -0800287struct progress *start_progress(const char *title, uint64_t total)
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400288{
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -0700289 return start_progress_delay(title, total, 0, 0);
290}
291
292/*
293 * Here "sparse" means that the caller might use some sampling criteria to
294 * decide when to call display_progress() rather than calling it for every
295 * integer value in[0 .. total). In particular, the caller might not call
296 * display_progress() for the last value in the range.
297 *
298 * When "sparse" is set, stop_progress() will automatically force the done
299 * message to show 100%.
300 */
301struct progress *start_sparse_progress(const char *title, uint64_t total)
302{
303 return start_progress_delay(title, total, 0, 1);
304}
305
306struct progress *start_delayed_sparse_progress(const char *title,
307 uint64_t total)
308{
Derrick Stolee44a46932019-11-25 21:28:22 +0000309 return start_progress_delay(title, total, get_default_delay(), 1);
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -0700310}
311
312static void finish_if_sparse(struct progress *progress)
313{
Ævar Arnfjörð Bjarmason74900a62022-02-03 22:40:18 +0100314 if (progress->sparse &&
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -0700315 progress->last_value != progress->total)
316 display_progress(progress, progress->total);
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400317}
318
Ævar Arnfjörð Bjarmasonaccf1eb2022-02-03 22:40:17 +0100319static void force_last_update(struct progress *progress, const char *msg)
320{
321 char *buf;
322 struct throughput *tp = progress->throughput;
323
324 if (tp) {
325 uint64_t now_ns = progress_getnanotime(progress);
326 unsigned int misecs, rate;
327 misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
328 rate = tp->curr_total / (misecs ? misecs : 1);
329 throughput_string(&tp->display, tp->curr_total, rate);
330 }
331 progress_update = 1;
332 buf = xstrfmt(", %s.\n", msg);
333 display(progress, progress->last_value, buf);
334 free(buf);
335}
336
337static void log_trace2(struct progress *progress)
338{
339 trace2_data_intmax("progress", the_repository, "total_objects",
340 progress->total);
341
342 if (progress->throughput)
343 trace2_data_intmax("progress", the_repository, "total_bytes",
344 progress->throughput->curr_total);
345
346 trace2_region_leave("progress", progress->title, the_repository);
347}
348
Nicolas Pitrea984a062007-11-08 15:45:41 -0500349void stop_progress_msg(struct progress **p_progress, const char *msg)
350{
Martin Ågrenac900fd2020-08-10 21:47:48 +0200351 struct progress *progress;
352
353 if (!p_progress)
354 BUG("don't provide NULL to stop_progress_msg");
355
356 progress = *p_progress;
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400357 if (!progress)
358 return;
359 *p_progress = NULL;
Boyd Lynn Gerberd4c44442008-06-08 09:26:15 -0600360
Ævar Arnfjörð Bjarmason74900a62022-02-03 22:40:18 +0100361 finish_if_sparse(progress);
Ævar Arnfjörð Bjarmasonaccf1eb2022-02-03 22:40:17 +0100362 if (progress->last_value != -1)
363 force_last_update(progress, msg);
Ævar Arnfjörð Bjarmason74900a62022-02-03 22:40:18 +0100364 log_trace2(progress);
Ævar Arnfjörð Bjarmasonaccf1eb2022-02-03 22:40:17 +0100365
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400366 clear_progress_signal();
SZEDER Gábord53ba842019-04-05 02:45:37 +0200367 strbuf_release(&progress->counters_sb);
Jeff King31319772015-09-24 17:05:57 -0400368 if (progress->throughput)
369 strbuf_release(&progress->throughput->display);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400370 free(progress->throughput);
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400371 free(progress);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400372}