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