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
;
91 int last_count_len
= counters_sb
->len
;
93 if (progress
->delay
&& (!progress_update
|| --progress
->delay
))
96 progress
->last_value
= n
;
97 tp
= (progress
->throughput
) ? progress
->throughput
->display
.buf
: "";
98 if (progress
->total
) {
99 unsigned percent
= n
* 100 / progress
->total
;
100 if (percent
!= progress
->last_percent
|| progress_update
) {
101 progress
->last_percent
= percent
;
103 strbuf_reset(counters_sb
);
104 strbuf_addf(counters_sb
,
105 "%3u%% (%"PRIuMAX
"/%"PRIuMAX
")%s", percent
,
106 (uintmax_t)n
, (uintmax_t)progress
->total
,
110 } else if (progress_update
) {
111 strbuf_reset(counters_sb
);
112 strbuf_addf(counters_sb
, "%"PRIuMAX
"%s", (uintmax_t)n
, tp
);
117 if (is_foreground_fd(fileno(stderr
)) || done
) {
118 const char *eol
= done
? done
: "\r";
119 size_t clear_len
= counters_sb
->len
< last_count_len
?
120 last_count_len
- counters_sb
->len
+ 1 :
122 size_t progress_line_len
= progress
->title_len
+
123 counters_sb
->len
+ 2;
124 int cols
= term_columns();
126 if (progress
->split
) {
127 fprintf(stderr
, " %s%*s", counters_sb
->buf
,
128 (int) clear_len
, eol
);
129 } else if (!done
&& cols
< progress_line_len
) {
130 clear_len
= progress
->title_len
+ 1 < cols
?
131 cols
- progress
->title_len
: 0;
132 fprintf(stderr
, "%s:%*s\n %s%s",
133 progress
->title
, (int) clear_len
, "",
134 counters_sb
->buf
, eol
);
137 fprintf(stderr
, "%s: %s%*s", progress
->title
,
138 counters_sb
->buf
, (int) clear_len
, eol
);
146 static void throughput_string(struct strbuf
*buf
, uint64_t total
,
150 strbuf_addstr(buf
, ", ");
151 strbuf_humanise_bytes(buf
, total
);
152 strbuf_addstr(buf
, " | ");
153 strbuf_humanise_bytes(buf
, rate
* 1024);
154 strbuf_addstr(buf
, "/s");
157 void display_throughput(struct progress
*progress
, uint64_t total
)
159 struct throughput
*tp
;
161 unsigned int misecs
, count
, rate
;
165 tp
= progress
->throughput
;
167 now_ns
= getnanotime();
170 progress
->throughput
= tp
= calloc(1, sizeof(*tp
));
172 tp
->prev_total
= tp
->curr_total
= total
;
173 tp
->prev_ns
= now_ns
;
174 strbuf_init(&tp
->display
, 0);
178 tp
->curr_total
= total
;
180 /* only update throughput every 0.5 s */
181 if (now_ns
- tp
->prev_ns
<= 500000000)
185 * We have x = bytes and y = nanosecs. We want z = KiB/s:
187 * z = (x / 1024) / (y / 1000000000)
188 * z = x / y * 1000000000 / 1024
189 * z = x / (y * 1024 / 1000000000)
192 * To simplify things we'll keep track of misecs, or 1024th of a sec
195 * y' = y * 1024 / 1000000000
196 * y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
197 * y' = y / 2^32 * 4398
198 * y' = (y * 4398) >> 32
200 misecs
= ((now_ns
- tp
->prev_ns
) * 4398) >> 32;
202 count
= total
- tp
->prev_total
;
203 tp
->prev_total
= total
;
204 tp
->prev_ns
= now_ns
;
205 tp
->avg_bytes
+= count
;
206 tp
->avg_misecs
+= misecs
;
207 rate
= tp
->avg_bytes
/ tp
->avg_misecs
;
208 tp
->avg_bytes
-= tp
->last_bytes
[tp
->idx
];
209 tp
->avg_misecs
-= tp
->last_misecs
[tp
->idx
];
210 tp
->last_bytes
[tp
->idx
] = count
;
211 tp
->last_misecs
[tp
->idx
] = misecs
;
212 tp
->idx
= (tp
->idx
+ 1) % TP_IDX_MAX
;
214 throughput_string(&tp
->display
, total
, rate
);
215 if (progress
->last_value
!= -1 && progress_update
)
216 display(progress
, progress
->last_value
, NULL
);
219 void display_progress(struct progress
*progress
, uint64_t n
)
222 display(progress
, n
, NULL
);
225 static struct progress
*start_progress_delay(const char *title
, uint64_t total
,
226 unsigned delay
, unsigned sparse
)
228 struct progress
*progress
= malloc(sizeof(*progress
));
230 /* unlikely, but here's a good fallback */
231 fprintf(stderr
, "%s...\n", title
);
235 progress
->title
= title
;
236 progress
->total
= total
;
237 progress
->last_value
= -1;
238 progress
->last_percent
= -1;
239 progress
->delay
= delay
;
240 progress
->sparse
= sparse
;
241 progress
->throughput
= NULL
;
242 progress
->start_ns
= getnanotime();
243 strbuf_init(&progress
->counters_sb
, 0);
244 progress
->title_len
= utf8_strwidth(title
);
246 set_progress_signal();
250 struct progress
*start_delayed_progress(const char *title
, uint64_t total
)
252 return start_progress_delay(title
, total
, 2, 0);
255 struct progress
*start_progress(const char *title
, uint64_t total
)
257 return start_progress_delay(title
, total
, 0, 0);
261 * Here "sparse" means that the caller might use some sampling criteria to
262 * decide when to call display_progress() rather than calling it for every
263 * integer value in[0 .. total). In particular, the caller might not call
264 * display_progress() for the last value in the range.
266 * When "sparse" is set, stop_progress() will automatically force the done
267 * message to show 100%.
269 struct progress
*start_sparse_progress(const char *title
, uint64_t total
)
271 return start_progress_delay(title
, total
, 0, 1);
274 struct progress
*start_delayed_sparse_progress(const char *title
,
277 return start_progress_delay(title
, total
, 2, 1);
280 static void finish_if_sparse(struct progress
*progress
)
284 progress
->last_value
!= progress
->total
)
285 display_progress(progress
, progress
->total
);
288 void stop_progress(struct progress
**p_progress
)
290 finish_if_sparse(*p_progress
);
292 stop_progress_msg(p_progress
, _("done"));
295 void stop_progress_msg(struct progress
**p_progress
, const char *msg
)
297 struct progress
*progress
= *p_progress
;
301 if (progress
->last_value
!= -1) {
302 /* Force the last update */
304 struct throughput
*tp
= progress
->throughput
;
307 uint64_t now_ns
= getnanotime();
308 unsigned int misecs
, rate
;
309 misecs
= ((now_ns
- progress
->start_ns
) * 4398) >> 32;
310 rate
= tp
->curr_total
/ (misecs
? misecs
: 1);
311 throughput_string(&tp
->display
, tp
->curr_total
, rate
);
314 buf
= xstrfmt(", %s.\n", msg
);
315 display(progress
, progress
->last_value
, buf
);
318 clear_progress_signal();
319 strbuf_release(&progress
->counters_sb
);
320 if (progress
->throughput
)
321 strbuf_release(&progress
->throughput
->display
);
322 free(progress
->throughput
);