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
;
39 struct throughput
*throughput
;
41 struct strbuf counters_sb
;
46 static volatile sig_atomic_t progress_update
;
48 static void progress_interval(int signum
)
53 static void set_progress_signal(void)
60 memset(&sa
, 0, sizeof(sa
));
61 sa
.sa_handler
= progress_interval
;
62 sigemptyset(&sa
.sa_mask
);
63 sa
.sa_flags
= SA_RESTART
;
64 sigaction(SIGALRM
, &sa
, NULL
);
66 v
.it_interval
.tv_sec
= 1;
67 v
.it_interval
.tv_usec
= 0;
68 v
.it_value
= v
.it_interval
;
69 setitimer(ITIMER_REAL
, &v
, NULL
);
72 static void clear_progress_signal(void)
74 struct itimerval v
= {{0,},};
75 setitimer(ITIMER_REAL
, &v
, NULL
);
76 signal(SIGALRM
, SIG_IGN
);
80 static int is_foreground_fd(int fd
)
82 int tpgrp
= tcgetpgrp(fd
);
83 return tpgrp
< 0 || tpgrp
== getpgid(0);
86 static void display(struct progress
*progress
, uint64_t n
, const char *done
)
89 struct strbuf
*counters_sb
= &progress
->counters_sb
;
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";
120 if (progress
->split
) {
121 fprintf(stderr
, " %s%s", counters_sb
->buf
,
124 /* The "+ 2" accounts for the ": ". */
125 term_columns() < progress
->title_len
+
126 counters_sb
->len
+ 2) {
127 fprintf(stderr
, "%s:\n %s%s",
128 progress
->title
, counters_sb
->buf
, eol
);
131 fprintf(stderr
, "%s: %s%s", progress
->title
,
132 counters_sb
->buf
, eol
);
140 static void throughput_string(struct strbuf
*buf
, uint64_t total
,
144 strbuf_addstr(buf
, ", ");
145 strbuf_humanise_bytes(buf
, total
);
146 strbuf_addstr(buf
, " | ");
147 strbuf_humanise_bytes(buf
, rate
* 1024);
148 strbuf_addstr(buf
, "/s");
151 void display_throughput(struct progress
*progress
, uint64_t total
)
153 struct throughput
*tp
;
155 unsigned int misecs
, count
, rate
;
159 tp
= progress
->throughput
;
161 now_ns
= getnanotime();
164 progress
->throughput
= tp
= xcalloc(1, sizeof(*tp
));
165 tp
->prev_total
= tp
->curr_total
= total
;
166 tp
->prev_ns
= now_ns
;
167 strbuf_init(&tp
->display
, 0);
170 tp
->curr_total
= total
;
172 /* only update throughput every 0.5 s */
173 if (now_ns
- tp
->prev_ns
<= 500000000)
177 * We have x = bytes and y = nanosecs. We want z = KiB/s:
179 * z = (x / 1024) / (y / 1000000000)
180 * z = x / y * 1000000000 / 1024
181 * z = x / (y * 1024 / 1000000000)
184 * To simplify things we'll keep track of misecs, or 1024th of a sec
187 * y' = y * 1024 / 1000000000
188 * y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
189 * y' = y / 2^32 * 4398
190 * y' = (y * 4398) >> 32
192 misecs
= ((now_ns
- tp
->prev_ns
) * 4398) >> 32;
194 count
= total
- tp
->prev_total
;
195 tp
->prev_total
= total
;
196 tp
->prev_ns
= now_ns
;
197 tp
->avg_bytes
+= count
;
198 tp
->avg_misecs
+= misecs
;
199 rate
= tp
->avg_bytes
/ tp
->avg_misecs
;
200 tp
->avg_bytes
-= tp
->last_bytes
[tp
->idx
];
201 tp
->avg_misecs
-= tp
->last_misecs
[tp
->idx
];
202 tp
->last_bytes
[tp
->idx
] = count
;
203 tp
->last_misecs
[tp
->idx
] = misecs
;
204 tp
->idx
= (tp
->idx
+ 1) % TP_IDX_MAX
;
206 throughput_string(&tp
->display
, total
, rate
);
207 if (progress
->last_value
!= -1 && progress_update
)
208 display(progress
, progress
->last_value
, NULL
);
211 void display_progress(struct progress
*progress
, uint64_t n
)
214 display(progress
, n
, NULL
);
217 static struct progress
*start_progress_delay(const char *title
, uint64_t total
,
218 unsigned delay
, unsigned sparse
)
220 struct progress
*progress
= xmalloc(sizeof(*progress
));
221 progress
->title
= title
;
222 progress
->total
= total
;
223 progress
->last_value
= -1;
224 progress
->last_percent
= -1;
225 progress
->delay
= delay
;
226 progress
->sparse
= sparse
;
227 progress
->throughput
= NULL
;
228 progress
->start_ns
= getnanotime();
229 strbuf_init(&progress
->counters_sb
, 0);
230 progress
->title_len
= utf8_strwidth(title
);
232 set_progress_signal();
236 struct progress
*start_delayed_progress(const char *title
, uint64_t total
)
238 return start_progress_delay(title
, total
, 2, 0);
241 struct progress
*start_progress(const char *title
, uint64_t total
)
243 return start_progress_delay(title
, total
, 0, 0);
247 * Here "sparse" means that the caller might use some sampling criteria to
248 * decide when to call display_progress() rather than calling it for every
249 * integer value in[0 .. total). In particular, the caller might not call
250 * display_progress() for the last value in the range.
252 * When "sparse" is set, stop_progress() will automatically force the done
253 * message to show 100%.
255 struct progress
*start_sparse_progress(const char *title
, uint64_t total
)
257 return start_progress_delay(title
, total
, 0, 1);
260 struct progress
*start_delayed_sparse_progress(const char *title
,
263 return start_progress_delay(title
, total
, 2, 1);
266 static void finish_if_sparse(struct progress
*progress
)
270 progress
->last_value
!= progress
->total
)
271 display_progress(progress
, progress
->total
);
274 void stop_progress(struct progress
**p_progress
)
276 finish_if_sparse(*p_progress
);
278 stop_progress_msg(p_progress
, _("done"));
281 void stop_progress_msg(struct progress
**p_progress
, const char *msg
)
283 struct progress
*progress
= *p_progress
;
287 if (progress
->last_value
!= -1) {
288 /* Force the last update */
290 struct throughput
*tp
= progress
->throughput
;
293 uint64_t now_ns
= getnanotime();
294 unsigned int misecs
, rate
;
295 misecs
= ((now_ns
- progress
->start_ns
) * 4398) >> 32;
296 rate
= tp
->curr_total
/ (misecs
? misecs
: 1);
297 throughput_string(&tp
->display
, tp
->curr_total
, rate
);
300 buf
= xstrfmt(", %s.\n", msg
);
301 display(progress
, progress
->last_value
, buf
);
304 clear_progress_signal();
305 strbuf_release(&progress
->counters_sb
);
306 if (progress
->throughput
)
307 strbuf_release(&progress
->throughput
->display
);
308 free(progress
->throughput
);