🌐 AI搜索 & 代理 主页
blob: 0063559aab6e233abd0a69aef6cead9d01e39948 [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
SZEDER Gábor545dc342019-04-12 19:45:1511#include "cache.h"
Nguyễn Thái Ngọc Duy754dbc42014-02-21 12:50:1812#include "gettext.h"
Nicolas Pitre96a02f82007-04-18 18:27:4513#include "progress.h"
Antoine Pelisse079b5462013-04-10 19:03:2314#include "strbuf.h"
Karsten Blees83d26fa2014-07-12 00:08:1115#include "trace.h"
SZEDER Gábor545dc342019-04-12 19:45:1516#include "utf8.h"
Nicolas Pitre96a02f82007-04-18 18:27:4517
Nicolas Pitrecf84d512007-10-30 18:57:3418#define TP_IDX_MAX 8
19
20struct throughput {
Nicolas Pitre53ed7b52007-11-06 21:30:2821 off_t curr_total;
Nicolas Pitre218558a2007-11-05 03:15:4122 off_t prev_total;
Karsten Blees83d26fa2014-07-12 00:08:1123 uint64_t prev_ns;
Nicolas Pitre218558a2007-11-05 03:15:4124 unsigned int avg_bytes;
Nicolas Pitrecf84d512007-10-30 18:57:3425 unsigned int avg_misecs;
Nicolas Pitre53ed7b52007-11-06 21:30:2826 unsigned int last_bytes[TP_IDX_MAX];
Nicolas Pitrecf84d512007-10-30 18:57:3427 unsigned int last_misecs[TP_IDX_MAX];
28 unsigned int idx;
Jeff King31319772015-09-24 21:05:5729 struct strbuf display;
Nicolas Pitrecf84d512007-10-30 18:57:3430};
31
Nicolas Pitredc6a0752007-10-30 18:57:3232struct progress {
33 const char *title;
Elijah Newrend6861d02017-11-13 20:15:5834 uint64_t last_value;
35 uint64_t total;
Nicolas Pitredc6a0752007-10-30 18:57:3236 unsigned last_percent;
37 unsigned delay;
Jeff Hostetler9d81ecb2019-03-21 19:36:1138 unsigned sparse;
Nicolas Pitrecf84d512007-10-30 18:57:3439 struct throughput *throughput;
René Scharfe0fae1e02017-07-08 16:43:4240 uint64_t start_ns;
SZEDER Gábord53ba842019-04-05 00:45:3741 struct strbuf counters_sb;
SZEDER Gábor545dc342019-04-12 19:45:1542 int title_len;
43 int split;
Nicolas Pitredc6a0752007-10-30 18:57:3244};
45
Nicolas Pitre96a02f82007-04-18 18:27:4546static volatile sig_atomic_t progress_update;
47
SZEDER Gábor2bb74b52019-09-16 20:54:1248/*
49 * These are only intended for testing the progress output, i.e. exclusively
50 * for 'test-tool progress'.
51 */
52int progress_testing;
53uint64_t progress_test_ns = 0;
54void progress_test_force_update(void); /* To silence -Wmissing-prototypes */
55void progress_test_force_update(void)
56{
57 progress_update = 1;
58}
59
60
Nicolas Pitre96a02f82007-04-18 18:27:4561static void progress_interval(int signum)
62{
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 20:54:1271 if (progress_testing)
72 return;
73
Nicolas Pitre180a9f22007-04-20 19:05:2774 progress_update = 0;
75
Nicolas Pitre96a02f82007-04-18 18:27:4576 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 20:54:1291
92 if (progress_testing)
93 return;
94
Nicolas Pitre96a02f82007-04-18 18:27:4595 setitimer(ITIMER_REAL, &v, NULL);
96 signal(SIGALRM, SIG_IGN);
97 progress_update = 0;
98}
99
Luke Mewburn85cb8902015-04-13 13:30:51100static int is_foreground_fd(int fd)
101{
Jeff Kinga4fb76c2015-05-19 05:24:57102 int tpgrp = tcgetpgrp(fd);
103 return tpgrp < 0 || tpgrp == getpgid(0);
Luke Mewburn85cb8902015-04-13 13:30:51104}
105
SZEDER Gábor9219d122019-04-05 00:45:36106static void display(struct progress *progress, uint64_t n, const char *done)
Nicolas Pitre96a02f82007-04-18 18:27:45107{
SZEDER Gábord53ba842019-04-05 00:45:37108 const char *tp;
109 struct strbuf *counters_sb = &progress->counters_sb;
110 int show_update = 0;
SZEDER Gáborbbf47562019-09-16 20:54:11111 int last_count_len = counters_sb->len;
Nicolas Pitre42e18fb2007-10-17 01:55:45112
Lars Schneider9c5951c2017-12-04 22:07:00113 if (progress->delay && (!progress_update || --progress->delay))
SZEDER Gábor9219d122019-04-05 00:45:36114 return;
Nicolas Pitre42e18fb2007-10-17 01:55:45115
116 progress->last_value = n;
Jeff King31319772015-09-24 21:05:57117 tp = (progress->throughput) ? progress->throughput->display.buf : "";
Nicolas Pitre96a02f82007-04-18 18:27:45118 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 00:45:37122
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 18:27:45129 }
130 } else if (progress_update) {
SZEDER Gábord53ba842019-04-05 00:45:37131 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 13:30:51137 if (is_foreground_fd(fileno(stderr)) || done) {
SZEDER Gábor9f1fd842019-04-12 19:45:14138 const char *eol = done ? done : "\r";
SZEDER Gáborbbf47562019-09-16 20:54:11139 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 19:45:15146
147 if (progress->split) {
SZEDER Gáborbbf47562019-09-16 20:54:11148 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 19:45:15156 progress->split = 1;
157 } else {
SZEDER Gáborbbf47562019-09-16 20:54:11158 fprintf(stderr, "%s: %s%*s", progress->title,
159 counters_sb->buf, (int) clear_len, eol);
SZEDER Gábor545dc342019-04-12 19:45:15160 }
Luke Mewburn85cb8902015-04-13 13:30:51161 fflush(stderr);
162 }
Nicolas Pitre96a02f82007-04-18 18:27:45163 progress_update = 0;
Nicolas Pitre96a02f82007-04-18 18:27:45164 }
Nicolas Pitre96a02f82007-04-18 18:27:45165}
166
Elijah Newrend6861d02017-11-13 20:15:58167static void throughput_string(struct strbuf *buf, uint64_t total,
Nicolas Pitre53ed7b52007-11-06 21:30:28168 unsigned int rate)
169{
Jeff King31319772015-09-24 21:05:57170 strbuf_reset(buf);
Antoine Pelisse079b5462013-04-10 19:03:23171 strbuf_addstr(buf, ", ");
172 strbuf_humanise_bytes(buf, total);
173 strbuf_addstr(buf, " | ");
Dimitriy Ryazantcev8f354a12019-07-02 18:22:48174 strbuf_humanise_rate(buf, rate * 1024);
Nicolas Pitre53ed7b52007-11-06 21:30:28175}
176
SZEDER Gábor2bb74b52019-09-16 20:54:12177static 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 20:15:58185void display_throughput(struct progress *progress, uint64_t total)
Nicolas Pitrecf84d512007-10-30 18:57:34186{
187 struct throughput *tp;
Karsten Blees83d26fa2014-07-12 00:08:11188 uint64_t now_ns;
189 unsigned int misecs, count, rate;
Nicolas Pitrecf84d512007-10-30 18:57:34190
191 if (!progress)
192 return;
193 tp = progress->throughput;
194
SZEDER Gábor2bb74b52019-09-16 20:54:12195 now_ns = progress_getnanotime(progress);
Nicolas Pitrecf84d512007-10-30 18:57:34196
197 if (!tp) {
Jeff King999b9512019-04-11 13:49:57198 progress->throughput = tp = xcalloc(1, sizeof(*tp));
199 tp->prev_total = tp->curr_total = total;
200 tp->prev_ns = now_ns;
201 strbuf_init(&tp->display, 0);
Nicolas Pitrecf84d512007-10-30 18:57:34202 return;
203 }
Nicolas Pitre53ed7b52007-11-06 21:30:28204 tp->curr_total = total;
Nicolas Pitrecf84d512007-10-30 18:57:34205
Karsten Blees83d26fa2014-07-12 00:08:11206 /* only update throughput every 0.5 s */
207 if (now_ns - tp->prev_ns <= 500000000)
208 return;
209
Nicolas Pitrecf84d512007-10-30 18:57:34210 /*
Karsten Blees83d26fa2014-07-12 00:08:11211 * We have x = bytes and y = nanosecs. We want z = KiB/s:
Nicolas Pitrecf84d512007-10-30 18:57:34212 *
Karsten Blees83d26fa2014-07-12 00:08:11213 * z = (x / 1024) / (y / 1000000000)
214 * z = x / y * 1000000000 / 1024
215 * z = x / (y * 1024 / 1000000000)
Nicolas Pitrecf84d512007-10-30 18:57:34216 * 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 00:08:11221 * 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 18:57:34225 */
Karsten Blees83d26fa2014-07-12 00:08:11226 misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
Nicolas Pitrecf84d512007-10-30 18:57:34227
Karsten Blees83d26fa2014-07-12 00:08:11228 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 21:30:28239
Jeff King31319772015-09-24 21:05:57240 throughput_string(&tp->display, total, rate);
Karsten Blees83d26fa2014-07-12 00:08:11241 if (progress->last_value != -1 && progress_update)
242 display(progress, progress->last_value, NULL);
Nicolas Pitrecf84d512007-10-30 18:57:34243}
244
SZEDER Gábor9219d122019-04-05 00:45:36245void display_progress(struct progress *progress, uint64_t n)
Nicolas Pitre96a02f82007-04-18 18:27:45246{
SZEDER Gábor9219d122019-04-05 00:45:36247 if (progress)
248 display(progress, n, NULL);
Nicolas Pitre96a02f82007-04-18 18:27:45249}
250
Elijah Newrend6861d02017-11-13 20:15:58251static struct progress *start_progress_delay(const char *title, uint64_t total,
Jeff Hostetler9d81ecb2019-03-21 19:36:11252 unsigned delay, unsigned sparse)
Nicolas Pitre180a9f22007-04-20 19:05:27253{
Jeff King999b9512019-04-11 13:49:57254 struct progress *progress = xmalloc(sizeof(*progress));
Nicolas Pitre42e18fb2007-10-17 01:55:45255 progress->title = title;
Nicolas Pitre180a9f22007-04-20 19:05:27256 progress->total = total;
Nicolas Pitre42e18fb2007-10-17 01:55:45257 progress->last_value = -1;
Nicolas Pitre180a9f22007-04-20 19:05:27258 progress->last_percent = -1;
Nicolas Pitre180a9f22007-04-20 19:05:27259 progress->delay = delay;
Jeff Hostetler9d81ecb2019-03-21 19:36:11260 progress->sparse = sparse;
Nicolas Pitrecf84d512007-10-30 18:57:34261 progress->throughput = NULL;
René Scharfe0fae1e02017-07-08 16:43:42262 progress->start_ns = getnanotime();
SZEDER Gábord53ba842019-04-05 00:45:37263 strbuf_init(&progress->counters_sb, 0);
SZEDER Gábor545dc342019-04-12 19:45:15264 progress->title_len = utf8_strwidth(title);
265 progress->split = 0;
Nicolas Pitre180a9f22007-04-20 19:05:27266 set_progress_signal();
Nicolas Pitredc6a0752007-10-30 18:57:32267 return progress;
Nicolas Pitre180a9f22007-04-20 19:05:27268}
269
Elijah Newrend6861d02017-11-13 20:15:58270struct progress *start_delayed_progress(const char *title, uint64_t total)
Junio C Hamano8aade102017-08-19 17:39:41271{
Jeff Hostetler9d81ecb2019-03-21 19:36:11272 return start_progress_delay(title, total, 2, 0);
Junio C Hamano8aade102017-08-19 17:39:41273}
274
Elijah Newrend6861d02017-11-13 20:15:58275struct progress *start_progress(const char *title, uint64_t total)
Nicolas Pitre42e18fb2007-10-17 01:55:45276{
Jeff Hostetler9d81ecb2019-03-21 19:36:11277 return start_progress_delay(title, total, 0, 0);
278}
279
280/*
281 * Here "sparse" means that the caller might use some sampling criteria to
282 * decide when to call display_progress() rather than calling it for every
283 * integer value in[0 .. total). In particular, the caller might not call
284 * display_progress() for the last value in the range.
285 *
286 * When "sparse" is set, stop_progress() will automatically force the done
287 * message to show 100%.
288 */
289struct progress *start_sparse_progress(const char *title, uint64_t total)
290{
291 return start_progress_delay(title, total, 0, 1);
292}
293
294struct progress *start_delayed_sparse_progress(const char *title,
295 uint64_t total)
296{
297 return start_progress_delay(title, total, 2, 1);
298}
299
300static void finish_if_sparse(struct progress *progress)
301{
302 if (progress &&
303 progress->sparse &&
304 progress->last_value != progress->total)
305 display_progress(progress, progress->total);
Nicolas Pitre42e18fb2007-10-17 01:55:45306}
307
Nicolas Pitredc6a0752007-10-30 18:57:32308void stop_progress(struct progress **p_progress)
Nicolas Pitre96a02f82007-04-18 18:27:45309{
Jeff Hostetler9d81ecb2019-03-21 19:36:11310 finish_if_sparse(*p_progress);
311
Nguyễn Thái Ngọc Duy754dbc42014-02-21 12:50:18312 stop_progress_msg(p_progress, _("done"));
Nicolas Pitrea984a062007-11-08 20:45:41313}
314
315void stop_progress_msg(struct progress **p_progress, const char *msg)
316{
Nicolas Pitredc6a0752007-10-30 18:57:32317 struct progress *progress = *p_progress;
318 if (!progress)
319 return;
320 *p_progress = NULL;
Nicolas Pitre42e18fb2007-10-17 01:55:45321 if (progress->last_value != -1) {
322 /* Force the last update */
Maxim Moseychukfbd09432017-02-16 17:07:13323 char *buf;
Nicolas Pitre53ed7b52007-11-06 21:30:28324 struct throughput *tp = progress->throughput;
Boyd Lynn Gerberd4c44442008-06-08 15:26:15325
Nicolas Pitre53ed7b52007-11-06 21:30:28326 if (tp) {
SZEDER Gábor2bb74b52019-09-16 20:54:12327 uint64_t now_ns = progress_getnanotime(progress);
René Scharfe0fae1e02017-07-08 16:43:42328 unsigned int misecs, rate;
329 misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
330 rate = tp->curr_total / (misecs ? misecs : 1);
Jeff King31319772015-09-24 21:05:57331 throughput_string(&tp->display, tp->curr_total, rate);
Nicolas Pitre53ed7b52007-11-06 21:30:28332 }
Nicolas Pitre42e18fb2007-10-17 01:55:45333 progress_update = 1;
Maxim Moseychukfbd09432017-02-16 17:07:13334 buf = xstrfmt(", %s.\n", msg);
335 display(progress, progress->last_value, buf);
336 free(buf);
Nicolas Pitre42e18fb2007-10-17 01:55:45337 }
Nicolas Pitre96a02f82007-04-18 18:27:45338 clear_progress_signal();
SZEDER Gábord53ba842019-04-05 00:45:37339 strbuf_release(&progress->counters_sb);
Jeff King31319772015-09-24 21:05:57340 if (progress->throughput)
341 strbuf_release(&progress->throughput->display);
Nicolas Pitrecf84d512007-10-30 18:57:34342 free(progress->throughput);
Nicolas Pitredc6a0752007-10-30 18:57:32343 free(progress);
Nicolas Pitre96a02f82007-04-18 18:27:45344}