]> git.ipfire.org Git - thirdparty/git.git/blame - progress.c
progress.c: refactor stop_progress{,_msg}() to use helpers
[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
3cacb9aa 11#define GIT_TEST_PROGRESS_ONLY
545dc345 12#include "cache.h"
754dbc43 13#include "gettext.h"
96a02f8f 14#include "progress.h"
079b546a 15#include "strbuf.h"
83d26fa7 16#include "trace.h"
545dc345 17#include "utf8.h"
44a4693b 18#include "config.h"
96a02f8f 19
cf84d51c
NP
20#define TP_IDX_MAX 8
21
22struct throughput {
53ed7b5a 23 off_t curr_total;
218558af 24 off_t prev_total;
83d26fa7 25 uint64_t prev_ns;
218558af 26 unsigned int avg_bytes;
cf84d51c 27 unsigned int avg_misecs;
53ed7b5a 28 unsigned int last_bytes[TP_IDX_MAX];
cf84d51c
NP
29 unsigned int last_misecs[TP_IDX_MAX];
30 unsigned int idx;
3131977d 31 struct strbuf display;
cf84d51c
NP
32};
33
dc6a0757
NP
34struct progress {
35 const char *title;
d6861d02
EN
36 uint64_t last_value;
37 uint64_t total;
dc6a0757
NP
38 unsigned last_percent;
39 unsigned delay;
9d81ecb5 40 unsigned sparse;
cf84d51c 41 struct throughput *throughput;
0fae1e07 42 uint64_t start_ns;
d53ba841 43 struct strbuf counters_sb;
545dc345
SG
44 int title_len;
45 int split;
dc6a0757
NP
46};
47
96a02f8f
NP
48static volatile sig_atomic_t progress_update;
49
2bb74b53
SG
50/*
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;
2bb74b53
SG
56void progress_test_force_update(void)
57{
58 progress_update = 1;
59}
60
61
96a02f8f
NP
62static 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
2bb74b53
SG
72 if (progress_testing)
73 return;
74
180a9f22
NP
75 progress_update = 0;
76
96a02f8f
NP
77 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,},};
2bb74b53
SG
92
93 if (progress_testing)
94 return;
95
96a02f8f
NP
96 setitimer(ITIMER_REAL, &v, NULL);
97 signal(SIGALRM, SIG_IGN);
98 progress_update = 0;
99}
100
85cb8906
LM
101static int is_foreground_fd(int fd)
102{
a4fb76ce
JK
103 int tpgrp = tcgetpgrp(fd);
104 return tpgrp < 0 || tpgrp == getpgid(0);
85cb8906
LM
105}
106
9219d127 107static void display(struct progress *progress, uint64_t n, const char *done)
96a02f8f 108{
d53ba841
SG
109 const char *tp;
110 struct strbuf *counters_sb = &progress->counters_sb;
111 int show_update = 0;
bbf47568 112 int last_count_len = counters_sb->len;
42e18fbf 113
9c5951ca 114 if (progress->delay && (!progress_update || --progress->delay))
9219d127 115 return;
42e18fbf
NP
116
117 progress->last_value = n;
3131977d 118 tp = (progress->throughput) ? progress->throughput->display.buf : "";
96a02f8f
NP
119 if (progress->total) {
120 unsigned percent = n * 100 / progress->total;
121 if (percent != progress->last_percent || progress_update) {
122 progress->last_percent = percent;
d53ba841
SG
123
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;
96a02f8f
NP
130 }
131 } else if (progress_update) {
d53ba841
SG
132 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) {
85cb8906 138 if (is_foreground_fd(fileno(stderr)) || done) {
9f1fd84e 139 const char *eol = done ? done : "\r";
bbf47568
SG
140 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();
545dc345
SG
147
148 if (progress->split) {
bbf47568
SG
149 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);
545dc345
SG
157 progress->split = 1;
158 } else {
bbf47568
SG
159 fprintf(stderr, "%s: %s%*s", progress->title,
160 counters_sb->buf, (int) clear_len, eol);
545dc345 161 }
85cb8906
LM
162 fflush(stderr);
163 }
96a02f8f 164 progress_update = 0;
96a02f8f 165 }
96a02f8f
NP
166}
167
d6861d02 168static void throughput_string(struct strbuf *buf, uint64_t total,
53ed7b5a
NP
169 unsigned int rate)
170{
3131977d 171 strbuf_reset(buf);
079b546a
AP
172 strbuf_addstr(buf, ", ");
173 strbuf_humanise_bytes(buf, total);
174 strbuf_addstr(buf, " | ");
8f354a1f 175 strbuf_humanise_rate(buf, rate * 1024);
53ed7b5a
NP
176}
177
2bb74b53
SG
178static 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
d6861d02 186void display_throughput(struct progress *progress, uint64_t total)
cf84d51c
NP
187{
188 struct throughput *tp;
83d26fa7
KB
189 uint64_t now_ns;
190 unsigned int misecs, count, rate;
cf84d51c
NP
191
192 if (!progress)
193 return;
194 tp = progress->throughput;
195
2bb74b53 196 now_ns = progress_getnanotime(progress);
cf84d51c
NP
197
198 if (!tp) {
ca56dadb 199 progress->throughput = CALLOC_ARRAY(tp, 1);
999b951b
JK
200 tp->prev_total = tp->curr_total = total;
201 tp->prev_ns = now_ns;
202 strbuf_init(&tp->display, 0);
cf84d51c
NP
203 return;
204 }
53ed7b5a 205 tp->curr_total = total;
cf84d51c 206
83d26fa7
KB
207 /* only update throughput every 0.5 s */
208 if (now_ns - tp->prev_ns <= 500000000)
209 return;
210
cf84d51c 211 /*
83d26fa7 212 * We have x = bytes and y = nanosecs. We want z = KiB/s:
cf84d51c 213 *
83d26fa7
KB
214 * z = (x / 1024) / (y / 1000000000)
215 * z = x / y * 1000000000 / 1024
216 * z = x / (y * 1024 / 1000000000)
cf84d51c
NP
217 * z = x / y'
218 *
219 * To simplify things we'll keep track of misecs, or 1024th of a sec
220 * obtained with:
221 *
83d26fa7
KB
222 * 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
cf84d51c 226 */
83d26fa7
KB
227 misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
228
229 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;
240
3131977d 241 throughput_string(&tp->display, total, rate);
83d26fa7
KB
242 if (progress->last_value != -1 && progress_update)
243 display(progress, progress->last_value, NULL);
cf84d51c
NP
244}
245
9219d127 246void display_progress(struct progress *progress, uint64_t n)
96a02f8f 247{
9219d127
SG
248 if (progress)
249 display(progress, n, NULL);
96a02f8f
NP
250}
251
d6861d02 252static struct progress *start_progress_delay(const char *title, uint64_t total,
9d81ecb5 253 unsigned delay, unsigned sparse)
180a9f22 254{
999b951b 255 struct progress *progress = xmalloc(sizeof(*progress));
42e18fbf 256 progress->title = title;
180a9f22 257 progress->total = total;
42e18fbf 258 progress->last_value = -1;
180a9f22 259 progress->last_percent = -1;
180a9f22 260 progress->delay = delay;
9d81ecb5 261 progress->sparse = sparse;
cf84d51c 262 progress->throughput = NULL;
0fae1e07 263 progress->start_ns = getnanotime();
d53ba841 264 strbuf_init(&progress->counters_sb, 0);
545dc345
SG
265 progress->title_len = utf8_strwidth(title);
266 progress->split = 0;
180a9f22 267 set_progress_signal();
98a13647 268 trace2_region_enter("progress", title, the_repository);
dc6a0757 269 return progress;
180a9f22
NP
270}
271
44a4693b
DS
272static 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
d6861d02 282struct progress *start_delayed_progress(const char *title, uint64_t total)
8aade107 283{
44a4693b 284 return start_progress_delay(title, total, get_default_delay(), 0);
8aade107
JH
285}
286
d6861d02 287struct progress *start_progress(const char *title, uint64_t total)
42e18fbf 288{
9d81ecb5
JH
289 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{
44a4693b 309 return start_progress_delay(title, total, get_default_delay(), 1);
9d81ecb5
JH
310}
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);
42e18fbf
NP
318}
319
accf1eb1
ÆAB
320static void force_last_update(struct progress *progress, const char *msg)
321{
322 char *buf;
323 struct throughput *tp = progress->throughput;
324
325 if (tp) {
326 uint64_t now_ns = progress_getnanotime(progress);
327 unsigned int misecs, rate;
328 misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
329 rate = tp->curr_total / (misecs ? misecs : 1);
330 throughput_string(&tp->display, tp->curr_total, rate);
331 }
332 progress_update = 1;
333 buf = xstrfmt(", %s.\n", msg);
334 display(progress, progress->last_value, buf);
335 free(buf);
336}
337
338static void log_trace2(struct progress *progress)
339{
340 trace2_data_intmax("progress", the_repository, "total_objects",
341 progress->total);
342
343 if (progress->throughput)
344 trace2_data_intmax("progress", the_repository, "total_bytes",
345 progress->throughput->curr_total);
346
347 trace2_region_leave("progress", progress->title, the_repository);
348}
349
dc6a0757 350void stop_progress(struct progress **p_progress)
a984a06a 351{
1ccad6a1
ÆAB
352 struct progress *progress;
353
ac900fdd
354 if (!p_progress)
355 BUG("don't provide NULL to stop_progress");
1ccad6a1 356 progress = *p_progress;
ac900fdd 357
1ccad6a1 358 finish_if_sparse(progress);
9d81ecb5 359
accf1eb1
ÆAB
360 if (progress)
361 log_trace2(*p_progress);
98a13647 362
754dbc43 363 stop_progress_msg(p_progress, _("done"));
a984a06a
NP
364}
365
366void stop_progress_msg(struct progress **p_progress, const char *msg)
96a02f8f 367{
ac900fdd
368 struct progress *progress;
369
370 if (!p_progress)
371 BUG("don't provide NULL to stop_progress_msg");
372
373 progress = *p_progress;
dc6a0757
NP
374 if (!progress)
375 return;
376 *p_progress = NULL;
accf1eb1
ÆAB
377
378 if (progress->last_value != -1)
379 force_last_update(progress, msg);
380
96a02f8f 381 clear_progress_signal();
d53ba841 382 strbuf_release(&progress->counters_sb);
3131977d
JK
383 if (progress->throughput)
384 strbuf_release(&progress->throughput->display);
cf84d51c 385 free(progress->throughput);
dc6a0757 386 free(progress);
96a02f8f 387}