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.
24 unsigned int avg_bytes;
25 unsigned int avg_misecs;
26 unsigned int last_bytes[TP_IDX_MAX];
27 unsigned int last_misecs[TP_IDX_MAX];
29 struct strbuf display;
36 unsigned last_percent;
38 struct throughput *throughput;
40 struct strbuf counters_sb;
45 static volatile sig_atomic_t progress_update;
47 static void progress_interval(int signum)
52 static void set_progress_signal(void)
59 memset(&sa, 0, sizeof(sa));
60 sa.sa_handler = progress_interval;
61 sigemptyset(&sa.sa_mask);
62 sa.sa_flags = SA_RESTART;
63 sigaction(SIGALRM, &sa, NULL);
65 v.it_interval.tv_sec = 1;
66 v.it_interval.tv_usec = 0;
67 v.it_value = v.it_interval;
68 setitimer(ITIMER_REAL, &v, NULL);
71 static void clear_progress_signal(void)
73 struct itimerval v = {{0,},};
74 setitimer(ITIMER_REAL, &v, NULL);
75 signal(SIGALRM, SIG_IGN);
79 static int is_foreground_fd(int fd)
81 int tpgrp = tcgetpgrp(fd);
82 return tpgrp < 0 || tpgrp == getpgid(0);
85 static void display(struct progress *progress, uint64_t n, const char *done)
88 struct strbuf *counters_sb = &progress->counters_sb;
90 int last_count_len = counters_sb->len;
92 if (progress->delay && (!progress_update || --progress->delay))
95 progress->last_value = n;
96 tp = (progress->throughput) ? progress->throughput->display.buf : "";
97 if (progress->total) {
98 unsigned percent = n * 100 / progress->total;
99 if (percent != progress->last_percent || progress_update) {
100 progress->last_percent = percent;
102 strbuf_reset(counters_sb);
103 strbuf_addf(counters_sb,
104 "%3u%% (%"PRIuMAX"/%"PRIuMAX")%s", percent,
105 (uintmax_t)n, (uintmax_t)progress->total,
109 } else if (progress_update) {
110 strbuf_reset(counters_sb);
111 strbuf_addf(counters_sb, "%"PRIuMAX"%s", (uintmax_t)n, tp);
116 if (is_foreground_fd(fileno(stderr)) || done) {
117 const char *eol = done ? done : "\r";
118 size_t clear_len = counters_sb->len < last_count_len ?
119 last_count_len - counters_sb->len + 1 :
121 size_t progress_line_len = progress->title_len +
122 counters_sb->len + 2;
123 int cols = term_columns();
125 if (progress->split) {
126 fprintf(stderr, " %s%*s", counters_sb->buf,
127 (int) clear_len, eol);
128 } else if (!done && cols < progress_line_len) {
129 clear_len = progress->title_len + 1 < cols ?
130 cols - progress->title_len - 1 : 0;
131 fprintf(stderr, "%s:%*s\n %s%s",
132 progress->title, (int) clear_len, "",
133 counters_sb->buf, eol);
136 fprintf(stderr, "%s: %s%*s", progress->title,
137 counters_sb->buf, (int) clear_len, eol);
145 static void throughput_string(struct strbuf *buf, uint64_t total,
149 strbuf_addstr(buf, ", ");
150 strbuf_humanise_bytes(buf, total);
151 strbuf_addstr(buf, " | ");
152 strbuf_humanise_bytes(buf, rate * 1024);
153 strbuf_addstr(buf, "/s");
156 void display_throughput(struct progress *progress, uint64_t total)
158 struct throughput *tp;
160 unsigned int misecs, count, rate;
164 tp = progress->throughput;
166 now_ns = getnanotime();
169 progress->throughput = tp = calloc(1, sizeof(*tp));
171 tp->prev_total = tp->curr_total = total;
172 tp->prev_ns = now_ns;
173 strbuf_init(&tp->display, 0);
177 tp->curr_total = total;
179 /* only update throughput every 0.5 s */
180 if (now_ns - tp->prev_ns <= 500000000)
184 * We have x = bytes and y = nanosecs. We want z = KiB/s:
186 * z = (x / 1024) / (y / 1000000000)
187 * z = x / y * 1000000000 / 1024
188 * z = x / (y * 1024 / 1000000000)
191 * To simplify things we'll keep track of misecs, or 1024th of a sec
194 * y' = y * 1024 / 1000000000
195 * y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
196 * y' = y / 2^32 * 4398
197 * y' = (y * 4398) >> 32
199 misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
201 count = total - tp->prev_total;
202 tp->prev_total = total;
203 tp->prev_ns = now_ns;
204 tp->avg_bytes += count;
205 tp->avg_misecs += misecs;
206 rate = tp->avg_bytes / tp->avg_misecs;
207 tp->avg_bytes -= tp->last_bytes[tp->idx];
208 tp->avg_misecs -= tp->last_misecs[tp->idx];
209 tp->last_bytes[tp->idx] = count;
210 tp->last_misecs[tp->idx] = misecs;
211 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
213 throughput_string(&tp->display, total, rate);
214 if (progress->last_value != -1 && progress_update)
215 display(progress, progress->last_value, NULL);
218 void display_progress(struct progress *progress, uint64_t n)
221 display(progress, n, NULL);
224 static struct progress *start_progress_delay(const char *title, uint64_t total,
227 struct progress *progress = malloc(sizeof(*progress));
229 /* unlikely, but here's a good fallback */
230 fprintf(stderr, "%s...\n", title);
234 progress->title = title;
235 progress->total = total;
236 progress->last_value = -1;
237 progress->last_percent = -1;
238 progress->delay = delay;
239 progress->throughput = NULL;
240 progress->start_ns = getnanotime();
241 strbuf_init(&progress->counters_sb, 0);
242 progress->title_len = utf8_strwidth(title);
244 set_progress_signal();
248 struct progress *start_delayed_progress(const char *title, uint64_t total)
250 return start_progress_delay(title, total, 2);
253 struct progress *start_progress(const char *title, uint64_t total)
255 return start_progress_delay(title, total, 0);
258 void stop_progress(struct progress **p_progress)
260 stop_progress_msg(p_progress, _("done"));
263 void stop_progress_msg(struct progress **p_progress, const char *msg)
265 struct progress *progress = *p_progress;
269 if (progress->last_value != -1) {
270 /* Force the last update */
272 struct throughput *tp = progress->throughput;
275 uint64_t now_ns = getnanotime();
276 unsigned int misecs, rate;
277 misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
278 rate = tp->curr_total / (misecs ? misecs : 1);
279 throughput_string(&tp->display, tp->curr_total, rate);
282 buf = xstrfmt(", %s.\n", msg);
283 display(progress, progress->last_value, buf);
286 clear_progress_signal();
287 strbuf_release(&progress->counters_sb);
288 if (progress->throughput)
289 strbuf_release(&progress->throughput->display);
290 free(progress->throughput);