]>
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; | |
81f6654a | 28 | char display[32]; |
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; | |
37 | unsigned delayed_percent_treshold; | |
cf84d51c | 38 | struct throughput *throughput; |
dc6a0757 NP |
39 | }; |
40 | ||
96a02f8f NP |
41 | static volatile sig_atomic_t progress_update; |
42 | ||
43 | static void progress_interval(int signum) | |
44 | { | |
45 | progress_update = 1; | |
46 | } | |
47 | ||
48 | static void set_progress_signal(void) | |
49 | { | |
50 | struct sigaction sa; | |
51 | struct itimerval v; | |
52 | ||
180a9f22 NP |
53 | progress_update = 0; |
54 | ||
96a02f8f NP |
55 | memset(&sa, 0, sizeof(sa)); |
56 | sa.sa_handler = progress_interval; | |
57 | sigemptyset(&sa.sa_mask); | |
58 | sa.sa_flags = SA_RESTART; | |
59 | sigaction(SIGALRM, &sa, NULL); | |
60 | ||
61 | v.it_interval.tv_sec = 1; | |
62 | v.it_interval.tv_usec = 0; | |
63 | v.it_value = v.it_interval; | |
64 | setitimer(ITIMER_REAL, &v, NULL); | |
65 | } | |
66 | ||
67 | static void clear_progress_signal(void) | |
68 | { | |
69 | struct itimerval v = {{0,},}; | |
70 | setitimer(ITIMER_REAL, &v, NULL); | |
71 | signal(SIGALRM, SIG_IGN); | |
72 | progress_update = 0; | |
73 | } | |
74 | ||
a984a06a | 75 | static int display(struct progress *progress, unsigned n, const char *done) |
96a02f8f | 76 | { |
a984a06a | 77 | const char *eol, *tp; |
42e18fbf | 78 | |
180a9f22 | 79 | if (progress->delay) { |
180a9f22 NP |
80 | if (!progress_update || --progress->delay) |
81 | return 0; | |
82 | if (progress->total) { | |
83 | unsigned percent = n * 100 / progress->total; | |
84 | if (percent > progress->delayed_percent_treshold) { | |
85 | /* inhibit this progress report entirely */ | |
86 | clear_progress_signal(); | |
87 | progress->delay = -1; | |
88 | progress->total = 0; | |
89 | return 0; | |
90 | } | |
91 | } | |
180a9f22 | 92 | } |
42e18fbf NP |
93 | |
94 | progress->last_value = n; | |
cf84d51c | 95 | tp = (progress->throughput) ? progress->throughput->display : ""; |
a984a06a | 96 | eol = done ? done : " \r"; |
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; | |
cf84d51c NP |
101 | fprintf(stderr, "%s: %3u%% (%u/%u)%s%s", |
102 | progress->title, percent, n, | |
103 | progress->total, tp, eol); | |
137a0d0e | 104 | fflush(stderr); |
96a02f8f NP |
105 | progress_update = 0; |
106 | return 1; | |
107 | } | |
108 | } else if (progress_update) { | |
cf84d51c | 109 | fprintf(stderr, "%s: %u%s%s", progress->title, n, tp, eol); |
137a0d0e | 110 | fflush(stderr); |
96a02f8f NP |
111 | progress_update = 0; |
112 | return 1; | |
113 | } | |
42e18fbf | 114 | |
96a02f8f NP |
115 | return 0; |
116 | } | |
117 | ||
079b546a | 118 | static void throughput_string(struct strbuf *buf, off_t total, |
53ed7b5a NP |
119 | unsigned int rate) |
120 | { | |
079b546a AP |
121 | strbuf_addstr(buf, ", "); |
122 | strbuf_humanise_bytes(buf, total); | |
123 | strbuf_addstr(buf, " | "); | |
124 | strbuf_humanise_bytes(buf, rate * 1024); | |
125 | strbuf_addstr(buf, "/s"); | |
53ed7b5a NP |
126 | } |
127 | ||
218558af | 128 | void display_throughput(struct progress *progress, off_t total) |
cf84d51c NP |
129 | { |
130 | struct throughput *tp; | |
83d26fa7 KB |
131 | uint64_t now_ns; |
132 | unsigned int misecs, count, rate; | |
133 | struct strbuf buf = STRBUF_INIT; | |
cf84d51c NP |
134 | |
135 | if (!progress) | |
136 | return; | |
137 | tp = progress->throughput; | |
138 | ||
83d26fa7 | 139 | now_ns = getnanotime(); |
cf84d51c NP |
140 | |
141 | if (!tp) { | |
142 | progress->throughput = tp = calloc(1, sizeof(*tp)); | |
218558af | 143 | if (tp) { |
53ed7b5a | 144 | tp->prev_total = tp->curr_total = total; |
83d26fa7 | 145 | tp->prev_ns = now_ns; |
218558af | 146 | } |
cf84d51c NP |
147 | return; |
148 | } | |
53ed7b5a | 149 | tp->curr_total = total; |
cf84d51c | 150 | |
83d26fa7 KB |
151 | /* only update throughput every 0.5 s */ |
152 | if (now_ns - tp->prev_ns <= 500000000) | |
153 | return; | |
154 | ||
cf84d51c | 155 | /* |
83d26fa7 | 156 | * We have x = bytes and y = nanosecs. We want z = KiB/s: |
cf84d51c | 157 | * |
83d26fa7 KB |
158 | * z = (x / 1024) / (y / 1000000000) |
159 | * z = x / y * 1000000000 / 1024 | |
160 | * z = x / (y * 1024 / 1000000000) | |
cf84d51c NP |
161 | * z = x / y' |
162 | * | |
163 | * To simplify things we'll keep track of misecs, or 1024th of a sec | |
164 | * obtained with: | |
165 | * | |
83d26fa7 KB |
166 | * y' = y * 1024 / 1000000000 |
167 | * y' = y * (2^10 / 2^42) * (2^42 / 1000000000) | |
168 | * y' = y / 2^32 * 4398 | |
169 | * y' = (y * 4398) >> 32 | |
cf84d51c | 170 | */ |
83d26fa7 KB |
171 | misecs = ((now_ns - tp->prev_ns) * 4398) >> 32; |
172 | ||
173 | count = total - tp->prev_total; | |
174 | tp->prev_total = total; | |
175 | tp->prev_ns = now_ns; | |
176 | tp->avg_bytes += count; | |
177 | tp->avg_misecs += misecs; | |
178 | rate = tp->avg_bytes / tp->avg_misecs; | |
179 | tp->avg_bytes -= tp->last_bytes[tp->idx]; | |
180 | tp->avg_misecs -= tp->last_misecs[tp->idx]; | |
181 | tp->last_bytes[tp->idx] = count; | |
182 | tp->last_misecs[tp->idx] = misecs; | |
183 | tp->idx = (tp->idx + 1) % TP_IDX_MAX; | |
184 | ||
185 | throughput_string(&buf, total, rate); | |
186 | strncpy(tp->display, buf.buf, sizeof(tp->display)); | |
187 | strbuf_release(&buf); | |
188 | if (progress->last_value != -1 && progress_update) | |
189 | display(progress, progress->last_value, NULL); | |
cf84d51c NP |
190 | } |
191 | ||
42e18fbf | 192 | int display_progress(struct progress *progress, unsigned n) |
96a02f8f | 193 | { |
a984a06a | 194 | return progress ? display(progress, n, NULL) : 0; |
96a02f8f NP |
195 | } |
196 | ||
dc6a0757 NP |
197 | struct progress *start_progress_delay(const char *title, unsigned total, |
198 | unsigned percent_treshold, unsigned delay) | |
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 NP |
210 | progress->last_percent = -1; |
211 | progress->delayed_percent_treshold = percent_treshold; | |
180a9f22 | 212 | progress->delay = delay; |
cf84d51c | 213 | progress->throughput = NULL; |
180a9f22 | 214 | set_progress_signal(); |
dc6a0757 | 215 | return progress; |
180a9f22 NP |
216 | } |
217 | ||
dc6a0757 | 218 | struct progress *start_progress(const char *title, unsigned total) |
42e18fbf | 219 | { |
dc6a0757 | 220 | return start_progress_delay(title, total, 0, 0); |
42e18fbf NP |
221 | } |
222 | ||
dc6a0757 | 223 | void stop_progress(struct progress **p_progress) |
a984a06a | 224 | { |
754dbc43 | 225 | stop_progress_msg(p_progress, _("done")); |
a984a06a NP |
226 | } |
227 | ||
228 | void stop_progress_msg(struct progress **p_progress, const char *msg) | |
96a02f8f | 229 | { |
dc6a0757 NP |
230 | struct progress *progress = *p_progress; |
231 | if (!progress) | |
232 | return; | |
233 | *p_progress = NULL; | |
42e18fbf NP |
234 | if (progress->last_value != -1) { |
235 | /* Force the last update */ | |
d4c44443 BLG |
236 | char buf[128], *bufp; |
237 | size_t len = strlen(msg) + 5; | |
53ed7b5a | 238 | struct throughput *tp = progress->throughput; |
d4c44443 BLG |
239 | |
240 | bufp = (len < sizeof(buf)) ? buf : xmalloc(len + 1); | |
53ed7b5a | 241 | if (tp) { |
079b546a | 242 | struct strbuf strbuf = STRBUF_INIT; |
53ed7b5a NP |
243 | unsigned int rate = !tp->avg_misecs ? 0 : |
244 | tp->avg_bytes / tp->avg_misecs; | |
079b546a AP |
245 | throughput_string(&strbuf, tp->curr_total, rate); |
246 | strncpy(tp->display, strbuf.buf, sizeof(tp->display)); | |
247 | strbuf_release(&strbuf); | |
53ed7b5a | 248 | } |
42e18fbf | 249 | progress_update = 1; |
d4c44443 BLG |
250 | sprintf(bufp, ", %s.\n", msg); |
251 | display(progress, progress->last_value, bufp); | |
252 | if (buf != bufp) | |
253 | free(bufp); | |
42e18fbf | 254 | } |
96a02f8f | 255 | clear_progress_signal(); |
cf84d51c | 256 | free(progress->throughput); |
dc6a0757 | 257 | free(progress); |
96a02f8f | 258 | } |