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"
21 struct timeval prev_tv;
22 unsigned int avg_bytes;
23 unsigned int avg_misecs;
24 unsigned int last_bytes[TP_IDX_MAX];
25 unsigned int last_misecs[TP_IDX_MAX];
34 unsigned last_percent;
36 unsigned delayed_percent_treshold;
37 struct throughput *throughput;
40 static volatile sig_atomic_t progress_update;
42 static void progress_interval(int signum)
47 static void set_progress_signal(void)
54 memset(&sa, 0, sizeof(sa));
55 sa.sa_handler = progress_interval;
56 sigemptyset(&sa.sa_mask);
57 sa.sa_flags = SA_RESTART;
58 sigaction(SIGALRM, &sa, NULL);
60 v.it_interval.tv_sec = 1;
61 v.it_interval.tv_usec = 0;
62 v.it_value = v.it_interval;
63 setitimer(ITIMER_REAL, &v, NULL);
66 static void clear_progress_signal(void)
68 struct itimerval v = {{0,},};
69 setitimer(ITIMER_REAL, &v, NULL);
70 signal(SIGALRM, SIG_IGN);
74 static int display(struct progress *progress, unsigned n, const char *done)
78 if (progress->delay) {
79 if (!progress_update || --progress->delay)
81 if (progress->total) {
82 unsigned percent = n * 100 / progress->total;
83 if (percent > progress->delayed_percent_treshold) {
84 /* inhibit this progress report entirely */
85 clear_progress_signal();
93 progress->last_value = n;
94 tp = (progress->throughput) ? progress->throughput->display : "";
95 eol = done ? done : " \r";
96 if (progress->total) {
97 unsigned percent = n * 100 / progress->total;
98 if (percent != progress->last_percent || progress_update) {
99 progress->last_percent = percent;
100 fprintf(stderr, "%s: %3u%% (%u/%u)%s%s",
101 progress->title, percent, n,
102 progress->total, tp, eol);
107 } else if (progress_update) {
108 fprintf(stderr, "%s: %u%s%s", progress->title, n, tp, eol);
117 static void throughput_string(struct strbuf *buf, off_t total,
120 strbuf_addstr(buf, ", ");
121 strbuf_humanise_bytes(buf, total);
122 strbuf_addstr(buf, " | ");
123 strbuf_humanise_bytes(buf, rate * 1024);
124 strbuf_addstr(buf, "/s");
127 void display_throughput(struct progress *progress, off_t total)
129 struct throughput *tp;
135 tp = progress->throughput;
137 gettimeofday(&tv, NULL);
140 progress->throughput = tp = calloc(1, sizeof(*tp));
142 tp->prev_total = tp->curr_total = total;
147 tp->curr_total = total;
150 * We have x = bytes and y = microsecs. We want z = KiB/s:
152 * z = (x / 1024) / (y / 1000000)
153 * z = x / y * 1000000 / 1024
154 * z = x / (y * 1024 / 1000000)
157 * To simplify things we'll keep track of misecs, or 1024th of a sec
160 * y' = y * 1024 / 1000000
161 * y' = y / (1000000 / 1024)
164 misecs = (tv.tv_sec - tp->prev_tv.tv_sec) * 1024;
165 misecs += (int)(tv.tv_usec - tp->prev_tv.tv_usec) / 977;
168 struct strbuf buf = STRBUF_INIT;
169 unsigned int count, rate;
171 count = total - tp->prev_total;
172 tp->prev_total = total;
174 tp->avg_bytes += count;
175 tp->avg_misecs += misecs;
176 rate = tp->avg_bytes / tp->avg_misecs;
177 tp->avg_bytes -= tp->last_bytes[tp->idx];
178 tp->avg_misecs -= tp->last_misecs[tp->idx];
179 tp->last_bytes[tp->idx] = count;
180 tp->last_misecs[tp->idx] = misecs;
181 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
183 throughput_string(&buf, total, rate);
184 strncpy(tp->display, buf.buf, sizeof(tp->display));
185 strbuf_release(&buf);
186 if (progress->last_value != -1 && progress_update)
187 display(progress, progress->last_value, NULL);
191 int display_progress(struct progress *progress, unsigned n)
193 return progress ? display(progress, n, NULL) : 0;
196 struct progress *start_progress_delay(const char *title, unsigned total,
197 unsigned percent_treshold, unsigned delay)
199 struct progress *progress = malloc(sizeof(*progress));
201 /* unlikely, but here's a good fallback */
202 fprintf(stderr, "%s...\n", title);
206 progress->title = title;
207 progress->total = total;
208 progress->last_value = -1;
209 progress->last_percent = -1;
210 progress->delayed_percent_treshold = percent_treshold;
211 progress->delay = delay;
212 progress->throughput = NULL;
213 set_progress_signal();
217 struct progress *start_progress(const char *title, unsigned total)
219 return start_progress_delay(title, total, 0, 0);
222 void stop_progress(struct progress **p_progress)
224 stop_progress_msg(p_progress, _("done"));
227 void stop_progress_msg(struct progress **p_progress, const char *msg)
229 struct progress *progress = *p_progress;
233 if (progress->last_value != -1) {
234 /* Force the last update */
235 char buf[128], *bufp;
236 size_t len = strlen(msg) + 5;
237 struct throughput *tp = progress->throughput;
239 bufp = (len < sizeof(buf)) ? buf : xmalloc(len + 1);
241 struct strbuf strbuf = STRBUF_INIT;
242 unsigned int rate = !tp->avg_misecs ? 0 :
243 tp->avg_bytes / tp->avg_misecs;
244 throughput_string(&strbuf, tp->curr_total, rate);
245 strncpy(tp->display, strbuf.buf, sizeof(tp->display));
246 strbuf_release(&strbuf);
249 sprintf(bufp, ", %s.\n", msg);
250 display(progress, progress->last_value, bufp);
254 clear_progress_signal();
255 free(progress->throughput);