1 /*
2  * Simple text-based progress display module for GIT
3  *
4  * Copyright (c) 2007 by Nicolas Pitre <nico@fluxnic.net>
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 
11 #include "cache.h"
12 #include "gettext.h"
13 #include "progress.h"
14 #include "strbuf.h"
15 #include "trace.h"
16 #include "utf8.h"
17 #include "config.h"
18 
19 #define TP_IDX_MAX      8
20 
21 struct throughput {
22 	off_t curr_total;
23 	off_t prev_total;
24 	uint64_t prev_ns;
25 	unsigned int avg_bytes;
26 	unsigned int avg_misecs;
27 	unsigned int last_bytes[TP_IDX_MAX];
28 	unsigned int last_misecs[TP_IDX_MAX];
29 	unsigned int idx;
30 	struct strbuf display;
31 };
32 
33 struct progress {
34 	const char *title;
35 	uint64_t last_value;
36 	uint64_t total;
37 	unsigned last_percent;
38 	unsigned delay;
39 	unsigned sparse;
40 	struct throughput *throughput;
41 	uint64_t start_ns;
42 	struct strbuf counters_sb;
43 	int title_len;
44 	int split;
45 };
46 
47 static volatile sig_atomic_t progress_update;
48 
49 /*
50  * These are only intended for testing the progress output, i.e. exclusively
51  * for 'test-tool progress'.
52  */
53 int progress_testing;
54 uint64_t progress_test_ns = 0;
55 void progress_test_force_update(void); /* To silence -Wmissing-prototypes */
progress_test_force_update(void)56 void progress_test_force_update(void)
57 {
58 	progress_update = 1;
59 }
60 
61 
progress_interval(int signum)62 static void progress_interval(int signum)
63 {
64 	progress_update = 1;
65 }
66 
set_progress_signal(void)67 static void set_progress_signal(void)
68 {
69 	struct sigaction sa;
70 	struct itimerval v;
71 
72 	if (progress_testing)
73 		return;
74 
75 	progress_update = 0;
76 
77 	memset(&sa, 0, sizeof(sa));
78 	sa.sa_handler = progress_interval;
79 	sigemptyset(&sa.sa_mask);
80 	sa.sa_flags = SA_RESTART;
81 	sigaction(SIGALRM, &sa, NULL);
82 
83 	v.it_interval.tv_sec = 1;
84 	v.it_interval.tv_usec = 0;
85 	v.it_value = v.it_interval;
86 	setitimer(ITIMER_REAL, &v, NULL);
87 }
88 
clear_progress_signal(void)89 static void clear_progress_signal(void)
90 {
91 	struct itimerval v = {{0,},};
92 
93 	if (progress_testing)
94 		return;
95 
96 	setitimer(ITIMER_REAL, &v, NULL);
97 	signal(SIGALRM, SIG_IGN);
98 	progress_update = 0;
99 }
100 
is_foreground_fd(int fd)101 static int is_foreground_fd(int fd)
102 {
103 	int tpgrp = tcgetpgrp(fd);
104 	return tpgrp < 0 || tpgrp == getpgid(0);
105 }
106 
display(struct progress * progress,uint64_t n,const char * done)107 static void display(struct progress *progress, uint64_t n, const char *done)
108 {
109 	const char *tp;
110 	struct strbuf *counters_sb = &progress->counters_sb;
111 	int show_update = 0;
112 	int last_count_len = counters_sb->len;
113 
114 	if (progress->delay && (!progress_update || --progress->delay))
115 		return;
116 
117 	progress->last_value = n;
118 	tp = (progress->throughput) ? progress->throughput->display.buf : "";
119 	if (progress->total) {
120 		unsigned percent = n * 100 / progress->total;
121 		if (percent != progress->last_percent || progress_update) {
122 			progress->last_percent = percent;
123 
124 			strbuf_reset(counters_sb);
125 			strbuf_addf(counters_sb,
126 				    "%3u%% (%"PRIuMAX"/%"PRIuMAX")%s", percent,
127 				    (uintmax_t)n, (uintmax_t)progress->total,
128 				    tp);
129 			show_update = 1;
130 		}
131 	} else if (progress_update) {
132 		strbuf_reset(counters_sb);
133 		strbuf_addf(counters_sb, "%"PRIuMAX"%s", (uintmax_t)n, tp);
134 		show_update = 1;
135 	}
136 
137 	if (show_update) {
138 		if (is_foreground_fd(fileno(stderr)) || done) {
139 			const char *eol = done ? done : "\r";
140 			size_t clear_len = counters_sb->len < last_count_len ?
141 					last_count_len - counters_sb->len + 1 :
142 					0;
143 			/* The "+ 2" accounts for the ": ". */
144 			size_t progress_line_len = progress->title_len +
145 						counters_sb->len + 2;
146 			int cols = term_columns();
147 
148 			if (progress->split) {
149 				fprintf(stderr, "  %s%*s", counters_sb->buf,
150 					(int) clear_len, eol);
151 			} else if (!done && cols < progress_line_len) {
152 				clear_len = progress->title_len + 1 < cols ?
153 					    cols - progress->title_len - 1 : 0;
154 				fprintf(stderr, "%s:%*s\n  %s%s",
155 					progress->title, (int) clear_len, "",
156 					counters_sb->buf, eol);
157 				progress->split = 1;
158 			} else {
159 				fprintf(stderr, "%s: %s%*s", progress->title,
160 					counters_sb->buf, (int) clear_len, eol);
161 			}
162 			fflush(stderr);
163 		}
164 		progress_update = 0;
165 	}
166 }
167 
throughput_string(struct strbuf * buf,uint64_t total,unsigned int rate)168 static void throughput_string(struct strbuf *buf, uint64_t total,
169 			      unsigned int rate)
170 {
171 	strbuf_reset(buf);
172 	strbuf_addstr(buf, ", ");
173 	strbuf_humanise_bytes(buf, total);
174 	strbuf_addstr(buf, " | ");
175 	strbuf_humanise_rate(buf, rate * 1024);
176 }
177 
progress_getnanotime(struct progress * progress)178 static uint64_t progress_getnanotime(struct progress *progress)
179 {
180 	if (progress_testing)
181 		return progress->start_ns + progress_test_ns;
182 	else
183 		return getnanotime();
184 }
185 
display_throughput(struct progress * progress,uint64_t total)186 void display_throughput(struct progress *progress, uint64_t total)
187 {
188 	struct throughput *tp;
189 	uint64_t now_ns;
190 	unsigned int misecs, count, rate;
191 
192 	if (!progress)
193 		return;
194 	tp = progress->throughput;
195 
196 	now_ns = progress_getnanotime(progress);
197 
198 	if (!tp) {
199 		progress->throughput = tp = xcalloc(1, sizeof(*tp));
200 		tp->prev_total = tp->curr_total = total;
201 		tp->prev_ns = now_ns;
202 		strbuf_init(&tp->display, 0);
203 		return;
204 	}
205 	tp->curr_total = total;
206 
207 	/* only update throughput every 0.5 s */
208 	if (now_ns - tp->prev_ns <= 500000000)
209 		return;
210 
211 	/*
212 	 * We have x = bytes and y = nanosecs.  We want z = KiB/s:
213 	 *
214 	 *	z = (x / 1024) / (y / 1000000000)
215 	 *	z = x / y * 1000000000 / 1024
216 	 *	z = x / (y * 1024 / 1000000000)
217 	 *	z = x / y'
218 	 *
219 	 * To simplify things we'll keep track of misecs, or 1024th of a sec
220 	 * obtained with:
221 	 *
222 	 *	y' = y * 1024 / 1000000000
223 	 *	y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
224 	 *	y' = y / 2^32 * 4398
225 	 *	y' = (y * 4398) >> 32
226 	 */
227 	misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
228 
229 	count = total - tp->prev_total;
230 	tp->prev_total = total;
231 	tp->prev_ns = now_ns;
232 	tp->avg_bytes += count;
233 	tp->avg_misecs += misecs;
234 	rate = tp->avg_bytes / tp->avg_misecs;
235 	tp->avg_bytes -= tp->last_bytes[tp->idx];
236 	tp->avg_misecs -= tp->last_misecs[tp->idx];
237 	tp->last_bytes[tp->idx] = count;
238 	tp->last_misecs[tp->idx] = misecs;
239 	tp->idx = (tp->idx + 1) % TP_IDX_MAX;
240 
241 	throughput_string(&tp->display, total, rate);
242 	if (progress->last_value != -1 && progress_update)
243 		display(progress, progress->last_value, NULL);
244 }
245 
display_progress(struct progress * progress,uint64_t n)246 void display_progress(struct progress *progress, uint64_t n)
247 {
248 	if (progress)
249 		display(progress, n, NULL);
250 }
251 
start_progress_delay(const char * title,uint64_t total,unsigned delay,unsigned sparse)252 static struct progress *start_progress_delay(const char *title, uint64_t total,
253 					     unsigned delay, unsigned sparse)
254 {
255 	struct progress *progress = xmalloc(sizeof(*progress));
256 	progress->title = title;
257 	progress->total = total;
258 	progress->last_value = -1;
259 	progress->last_percent = -1;
260 	progress->delay = delay;
261 	progress->sparse = sparse;
262 	progress->throughput = NULL;
263 	progress->start_ns = getnanotime();
264 	strbuf_init(&progress->counters_sb, 0);
265 	progress->title_len = utf8_strwidth(title);
266 	progress->split = 0;
267 	set_progress_signal();
268 	return progress;
269 }
270 
get_default_delay(void)271 static int get_default_delay(void)
272 {
273 	static int delay_in_secs = -1;
274 
275 	if (delay_in_secs < 0)
276 		delay_in_secs = git_env_ulong("GIT_PROGRESS_DELAY", 2);
277 
278 	return delay_in_secs;
279 }
280 
start_delayed_progress(const char * title,uint64_t total)281 struct progress *start_delayed_progress(const char *title, uint64_t total)
282 {
283 	return start_progress_delay(title, total, get_default_delay(), 0);
284 }
285 
start_progress(const char * title,uint64_t total)286 struct progress *start_progress(const char *title, uint64_t total)
287 {
288 	return start_progress_delay(title, total, 0, 0);
289 }
290 
291 /*
292  * Here "sparse" means that the caller might use some sampling criteria to
293  * decide when to call display_progress() rather than calling it for every
294  * integer value in[0 .. total).  In particular, the caller might not call
295  * display_progress() for the last value in the range.
296  *
297  * When "sparse" is set, stop_progress() will automatically force the done
298  * message to show 100%.
299  */
start_sparse_progress(const char * title,uint64_t total)300 struct progress *start_sparse_progress(const char *title, uint64_t total)
301 {
302 	return start_progress_delay(title, total, 0, 1);
303 }
304 
start_delayed_sparse_progress(const char * title,uint64_t total)305 struct progress *start_delayed_sparse_progress(const char *title,
306 					       uint64_t total)
307 {
308 	return start_progress_delay(title, total, get_default_delay(), 1);
309 }
310 
finish_if_sparse(struct progress * progress)311 static void finish_if_sparse(struct progress *progress)
312 {
313 	if (progress &&
314 	    progress->sparse &&
315 	    progress->last_value != progress->total)
316 		display_progress(progress, progress->total);
317 }
318 
stop_progress(struct progress ** p_progress)319 void stop_progress(struct progress **p_progress)
320 {
321 	finish_if_sparse(*p_progress);
322 
323 	stop_progress_msg(p_progress, _("done"));
324 }
325 
stop_progress_msg(struct progress ** p_progress,const char * msg)326 void stop_progress_msg(struct progress **p_progress, const char *msg)
327 {
328 	struct progress *progress = *p_progress;
329 	if (!progress)
330 		return;
331 	*p_progress = NULL;
332 	if (progress->last_value != -1) {
333 		/* Force the last update */
334 		char *buf;
335 		struct throughput *tp = progress->throughput;
336 
337 		if (tp) {
338 			uint64_t now_ns = progress_getnanotime(progress);
339 			unsigned int misecs, rate;
340 			misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
341 			rate = tp->curr_total / (misecs ? misecs : 1);
342 			throughput_string(&tp->display, tp->curr_total, rate);
343 		}
344 		progress_update = 1;
345 		buf = xstrfmt(", %s.\n", msg);
346 		display(progress, progress->last_value, buf);
347 		free(buf);
348 	}
349 	clear_progress_signal();
350 	strbuf_release(&progress->counters_sb);
351 	if (progress->throughput)
352 		strbuf_release(&progress->throughput->display);
353 	free(progress->throughput);
354 	free(progress);
355 }
356