🌐 AI搜索 & 代理 主页
blob: 680c6a8bf93b514a7b82510c475984ca32067eb7 [file] [log] [blame]
Nicolas Pitre74b67922007-10-30 19:41:131/*
2 * Simple text-based progress display module for GIT
3 *
Nicolas Pitre03aa8ff2009-09-14 06:41:164 * Copyright (c) 2007 by Nicolas Pitre <nico@fluxnic.net>
Nicolas Pitre74b67922007-10-30 19:41:135 *
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 14:22:3711#define GIT_TEST_PROGRESS_ONLY
SZEDER Gábor545dc342019-04-12 19:45:1512#include "cache.h"
Nguyễn Thái Ngọc Duy754dbc42014-02-21 12:50:1813#include "gettext.h"
Nicolas Pitre96a02f82007-04-18 18:27:4514#include "progress.h"
Antoine Pelisse079b5462013-04-10 19:03:2315#include "strbuf.h"
Karsten Blees83d26fa2014-07-12 00:08:1116#include "trace.h"
SZEDER Gábor545dc342019-04-12 19:45:1517#include "utf8.h"
Derrick Stolee44a46932019-11-25 21:28:2218#include "config.h"
Nicolas Pitre96a02f82007-04-18 18:27:4519
Nicolas Pitrecf84d512007-10-30 18:57:3420#define TP_IDX_MAX 8
21
22struct throughput {
Nicolas Pitre53ed7b52007-11-06 21:30:2823 off_t curr_total;
Nicolas Pitre218558a2007-11-05 03:15:4124 off_t prev_total;
Karsten Blees83d26fa2014-07-12 00:08:1125 uint64_t prev_ns;
Nicolas Pitre218558a2007-11-05 03:15:4126 unsigned int avg_bytes;
Nicolas Pitrecf84d512007-10-30 18:57:3427 unsigned int avg_misecs;
Nicolas Pitre53ed7b52007-11-06 21:30:2828 unsigned int last_bytes[TP_IDX_MAX];
Nicolas Pitrecf84d512007-10-30 18:57:3429 unsigned int last_misecs[TP_IDX_MAX];
30 unsigned int idx;
Jeff King31319772015-09-24 21:05:5731 struct strbuf display;
Nicolas Pitrecf84d512007-10-30 18:57:3432};
33
Nicolas Pitredc6a0752007-10-30 18:57:3234struct progress {
35 const char *title;
Elijah Newrend6861d02017-11-13 20:15:5836 uint64_t last_value;
37 uint64_t total;
Nicolas Pitredc6a0752007-10-30 18:57:3238 unsigned last_percent;
39 unsigned delay;
Jeff Hostetler9d81ecb2019-03-21 19:36:1140 unsigned sparse;
Nicolas Pitrecf84d512007-10-30 18:57:3441 struct throughput *throughput;
René Scharfe0fae1e02017-07-08 16:43:4242 uint64_t start_ns;
SZEDER Gábord53ba842019-04-05 00:45:3743 struct strbuf counters_sb;
SZEDER Gábor545dc342019-04-12 19:45:1544 int title_len;
45 int split;
Nicolas Pitredc6a0752007-10-30 18:57:3246};
47
Nicolas Pitre96a02f82007-04-18 18:27:4548static volatile sig_atomic_t progress_update;
49
SZEDER Gábor2bb74b52019-09-16 20:54:1250/*
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 20:54:1256void progress_test_force_update(void)
57{
58 progress_update = 1;
59}
60
61
Nicolas Pitre96a02f82007-04-18 18:27:4562static 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 20:54:1272 if (progress_testing)
73 return;
74
Nicolas Pitre180a9f22007-04-20 19:05:2775 progress_update = 0;
76
Nicolas Pitre96a02f82007-04-18 18:27:4577 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 20:54:1292
93 if (progress_testing)
94 return;
95
Nicolas Pitre96a02f82007-04-18 18:27:4596 setitimer(ITIMER_REAL, &v, NULL);
97 signal(SIGALRM, SIG_IGN);
98 progress_update = 0;
99}
100
Luke Mewburn85cb8902015-04-13 13:30:51101static int is_foreground_fd(int fd)
102{
Jeff Kinga4fb76c2015-05-19 05:24:57103 int tpgrp = tcgetpgrp(fd);
104 return tpgrp < 0 || tpgrp == getpgid(0);
Luke Mewburn85cb8902015-04-13 13:30:51105}
106
SZEDER Gábor9219d122019-04-05 00:45:36107static void display(struct progress *progress, uint64_t n, const char *done)
Nicolas Pitre96a02f82007-04-18 18:27:45108{
SZEDER Gábord53ba842019-04-05 00:45:37109 const char *tp;
110 struct strbuf *counters_sb = &progress->counters_sb;
111 int show_update = 0;
SZEDER Gáborbbf47562019-09-16 20:54:11112 int last_count_len = counters_sb->len;
Nicolas Pitre42e18fb2007-10-17 01:55:45113
Lars Schneider9c5951c2017-12-04 22:07:00114 if (progress->delay && (!progress_update || --progress->delay))
SZEDER Gábor9219d122019-04-05 00:45:36115 return;
Nicolas Pitre42e18fb2007-10-17 01:55:45116
117 progress->last_value = n;
Jeff King31319772015-09-24 21:05:57118 tp = (progress->throughput) ? progress->throughput->display.buf : "";
Nicolas Pitre96a02f82007-04-18 18:27:45119 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 00:45:37123
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 18:27:45130 }
131 } else if (progress_update) {
SZEDER Gábord53ba842019-04-05 00:45:37132 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 13:30:51138 if (is_foreground_fd(fileno(stderr)) || done) {
SZEDER Gábor9f1fd842019-04-12 19:45:14139 const char *eol = done ? done : "\r";
SZEDER Gáborbbf47562019-09-16 20:54:11140 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 19:45:15147
148 if (progress->split) {
SZEDER Gáborbbf47562019-09-16 20:54:11149 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 19:45:15157 progress->split = 1;
158 } else {
SZEDER Gáborbbf47562019-09-16 20:54:11159 fprintf(stderr, "%s: %s%*s", progress->title,
160 counters_sb->buf, (int) clear_len, eol);
SZEDER Gábor545dc342019-04-12 19:45:15161 }
Luke Mewburn85cb8902015-04-13 13:30:51162 fflush(stderr);
163 }
Nicolas Pitre96a02f82007-04-18 18:27:45164 progress_update = 0;
Nicolas Pitre96a02f82007-04-18 18:27:45165 }
Nicolas Pitre96a02f82007-04-18 18:27:45166}
167
Elijah Newrend6861d02017-11-13 20:15:58168static void throughput_string(struct strbuf *buf, uint64_t total,
Nicolas Pitre53ed7b52007-11-06 21:30:28169 unsigned int rate)
170{
Jeff King31319772015-09-24 21:05:57171 strbuf_reset(buf);
Antoine Pelisse079b5462013-04-10 19:03:23172 strbuf_addstr(buf, ", ");
173 strbuf_humanise_bytes(buf, total);
174 strbuf_addstr(buf, " | ");
Dimitriy Ryazantcev8f354a12019-07-02 18:22:48175 strbuf_humanise_rate(buf, rate * 1024);
Nicolas Pitre53ed7b52007-11-06 21:30:28176}
177
SZEDER Gábor2bb74b52019-09-16 20:54:12178static 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 20:15:58186void display_throughput(struct progress *progress, uint64_t total)
Nicolas Pitrecf84d512007-10-30 18:57:34187{
188 struct throughput *tp;
Karsten Blees83d26fa2014-07-12 00:08:11189 uint64_t now_ns;
190 unsigned int misecs, count, rate;
Nicolas Pitrecf84d512007-10-30 18:57:34191
192 if (!progress)
193 return;
194 tp = progress->throughput;
195
SZEDER Gábor2bb74b52019-09-16 20:54:12196 now_ns = progress_getnanotime(progress);
Nicolas Pitrecf84d512007-10-30 18:57:34197
198 if (!tp) {
René Scharfeca56dad2021-03-13 16:17:22199 progress->throughput = CALLOC_ARRAY(tp, 1);
Jeff King999b9512019-04-11 13:49:57200 tp->prev_total = tp->curr_total = total;
201 tp->prev_ns = now_ns;
202 strbuf_init(&tp->display, 0);
Nicolas Pitrecf84d512007-10-30 18:57:34203 return;
204 }
Nicolas Pitre53ed7b52007-11-06 21:30:28205 tp->curr_total = total;
Nicolas Pitrecf84d512007-10-30 18:57:34206
Karsten Blees83d26fa2014-07-12 00:08:11207 /* only update throughput every 0.5 s */
208 if (now_ns - tp->prev_ns <= 500000000)
209 return;
210
Nicolas Pitrecf84d512007-10-30 18:57:34211 /*
Karsten Blees83d26fa2014-07-12 00:08:11212 * We have x = bytes and y = nanosecs. We want z = KiB/s:
Nicolas Pitrecf84d512007-10-30 18:57:34213 *
Karsten Blees83d26fa2014-07-12 00:08:11214 * z = (x / 1024) / (y / 1000000000)
215 * z = x / y * 1000000000 / 1024
216 * z = x / (y * 1024 / 1000000000)
Nicolas Pitrecf84d512007-10-30 18:57:34217 * 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 00:08:11222 * 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 18:57:34226 */
Karsten Blees83d26fa2014-07-12 00:08:11227 misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
Nicolas Pitrecf84d512007-10-30 18:57:34228
Karsten Blees83d26fa2014-07-12 00:08:11229 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 21:30:28240
Jeff King31319772015-09-24 21:05:57241 throughput_string(&tp->display, total, rate);
Karsten Blees83d26fa2014-07-12 00:08:11242 if (progress->last_value != -1 && progress_update)
243 display(progress, progress->last_value, NULL);
Nicolas Pitrecf84d512007-10-30 18:57:34244}
245
SZEDER Gábor9219d122019-04-05 00:45:36246void display_progress(struct progress *progress, uint64_t n)
Nicolas Pitre96a02f82007-04-18 18:27:45247{
SZEDER Gábor9219d122019-04-05 00:45:36248 if (progress)
249 display(progress, n, NULL);
Nicolas Pitre96a02f82007-04-18 18:27:45250}
251
Elijah Newrend6861d02017-11-13 20:15:58252static struct progress *start_progress_delay(const char *title, uint64_t total,
Jeff Hostetler9d81ecb2019-03-21 19:36:11253 unsigned delay, unsigned sparse)
Nicolas Pitre180a9f22007-04-20 19:05:27254{
Jeff King999b9512019-04-11 13:49:57255 struct progress *progress = xmalloc(sizeof(*progress));
Nicolas Pitre42e18fb2007-10-17 01:55:45256 progress->title = title;
Nicolas Pitre180a9f22007-04-20 19:05:27257 progress->total = total;
Nicolas Pitre42e18fb2007-10-17 01:55:45258 progress->last_value = -1;
Nicolas Pitre180a9f22007-04-20 19:05:27259 progress->last_percent = -1;
Nicolas Pitre180a9f22007-04-20 19:05:27260 progress->delay = delay;
Jeff Hostetler9d81ecb2019-03-21 19:36:11261 progress->sparse = sparse;
Nicolas Pitrecf84d512007-10-30 18:57:34262 progress->throughput = NULL;
René Scharfe0fae1e02017-07-08 16:43:42263 progress->start_ns = getnanotime();
SZEDER Gábord53ba842019-04-05 00:45:37264 strbuf_init(&progress->counters_sb, 0);
SZEDER Gábor545dc342019-04-12 19:45:15265 progress->title_len = utf8_strwidth(title);
266 progress->split = 0;
Nicolas Pitre180a9f22007-04-20 19:05:27267 set_progress_signal();
Emily Shaffer98a13642020-05-12 21:44:20268 trace2_region_enter("progress", title, the_repository);
Nicolas Pitredc6a0752007-10-30 18:57:32269 return progress;
Nicolas Pitre180a9f22007-04-20 19:05:27270}
271
Derrick Stolee44a46932019-11-25 21:28:22272static 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 20:15:58282struct progress *start_delayed_progress(const char *title, uint64_t total)
Junio C Hamano8aade102017-08-19 17:39:41283{
Derrick Stolee44a46932019-11-25 21:28:22284 return start_progress_delay(title, total, get_default_delay(), 0);
Junio C Hamano8aade102017-08-19 17:39:41285}
286
Elijah Newrend6861d02017-11-13 20:15:58287struct progress *start_progress(const char *title, uint64_t total)
Nicolas Pitre42e18fb2007-10-17 01:55:45288{
Jeff Hostetler9d81ecb2019-03-21 19:36:11289 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:22309 return start_progress_delay(title, total, get_default_delay(), 1);
Jeff Hostetler9d81ecb2019-03-21 19:36:11310}
311
312static void finish_if_sparse(struct progress *progress)
313{
314 if (progress &&
315 progress->sparse &&
316 progress->last_value != progress->total)
317 display_progress(progress, progress->total);
Nicolas Pitre42e18fb2007-10-17 01:55:45318}
319
Nicolas Pitredc6a0752007-10-30 18:57:32320void stop_progress(struct progress **p_progress)
Nicolas Pitre96a02f82007-04-18 18:27:45321{
Martin Ågrenac900fd2020-08-10 19:47:48322 if (!p_progress)
323 BUG("don't provide NULL to stop_progress");
324
Jeff Hostetler9d81ecb2019-03-21 19:36:11325 finish_if_sparse(*p_progress);
326
Martin Ågrenac900fd2020-08-10 19:47:48327 if (*p_progress) {
Emily Shaffer98a13642020-05-12 21:44:20328 trace2_data_intmax("progress", the_repository, "total_objects",
329 (*p_progress)->total);
330
331 if ((*p_progress)->throughput)
332 trace2_data_intmax("progress", the_repository,
333 "total_bytes",
334 (*p_progress)->throughput->curr_total);
Emily Shaffer98a13642020-05-12 21:44:20335
Derrick Stolee3af029c2020-05-15 16:09:28336 trace2_region_leave("progress", (*p_progress)->title, the_repository);
337 }
Emily Shaffer98a13642020-05-12 21:44:20338
Nguyễn Thái Ngọc Duy754dbc42014-02-21 12:50:18339 stop_progress_msg(p_progress, _("done"));
Nicolas Pitrea984a062007-11-08 20:45:41340}
341
342void stop_progress_msg(struct progress **p_progress, const char *msg)
343{
Martin Ågrenac900fd2020-08-10 19:47:48344 struct progress *progress;
345
346 if (!p_progress)
347 BUG("don't provide NULL to stop_progress_msg");
348
349 progress = *p_progress;
Nicolas Pitredc6a0752007-10-30 18:57:32350 if (!progress)
351 return;
352 *p_progress = NULL;
Nicolas Pitre42e18fb2007-10-17 01:55:45353 if (progress->last_value != -1) {
354 /* Force the last update */
Maxim Moseychukfbd09432017-02-16 17:07:13355 char *buf;
Nicolas Pitre53ed7b52007-11-06 21:30:28356 struct throughput *tp = progress->throughput;
Boyd Lynn Gerberd4c44442008-06-08 15:26:15357
Nicolas Pitre53ed7b52007-11-06 21:30:28358 if (tp) {
SZEDER Gábor2bb74b52019-09-16 20:54:12359 uint64_t now_ns = progress_getnanotime(progress);
René Scharfe0fae1e02017-07-08 16:43:42360 unsigned int misecs, rate;
361 misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
362 rate = tp->curr_total / (misecs ? misecs : 1);
Jeff King31319772015-09-24 21:05:57363 throughput_string(&tp->display, tp->curr_total, rate);
Nicolas Pitre53ed7b52007-11-06 21:30:28364 }
Nicolas Pitre42e18fb2007-10-17 01:55:45365 progress_update = 1;
Maxim Moseychukfbd09432017-02-16 17:07:13366 buf = xstrfmt(", %s.\n", msg);
367 display(progress, progress->last_value, buf);
368 free(buf);
Nicolas Pitre42e18fb2007-10-17 01:55:45369 }
Nicolas Pitre96a02f82007-04-18 18:27:45370 clear_progress_signal();
SZEDER Gábord53ba842019-04-05 00:45:37371 strbuf_release(&progress->counters_sb);
Jeff King31319772015-09-24 21:05:57372 if (progress->throughput)
373 strbuf_release(&progress->throughput->display);
Nicolas Pitrecf84d512007-10-30 18:57:34374 free(progress->throughput);
Nicolas Pitredc6a0752007-10-30 18:57:32375 free(progress);
Nicolas Pitre96a02f82007-04-18 18:27:45376}