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