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