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