]>
Commit | Line | Data |
---|---|---|
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 |
77f091ed | 12 | #include "git-compat-util.h" |
ca4eed70 | 13 | #include "pager.h" |
96a02f8f | 14 | #include "progress.h" |
d1cbe1e6 | 15 | #include "repository.h" |
079b546a | 16 | #include "strbuf.h" |
83d26fa7 | 17 | #include "trace.h" |
74ea5c95 | 18 | #include "trace2.h" |
545dc345 | 19 | #include "utf8.h" |
44a4693b | 20 | #include "config.h" |
96a02f8f | 21 | |
cf84d51c NP |
22 | #define TP_IDX_MAX 8 |
23 | ||
24 | struct throughput { | |
53ed7b5a | 25 | off_t curr_total; |
218558af | 26 | off_t prev_total; |
83d26fa7 | 27 | uint64_t prev_ns; |
218558af | 28 | unsigned int avg_bytes; |
cf84d51c | 29 | unsigned int avg_misecs; |
53ed7b5a | 30 | unsigned int last_bytes[TP_IDX_MAX]; |
cf84d51c NP |
31 | unsigned int last_misecs[TP_IDX_MAX]; |
32 | unsigned int idx; | |
3131977d | 33 | struct strbuf display; |
cf84d51c NP |
34 | }; |
35 | ||
dc6a0757 NP |
36 | struct progress { |
37 | const char *title; | |
d6861d02 EN |
38 | uint64_t last_value; |
39 | uint64_t total; | |
dc6a0757 NP |
40 | unsigned last_percent; |
41 | unsigned delay; | |
9d81ecb5 | 42 | unsigned sparse; |
cf84d51c | 43 | struct throughput *throughput; |
0fae1e07 | 44 | uint64_t start_ns; |
d53ba841 | 45 | struct strbuf counters_sb; |
545dc345 SG |
46 | int title_len; |
47 | int split; | |
dc6a0757 NP |
48 | }; |
49 | ||
96a02f8f NP |
50 | static volatile sig_atomic_t progress_update; |
51 | ||
2bb74b53 SG |
52 | /* |
53 | * These are only intended for testing the progress output, i.e. exclusively | |
54 | * for 'test-tool progress'. | |
55 | */ | |
56 | int progress_testing; | |
57 | uint64_t progress_test_ns = 0; | |
2bb74b53 SG |
58 | void progress_test_force_update(void) |
59 | { | |
60 | progress_update = 1; | |
61 | } | |
62 | ||
63 | ||
9ec03b59 | 64 | static void progress_interval(int signum UNUSED) |
96a02f8f NP |
65 | { |
66 | progress_update = 1; | |
67 | } | |
68 | ||
69 | static void set_progress_signal(void) | |
70 | { | |
71 | struct sigaction sa; | |
72 | struct itimerval v; | |
73 | ||
2bb74b53 SG |
74 | if (progress_testing) |
75 | return; | |
76 | ||
180a9f22 NP |
77 | progress_update = 0; |
78 | ||
96a02f8f NP |
79 | memset(&sa, 0, sizeof(sa)); |
80 | sa.sa_handler = progress_interval; | |
81 | sigemptyset(&sa.sa_mask); | |
82 | sa.sa_flags = SA_RESTART; | |
83 | sigaction(SIGALRM, &sa, NULL); | |
84 | ||
85 | v.it_interval.tv_sec = 1; | |
86 | v.it_interval.tv_usec = 0; | |
87 | v.it_value = v.it_interval; | |
88 | setitimer(ITIMER_REAL, &v, NULL); | |
89 | } | |
90 | ||
91 | static void clear_progress_signal(void) | |
92 | { | |
93 | struct itimerval v = {{0,},}; | |
2bb74b53 SG |
94 | |
95 | if (progress_testing) | |
96 | return; | |
97 | ||
96a02f8f NP |
98 | setitimer(ITIMER_REAL, &v, NULL); |
99 | signal(SIGALRM, SIG_IGN); | |
100 | progress_update = 0; | |
101 | } | |
102 | ||
85cb8906 LM |
103 | static int is_foreground_fd(int fd) |
104 | { | |
a4fb76ce JK |
105 | int tpgrp = tcgetpgrp(fd); |
106 | return tpgrp < 0 || tpgrp == getpgid(0); | |
85cb8906 LM |
107 | } |
108 | ||
9219d127 | 109 | static void display(struct progress *progress, uint64_t n, const char *done) |
96a02f8f | 110 | { |
d53ba841 SG |
111 | const char *tp; |
112 | struct strbuf *counters_sb = &progress->counters_sb; | |
113 | int show_update = 0; | |
bbf47568 | 114 | int last_count_len = counters_sb->len; |
42e18fbf | 115 | |
9c5951ca | 116 | if (progress->delay && (!progress_update || --progress->delay)) |
9219d127 | 117 | return; |
42e18fbf NP |
118 | |
119 | progress->last_value = n; | |
3131977d | 120 | tp = (progress->throughput) ? progress->throughput->display.buf : ""; |
96a02f8f NP |
121 | if (progress->total) { |
122 | unsigned percent = n * 100 / progress->total; | |
123 | if (percent != progress->last_percent || progress_update) { | |
124 | progress->last_percent = percent; | |
d53ba841 SG |
125 | |
126 | strbuf_reset(counters_sb); | |
127 | strbuf_addf(counters_sb, | |
128 | "%3u%% (%"PRIuMAX"/%"PRIuMAX")%s", percent, | |
129 | (uintmax_t)n, (uintmax_t)progress->total, | |
130 | tp); | |
131 | show_update = 1; | |
96a02f8f NP |
132 | } |
133 | } else if (progress_update) { | |
d53ba841 SG |
134 | strbuf_reset(counters_sb); |
135 | strbuf_addf(counters_sb, "%"PRIuMAX"%s", (uintmax_t)n, tp); | |
136 | show_update = 1; | |
137 | } | |
138 | ||
139 | if (show_update) { | |
85cb8906 | 140 | if (is_foreground_fd(fileno(stderr)) || done) { |
9f1fd84e | 141 | const char *eol = done ? done : "\r"; |
bbf47568 SG |
142 | size_t clear_len = counters_sb->len < last_count_len ? |
143 | last_count_len - counters_sb->len + 1 : | |
144 | 0; | |
145 | /* The "+ 2" accounts for the ": ". */ | |
146 | size_t progress_line_len = progress->title_len + | |
147 | counters_sb->len + 2; | |
148 | int cols = term_columns(); | |
545dc345 SG |
149 | |
150 | if (progress->split) { | |
bbf47568 SG |
151 | fprintf(stderr, " %s%*s", counters_sb->buf, |
152 | (int) clear_len, eol); | |
153 | } else if (!done && cols < progress_line_len) { | |
154 | clear_len = progress->title_len + 1 < cols ? | |
155 | cols - progress->title_len - 1 : 0; | |
156 | fprintf(stderr, "%s:%*s\n %s%s", | |
157 | progress->title, (int) clear_len, "", | |
158 | counters_sb->buf, eol); | |
545dc345 SG |
159 | progress->split = 1; |
160 | } else { | |
bbf47568 SG |
161 | fprintf(stderr, "%s: %s%*s", progress->title, |
162 | counters_sb->buf, (int) clear_len, eol); | |
545dc345 | 163 | } |
85cb8906 LM |
164 | fflush(stderr); |
165 | } | |
96a02f8f | 166 | progress_update = 0; |
96a02f8f | 167 | } |
96a02f8f NP |
168 | } |
169 | ||
d6861d02 | 170 | static void throughput_string(struct strbuf *buf, uint64_t total, |
53ed7b5a NP |
171 | unsigned int rate) |
172 | { | |
3131977d | 173 | strbuf_reset(buf); |
079b546a AP |
174 | strbuf_addstr(buf, ", "); |
175 | strbuf_humanise_bytes(buf, total); | |
176 | strbuf_addstr(buf, " | "); | |
8f354a1f | 177 | strbuf_humanise_rate(buf, rate * 1024); |
53ed7b5a NP |
178 | } |
179 | ||
2bb74b53 SG |
180 | static uint64_t progress_getnanotime(struct progress *progress) |
181 | { | |
182 | if (progress_testing) | |
183 | return progress->start_ns + progress_test_ns; | |
184 | else | |
185 | return getnanotime(); | |
186 | } | |
187 | ||
d6861d02 | 188 | void display_throughput(struct progress *progress, uint64_t total) |
cf84d51c NP |
189 | { |
190 | struct throughput *tp; | |
83d26fa7 KB |
191 | uint64_t now_ns; |
192 | unsigned int misecs, count, rate; | |
cf84d51c NP |
193 | |
194 | if (!progress) | |
195 | return; | |
196 | tp = progress->throughput; | |
197 | ||
2bb74b53 | 198 | now_ns = progress_getnanotime(progress); |
cf84d51c NP |
199 | |
200 | if (!tp) { | |
ca56dadb | 201 | progress->throughput = CALLOC_ARRAY(tp, 1); |
999b951b JK |
202 | tp->prev_total = tp->curr_total = total; |
203 | tp->prev_ns = now_ns; | |
204 | strbuf_init(&tp->display, 0); | |
cf84d51c NP |
205 | return; |
206 | } | |
53ed7b5a | 207 | tp->curr_total = total; |
cf84d51c | 208 | |
83d26fa7 KB |
209 | /* only update throughput every 0.5 s */ |
210 | if (now_ns - tp->prev_ns <= 500000000) | |
211 | return; | |
212 | ||
cf84d51c | 213 | /* |
83d26fa7 | 214 | * We have x = bytes and y = nanosecs. We want z = KiB/s: |
cf84d51c | 215 | * |
83d26fa7 KB |
216 | * z = (x / 1024) / (y / 1000000000) |
217 | * z = x / y * 1000000000 / 1024 | |
218 | * z = x / (y * 1024 / 1000000000) | |
cf84d51c NP |
219 | * z = x / y' |
220 | * | |
221 | * To simplify things we'll keep track of misecs, or 1024th of a sec | |
222 | * obtained with: | |
223 | * | |
83d26fa7 KB |
224 | * y' = y * 1024 / 1000000000 |
225 | * y' = y * (2^10 / 2^42) * (2^42 / 1000000000) | |
226 | * y' = y / 2^32 * 4398 | |
227 | * y' = (y * 4398) >> 32 | |
cf84d51c | 228 | */ |
83d26fa7 KB |
229 | misecs = ((now_ns - tp->prev_ns) * 4398) >> 32; |
230 | ||
231 | count = total - tp->prev_total; | |
232 | tp->prev_total = total; | |
233 | tp->prev_ns = now_ns; | |
234 | tp->avg_bytes += count; | |
235 | tp->avg_misecs += misecs; | |
236 | rate = tp->avg_bytes / tp->avg_misecs; | |
237 | tp->avg_bytes -= tp->last_bytes[tp->idx]; | |
238 | tp->avg_misecs -= tp->last_misecs[tp->idx]; | |
239 | tp->last_bytes[tp->idx] = count; | |
240 | tp->last_misecs[tp->idx] = misecs; | |
241 | tp->idx = (tp->idx + 1) % TP_IDX_MAX; | |
242 | ||
3131977d | 243 | throughput_string(&tp->display, total, rate); |
83d26fa7 KB |
244 | if (progress->last_value != -1 && progress_update) |
245 | display(progress, progress->last_value, NULL); | |
cf84d51c NP |
246 | } |
247 | ||
9219d127 | 248 | void display_progress(struct progress *progress, uint64_t n) |
96a02f8f | 249 | { |
9219d127 SG |
250 | if (progress) |
251 | display(progress, n, NULL); | |
96a02f8f NP |
252 | } |
253 | ||
d6861d02 | 254 | static struct progress *start_progress_delay(const char *title, uint64_t total, |
9d81ecb5 | 255 | unsigned delay, unsigned sparse) |
180a9f22 | 256 | { |
999b951b | 257 | struct progress *progress = xmalloc(sizeof(*progress)); |
42e18fbf | 258 | progress->title = title; |
180a9f22 | 259 | progress->total = total; |
42e18fbf | 260 | progress->last_value = -1; |
180a9f22 | 261 | progress->last_percent = -1; |
180a9f22 | 262 | progress->delay = delay; |
9d81ecb5 | 263 | progress->sparse = sparse; |
cf84d51c | 264 | progress->throughput = NULL; |
0fae1e07 | 265 | progress->start_ns = getnanotime(); |
d53ba841 | 266 | strbuf_init(&progress->counters_sb, 0); |
545dc345 SG |
267 | progress->title_len = utf8_strwidth(title); |
268 | progress->split = 0; | |
180a9f22 | 269 | set_progress_signal(); |
98a13647 | 270 | trace2_region_enter("progress", title, the_repository); |
dc6a0757 | 271 | return progress; |
180a9f22 NP |
272 | } |
273 | ||
44a4693b DS |
274 | static int get_default_delay(void) |
275 | { | |
276 | static int delay_in_secs = -1; | |
277 | ||
278 | if (delay_in_secs < 0) | |
279 | delay_in_secs = git_env_ulong("GIT_PROGRESS_DELAY", 2); | |
280 | ||
281 | return delay_in_secs; | |
282 | } | |
283 | ||
d6861d02 | 284 | struct progress *start_delayed_progress(const char *title, uint64_t total) |
8aade107 | 285 | { |
44a4693b | 286 | return start_progress_delay(title, total, get_default_delay(), 0); |
8aade107 JH |
287 | } |
288 | ||
d6861d02 | 289 | struct progress *start_progress(const char *title, uint64_t total) |
42e18fbf | 290 | { |
9d81ecb5 JH |
291 | return start_progress_delay(title, total, 0, 0); |
292 | } | |
293 | ||
294 | /* | |
295 | * Here "sparse" means that the caller might use some sampling criteria to | |
296 | * decide when to call display_progress() rather than calling it for every | |
297 | * integer value in[0 .. total). In particular, the caller might not call | |
298 | * display_progress() for the last value in the range. | |
299 | * | |
300 | * When "sparse" is set, stop_progress() will automatically force the done | |
301 | * message to show 100%. | |
302 | */ | |
303 | struct progress *start_sparse_progress(const char *title, uint64_t total) | |
304 | { | |
305 | return start_progress_delay(title, total, 0, 1); | |
306 | } | |
307 | ||
308 | struct progress *start_delayed_sparse_progress(const char *title, | |
309 | uint64_t total) | |
310 | { | |
44a4693b | 311 | return start_progress_delay(title, total, get_default_delay(), 1); |
9d81ecb5 JH |
312 | } |
313 | ||
314 | static void finish_if_sparse(struct progress *progress) | |
315 | { | |
74900a6b | 316 | if (progress->sparse && |
9d81ecb5 JH |
317 | progress->last_value != progress->total) |
318 | display_progress(progress, progress->total); | |
42e18fbf NP |
319 | } |
320 | ||
accf1eb1 ÆAB |
321 | static void force_last_update(struct progress *progress, const char *msg) |
322 | { | |
323 | char *buf; | |
324 | struct throughput *tp = progress->throughput; | |
325 | ||
326 | if (tp) { | |
327 | uint64_t now_ns = progress_getnanotime(progress); | |
328 | unsigned int misecs, rate; | |
329 | misecs = ((now_ns - progress->start_ns) * 4398) >> 32; | |
330 | rate = tp->curr_total / (misecs ? misecs : 1); | |
331 | throughput_string(&tp->display, tp->curr_total, rate); | |
332 | } | |
333 | progress_update = 1; | |
334 | buf = xstrfmt(", %s.\n", msg); | |
335 | display(progress, progress->last_value, buf); | |
336 | free(buf); | |
337 | } | |
338 | ||
339 | static void log_trace2(struct progress *progress) | |
340 | { | |
341 | trace2_data_intmax("progress", the_repository, "total_objects", | |
342 | progress->total); | |
343 | ||
344 | if (progress->throughput) | |
345 | trace2_data_intmax("progress", the_repository, "total_bytes", | |
346 | progress->throughput->curr_total); | |
347 | ||
348 | trace2_region_leave("progress", progress->title, the_repository); | |
349 | } | |
350 | ||
a984a06a | 351 | void stop_progress_msg(struct progress **p_progress, const char *msg) |
96a02f8f | 352 | { |
ac900fdd MÅ |
353 | struct progress *progress; |
354 | ||
355 | if (!p_progress) | |
356 | BUG("don't provide NULL to stop_progress_msg"); | |
357 | ||
358 | progress = *p_progress; | |
dc6a0757 NP |
359 | if (!progress) |
360 | return; | |
361 | *p_progress = NULL; | |
accf1eb1 | 362 | |
74900a6b | 363 | finish_if_sparse(progress); |
accf1eb1 ÆAB |
364 | if (progress->last_value != -1) |
365 | force_last_update(progress, msg); | |
74900a6b | 366 | log_trace2(progress); |
accf1eb1 | 367 | |
96a02f8f | 368 | clear_progress_signal(); |
d53ba841 | 369 | strbuf_release(&progress->counters_sb); |
3131977d JK |
370 | if (progress->throughput) |
371 | strbuf_release(&progress->throughput->display); | |
cf84d51c | 372 | free(progress->throughput); |
dc6a0757 | 373 | free(progress); |
96a02f8f | 374 | } |