]>
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 | ||
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 | ||
20 | struct 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 |
32 | struct 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 |
46 | static volatile sig_atomic_t progress_update; |
47 | ||
2bb74b53 SG |
48 | /* |
49 | * These are only intended for testing the progress output, i.e. exclusively | |
50 | * for 'test-tool progress'. | |
51 | */ | |
52 | int progress_testing; | |
53 | uint64_t progress_test_ns = 0; | |
54 | void progress_test_force_update(void); /* To silence -Wmissing-prototypes */ | |
55 | void progress_test_force_update(void) | |
56 | { | |
57 | progress_update = 1; | |
58 | } | |
59 | ||
60 | ||
96a02f8f NP |
61 | static void progress_interval(int signum) |
62 | { | |
63 | progress_update = 1; | |
64 | } | |
65 | ||
66 | static void set_progress_signal(void) | |
67 | { | |
68 | struct sigaction sa; | |
69 | struct itimerval v; | |
70 | ||
2bb74b53 SG |
71 | if (progress_testing) |
72 | return; | |
73 | ||
180a9f22 NP |
74 | progress_update = 0; |
75 | ||
96a02f8f NP |
76 | 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 | ||
88 | static void clear_progress_signal(void) | |
89 | { | |
90 | struct itimerval v = {{0,},}; | |
2bb74b53 SG |
91 | |
92 | if (progress_testing) | |
93 | return; | |
94 | ||
96a02f8f NP |
95 | setitimer(ITIMER_REAL, &v, NULL); |
96 | signal(SIGALRM, SIG_IGN); | |
97 | progress_update = 0; | |
98 | } | |
99 | ||
85cb8906 LM |
100 | static int is_foreground_fd(int fd) |
101 | { | |
a4fb76ce JK |
102 | int tpgrp = tcgetpgrp(fd); |
103 | return tpgrp < 0 || tpgrp == getpgid(0); | |
85cb8906 LM |
104 | } |
105 | ||
9219d127 | 106 | static void display(struct progress *progress, uint64_t n, const char *done) |
96a02f8f | 107 | { |
d53ba841 SG |
108 | const char *tp; |
109 | struct strbuf *counters_sb = &progress->counters_sb; | |
110 | int show_update = 0; | |
bbf47568 | 111 | int last_count_len = counters_sb->len; |
42e18fbf | 112 | |
9c5951ca | 113 | if (progress->delay && (!progress_update || --progress->delay)) |
9219d127 | 114 | return; |
42e18fbf NP |
115 | |
116 | progress->last_value = n; | |
3131977d | 117 | tp = (progress->throughput) ? progress->throughput->display.buf : ""; |
96a02f8f NP |
118 | if (progress->total) { |
119 | unsigned percent = n * 100 / progress->total; | |
120 | if (percent != progress->last_percent || progress_update) { | |
121 | progress->last_percent = percent; | |
d53ba841 SG |
122 | |
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; | |
96a02f8f NP |
129 | } |
130 | } else if (progress_update) { | |
d53ba841 SG |
131 | 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) { | |
85cb8906 | 137 | if (is_foreground_fd(fileno(stderr)) || done) { |
9f1fd84e | 138 | const char *eol = done ? done : "\r"; |
bbf47568 SG |
139 | 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(); | |
545dc345 SG |
146 | |
147 | if (progress->split) { | |
bbf47568 SG |
148 | 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); | |
545dc345 SG |
156 | progress->split = 1; |
157 | } else { | |
bbf47568 SG |
158 | fprintf(stderr, "%s: %s%*s", progress->title, |
159 | counters_sb->buf, (int) clear_len, eol); | |
545dc345 | 160 | } |
85cb8906 LM |
161 | fflush(stderr); |
162 | } | |
96a02f8f | 163 | progress_update = 0; |
96a02f8f | 164 | } |
96a02f8f NP |
165 | } |
166 | ||
d6861d02 | 167 | static void throughput_string(struct strbuf *buf, uint64_t total, |
53ed7b5a NP |
168 | unsigned int rate) |
169 | { | |
3131977d | 170 | strbuf_reset(buf); |
079b546a AP |
171 | strbuf_addstr(buf, ", "); |
172 | strbuf_humanise_bytes(buf, total); | |
173 | strbuf_addstr(buf, " | "); | |
8f354a1f | 174 | strbuf_humanise_rate(buf, rate * 1024); |
53ed7b5a NP |
175 | } |
176 | ||
2bb74b53 SG |
177 | static 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 | ||
d6861d02 | 185 | void display_throughput(struct progress *progress, uint64_t total) |
cf84d51c NP |
186 | { |
187 | struct throughput *tp; | |
83d26fa7 KB |
188 | uint64_t now_ns; |
189 | unsigned int misecs, count, rate; | |
cf84d51c NP |
190 | |
191 | if (!progress) | |
192 | return; | |
193 | tp = progress->throughput; | |
194 | ||
2bb74b53 | 195 | now_ns = progress_getnanotime(progress); |
cf84d51c NP |
196 | |
197 | if (!tp) { | |
999b951b JK |
198 | 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); | |
cf84d51c NP |
202 | return; |
203 | } | |
53ed7b5a | 204 | tp->curr_total = total; |
cf84d51c | 205 | |
83d26fa7 KB |
206 | /* only update throughput every 0.5 s */ |
207 | if (now_ns - tp->prev_ns <= 500000000) | |
208 | return; | |
209 | ||
cf84d51c | 210 | /* |
83d26fa7 | 211 | * We have x = bytes and y = nanosecs. We want z = KiB/s: |
cf84d51c | 212 | * |
83d26fa7 KB |
213 | * z = (x / 1024) / (y / 1000000000) |
214 | * z = x / y * 1000000000 / 1024 | |
215 | * z = x / (y * 1024 / 1000000000) | |
cf84d51c NP |
216 | * z = x / y' |
217 | * | |
218 | * To simplify things we'll keep track of misecs, or 1024th of a sec | |
219 | * obtained with: | |
220 | * | |
83d26fa7 KB |
221 | * 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 | |
cf84d51c | 225 | */ |
83d26fa7 KB |
226 | misecs = ((now_ns - tp->prev_ns) * 4398) >> 32; |
227 | ||
228 | 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; | |
239 | ||
3131977d | 240 | throughput_string(&tp->display, total, rate); |
83d26fa7 KB |
241 | if (progress->last_value != -1 && progress_update) |
242 | display(progress, progress->last_value, NULL); | |
cf84d51c NP |
243 | } |
244 | ||
9219d127 | 245 | void display_progress(struct progress *progress, uint64_t n) |
96a02f8f | 246 | { |
9219d127 SG |
247 | if (progress) |
248 | display(progress, n, NULL); | |
96a02f8f NP |
249 | } |
250 | ||
d6861d02 | 251 | static struct progress *start_progress_delay(const char *title, uint64_t total, |
9d81ecb5 | 252 | unsigned delay, unsigned sparse) |
180a9f22 | 253 | { |
999b951b | 254 | struct progress *progress = xmalloc(sizeof(*progress)); |
42e18fbf | 255 | progress->title = title; |
180a9f22 | 256 | progress->total = total; |
42e18fbf | 257 | progress->last_value = -1; |
180a9f22 | 258 | progress->last_percent = -1; |
180a9f22 | 259 | progress->delay = delay; |
9d81ecb5 | 260 | progress->sparse = sparse; |
cf84d51c | 261 | progress->throughput = NULL; |
0fae1e07 | 262 | progress->start_ns = getnanotime(); |
d53ba841 | 263 | strbuf_init(&progress->counters_sb, 0); |
545dc345 SG |
264 | progress->title_len = utf8_strwidth(title); |
265 | progress->split = 0; | |
180a9f22 | 266 | set_progress_signal(); |
dc6a0757 | 267 | return progress; |
180a9f22 NP |
268 | } |
269 | ||
d6861d02 | 270 | struct progress *start_delayed_progress(const char *title, uint64_t total) |
8aade107 | 271 | { |
9d81ecb5 | 272 | return start_progress_delay(title, total, 2, 0); |
8aade107 JH |
273 | } |
274 | ||
d6861d02 | 275 | struct progress *start_progress(const char *title, uint64_t total) |
42e18fbf | 276 | { |
9d81ecb5 JH |
277 | 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 | */ | |
289 | struct progress *start_sparse_progress(const char *title, uint64_t total) | |
290 | { | |
291 | return start_progress_delay(title, total, 0, 1); | |
292 | } | |
293 | ||
294 | struct progress *start_delayed_sparse_progress(const char *title, | |
295 | uint64_t total) | |
296 | { | |
297 | return start_progress_delay(title, total, 2, 1); | |
298 | } | |
299 | ||
300 | static 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); | |
42e18fbf NP |
306 | } |
307 | ||
dc6a0757 | 308 | void stop_progress(struct progress **p_progress) |
a984a06a | 309 | { |
9d81ecb5 JH |
310 | finish_if_sparse(*p_progress); |
311 | ||
754dbc43 | 312 | stop_progress_msg(p_progress, _("done")); |
a984a06a NP |
313 | } |
314 | ||
315 | void stop_progress_msg(struct progress **p_progress, const char *msg) | |
96a02f8f | 316 | { |
dc6a0757 NP |
317 | struct progress *progress = *p_progress; |
318 | if (!progress) | |
319 | return; | |
320 | *p_progress = NULL; | |
42e18fbf NP |
321 | if (progress->last_value != -1) { |
322 | /* Force the last update */ | |
fbd09439 | 323 | char *buf; |
53ed7b5a | 324 | struct throughput *tp = progress->throughput; |
d4c44443 | 325 | |
53ed7b5a | 326 | if (tp) { |
2bb74b53 | 327 | uint64_t now_ns = progress_getnanotime(progress); |
0fae1e07 RS |
328 | unsigned int misecs, rate; |
329 | misecs = ((now_ns - progress->start_ns) * 4398) >> 32; | |
330 | rate = tp->curr_total / (misecs ? misecs : 1); | |
3131977d | 331 | throughput_string(&tp->display, tp->curr_total, rate); |
53ed7b5a | 332 | } |
42e18fbf | 333 | progress_update = 1; |
fbd09439 MM |
334 | buf = xstrfmt(", %s.\n", msg); |
335 | display(progress, progress->last_value, buf); | |
336 | free(buf); | |
42e18fbf | 337 | } |
96a02f8f | 338 | clear_progress_signal(); |
d53ba841 | 339 | strbuf_release(&progress->counters_sb); |
3131977d JK |
340 | if (progress->throughput) |
341 | strbuf_release(&progress->throughput->display); | |
cf84d51c | 342 | free(progress->throughput); |
dc6a0757 | 343 | free(progress); |
96a02f8f | 344 | } |