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 #define GIT_TEST_PROGRESS_ONLY
12 #include "git-compat-util.h"
27 unsigned int avg_bytes
;
28 unsigned int avg_misecs
;
29 unsigned int last_bytes
[TP_IDX_MAX
];
30 unsigned int last_misecs
[TP_IDX_MAX
];
32 struct strbuf display
;
39 unsigned last_percent
;
42 struct throughput
*throughput
;
44 struct strbuf counters_sb
;
49 static volatile sig_atomic_t progress_update
;
52 * These are only intended for testing the progress output, i.e. exclusively
53 * for 'test-tool progress'.
56 uint64_t progress_test_ns
= 0;
57 void progress_test_force_update(void)
63 static void progress_interval(int signum UNUSED
)
68 static void set_progress_signal(void)
78 memset(&sa
, 0, sizeof(sa
));
79 sa
.sa_handler
= progress_interval
;
80 sigemptyset(&sa
.sa_mask
);
81 sa
.sa_flags
= SA_RESTART
;
82 sigaction(SIGALRM
, &sa
, NULL
);
84 v
.it_interval
.tv_sec
= 1;
85 v
.it_interval
.tv_usec
= 0;
86 v
.it_value
= v
.it_interval
;
87 setitimer(ITIMER_REAL
, &v
, NULL
);
90 static void clear_progress_signal(void)
92 struct itimerval v
= {{0,},};
97 setitimer(ITIMER_REAL
, &v
, NULL
);
98 signal(SIGALRM
, SIG_IGN
);
102 static int is_foreground_fd(int fd
)
104 int tpgrp
= tcgetpgrp(fd
);
105 return tpgrp
< 0 || tpgrp
== getpgid(0);
108 static void display(struct progress
*progress
, uint64_t n
, const char *done
)
111 struct strbuf
*counters_sb
= &progress
->counters_sb
;
113 int last_count_len
= counters_sb
->len
;
115 if (progress
->delay
&& (!progress_update
|| --progress
->delay
))
118 progress
->last_value
= n
;
119 tp
= (progress
->throughput
) ? progress
->throughput
->display
.buf
: "";
120 if (progress
->total
) {
121 unsigned percent
= n
* 100 / progress
->total
;
122 if (percent
!= progress
->last_percent
|| progress_update
) {
123 progress
->last_percent
= percent
;
125 strbuf_reset(counters_sb
);
126 strbuf_addf(counters_sb
,
127 "%3u%% (%"PRIuMAX
"/%"PRIuMAX
")%s", percent
,
128 (uintmax_t)n
, (uintmax_t)progress
->total
,
132 } else if (progress_update
) {
133 strbuf_reset(counters_sb
);
134 strbuf_addf(counters_sb
, "%"PRIuMAX
"%s", (uintmax_t)n
, tp
);
139 if (is_foreground_fd(fileno(stderr
)) || done
) {
140 const char *eol
= done
? done
: "\r";
141 size_t clear_len
= counters_sb
->len
< last_count_len
?
142 last_count_len
- counters_sb
->len
+ 1 :
144 /* The "+ 2" accounts for the ": ". */
145 size_t progress_line_len
= progress
->title_len
+
146 counters_sb
->len
+ 2;
147 int cols
= term_columns();
149 if (progress
->split
) {
150 fprintf(stderr
, " %s%*s", counters_sb
->buf
,
151 (int) clear_len
, eol
);
152 } else if (!done
&& cols
< progress_line_len
) {
153 clear_len
= progress
->title_len
+ 1 < cols
?
154 cols
- progress
->title_len
- 1 : 0;
155 fprintf(stderr
, "%s:%*s\n %s%s",
156 progress
->title
, (int) clear_len
, "",
157 counters_sb
->buf
, eol
);
160 fprintf(stderr
, "%s: %s%*s", progress
->title
,
161 counters_sb
->buf
, (int) clear_len
, eol
);
169 static void throughput_string(struct strbuf
*buf
, uint64_t total
,
173 strbuf_addstr(buf
, ", ");
174 strbuf_humanise_bytes(buf
, total
);
175 strbuf_addstr(buf
, " | ");
176 strbuf_humanise_rate(buf
, rate
* 1024);
179 static uint64_t progress_getnanotime(struct progress
*progress
)
181 if (progress_testing
)
182 return progress
->start_ns
+ progress_test_ns
;
184 return getnanotime();
187 void display_throughput(struct progress
*progress
, uint64_t total
)
189 struct throughput
*tp
;
191 unsigned int misecs
, count
, rate
;
195 tp
= progress
->throughput
;
197 now_ns
= progress_getnanotime(progress
);
200 progress
->throughput
= CALLOC_ARRAY(tp
, 1);
201 tp
->prev_total
= tp
->curr_total
= total
;
202 tp
->prev_ns
= now_ns
;
203 strbuf_init(&tp
->display
, 0);
206 tp
->curr_total
= total
;
208 /* only update throughput every 0.5 s */
209 if (now_ns
- tp
->prev_ns
<= 500000000)
213 * We have x = bytes and y = nanosecs. We want z = KiB/s:
215 * z = (x / 1024) / (y / 1000000000)
216 * z = x / y * 1000000000 / 1024
217 * z = x / (y * 1024 / 1000000000)
220 * To simplify things we'll keep track of misecs, or 1024th of a sec
223 * y' = y * 1024 / 1000000000
224 * y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
225 * y' = y / 2^32 * 4398
226 * y' = (y * 4398) >> 32
228 misecs
= ((now_ns
- tp
->prev_ns
) * 4398) >> 32;
230 count
= total
- tp
->prev_total
;
231 tp
->prev_total
= total
;
232 tp
->prev_ns
= now_ns
;
233 tp
->avg_bytes
+= count
;
234 tp
->avg_misecs
+= misecs
;
235 rate
= tp
->avg_bytes
/ tp
->avg_misecs
;
236 tp
->avg_bytes
-= tp
->last_bytes
[tp
->idx
];
237 tp
->avg_misecs
-= tp
->last_misecs
[tp
->idx
];
238 tp
->last_bytes
[tp
->idx
] = count
;
239 tp
->last_misecs
[tp
->idx
] = misecs
;
240 tp
->idx
= (tp
->idx
+ 1) % TP_IDX_MAX
;
242 throughput_string(&tp
->display
, total
, rate
);
243 if (progress
->last_value
!= -1 && progress_update
)
244 display(progress
, progress
->last_value
, NULL
);
247 void display_progress(struct progress
*progress
, uint64_t n
)
250 display(progress
, n
, NULL
);
253 static struct progress
*start_progress_delay(const char *title
, uint64_t total
,
254 unsigned delay
, unsigned sparse
)
256 struct progress
*progress
= xmalloc(sizeof(*progress
));
257 progress
->title
= title
;
258 progress
->total
= total
;
259 progress
->last_value
= -1;
260 progress
->last_percent
= -1;
261 progress
->delay
= delay
;
262 progress
->sparse
= sparse
;
263 progress
->throughput
= NULL
;
264 progress
->start_ns
= getnanotime();
265 strbuf_init(&progress
->counters_sb
, 0);
266 progress
->title_len
= utf8_strwidth(title
);
268 set_progress_signal();
269 trace2_region_enter("progress", title
, the_repository
);
273 static int get_default_delay(void)
275 static int delay_in_secs
= -1;
277 if (delay_in_secs
< 0)
278 delay_in_secs
= git_env_ulong("GIT_PROGRESS_DELAY", 2);
280 return delay_in_secs
;
283 struct progress
*start_delayed_progress(const char *title
, uint64_t total
)
285 return start_progress_delay(title
, total
, get_default_delay(), 0);
288 struct progress
*start_progress(const char *title
, uint64_t total
)
290 return start_progress_delay(title
, total
, 0, 0);
294 * Here "sparse" means that the caller might use some sampling criteria to
295 * decide when to call display_progress() rather than calling it for every
296 * integer value in[0 .. total). In particular, the caller might not call
297 * display_progress() for the last value in the range.
299 * When "sparse" is set, stop_progress() will automatically force the done
300 * message to show 100%.
302 struct progress
*start_sparse_progress(const char *title
, uint64_t total
)
304 return start_progress_delay(title
, total
, 0, 1);
307 struct progress
*start_delayed_sparse_progress(const char *title
,
310 return start_progress_delay(title
, total
, get_default_delay(), 1);
313 static void finish_if_sparse(struct progress
*progress
)
315 if (progress
->sparse
&&
316 progress
->last_value
!= progress
->total
)
317 display_progress(progress
, progress
->total
);
320 static void force_last_update(struct progress
*progress
, const char *msg
)
323 struct throughput
*tp
= progress
->throughput
;
326 uint64_t now_ns
= progress_getnanotime(progress
);
327 unsigned int misecs
, rate
;
328 misecs
= ((now_ns
- progress
->start_ns
) * 4398) >> 32;
329 rate
= tp
->curr_total
/ (misecs
? misecs
: 1);
330 throughput_string(&tp
->display
, tp
->curr_total
, rate
);
333 buf
= xstrfmt(", %s.\n", msg
);
334 display(progress
, progress
->last_value
, buf
);
338 static void log_trace2(struct progress
*progress
)
340 trace2_data_intmax("progress", the_repository
, "total_objects",
343 if (progress
->throughput
)
344 trace2_data_intmax("progress", the_repository
, "total_bytes",
345 progress
->throughput
->curr_total
);
347 trace2_region_leave("progress", progress
->title
, the_repository
);
350 void stop_progress_msg(struct progress
**p_progress
, const char *msg
)
352 struct progress
*progress
;
355 BUG("don't provide NULL to stop_progress_msg");
357 progress
= *p_progress
;
362 finish_if_sparse(progress
);
363 if (progress
->last_value
!= -1)
364 force_last_update(progress
, msg
);
365 log_trace2(progress
);
367 clear_progress_signal();
368 strbuf_release(&progress
->counters_sb
);
369 if (progress
->throughput
)
370 strbuf_release(&progress
->throughput
->display
);
371 free(progress
->throughput
);