]> git.ipfire.org Git - thirdparty/git.git/blame - progress.c
sequencer: run post-commit hook
[thirdparty/git.git] / progress.c
CommitLineData
74b6792f
NP
1/*
2 * Simple text-based progress display module for GIT
3 *
03aa8ff3 4 * Copyright (c) 2007 by Nicolas Pitre <nico@fluxnic.net>
74b6792f
NP
5 *
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
545dc345 11#include "cache.h"
754dbc43 12#include "gettext.h"
96a02f8f 13#include "progress.h"
079b546a 14#include "strbuf.h"
83d26fa7 15#include "trace.h"
545dc345 16#include "utf8.h"
96a02f8f 17
cf84d51c
NP
18#define TP_IDX_MAX 8
19
20struct throughput {
53ed7b5a 21 off_t curr_total;
218558af 22 off_t prev_total;
83d26fa7 23 uint64_t prev_ns;
218558af 24 unsigned int avg_bytes;
cf84d51c 25 unsigned int avg_misecs;
53ed7b5a 26 unsigned int last_bytes[TP_IDX_MAX];
cf84d51c
NP
27 unsigned int last_misecs[TP_IDX_MAX];
28 unsigned int idx;
3131977d 29 struct strbuf display;
cf84d51c
NP
30};
31
dc6a0757
NP
32struct progress {
33 const char *title;
d6861d02
EN
34 uint64_t last_value;
35 uint64_t total;
dc6a0757
NP
36 unsigned last_percent;
37 unsigned delay;
9d81ecb5 38 unsigned sparse;
cf84d51c 39 struct throughput *throughput;
0fae1e07 40 uint64_t start_ns;
d53ba841 41 struct strbuf counters_sb;
545dc345
SG
42 int title_len;
43 int split;
dc6a0757
NP
44};
45
96a02f8f
NP
46static volatile sig_atomic_t progress_update;
47
48static void progress_interval(int signum)
49{
50 progress_update = 1;
51}
52
53static void set_progress_signal(void)
54{
55 struct sigaction sa;
56 struct itimerval v;
57
180a9f22
NP
58 progress_update = 0;
59
96a02f8f
NP
60 memset(&sa, 0, sizeof(sa));
61 sa.sa_handler = progress_interval;
62 sigemptyset(&sa.sa_mask);
63 sa.sa_flags = SA_RESTART;
64 sigaction(SIGALRM, &sa, NULL);
65
66 v.it_interval.tv_sec = 1;
67 v.it_interval.tv_usec = 0;
68 v.it_value = v.it_interval;
69 setitimer(ITIMER_REAL, &v, NULL);
70}
71
72static void clear_progress_signal(void)
73{
74 struct itimerval v = {{0,},};
75 setitimer(ITIMER_REAL, &v, NULL);
76 signal(SIGALRM, SIG_IGN);
77 progress_update = 0;
78}
79
85cb8906
LM
80static int is_foreground_fd(int fd)
81{
a4fb76ce
JK
82 int tpgrp = tcgetpgrp(fd);
83 return tpgrp < 0 || tpgrp == getpgid(0);
85cb8906
LM
84}
85
9219d127 86static void display(struct progress *progress, uint64_t n, const char *done)
96a02f8f 87{
d53ba841
SG
88 const char *tp;
89 struct strbuf *counters_sb = &progress->counters_sb;
90 int show_update = 0;
42e18fbf 91
9c5951ca 92 if (progress->delay && (!progress_update || --progress->delay))
9219d127 93 return;
42e18fbf
NP
94
95 progress->last_value = n;
3131977d 96 tp = (progress->throughput) ? progress->throughput->display.buf : "";
96a02f8f
NP
97 if (progress->total) {
98 unsigned percent = n * 100 / progress->total;
99 if (percent != progress->last_percent || progress_update) {
100 progress->last_percent = percent;
d53ba841
SG
101
102 strbuf_reset(counters_sb);
103 strbuf_addf(counters_sb,
104 "%3u%% (%"PRIuMAX"/%"PRIuMAX")%s", percent,
105 (uintmax_t)n, (uintmax_t)progress->total,
106 tp);
107 show_update = 1;
96a02f8f
NP
108 }
109 } else if (progress_update) {
d53ba841
SG
110 strbuf_reset(counters_sb);
111 strbuf_addf(counters_sb, "%"PRIuMAX"%s", (uintmax_t)n, tp);
112 show_update = 1;
113 }
114
115 if (show_update) {
85cb8906 116 if (is_foreground_fd(fileno(stderr)) || done) {
9f1fd84e 117 const char *eol = done ? done : "\r";
545dc345 118
5b12e312 119 term_clear_line();
545dc345 120 if (progress->split) {
5b12e312
SG
121 fprintf(stderr, " %s%s", counters_sb->buf,
122 eol);
123 } else if (!done &&
124 /* The "+ 2" accounts for the ": ". */
125 term_columns() < progress->title_len +
126 counters_sb->len + 2) {
127 fprintf(stderr, "%s:\n %s%s",
128 progress->title, counters_sb->buf, eol);
545dc345
SG
129 progress->split = 1;
130 } else {
5b12e312
SG
131 fprintf(stderr, "%s: %s%s", progress->title,
132 counters_sb->buf, eol);
545dc345 133 }
85cb8906
LM
134 fflush(stderr);
135 }
96a02f8f 136 progress_update = 0;
96a02f8f 137 }
96a02f8f
NP
138}
139
d6861d02 140static void throughput_string(struct strbuf *buf, uint64_t total,
53ed7b5a
NP
141 unsigned int rate)
142{
3131977d 143 strbuf_reset(buf);
079b546a
AP
144 strbuf_addstr(buf, ", ");
145 strbuf_humanise_bytes(buf, total);
146 strbuf_addstr(buf, " | ");
147 strbuf_humanise_bytes(buf, rate * 1024);
148 strbuf_addstr(buf, "/s");
53ed7b5a
NP
149}
150
d6861d02 151void display_throughput(struct progress *progress, uint64_t total)
cf84d51c
NP
152{
153 struct throughput *tp;
83d26fa7
KB
154 uint64_t now_ns;
155 unsigned int misecs, count, rate;
cf84d51c
NP
156
157 if (!progress)
158 return;
159 tp = progress->throughput;
160
83d26fa7 161 now_ns = getnanotime();
cf84d51c
NP
162
163 if (!tp) {
999b951b
JK
164 progress->throughput = tp = xcalloc(1, sizeof(*tp));
165 tp->prev_total = tp->curr_total = total;
166 tp->prev_ns = now_ns;
167 strbuf_init(&tp->display, 0);
cf84d51c
NP
168 return;
169 }
53ed7b5a 170 tp->curr_total = total;
cf84d51c 171
83d26fa7
KB
172 /* only update throughput every 0.5 s */
173 if (now_ns - tp->prev_ns <= 500000000)
174 return;
175
cf84d51c 176 /*
83d26fa7 177 * We have x = bytes and y = nanosecs. We want z = KiB/s:
cf84d51c 178 *
83d26fa7
KB
179 * z = (x / 1024) / (y / 1000000000)
180 * z = x / y * 1000000000 / 1024
181 * z = x / (y * 1024 / 1000000000)
cf84d51c
NP
182 * z = x / y'
183 *
184 * To simplify things we'll keep track of misecs, or 1024th of a sec
185 * obtained with:
186 *
83d26fa7
KB
187 * y' = y * 1024 / 1000000000
188 * y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
189 * y' = y / 2^32 * 4398
190 * y' = (y * 4398) >> 32
cf84d51c 191 */
83d26fa7
KB
192 misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
193
194 count = total - tp->prev_total;
195 tp->prev_total = total;
196 tp->prev_ns = now_ns;
197 tp->avg_bytes += count;
198 tp->avg_misecs += misecs;
199 rate = tp->avg_bytes / tp->avg_misecs;
200 tp->avg_bytes -= tp->last_bytes[tp->idx];
201 tp->avg_misecs -= tp->last_misecs[tp->idx];
202 tp->last_bytes[tp->idx] = count;
203 tp->last_misecs[tp->idx] = misecs;
204 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
205
3131977d 206 throughput_string(&tp->display, total, rate);
83d26fa7
KB
207 if (progress->last_value != -1 && progress_update)
208 display(progress, progress->last_value, NULL);
cf84d51c
NP
209}
210
9219d127 211void display_progress(struct progress *progress, uint64_t n)
96a02f8f 212{
9219d127
SG
213 if (progress)
214 display(progress, n, NULL);
96a02f8f
NP
215}
216
d6861d02 217static struct progress *start_progress_delay(const char *title, uint64_t total,
9d81ecb5 218 unsigned delay, unsigned sparse)
180a9f22 219{
999b951b 220 struct progress *progress = xmalloc(sizeof(*progress));
42e18fbf 221 progress->title = title;
180a9f22 222 progress->total = total;
42e18fbf 223 progress->last_value = -1;
180a9f22 224 progress->last_percent = -1;
180a9f22 225 progress->delay = delay;
9d81ecb5 226 progress->sparse = sparse;
cf84d51c 227 progress->throughput = NULL;
0fae1e07 228 progress->start_ns = getnanotime();
d53ba841 229 strbuf_init(&progress->counters_sb, 0);
545dc345
SG
230 progress->title_len = utf8_strwidth(title);
231 progress->split = 0;
180a9f22 232 set_progress_signal();
dc6a0757 233 return progress;
180a9f22
NP
234}
235
d6861d02 236struct progress *start_delayed_progress(const char *title, uint64_t total)
8aade107 237{
9d81ecb5 238 return start_progress_delay(title, total, 2, 0);
8aade107
JH
239}
240
d6861d02 241struct progress *start_progress(const char *title, uint64_t total)
42e18fbf 242{
9d81ecb5
JH
243 return start_progress_delay(title, total, 0, 0);
244}
245
246/*
247 * Here "sparse" means that the caller might use some sampling criteria to
248 * decide when to call display_progress() rather than calling it for every
249 * integer value in[0 .. total). In particular, the caller might not call
250 * display_progress() for the last value in the range.
251 *
252 * When "sparse" is set, stop_progress() will automatically force the done
253 * message to show 100%.
254 */
255struct progress *start_sparse_progress(const char *title, uint64_t total)
256{
257 return start_progress_delay(title, total, 0, 1);
258}
259
260struct progress *start_delayed_sparse_progress(const char *title,
261 uint64_t total)
262{
263 return start_progress_delay(title, total, 2, 1);
264}
265
266static void finish_if_sparse(struct progress *progress)
267{
268 if (progress &&
269 progress->sparse &&
270 progress->last_value != progress->total)
271 display_progress(progress, progress->total);
42e18fbf
NP
272}
273
dc6a0757 274void stop_progress(struct progress **p_progress)
a984a06a 275{
9d81ecb5
JH
276 finish_if_sparse(*p_progress);
277
754dbc43 278 stop_progress_msg(p_progress, _("done"));
a984a06a
NP
279}
280
281void stop_progress_msg(struct progress **p_progress, const char *msg)
96a02f8f 282{
dc6a0757
NP
283 struct progress *progress = *p_progress;
284 if (!progress)
285 return;
286 *p_progress = NULL;
42e18fbf
NP
287 if (progress->last_value != -1) {
288 /* Force the last update */
fbd09439 289 char *buf;
53ed7b5a 290 struct throughput *tp = progress->throughput;
d4c44443 291
53ed7b5a 292 if (tp) {
0fae1e07
RS
293 uint64_t now_ns = getnanotime();
294 unsigned int misecs, rate;
295 misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
296 rate = tp->curr_total / (misecs ? misecs : 1);
3131977d 297 throughput_string(&tp->display, tp->curr_total, rate);
53ed7b5a 298 }
42e18fbf 299 progress_update = 1;
fbd09439
MM
300 buf = xstrfmt(", %s.\n", msg);
301 display(progress, progress->last_value, buf);
302 free(buf);
42e18fbf 303 }
96a02f8f 304 clear_progress_signal();
d53ba841 305 strbuf_release(&progress->counters_sb);
3131977d
JK
306 if (progress->throughput)
307 strbuf_release(&progress->throughput->display);
cf84d51c 308 free(progress->throughput);
dc6a0757 309 free(progress);
96a02f8f 310}