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.
11 #include "git-compat-util.h"
23 unsigned int avg_bytes;
24 unsigned int avg_misecs;
25 unsigned int last_bytes[TP_IDX_MAX];
26 unsigned int last_misecs[TP_IDX_MAX];
28 struct strbuf display;
35 unsigned last_percent;
37 struct throughput *throughput;
41 static volatile sig_atomic_t progress_update;
43 static void progress_interval(int signum)
48 static void set_progress_signal(void)
55 memset(&sa, 0, sizeof(sa));
56 sa.sa_handler = progress_interval;
57 sigemptyset(&sa.sa_mask);
58 sa.sa_flags = SA_RESTART;
59 sigaction(SIGALRM, &sa, NULL);
61 v.it_interval.tv_sec = 1;
62 v.it_interval.tv_usec = 0;
63 v.it_value = v.it_interval;
64 setitimer(ITIMER_REAL, &v, NULL);
67 static void clear_progress_signal(void)
69 struct itimerval v = {{0,},};
70 setitimer(ITIMER_REAL, &v, NULL);
71 signal(SIGALRM, SIG_IGN);
75 static int is_foreground_fd(int fd)
77 int tpgrp = tcgetpgrp(fd);
78 return tpgrp < 0 || tpgrp == getpgid(0);
81 static int display(struct progress *progress, uint64_t n, const char *done)
85 if (progress->delay && (!progress_update || --progress->delay))
88 progress->last_value = n;
89 tp = (progress->throughput) ? progress->throughput->display.buf : "";
90 eol = done ? done : " \r";
91 if (progress->total) {
92 unsigned percent = n * 100 / progress->total;
93 if (percent != progress->last_percent || progress_update) {
94 progress->last_percent = percent;
95 if (is_foreground_fd(fileno(stderr)) || done) {
96 fprintf(stderr, "%s: %3u%% (%"PRIuMAX"/%"PRIuMAX")%s%s",
97 progress->title, percent,
98 (uintmax_t)n, (uintmax_t)progress->total,
105 } else if (progress_update) {
106 if (is_foreground_fd(fileno(stderr)) || done) {
107 fprintf(stderr, "%s: %"PRIuMAX"%s%s",
108 progress->title, (uintmax_t)n, tp, eol);
118 static void throughput_string(struct strbuf *buf, uint64_t total,
122 strbuf_addstr(buf, ", ");
123 strbuf_humanise_bytes(buf, total);
124 strbuf_addstr(buf, " | ");
125 strbuf_humanise_bytes(buf, rate * 1024);
126 strbuf_addstr(buf, "/s");
129 void display_throughput(struct progress *progress, uint64_t total)
131 struct throughput *tp;
133 unsigned int misecs, count, rate;
137 tp = progress->throughput;
139 now_ns = getnanotime();
142 progress->throughput = tp = xcalloc(1, sizeof(*tp));
143 tp->prev_total = tp->curr_total = total;
144 tp->prev_ns = now_ns;
145 strbuf_init(&tp->display, 0);
148 tp->curr_total = total;
150 /* only update throughput every 0.5 s */
151 if (now_ns - tp->prev_ns <= 500000000)
155 * We have x = bytes and y = nanosecs. We want z = KiB/s:
157 * z = (x / 1024) / (y / 1000000000)
158 * z = x / y * 1000000000 / 1024
159 * z = x / (y * 1024 / 1000000000)
162 * To simplify things we'll keep track of misecs, or 1024th of a sec
165 * y' = y * 1024 / 1000000000
166 * y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
167 * y' = y / 2^32 * 4398
168 * y' = (y * 4398) >> 32
170 misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
172 count = total - tp->prev_total;
173 tp->prev_total = total;
174 tp->prev_ns = now_ns;
175 tp->avg_bytes += count;
176 tp->avg_misecs += misecs;
177 rate = tp->avg_bytes / tp->avg_misecs;
178 tp->avg_bytes -= tp->last_bytes[tp->idx];
179 tp->avg_misecs -= tp->last_misecs[tp->idx];
180 tp->last_bytes[tp->idx] = count;
181 tp->last_misecs[tp->idx] = misecs;
182 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
184 throughput_string(&tp->display, total, rate);
185 if (progress->last_value != -1 && progress_update)
186 display(progress, progress->last_value, NULL);
189 int display_progress(struct progress *progress, uint64_t n)
191 return progress ? display(progress, n, NULL) : 0;
194 static struct progress *start_progress_delay(const char *title, uint64_t total,
197 struct progress *progress = xmalloc(sizeof(*progress));
198 progress->title = title;
199 progress->total = total;
200 progress->last_value = -1;
201 progress->last_percent = -1;
202 progress->delay = delay;
203 progress->throughput = NULL;
204 progress->start_ns = getnanotime();
205 set_progress_signal();
209 struct progress *start_delayed_progress(const char *title, uint64_t total)
211 return start_progress_delay(title, total, 2);
214 struct progress *start_progress(const char *title, uint64_t total)
216 return start_progress_delay(title, total, 0);
219 void stop_progress(struct progress **p_progress)
221 stop_progress_msg(p_progress, _("done"));
224 void stop_progress_msg(struct progress **p_progress, const char *msg)
226 struct progress *progress = *p_progress;
230 if (progress->last_value != -1) {
231 /* Force the last update */
233 struct throughput *tp = progress->throughput;
236 uint64_t now_ns = getnanotime();
237 unsigned int misecs, rate;
238 misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
239 rate = tp->curr_total / (misecs ? misecs : 1);
240 throughput_string(&tp->display, tp->curr_total, rate);
243 buf = xstrfmt(", %s.\n", msg);
244 display(progress, progress->last_value, buf);
247 clear_progress_signal();
248 if (progress->throughput)
249 strbuf_release(&progress->throughput->display);
250 free(progress->throughput);