notes.c: use designated initializers for clarity
[git/debian.git] / progress.c
blob44c784d75f106c066c85b7a7a6569744332a6be2
1 /*
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.
9 */
11 #define GIT_TEST_PROGRESS_ONLY
12 #include "cache.h"
13 #include "progress.h"
14 #include "strbuf.h"
15 #include "trace.h"
16 #include "utf8.h"
17 #include "config.h"
19 #define TP_IDX_MAX 8
21 struct throughput {
22 off_t curr_total;
23 off_t prev_total;
24 uint64_t prev_ns;
25 unsigned int avg_bytes;
26 unsigned int avg_misecs;
27 unsigned int last_bytes[TP_IDX_MAX];
28 unsigned int last_misecs[TP_IDX_MAX];
29 unsigned int idx;
30 struct strbuf display;
33 struct progress {
34 const char *title;
35 uint64_t last_value;
36 uint64_t total;
37 unsigned last_percent;
38 unsigned delay;
39 unsigned sparse;
40 struct throughput *throughput;
41 uint64_t start_ns;
42 struct strbuf counters_sb;
43 int title_len;
44 int split;
47 static volatile sig_atomic_t progress_update;
50 * These are only intended for testing the progress output, i.e. exclusively
51 * for 'test-tool progress'.
53 int progress_testing;
54 uint64_t progress_test_ns = 0;
55 void progress_test_force_update(void)
57 progress_update = 1;
61 static void progress_interval(int signum UNUSED)
63 progress_update = 1;
66 static void set_progress_signal(void)
68 struct sigaction sa;
69 struct itimerval v;
71 if (progress_testing)
72 return;
74 progress_update = 0;
76 memset(&sa, 0, sizeof(sa));
77 sa.sa_handler = progress_interval;
78 sigemptyset(&sa.sa_mask);
79 sa.sa_flags = SA_RESTART;
80 sigaction(SIGALRM, &sa, NULL);
82 v.it_interval.tv_sec = 1;
83 v.it_interval.tv_usec = 0;
84 v.it_value = v.it_interval;
85 setitimer(ITIMER_REAL, &v, NULL);
88 static void clear_progress_signal(void)
90 struct itimerval v = {{0,},};
92 if (progress_testing)
93 return;
95 setitimer(ITIMER_REAL, &v, NULL);
96 signal(SIGALRM, SIG_IGN);
97 progress_update = 0;
100 static int is_foreground_fd(int fd)
102 int tpgrp = tcgetpgrp(fd);
103 return tpgrp < 0 || tpgrp == getpgid(0);
106 static void display(struct progress *progress, uint64_t n, const char *done)
108 const char *tp;
109 struct strbuf *counters_sb = &progress->counters_sb;
110 int show_update = 0;
111 int last_count_len = counters_sb->len;
113 if (progress->delay && (!progress_update || --progress->delay))
114 return;
116 progress->last_value = n;
117 tp = (progress->throughput) ? progress->throughput->display.buf : "";
118 if (progress->total) {
119 unsigned percent = n * 100 / progress->total;
120 if (percent != progress->last_percent || progress_update) {
121 progress->last_percent = percent;
123 strbuf_reset(counters_sb);
124 strbuf_addf(counters_sb,
125 "%3u%% (%"PRIuMAX"/%"PRIuMAX")%s", percent,
126 (uintmax_t)n, (uintmax_t)progress->total,
127 tp);
128 show_update = 1;
130 } else if (progress_update) {
131 strbuf_reset(counters_sb);
132 strbuf_addf(counters_sb, "%"PRIuMAX"%s", (uintmax_t)n, tp);
133 show_update = 1;
136 if (show_update) {
137 if (is_foreground_fd(fileno(stderr)) || done) {
138 const char *eol = done ? done : "\r";
139 size_t clear_len = counters_sb->len < last_count_len ?
140 last_count_len - counters_sb->len + 1 :
142 /* The "+ 2" accounts for the ": ". */
143 size_t progress_line_len = progress->title_len +
144 counters_sb->len + 2;
145 int cols = term_columns();
147 if (progress->split) {
148 fprintf(stderr, " %s%*s", counters_sb->buf,
149 (int) clear_len, eol);
150 } else if (!done && cols < progress_line_len) {
151 clear_len = progress->title_len + 1 < cols ?
152 cols - progress->title_len - 1 : 0;
153 fprintf(stderr, "%s:%*s\n %s%s",
154 progress->title, (int) clear_len, "",
155 counters_sb->buf, eol);
156 progress->split = 1;
157 } else {
158 fprintf(stderr, "%s: %s%*s", progress->title,
159 counters_sb->buf, (int) clear_len, eol);
161 fflush(stderr);
163 progress_update = 0;
167 static void throughput_string(struct strbuf *buf, uint64_t total,
168 unsigned int rate)
170 strbuf_reset(buf);
171 strbuf_addstr(buf, ", ");
172 strbuf_humanise_bytes(buf, total);
173 strbuf_addstr(buf, " | ");
174 strbuf_humanise_rate(buf, rate * 1024);
177 static uint64_t progress_getnanotime(struct progress *progress)
179 if (progress_testing)
180 return progress->start_ns + progress_test_ns;
181 else
182 return getnanotime();
185 void display_throughput(struct progress *progress, uint64_t total)
187 struct throughput *tp;
188 uint64_t now_ns;
189 unsigned int misecs, count, rate;
191 if (!progress)
192 return;
193 tp = progress->throughput;
195 now_ns = progress_getnanotime(progress);
197 if (!tp) {
198 progress->throughput = CALLOC_ARRAY(tp, 1);
199 tp->prev_total = tp->curr_total = total;
200 tp->prev_ns = now_ns;
201 strbuf_init(&tp->display, 0);
202 return;
204 tp->curr_total = total;
206 /* only update throughput every 0.5 s */
207 if (now_ns - tp->prev_ns <= 500000000)
208 return;
211 * We have x = bytes and y = nanosecs. We want z = KiB/s:
213 * z = (x / 1024) / (y / 1000000000)
214 * z = x / y * 1000000000 / 1024
215 * z = x / (y * 1024 / 1000000000)
216 * z = x / y'
218 * To simplify things we'll keep track of misecs, or 1024th of a sec
219 * obtained with:
221 * y' = y * 1024 / 1000000000
222 * y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
223 * y' = y / 2^32 * 4398
224 * y' = (y * 4398) >> 32
226 misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
228 count = total - tp->prev_total;
229 tp->prev_total = total;
230 tp->prev_ns = now_ns;
231 tp->avg_bytes += count;
232 tp->avg_misecs += misecs;
233 rate = tp->avg_bytes / tp->avg_misecs;
234 tp->avg_bytes -= tp->last_bytes[tp->idx];
235 tp->avg_misecs -= tp->last_misecs[tp->idx];
236 tp->last_bytes[tp->idx] = count;
237 tp->last_misecs[tp->idx] = misecs;
238 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
240 throughput_string(&tp->display, total, rate);
241 if (progress->last_value != -1 && progress_update)
242 display(progress, progress->last_value, NULL);
245 void display_progress(struct progress *progress, uint64_t n)
247 if (progress)
248 display(progress, n, NULL);
251 static struct progress *start_progress_delay(const char *title, uint64_t total,
252 unsigned delay, unsigned sparse)
254 struct progress *progress = xmalloc(sizeof(*progress));
255 progress->title = title;
256 progress->total = total;
257 progress->last_value = -1;
258 progress->last_percent = -1;
259 progress->delay = delay;
260 progress->sparse = sparse;
261 progress->throughput = NULL;
262 progress->start_ns = getnanotime();
263 strbuf_init(&progress->counters_sb, 0);
264 progress->title_len = utf8_strwidth(title);
265 progress->split = 0;
266 set_progress_signal();
267 trace2_region_enter("progress", title, the_repository);
268 return progress;
271 static int get_default_delay(void)
273 static int delay_in_secs = -1;
275 if (delay_in_secs < 0)
276 delay_in_secs = git_env_ulong("GIT_PROGRESS_DELAY", 2);
278 return delay_in_secs;
281 struct progress *start_delayed_progress(const char *title, uint64_t total)
283 return start_progress_delay(title, total, get_default_delay(), 0);
286 struct progress *start_progress(const char *title, uint64_t total)
288 return start_progress_delay(title, total, 0, 0);
292 * Here "sparse" means that the caller might use some sampling criteria to
293 * decide when to call display_progress() rather than calling it for every
294 * integer value in[0 .. total). In particular, the caller might not call
295 * display_progress() for the last value in the range.
297 * When "sparse" is set, stop_progress() will automatically force the done
298 * message to show 100%.
300 struct progress *start_sparse_progress(const char *title, uint64_t total)
302 return start_progress_delay(title, total, 0, 1);
305 struct progress *start_delayed_sparse_progress(const char *title,
306 uint64_t total)
308 return start_progress_delay(title, total, get_default_delay(), 1);
311 static void finish_if_sparse(struct progress *progress)
313 if (progress->sparse &&
314 progress->last_value != progress->total)
315 display_progress(progress, progress->total);
318 static void force_last_update(struct progress *progress, const char *msg)
320 char *buf;
321 struct throughput *tp = progress->throughput;
323 if (tp) {
324 uint64_t now_ns = progress_getnanotime(progress);
325 unsigned int misecs, rate;
326 misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
327 rate = tp->curr_total / (misecs ? misecs : 1);
328 throughput_string(&tp->display, tp->curr_total, rate);
330 progress_update = 1;
331 buf = xstrfmt(", %s.\n", msg);
332 display(progress, progress->last_value, buf);
333 free(buf);
336 static void log_trace2(struct progress *progress)
338 trace2_data_intmax("progress", the_repository, "total_objects",
339 progress->total);
341 if (progress->throughput)
342 trace2_data_intmax("progress", the_repository, "total_bytes",
343 progress->throughput->curr_total);
345 trace2_region_leave("progress", progress->title, the_repository);
348 void stop_progress_msg(struct progress **p_progress, const char *msg)
350 struct progress *progress;
352 if (!p_progress)
353 BUG("don't provide NULL to stop_progress_msg");
355 progress = *p_progress;
356 if (!progress)
357 return;
358 *p_progress = NULL;
360 finish_if_sparse(progress);
361 if (progress->last_value != -1)
362 force_last_update(progress, msg);
363 log_trace2(progress);
365 clear_progress_signal();
366 strbuf_release(&progress->counters_sb);
367 if (progress->throughput)
368 strbuf_release(&progress->throughput->display);
369 free(progress->throughput);
370 free(progress);