]>
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; | |
33 | int last_value; | |
34 | unsigned total; | |
35 | unsigned last_percent; | |
36 | unsigned delay; | |
8aade107 | 37 | unsigned delayed_percent_threshold; |
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 | ||
a984a06a | 82 | static int display(struct progress *progress, unsigned n, const char *done) |
96a02f8f | 83 | { |
a984a06a | 84 | const char *eol, *tp; |
42e18fbf | 85 | |
180a9f22 | 86 | if (progress->delay) { |
180a9f22 NP |
87 | if (!progress_update || --progress->delay) |
88 | return 0; | |
89 | if (progress->total) { | |
90 | unsigned percent = n * 100 / progress->total; | |
8aade107 | 91 | if (percent > progress->delayed_percent_threshold) { |
180a9f22 NP |
92 | /* inhibit this progress report entirely */ |
93 | clear_progress_signal(); | |
94 | progress->delay = -1; | |
95 | progress->total = 0; | |
96 | return 0; | |
97 | } | |
98 | } | |
180a9f22 | 99 | } |
42e18fbf NP |
100 | |
101 | progress->last_value = n; | |
3131977d | 102 | tp = (progress->throughput) ? progress->throughput->display.buf : ""; |
a984a06a | 103 | eol = done ? done : " \r"; |
96a02f8f NP |
104 | if (progress->total) { |
105 | unsigned percent = n * 100 / progress->total; | |
106 | if (percent != progress->last_percent || progress_update) { | |
107 | progress->last_percent = percent; | |
85cb8906 LM |
108 | if (is_foreground_fd(fileno(stderr)) || done) { |
109 | fprintf(stderr, "%s: %3u%% (%u/%u)%s%s", | |
110 | progress->title, percent, n, | |
111 | progress->total, tp, eol); | |
112 | fflush(stderr); | |
113 | } | |
96a02f8f NP |
114 | progress_update = 0; |
115 | return 1; | |
116 | } | |
117 | } else if (progress_update) { | |
85cb8906 LM |
118 | if (is_foreground_fd(fileno(stderr)) || done) { |
119 | fprintf(stderr, "%s: %u%s%s", | |
120 | progress->title, n, tp, eol); | |
121 | fflush(stderr); | |
122 | } | |
96a02f8f NP |
123 | progress_update = 0; |
124 | return 1; | |
125 | } | |
42e18fbf | 126 | |
96a02f8f NP |
127 | return 0; |
128 | } | |
129 | ||
079b546a | 130 | static void throughput_string(struct strbuf *buf, off_t total, |
53ed7b5a NP |
131 | unsigned int rate) |
132 | { | |
3131977d | 133 | strbuf_reset(buf); |
079b546a AP |
134 | strbuf_addstr(buf, ", "); |
135 | strbuf_humanise_bytes(buf, total); | |
136 | strbuf_addstr(buf, " | "); | |
137 | strbuf_humanise_bytes(buf, rate * 1024); | |
138 | strbuf_addstr(buf, "/s"); | |
53ed7b5a NP |
139 | } |
140 | ||
218558af | 141 | void display_throughput(struct progress *progress, off_t total) |
cf84d51c NP |
142 | { |
143 | struct throughput *tp; | |
83d26fa7 KB |
144 | uint64_t now_ns; |
145 | unsigned int misecs, count, rate; | |
cf84d51c NP |
146 | |
147 | if (!progress) | |
148 | return; | |
149 | tp = progress->throughput; | |
150 | ||
83d26fa7 | 151 | now_ns = getnanotime(); |
cf84d51c NP |
152 | |
153 | if (!tp) { | |
154 | progress->throughput = tp = calloc(1, sizeof(*tp)); | |
218558af | 155 | if (tp) { |
53ed7b5a | 156 | tp->prev_total = tp->curr_total = total; |
83d26fa7 | 157 | tp->prev_ns = now_ns; |
3131977d | 158 | strbuf_init(&tp->display, 0); |
218558af | 159 | } |
cf84d51c NP |
160 | return; |
161 | } | |
53ed7b5a | 162 | tp->curr_total = total; |
cf84d51c | 163 | |
83d26fa7 KB |
164 | /* only update throughput every 0.5 s */ |
165 | if (now_ns - tp->prev_ns <= 500000000) | |
166 | return; | |
167 | ||
cf84d51c | 168 | /* |
83d26fa7 | 169 | * We have x = bytes and y = nanosecs. We want z = KiB/s: |
cf84d51c | 170 | * |
83d26fa7 KB |
171 | * z = (x / 1024) / (y / 1000000000) |
172 | * z = x / y * 1000000000 / 1024 | |
173 | * z = x / (y * 1024 / 1000000000) | |
cf84d51c NP |
174 | * z = x / y' |
175 | * | |
176 | * To simplify things we'll keep track of misecs, or 1024th of a sec | |
177 | * obtained with: | |
178 | * | |
83d26fa7 KB |
179 | * y' = y * 1024 / 1000000000 |
180 | * y' = y * (2^10 / 2^42) * (2^42 / 1000000000) | |
181 | * y' = y / 2^32 * 4398 | |
182 | * y' = (y * 4398) >> 32 | |
cf84d51c | 183 | */ |
83d26fa7 KB |
184 | misecs = ((now_ns - tp->prev_ns) * 4398) >> 32; |
185 | ||
186 | count = total - tp->prev_total; | |
187 | tp->prev_total = total; | |
188 | tp->prev_ns = now_ns; | |
189 | tp->avg_bytes += count; | |
190 | tp->avg_misecs += misecs; | |
191 | rate = tp->avg_bytes / tp->avg_misecs; | |
192 | tp->avg_bytes -= tp->last_bytes[tp->idx]; | |
193 | tp->avg_misecs -= tp->last_misecs[tp->idx]; | |
194 | tp->last_bytes[tp->idx] = count; | |
195 | tp->last_misecs[tp->idx] = misecs; | |
196 | tp->idx = (tp->idx + 1) % TP_IDX_MAX; | |
197 | ||
3131977d | 198 | throughput_string(&tp->display, total, rate); |
83d26fa7 KB |
199 | if (progress->last_value != -1 && progress_update) |
200 | display(progress, progress->last_value, NULL); | |
cf84d51c NP |
201 | } |
202 | ||
42e18fbf | 203 | int display_progress(struct progress *progress, unsigned n) |
96a02f8f | 204 | { |
a984a06a | 205 | return progress ? display(progress, n, NULL) : 0; |
96a02f8f NP |
206 | } |
207 | ||
8aade107 JH |
208 | static struct progress *start_progress_delay(const char *title, unsigned total, |
209 | unsigned percent_threshold, unsigned delay) | |
180a9f22 | 210 | { |
dc6a0757 NP |
211 | struct progress *progress = malloc(sizeof(*progress)); |
212 | if (!progress) { | |
213 | /* unlikely, but here's a good fallback */ | |
214 | fprintf(stderr, "%s...\n", title); | |
137a0d0e | 215 | fflush(stderr); |
dc6a0757 NP |
216 | return NULL; |
217 | } | |
42e18fbf | 218 | progress->title = title; |
180a9f22 | 219 | progress->total = total; |
42e18fbf | 220 | progress->last_value = -1; |
180a9f22 | 221 | progress->last_percent = -1; |
8aade107 | 222 | progress->delayed_percent_threshold = percent_threshold; |
180a9f22 | 223 | progress->delay = delay; |
cf84d51c | 224 | progress->throughput = NULL; |
0fae1e07 | 225 | progress->start_ns = getnanotime(); |
180a9f22 | 226 | set_progress_signal(); |
dc6a0757 | 227 | return progress; |
180a9f22 NP |
228 | } |
229 | ||
8aade107 JH |
230 | struct progress *start_delayed_progress(const char *title, unsigned total) |
231 | { | |
232 | return start_progress_delay(title, total, 0, 2); | |
233 | } | |
234 | ||
dc6a0757 | 235 | struct progress *start_progress(const char *title, unsigned total) |
42e18fbf | 236 | { |
dc6a0757 | 237 | return start_progress_delay(title, total, 0, 0); |
42e18fbf NP |
238 | } |
239 | ||
dc6a0757 | 240 | void stop_progress(struct progress **p_progress) |
a984a06a | 241 | { |
754dbc43 | 242 | stop_progress_msg(p_progress, _("done")); |
a984a06a NP |
243 | } |
244 | ||
245 | void stop_progress_msg(struct progress **p_progress, const char *msg) | |
96a02f8f | 246 | { |
dc6a0757 NP |
247 | struct progress *progress = *p_progress; |
248 | if (!progress) | |
249 | return; | |
250 | *p_progress = NULL; | |
42e18fbf NP |
251 | if (progress->last_value != -1) { |
252 | /* Force the last update */ | |
fbd09439 | 253 | char *buf; |
53ed7b5a | 254 | struct throughput *tp = progress->throughput; |
d4c44443 | 255 | |
53ed7b5a | 256 | if (tp) { |
0fae1e07 RS |
257 | uint64_t now_ns = getnanotime(); |
258 | unsigned int misecs, rate; | |
259 | misecs = ((now_ns - progress->start_ns) * 4398) >> 32; | |
260 | rate = tp->curr_total / (misecs ? misecs : 1); | |
3131977d | 261 | throughput_string(&tp->display, tp->curr_total, rate); |
53ed7b5a | 262 | } |
42e18fbf | 263 | progress_update = 1; |
fbd09439 MM |
264 | buf = xstrfmt(", %s.\n", msg); |
265 | display(progress, progress->last_value, buf); | |
266 | free(buf); | |
42e18fbf | 267 | } |
96a02f8f | 268 | clear_progress_signal(); |
3131977d JK |
269 | if (progress->throughput) |
270 | strbuf_release(&progress->throughput->display); | |
cf84d51c | 271 | free(progress->throughput); |
dc6a0757 | 272 | free(progress); |
96a02f8f | 273 | } |