]>
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; | |
cf84d51c | 38 | struct throughput *throughput; |
0fae1e07 | 39 | uint64_t start_ns; |
d53ba841 | 40 | struct strbuf counters_sb; |
545dc345 SG |
41 | int title_len; |
42 | int split; | |
dc6a0757 NP |
43 | }; |
44 | ||
96a02f8f NP |
45 | static volatile sig_atomic_t progress_update; |
46 | ||
47 | static void progress_interval(int signum) | |
48 | { | |
49 | progress_update = 1; | |
50 | } | |
51 | ||
52 | static void set_progress_signal(void) | |
53 | { | |
54 | struct sigaction sa; | |
55 | struct itimerval v; | |
56 | ||
180a9f22 NP |
57 | progress_update = 0; |
58 | ||
96a02f8f NP |
59 | memset(&sa, 0, sizeof(sa)); |
60 | sa.sa_handler = progress_interval; | |
61 | sigemptyset(&sa.sa_mask); | |
62 | sa.sa_flags = SA_RESTART; | |
63 | sigaction(SIGALRM, &sa, NULL); | |
64 | ||
65 | v.it_interval.tv_sec = 1; | |
66 | v.it_interval.tv_usec = 0; | |
67 | v.it_value = v.it_interval; | |
68 | setitimer(ITIMER_REAL, &v, NULL); | |
69 | } | |
70 | ||
71 | static void clear_progress_signal(void) | |
72 | { | |
73 | struct itimerval v = {{0,},}; | |
74 | setitimer(ITIMER_REAL, &v, NULL); | |
75 | signal(SIGALRM, SIG_IGN); | |
76 | progress_update = 0; | |
77 | } | |
78 | ||
85cb8906 LM |
79 | static int is_foreground_fd(int fd) |
80 | { | |
a4fb76ce JK |
81 | int tpgrp = tcgetpgrp(fd); |
82 | return tpgrp < 0 || tpgrp == getpgid(0); | |
85cb8906 LM |
83 | } |
84 | ||
9219d127 | 85 | static void display(struct progress *progress, uint64_t n, const char *done) |
96a02f8f | 86 | { |
d53ba841 SG |
87 | const char *tp; |
88 | struct strbuf *counters_sb = &progress->counters_sb; | |
89 | int show_update = 0; | |
9f1fd84e | 90 | int last_count_len = counters_sb->len; |
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 SG |
117 | const char *eol = done ? done : "\r"; |
118 | size_t clear_len = counters_sb->len < last_count_len ? | |
119 | last_count_len - counters_sb->len + 1 : | |
120 | 0; | |
545dc345 SG |
121 | size_t progress_line_len = progress->title_len + |
122 | counters_sb->len + 2; | |
123 | int cols = term_columns(); | |
124 | ||
125 | if (progress->split) { | |
126 | fprintf(stderr, " %s%*s", counters_sb->buf, | |
127 | (int) clear_len, eol); | |
128 | } else if (!done && cols < progress_line_len) { | |
129 | clear_len = progress->title_len + 1 < cols ? | |
1aed1a5f | 130 | cols - progress->title_len - 1 : 0; |
545dc345 SG |
131 | fprintf(stderr, "%s:%*s\n %s%s", |
132 | progress->title, (int) clear_len, "", | |
133 | counters_sb->buf, eol); | |
134 | progress->split = 1; | |
135 | } else { | |
136 | fprintf(stderr, "%s: %s%*s", progress->title, | |
137 | counters_sb->buf, (int) clear_len, eol); | |
138 | } | |
85cb8906 LM |
139 | fflush(stderr); |
140 | } | |
96a02f8f | 141 | progress_update = 0; |
96a02f8f | 142 | } |
96a02f8f NP |
143 | } |
144 | ||
d6861d02 | 145 | static void throughput_string(struct strbuf *buf, uint64_t total, |
53ed7b5a NP |
146 | unsigned int rate) |
147 | { | |
3131977d | 148 | strbuf_reset(buf); |
079b546a AP |
149 | strbuf_addstr(buf, ", "); |
150 | strbuf_humanise_bytes(buf, total); | |
151 | strbuf_addstr(buf, " | "); | |
152 | strbuf_humanise_bytes(buf, rate * 1024); | |
153 | strbuf_addstr(buf, "/s"); | |
53ed7b5a NP |
154 | } |
155 | ||
d6861d02 | 156 | void display_throughput(struct progress *progress, uint64_t total) |
cf84d51c NP |
157 | { |
158 | struct throughput *tp; | |
83d26fa7 KB |
159 | uint64_t now_ns; |
160 | unsigned int misecs, count, rate; | |
cf84d51c NP |
161 | |
162 | if (!progress) | |
163 | return; | |
164 | tp = progress->throughput; | |
165 | ||
83d26fa7 | 166 | now_ns = getnanotime(); |
cf84d51c NP |
167 | |
168 | if (!tp) { | |
169 | progress->throughput = tp = calloc(1, sizeof(*tp)); | |
218558af | 170 | if (tp) { |
53ed7b5a | 171 | tp->prev_total = tp->curr_total = total; |
83d26fa7 | 172 | tp->prev_ns = now_ns; |
3131977d | 173 | strbuf_init(&tp->display, 0); |
218558af | 174 | } |
cf84d51c NP |
175 | return; |
176 | } | |
53ed7b5a | 177 | tp->curr_total = total; |
cf84d51c | 178 | |
83d26fa7 KB |
179 | /* only update throughput every 0.5 s */ |
180 | if (now_ns - tp->prev_ns <= 500000000) | |
181 | return; | |
182 | ||
cf84d51c | 183 | /* |
83d26fa7 | 184 | * We have x = bytes and y = nanosecs. We want z = KiB/s: |
cf84d51c | 185 | * |
83d26fa7 KB |
186 | * z = (x / 1024) / (y / 1000000000) |
187 | * z = x / y * 1000000000 / 1024 | |
188 | * z = x / (y * 1024 / 1000000000) | |
cf84d51c NP |
189 | * z = x / y' |
190 | * | |
191 | * To simplify things we'll keep track of misecs, or 1024th of a sec | |
192 | * obtained with: | |
193 | * | |
83d26fa7 KB |
194 | * y' = y * 1024 / 1000000000 |
195 | * y' = y * (2^10 / 2^42) * (2^42 / 1000000000) | |
196 | * y' = y / 2^32 * 4398 | |
197 | * y' = (y * 4398) >> 32 | |
cf84d51c | 198 | */ |
83d26fa7 KB |
199 | misecs = ((now_ns - tp->prev_ns) * 4398) >> 32; |
200 | ||
201 | count = total - tp->prev_total; | |
202 | tp->prev_total = total; | |
203 | tp->prev_ns = now_ns; | |
204 | tp->avg_bytes += count; | |
205 | tp->avg_misecs += misecs; | |
206 | rate = tp->avg_bytes / tp->avg_misecs; | |
207 | tp->avg_bytes -= tp->last_bytes[tp->idx]; | |
208 | tp->avg_misecs -= tp->last_misecs[tp->idx]; | |
209 | tp->last_bytes[tp->idx] = count; | |
210 | tp->last_misecs[tp->idx] = misecs; | |
211 | tp->idx = (tp->idx + 1) % TP_IDX_MAX; | |
212 | ||
3131977d | 213 | throughput_string(&tp->display, total, rate); |
83d26fa7 KB |
214 | if (progress->last_value != -1 && progress_update) |
215 | display(progress, progress->last_value, NULL); | |
cf84d51c NP |
216 | } |
217 | ||
9219d127 | 218 | void display_progress(struct progress *progress, uint64_t n) |
96a02f8f | 219 | { |
9219d127 SG |
220 | if (progress) |
221 | display(progress, n, NULL); | |
96a02f8f NP |
222 | } |
223 | ||
d6861d02 | 224 | static struct progress *start_progress_delay(const char *title, uint64_t total, |
9c5951ca | 225 | unsigned delay) |
180a9f22 | 226 | { |
dc6a0757 NP |
227 | struct progress *progress = malloc(sizeof(*progress)); |
228 | if (!progress) { | |
229 | /* unlikely, but here's a good fallback */ | |
230 | fprintf(stderr, "%s...\n", title); | |
137a0d0e | 231 | fflush(stderr); |
dc6a0757 NP |
232 | return NULL; |
233 | } | |
42e18fbf | 234 | progress->title = title; |
180a9f22 | 235 | progress->total = total; |
42e18fbf | 236 | progress->last_value = -1; |
180a9f22 | 237 | progress->last_percent = -1; |
180a9f22 | 238 | progress->delay = delay; |
cf84d51c | 239 | progress->throughput = NULL; |
0fae1e07 | 240 | progress->start_ns = getnanotime(); |
d53ba841 | 241 | strbuf_init(&progress->counters_sb, 0); |
545dc345 SG |
242 | progress->title_len = utf8_strwidth(title); |
243 | progress->split = 0; | |
180a9f22 | 244 | set_progress_signal(); |
dc6a0757 | 245 | return progress; |
180a9f22 NP |
246 | } |
247 | ||
d6861d02 | 248 | struct progress *start_delayed_progress(const char *title, uint64_t total) |
8aade107 | 249 | { |
9c5951ca | 250 | return start_progress_delay(title, total, 2); |
8aade107 JH |
251 | } |
252 | ||
d6861d02 | 253 | struct progress *start_progress(const char *title, uint64_t total) |
42e18fbf | 254 | { |
9c5951ca | 255 | return start_progress_delay(title, total, 0); |
42e18fbf NP |
256 | } |
257 | ||
dc6a0757 | 258 | void stop_progress(struct progress **p_progress) |
a984a06a | 259 | { |
754dbc43 | 260 | stop_progress_msg(p_progress, _("done")); |
a984a06a NP |
261 | } |
262 | ||
263 | void stop_progress_msg(struct progress **p_progress, const char *msg) | |
96a02f8f | 264 | { |
dc6a0757 NP |
265 | struct progress *progress = *p_progress; |
266 | if (!progress) | |
267 | return; | |
268 | *p_progress = NULL; | |
42e18fbf NP |
269 | if (progress->last_value != -1) { |
270 | /* Force the last update */ | |
fbd09439 | 271 | char *buf; |
53ed7b5a | 272 | struct throughput *tp = progress->throughput; |
d4c44443 | 273 | |
53ed7b5a | 274 | if (tp) { |
0fae1e07 RS |
275 | uint64_t now_ns = getnanotime(); |
276 | unsigned int misecs, rate; | |
277 | misecs = ((now_ns - progress->start_ns) * 4398) >> 32; | |
278 | rate = tp->curr_total / (misecs ? misecs : 1); | |
3131977d | 279 | throughput_string(&tp->display, tp->curr_total, rate); |
53ed7b5a | 280 | } |
42e18fbf | 281 | progress_update = 1; |
fbd09439 MM |
282 | buf = xstrfmt(", %s.\n", msg); |
283 | display(progress, progress->last_value, buf); | |
284 | free(buf); | |
42e18fbf | 285 | } |
96a02f8f | 286 | clear_progress_signal(); |
d53ba841 | 287 | strbuf_release(&progress->counters_sb); |
3131977d JK |
288 | if (progress->throughput) |
289 | strbuf_release(&progress->throughput->display); | |
cf84d51c | 290 | free(progress->throughput); |
dc6a0757 | 291 | free(progress); |
96a02f8f | 292 | } |