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
;
39 struct strbuf counters_sb
;
42 static volatile sig_atomic_t progress_update
;
44 static void progress_interval(int signum
)
49 static void set_progress_signal(void)
56 memset(&sa
, 0, sizeof(sa
));
57 sa
.sa_handler
= progress_interval
;
58 sigemptyset(&sa
.sa_mask
);
59 sa
.sa_flags
= SA_RESTART
;
60 sigaction(SIGALRM
, &sa
, NULL
);
62 v
.it_interval
.tv_sec
= 1;
63 v
.it_interval
.tv_usec
= 0;
64 v
.it_value
= v
.it_interval
;
65 setitimer(ITIMER_REAL
, &v
, NULL
);
68 static void clear_progress_signal(void)
70 struct itimerval v
= {{0,},};
71 setitimer(ITIMER_REAL
, &v
, NULL
);
72 signal(SIGALRM
, SIG_IGN
);
76 static int is_foreground_fd(int fd
)
78 int tpgrp
= tcgetpgrp(fd
);
79 return tpgrp
< 0 || tpgrp
== getpgid(0);
82 static void display(struct progress
*progress
, uint64_t n
, const char *done
)
85 struct strbuf
*counters_sb
= &progress
->counters_sb
;
87 int last_count_len
= counters_sb
->len
;
89 if (progress
->delay
&& (!progress_update
|| --progress
->delay
))
92 progress
->last_value
= n
;
93 tp
= (progress
->throughput
) ? progress
->throughput
->display
.buf
: "";
94 if (progress
->total
) {
95 unsigned percent
= n
* 100 / progress
->total
;
96 if (percent
!= progress
->last_percent
|| progress_update
) {
97 progress
->last_percent
= percent
;
99 strbuf_reset(counters_sb
);
100 strbuf_addf(counters_sb
,
101 "%3u%% (%"PRIuMAX
"/%"PRIuMAX
")%s", percent
,
102 (uintmax_t)n
, (uintmax_t)progress
->total
,
106 } else if (progress_update
) {
107 strbuf_reset(counters_sb
);
108 strbuf_addf(counters_sb
, "%"PRIuMAX
"%s", (uintmax_t)n
, tp
);
113 if (is_foreground_fd(fileno(stderr
)) || done
) {
114 const char *eol
= done
? done
: "\r";
115 size_t clear_len
= counters_sb
->len
< last_count_len
?
116 last_count_len
- counters_sb
->len
+ 1 :
118 fprintf(stderr
, "%s: %s%*s", progress
->title
,
119 counters_sb
->buf
, (int) clear_len
, eol
);
126 static void throughput_string(struct strbuf
*buf
, uint64_t total
,
130 strbuf_addstr(buf
, ", ");
131 strbuf_humanise_bytes(buf
, total
);
132 strbuf_addstr(buf
, " | ");
133 strbuf_humanise_bytes(buf
, rate
* 1024);
134 strbuf_addstr(buf
, "/s");
137 void display_throughput(struct progress
*progress
, uint64_t total
)
139 struct throughput
*tp
;
141 unsigned int misecs
, count
, rate
;
145 tp
= progress
->throughput
;
147 now_ns
= getnanotime();
150 progress
->throughput
= tp
= calloc(1, sizeof(*tp
));
152 tp
->prev_total
= tp
->curr_total
= total
;
153 tp
->prev_ns
= now_ns
;
154 strbuf_init(&tp
->display
, 0);
158 tp
->curr_total
= total
;
160 /* only update throughput every 0.5 s */
161 if (now_ns
- tp
->prev_ns
<= 500000000)
165 * We have x = bytes and y = nanosecs. We want z = KiB/s:
167 * z = (x / 1024) / (y / 1000000000)
168 * z = x / y * 1000000000 / 1024
169 * z = x / (y * 1024 / 1000000000)
172 * To simplify things we'll keep track of misecs, or 1024th of a sec
175 * y' = y * 1024 / 1000000000
176 * y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
177 * y' = y / 2^32 * 4398
178 * y' = (y * 4398) >> 32
180 misecs
= ((now_ns
- tp
->prev_ns
) * 4398) >> 32;
182 count
= total
- tp
->prev_total
;
183 tp
->prev_total
= total
;
184 tp
->prev_ns
= now_ns
;
185 tp
->avg_bytes
+= count
;
186 tp
->avg_misecs
+= misecs
;
187 rate
= tp
->avg_bytes
/ tp
->avg_misecs
;
188 tp
->avg_bytes
-= tp
->last_bytes
[tp
->idx
];
189 tp
->avg_misecs
-= tp
->last_misecs
[tp
->idx
];
190 tp
->last_bytes
[tp
->idx
] = count
;
191 tp
->last_misecs
[tp
->idx
] = misecs
;
192 tp
->idx
= (tp
->idx
+ 1) % TP_IDX_MAX
;
194 throughput_string(&tp
->display
, total
, rate
);
195 if (progress
->last_value
!= -1 && progress_update
)
196 display(progress
, progress
->last_value
, NULL
);
199 void display_progress(struct progress
*progress
, uint64_t n
)
202 display(progress
, n
, NULL
);
205 static struct progress
*start_progress_delay(const char *title
, uint64_t total
,
208 struct progress
*progress
= malloc(sizeof(*progress
));
210 /* unlikely, but here's a good fallback */
211 fprintf(stderr
, "%s...\n", title
);
215 progress
->title
= title
;
216 progress
->total
= total
;
217 progress
->last_value
= -1;
218 progress
->last_percent
= -1;
219 progress
->delay
= delay
;
220 progress
->throughput
= NULL
;
221 progress
->start_ns
= getnanotime();
222 strbuf_init(&progress
->counters_sb
, 0);
223 set_progress_signal();
227 struct progress
*start_delayed_progress(const char *title
, uint64_t total
)
229 return start_progress_delay(title
, total
, 2);
232 struct progress
*start_progress(const char *title
, uint64_t total
)
234 return start_progress_delay(title
, total
, 0);
237 void stop_progress(struct progress
**p_progress
)
239 stop_progress_msg(p_progress
, _("done"));
242 void stop_progress_msg(struct progress
**p_progress
, const char *msg
)
244 struct progress
*progress
= *p_progress
;
248 if (progress
->last_value
!= -1) {
249 /* Force the last update */
251 struct throughput
*tp
= progress
->throughput
;
254 uint64_t now_ns
= getnanotime();
255 unsigned int misecs
, rate
;
256 misecs
= ((now_ns
- progress
->start_ns
) * 4398) >> 32;
257 rate
= tp
->curr_total
/ (misecs
? misecs
: 1);
258 throughput_string(&tp
->display
, tp
->curr_total
, rate
);
261 buf
= xstrfmt(", %s.\n", msg
);
262 display(progress
, progress
->last_value
, buf
);
265 clear_progress_signal();
266 strbuf_release(&progress
->counters_sb
);
267 if (progress
->throughput
)
268 strbuf_release(&progress
->throughput
->display
);
269 free(progress
->throughput
);