Merge branch 'jk/jump-quickfix-fixes'
[git/gitster.git] / progress.c
blob0d44c18edc0c522c3d8fb4769c26009439cc61ce
1 /*
2 * Simple text-based progress display module for GIT
4 * Copyright (c) 2007 by Nicolas Pitre <nico@fluxnic.net>
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 */
11 #define GIT_TEST_PROGRESS_ONLY
12 #define USE_THE_REPOSITORY_VARIABLE
14 #include "git-compat-util.h"
15 #include "pager.h"
16 #include "progress.h"
17 #include "repository.h"
18 #include "strbuf.h"
19 #include "trace.h"
20 #include "trace2.h"
21 #include "utf8.h"
22 #include "parse.h"
24 #define TP_IDX_MAX 8
26 struct throughput {
27 off_t curr_total;
28 off_t prev_total;
29 uint64_t prev_ns;
30 unsigned int avg_bytes;
31 unsigned int avg_misecs;
32 unsigned int last_bytes[TP_IDX_MAX];
33 unsigned int last_misecs[TP_IDX_MAX];
34 unsigned int idx;
35 struct strbuf display;
38 struct progress {
39 const char *title;
40 uint64_t last_value;
41 uint64_t total;
42 unsigned last_percent;
43 unsigned delay;
44 unsigned sparse;
45 struct throughput *throughput;
46 uint64_t start_ns;
47 struct strbuf counters_sb;
48 int title_len;
49 int split;
52 static volatile sig_atomic_t progress_update;
55 * These are only intended for testing the progress output, i.e. exclusively
56 * for 'test-tool progress'.
58 int progress_testing;
59 uint64_t progress_test_ns = 0;
60 void progress_test_force_update(void)
62 progress_update = 1;
66 static void progress_interval(int signum UNUSED)
68 progress_update = 1;
71 static void set_progress_signal(void)
73 struct sigaction sa;
74 struct itimerval v;
76 if (progress_testing)
77 return;
79 progress_update = 0;
81 memset(&sa, 0, sizeof(sa));
82 sa.sa_handler = progress_interval;
83 sigemptyset(&sa.sa_mask);
84 sa.sa_flags = SA_RESTART;
85 sigaction(SIGALRM, &sa, NULL);
87 v.it_interval.tv_sec = 1;
88 v.it_interval.tv_usec = 0;
89 v.it_value = v.it_interval;
90 setitimer(ITIMER_REAL, &v, NULL);
93 static void clear_progress_signal(void)
95 struct itimerval v = {{0,},};
97 if (progress_testing)
98 return;
100 setitimer(ITIMER_REAL, &v, NULL);
101 signal(SIGALRM, SIG_IGN);
102 progress_update = 0;
105 static int is_foreground_fd(int fd)
107 int tpgrp = tcgetpgrp(fd);
108 return tpgrp < 0 || tpgrp == getpgid(0);
111 static void display(struct progress *progress, uint64_t n, const char *done)
113 const char *tp;
114 struct strbuf *counters_sb = &progress->counters_sb;
115 int show_update = 0;
116 int last_count_len = counters_sb->len;
118 if (progress->delay && (!progress_update || --progress->delay))
119 return;
121 progress->last_value = n;
122 tp = (progress->throughput) ? progress->throughput->display.buf : "";
123 if (progress->total) {
124 unsigned percent = n * 100 / progress->total;
125 if (percent != progress->last_percent || progress_update) {
126 progress->last_percent = percent;
128 strbuf_reset(counters_sb);
129 strbuf_addf(counters_sb,
130 "%3u%% (%"PRIuMAX"/%"PRIuMAX")%s", percent,
131 (uintmax_t)n, (uintmax_t)progress->total,
132 tp);
133 show_update = 1;
135 } else if (progress_update) {
136 strbuf_reset(counters_sb);
137 strbuf_addf(counters_sb, "%"PRIuMAX"%s", (uintmax_t)n, tp);
138 show_update = 1;
141 if (show_update) {
142 if (is_foreground_fd(fileno(stderr)) || done) {
143 const char *eol = done ? done : "\r";
144 size_t clear_len = counters_sb->len < last_count_len ?
145 last_count_len - counters_sb->len + 1 :
147 /* The "+ 2" accounts for the ": ". */
148 size_t progress_line_len = progress->title_len +
149 counters_sb->len + 2;
150 int cols = term_columns();
152 if (progress->split) {
153 fprintf(stderr, " %s%*s", counters_sb->buf,
154 (int) clear_len, eol);
155 } else if (!done && cols < progress_line_len) {
156 clear_len = progress->title_len + 1 < cols ?
157 cols - progress->title_len - 1 : 0;
158 fprintf(stderr, "%s:%*s\n %s%s",
159 progress->title, (int) clear_len, "",
160 counters_sb->buf, eol);
161 progress->split = 1;
162 } else {
163 fprintf(stderr, "%s: %s%*s", progress->title,
164 counters_sb->buf, (int) clear_len, eol);
166 fflush(stderr);
168 progress_update = 0;
172 static void throughput_string(struct strbuf *buf, uint64_t total,
173 unsigned int rate)
175 strbuf_reset(buf);
176 strbuf_addstr(buf, ", ");
177 strbuf_humanise_bytes(buf, total);
178 strbuf_addstr(buf, " | ");
179 strbuf_humanise_rate(buf, rate * 1024);
182 static uint64_t progress_getnanotime(struct progress *progress)
184 if (progress_testing)
185 return progress->start_ns + progress_test_ns;
186 else
187 return getnanotime();
190 void display_throughput(struct progress *progress, uint64_t total)
192 struct throughput *tp;
193 uint64_t now_ns;
194 unsigned int misecs, count, rate;
196 if (!progress)
197 return;
198 tp = progress->throughput;
200 now_ns = progress_getnanotime(progress);
202 if (!tp) {
203 progress->throughput = CALLOC_ARRAY(tp, 1);
204 tp->prev_total = tp->curr_total = total;
205 tp->prev_ns = now_ns;
206 strbuf_init(&tp->display, 0);
207 return;
209 tp->curr_total = total;
211 /* only update throughput every 0.5 s */
212 if (now_ns - tp->prev_ns <= 500000000)
213 return;
216 * We have x = bytes and y = nanosecs. We want z = KiB/s:
218 * z = (x / 1024) / (y / 1000000000)
219 * z = x / y * 1000000000 / 1024
220 * z = x / (y * 1024 / 1000000000)
221 * z = x / y'
223 * To simplify things we'll keep track of misecs, or 1024th of a sec
224 * obtained with:
226 * y' = y * 1024 / 1000000000
227 * y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
228 * y' = y / 2^32 * 4398
229 * y' = (y * 4398) >> 32
231 misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
233 count = total - tp->prev_total;
234 tp->prev_total = total;
235 tp->prev_ns = now_ns;
236 tp->avg_bytes += count;
237 tp->avg_misecs += misecs;
238 rate = tp->avg_bytes / tp->avg_misecs;
239 tp->avg_bytes -= tp->last_bytes[tp->idx];
240 tp->avg_misecs -= tp->last_misecs[tp->idx];
241 tp->last_bytes[tp->idx] = count;
242 tp->last_misecs[tp->idx] = misecs;
243 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
245 throughput_string(&tp->display, total, rate);
246 if (progress->last_value != -1 && progress_update)
247 display(progress, progress->last_value, NULL);
250 void display_progress(struct progress *progress, uint64_t n)
252 if (progress)
253 display(progress, n, NULL);
256 static struct progress *start_progress_delay(const char *title, uint64_t total,
257 unsigned delay, unsigned sparse)
259 struct progress *progress = xmalloc(sizeof(*progress));
260 progress->title = title;
261 progress->total = total;
262 progress->last_value = -1;
263 progress->last_percent = -1;
264 progress->delay = delay;
265 progress->sparse = sparse;
266 progress->throughput = NULL;
267 progress->start_ns = getnanotime();
268 strbuf_init(&progress->counters_sb, 0);
269 progress->title_len = utf8_strwidth(title);
270 progress->split = 0;
271 set_progress_signal();
272 trace2_region_enter("progress", title, the_repository);
273 return progress;
276 static int get_default_delay(void)
278 static int delay_in_secs = -1;
280 if (delay_in_secs < 0)
281 delay_in_secs = git_env_ulong("GIT_PROGRESS_DELAY", 2);
283 return delay_in_secs;
286 struct progress *start_delayed_progress(const char *title, uint64_t total)
288 return start_progress_delay(title, total, get_default_delay(), 0);
291 struct progress *start_progress(const char *title, uint64_t total)
293 return start_progress_delay(title, total, 0, 0);
297 * Here "sparse" means that the caller might use some sampling criteria to
298 * decide when to call display_progress() rather than calling it for every
299 * integer value in[0 .. total). In particular, the caller might not call
300 * display_progress() for the last value in the range.
302 * When "sparse" is set, stop_progress() will automatically force the done
303 * message to show 100%.
305 struct progress *start_sparse_progress(const char *title, uint64_t total)
307 return start_progress_delay(title, total, 0, 1);
310 struct progress *start_delayed_sparse_progress(const char *title,
311 uint64_t total)
313 return start_progress_delay(title, total, get_default_delay(), 1);
316 static void finish_if_sparse(struct progress *progress)
318 if (progress->sparse &&
319 progress->last_value != progress->total)
320 display_progress(progress, progress->total);
323 static void force_last_update(struct progress *progress, const char *msg)
325 char *buf;
326 struct throughput *tp = progress->throughput;
328 if (tp) {
329 uint64_t now_ns = progress_getnanotime(progress);
330 unsigned int misecs, rate;
331 misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
332 rate = tp->curr_total / (misecs ? misecs : 1);
333 throughput_string(&tp->display, tp->curr_total, rate);
335 progress_update = 1;
336 buf = xstrfmt(", %s.\n", msg);
337 display(progress, progress->last_value, buf);
338 free(buf);
341 static void log_trace2(struct progress *progress)
343 trace2_data_intmax("progress", the_repository, "total_objects",
344 progress->total);
346 if (progress->throughput)
347 trace2_data_intmax("progress", the_repository, "total_bytes",
348 progress->throughput->curr_total);
350 trace2_region_leave("progress", progress->title, the_repository);
353 void stop_progress_msg(struct progress **p_progress, const char *msg)
355 struct progress *progress;
357 if (!p_progress)
358 BUG("don't provide NULL to stop_progress_msg");
360 progress = *p_progress;
361 if (!progress)
362 return;
363 *p_progress = NULL;
365 finish_if_sparse(progress);
366 if (progress->last_value != -1)
367 force_last_update(progress, msg);
368 log_trace2(progress);
370 clear_progress_signal();
371 strbuf_release(&progress->counters_sb);
372 if (progress->throughput)
373 strbuf_release(&progress->throughput->display);
374 free(progress->throughput);
375 free(progress);