]>
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 | ||
96a02f8f | 11 | #include "git-compat-util.h" |
754dbc43 | 12 | #include "gettext.h" |
96a02f8f | 13 | #include "progress.h" |
079b546a | 14 | #include "strbuf.h" |
83d26fa7 | 15 | #include "trace.h" |
96a02f8f | 16 | |
cf84d51c NP |
17 | #define TP_IDX_MAX 8 |
18 | ||
19 | struct throughput { | |
53ed7b5a | 20 | off_t curr_total; |
218558af | 21 | off_t prev_total; |
83d26fa7 | 22 | uint64_t prev_ns; |
218558af | 23 | unsigned int avg_bytes; |
cf84d51c | 24 | unsigned int avg_misecs; |
53ed7b5a | 25 | unsigned int last_bytes[TP_IDX_MAX]; |
cf84d51c NP |
26 | unsigned int last_misecs[TP_IDX_MAX]; |
27 | unsigned int idx; | |
3131977d | 28 | struct strbuf display; |
cf84d51c NP |
29 | }; |
30 | ||
dc6a0757 NP |
31 | struct progress { |
32 | const char *title; | |
d6861d02 EN |
33 | uint64_t last_value; |
34 | uint64_t total; | |
dc6a0757 NP |
35 | unsigned last_percent; |
36 | unsigned delay; | |
9d81ecb5 | 37 | unsigned sparse; |
cf84d51c | 38 | struct throughput *throughput; |
0fae1e07 | 39 | uint64_t start_ns; |
dc6a0757 NP |
40 | }; |
41 | ||
96a02f8f NP |
42 | static volatile sig_atomic_t progress_update; |
43 | ||
44 | static void progress_interval(int signum) | |
45 | { | |
46 | progress_update = 1; | |
47 | } | |
48 | ||
49 | static void set_progress_signal(void) | |
50 | { | |
51 | struct sigaction sa; | |
52 | struct itimerval v; | |
53 | ||
180a9f22 NP |
54 | progress_update = 0; |
55 | ||
96a02f8f NP |
56 | memset(&sa, 0, sizeof(sa)); |
57 | sa.sa_handler = progress_interval; | |
58 | sigemptyset(&sa.sa_mask); | |
59 | sa.sa_flags = SA_RESTART; | |
60 | sigaction(SIGALRM, &sa, NULL); | |
61 | ||
62 | v.it_interval.tv_sec = 1; | |
63 | v.it_interval.tv_usec = 0; | |
64 | v.it_value = v.it_interval; | |
65 | setitimer(ITIMER_REAL, &v, NULL); | |
66 | } | |
67 | ||
68 | static void clear_progress_signal(void) | |
69 | { | |
70 | struct itimerval v = {{0,},}; | |
71 | setitimer(ITIMER_REAL, &v, NULL); | |
72 | signal(SIGALRM, SIG_IGN); | |
73 | progress_update = 0; | |
74 | } | |
75 | ||
85cb8906 LM |
76 | static int is_foreground_fd(int fd) |
77 | { | |
a4fb76ce JK |
78 | int tpgrp = tcgetpgrp(fd); |
79 | return tpgrp < 0 || tpgrp == getpgid(0); | |
85cb8906 LM |
80 | } |
81 | ||
d6861d02 | 82 | static int display(struct progress *progress, uint64_t n, const char *done) |
96a02f8f | 83 | { |
a984a06a | 84 | const char *eol, *tp; |
42e18fbf | 85 | |
9c5951ca LS |
86 | if (progress->delay && (!progress_update || --progress->delay)) |
87 | return 0; | |
42e18fbf NP |
88 | |
89 | progress->last_value = n; | |
3131977d | 90 | tp = (progress->throughput) ? progress->throughput->display.buf : ""; |
a984a06a | 91 | eol = done ? done : " \r"; |
96a02f8f NP |
92 | if (progress->total) { |
93 | unsigned percent = n * 100 / progress->total; | |
94 | if (percent != progress->last_percent || progress_update) { | |
95 | progress->last_percent = percent; | |
85cb8906 | 96 | if (is_foreground_fd(fileno(stderr)) || done) { |
d6861d02 EN |
97 | fprintf(stderr, "%s: %3u%% (%"PRIuMAX"/%"PRIuMAX")%s%s", |
98 | progress->title, percent, | |
99 | (uintmax_t)n, (uintmax_t)progress->total, | |
100 | tp, eol); | |
85cb8906 LM |
101 | fflush(stderr); |
102 | } | |
96a02f8f NP |
103 | progress_update = 0; |
104 | return 1; | |
105 | } | |
106 | } else if (progress_update) { | |
85cb8906 | 107 | if (is_foreground_fd(fileno(stderr)) || done) { |
d6861d02 EN |
108 | fprintf(stderr, "%s: %"PRIuMAX"%s%s", |
109 | progress->title, (uintmax_t)n, tp, eol); | |
85cb8906 LM |
110 | fflush(stderr); |
111 | } | |
96a02f8f NP |
112 | progress_update = 0; |
113 | return 1; | |
114 | } | |
42e18fbf | 115 | |
96a02f8f NP |
116 | return 0; |
117 | } | |
118 | ||
d6861d02 | 119 | static void throughput_string(struct strbuf *buf, uint64_t total, |
53ed7b5a NP |
120 | unsigned int rate) |
121 | { | |
3131977d | 122 | strbuf_reset(buf); |
079b546a AP |
123 | strbuf_addstr(buf, ", "); |
124 | strbuf_humanise_bytes(buf, total); | |
125 | strbuf_addstr(buf, " | "); | |
126 | strbuf_humanise_bytes(buf, rate * 1024); | |
127 | strbuf_addstr(buf, "/s"); | |
53ed7b5a NP |
128 | } |
129 | ||
d6861d02 | 130 | void display_throughput(struct progress *progress, uint64_t total) |
cf84d51c NP |
131 | { |
132 | struct throughput *tp; | |
83d26fa7 KB |
133 | uint64_t now_ns; |
134 | unsigned int misecs, count, rate; | |
cf84d51c NP |
135 | |
136 | if (!progress) | |
137 | return; | |
138 | tp = progress->throughput; | |
139 | ||
83d26fa7 | 140 | now_ns = getnanotime(); |
cf84d51c NP |
141 | |
142 | if (!tp) { | |
143 | progress->throughput = tp = calloc(1, sizeof(*tp)); | |
218558af | 144 | if (tp) { |
53ed7b5a | 145 | tp->prev_total = tp->curr_total = total; |
83d26fa7 | 146 | tp->prev_ns = now_ns; |
3131977d | 147 | strbuf_init(&tp->display, 0); |
218558af | 148 | } |
cf84d51c NP |
149 | return; |
150 | } | |
53ed7b5a | 151 | tp->curr_total = total; |
cf84d51c | 152 | |
83d26fa7 KB |
153 | /* only update throughput every 0.5 s */ |
154 | if (now_ns - tp->prev_ns <= 500000000) | |
155 | return; | |
156 | ||
cf84d51c | 157 | /* |
83d26fa7 | 158 | * We have x = bytes and y = nanosecs. We want z = KiB/s: |
cf84d51c | 159 | * |
83d26fa7 KB |
160 | * z = (x / 1024) / (y / 1000000000) |
161 | * z = x / y * 1000000000 / 1024 | |
162 | * z = x / (y * 1024 / 1000000000) | |
cf84d51c NP |
163 | * z = x / y' |
164 | * | |
165 | * To simplify things we'll keep track of misecs, or 1024th of a sec | |
166 | * obtained with: | |
167 | * | |
83d26fa7 KB |
168 | * y' = y * 1024 / 1000000000 |
169 | * y' = y * (2^10 / 2^42) * (2^42 / 1000000000) | |
170 | * y' = y / 2^32 * 4398 | |
171 | * y' = (y * 4398) >> 32 | |
cf84d51c | 172 | */ |
83d26fa7 KB |
173 | misecs = ((now_ns - tp->prev_ns) * 4398) >> 32; |
174 | ||
175 | count = total - tp->prev_total; | |
176 | tp->prev_total = total; | |
177 | tp->prev_ns = now_ns; | |
178 | tp->avg_bytes += count; | |
179 | tp->avg_misecs += misecs; | |
180 | rate = tp->avg_bytes / tp->avg_misecs; | |
181 | tp->avg_bytes -= tp->last_bytes[tp->idx]; | |
182 | tp->avg_misecs -= tp->last_misecs[tp->idx]; | |
183 | tp->last_bytes[tp->idx] = count; | |
184 | tp->last_misecs[tp->idx] = misecs; | |
185 | tp->idx = (tp->idx + 1) % TP_IDX_MAX; | |
186 | ||
3131977d | 187 | throughput_string(&tp->display, total, rate); |
83d26fa7 KB |
188 | if (progress->last_value != -1 && progress_update) |
189 | display(progress, progress->last_value, NULL); | |
cf84d51c NP |
190 | } |
191 | ||
d6861d02 | 192 | int display_progress(struct progress *progress, uint64_t n) |
96a02f8f | 193 | { |
a984a06a | 194 | return progress ? display(progress, n, NULL) : 0; |
96a02f8f NP |
195 | } |
196 | ||
d6861d02 | 197 | static struct progress *start_progress_delay(const char *title, uint64_t total, |
9d81ecb5 | 198 | unsigned delay, unsigned sparse) |
180a9f22 | 199 | { |
dc6a0757 NP |
200 | struct progress *progress = malloc(sizeof(*progress)); |
201 | if (!progress) { | |
202 | /* unlikely, but here's a good fallback */ | |
203 | fprintf(stderr, "%s...\n", title); | |
137a0d0e | 204 | fflush(stderr); |
dc6a0757 NP |
205 | return NULL; |
206 | } | |
42e18fbf | 207 | progress->title = title; |
180a9f22 | 208 | progress->total = total; |
42e18fbf | 209 | progress->last_value = -1; |
180a9f22 | 210 | progress->last_percent = -1; |
180a9f22 | 211 | progress->delay = delay; |
9d81ecb5 | 212 | progress->sparse = sparse; |
cf84d51c | 213 | progress->throughput = NULL; |
0fae1e07 | 214 | progress->start_ns = getnanotime(); |
180a9f22 | 215 | set_progress_signal(); |
dc6a0757 | 216 | return progress; |
180a9f22 NP |
217 | } |
218 | ||
d6861d02 | 219 | struct progress *start_delayed_progress(const char *title, uint64_t total) |
8aade107 | 220 | { |
9d81ecb5 | 221 | return start_progress_delay(title, total, 2, 0); |
8aade107 JH |
222 | } |
223 | ||
d6861d02 | 224 | struct progress *start_progress(const char *title, uint64_t total) |
42e18fbf | 225 | { |
9d81ecb5 JH |
226 | return start_progress_delay(title, total, 0, 0); |
227 | } | |
228 | ||
229 | /* | |
230 | * Here "sparse" means that the caller might use some sampling criteria to | |
231 | * decide when to call display_progress() rather than calling it for every | |
232 | * integer value in[0 .. total). In particular, the caller might not call | |
233 | * display_progress() for the last value in the range. | |
234 | * | |
235 | * When "sparse" is set, stop_progress() will automatically force the done | |
236 | * message to show 100%. | |
237 | */ | |
238 | struct progress *start_sparse_progress(const char *title, uint64_t total) | |
239 | { | |
240 | return start_progress_delay(title, total, 0, 1); | |
241 | } | |
242 | ||
243 | struct progress *start_delayed_sparse_progress(const char *title, | |
244 | uint64_t total) | |
245 | { | |
246 | return start_progress_delay(title, total, 2, 1); | |
247 | } | |
248 | ||
249 | static void finish_if_sparse(struct progress *progress) | |
250 | { | |
251 | if (progress && | |
252 | progress->sparse && | |
253 | progress->last_value != progress->total) | |
254 | display_progress(progress, progress->total); | |
42e18fbf NP |
255 | } |
256 | ||
dc6a0757 | 257 | void stop_progress(struct progress **p_progress) |
a984a06a | 258 | { |
9d81ecb5 JH |
259 | finish_if_sparse(*p_progress); |
260 | ||
754dbc43 | 261 | stop_progress_msg(p_progress, _("done")); |
a984a06a NP |
262 | } |
263 | ||
264 | void stop_progress_msg(struct progress **p_progress, const char *msg) | |
96a02f8f | 265 | { |
dc6a0757 NP |
266 | struct progress *progress = *p_progress; |
267 | if (!progress) | |
268 | return; | |
269 | *p_progress = NULL; | |
42e18fbf NP |
270 | if (progress->last_value != -1) { |
271 | /* Force the last update */ | |
fbd09439 | 272 | char *buf; |
53ed7b5a | 273 | struct throughput *tp = progress->throughput; |
d4c44443 | 274 | |
53ed7b5a | 275 | if (tp) { |
0fae1e07 RS |
276 | uint64_t now_ns = getnanotime(); |
277 | unsigned int misecs, rate; | |
278 | misecs = ((now_ns - progress->start_ns) * 4398) >> 32; | |
279 | rate = tp->curr_total / (misecs ? misecs : 1); | |
3131977d | 280 | throughput_string(&tp->display, tp->curr_total, rate); |
53ed7b5a | 281 | } |
42e18fbf | 282 | progress_update = 1; |
fbd09439 MM |
283 | buf = xstrfmt(", %s.\n", msg); |
284 | display(progress, progress->last_value, buf); | |
285 | free(buf); | |
42e18fbf | 286 | } |
96a02f8f | 287 | clear_progress_signal(); |
3131977d JK |
288 | if (progress->throughput) |
289 | strbuf_release(&progress->throughput->display); | |
cf84d51c | 290 | free(progress->throughput); |
dc6a0757 | 291 | free(progress); |
96a02f8f | 292 | } |