progress: avoid empty line when breaking the progress line
[git] / progress.c
1 /*
2  * Simple text-based progress display module for GIT
3  *
4  * Copyright (c) 2007 by Nicolas Pitre <nico@fluxnic.net>
5  *
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  */
10
11 #include "cache.h"
12 #include "gettext.h"
13 #include "progress.h"
14 #include "strbuf.h"
15 #include "trace.h"
16 #include "utf8.h"
17
18 #define TP_IDX_MAX      8
19
20 struct throughput {
21         off_t curr_total;
22         off_t prev_total;
23         uint64_t prev_ns;
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];
28         unsigned int idx;
29         struct strbuf display;
30 };
31
32 struct progress {
33         const char *title;
34         uint64_t last_value;
35         uint64_t total;
36         unsigned last_percent;
37         unsigned delay;
38         struct throughput *throughput;
39         uint64_t start_ns;
40         struct strbuf counters_sb;
41         int title_len;
42         int split;
43 };
44
45 static volatile sig_atomic_t progress_update;
46
47 static void progress_interval(int signum)
48 {
49         progress_update = 1;
50 }
51
52 static void set_progress_signal(void)
53 {
54         struct sigaction sa;
55         struct itimerval v;
56
57         progress_update = 0;
58
59         memset(&sa, 0, sizeof(sa));
60         sa.sa_handler = progress_interval;
61         sigemptyset(&sa.sa_mask);
62         sa.sa_flags = SA_RESTART;
63         sigaction(SIGALRM, &sa, NULL);
64
65         v.it_interval.tv_sec = 1;
66         v.it_interval.tv_usec = 0;
67         v.it_value = v.it_interval;
68         setitimer(ITIMER_REAL, &v, NULL);
69 }
70
71 static void clear_progress_signal(void)
72 {
73         struct itimerval v = {{0,},};
74         setitimer(ITIMER_REAL, &v, NULL);
75         signal(SIGALRM, SIG_IGN);
76         progress_update = 0;
77 }
78
79 static int is_foreground_fd(int fd)
80 {
81         int tpgrp = tcgetpgrp(fd);
82         return tpgrp < 0 || tpgrp == getpgid(0);
83 }
84
85 static void display(struct progress *progress, uint64_t n, const char *done)
86 {
87         const char *tp;
88         struct strbuf *counters_sb = &progress->counters_sb;
89         int show_update = 0;
90         int last_count_len = counters_sb->len;
91
92         if (progress->delay && (!progress_update || --progress->delay))
93                 return;
94
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;
101
102                         strbuf_reset(counters_sb);
103                         strbuf_addf(counters_sb,
104                                     "%3u%% (%"PRIuMAX"/%"PRIuMAX")%s", percent,
105                                     (uintmax_t)n, (uintmax_t)progress->total,
106                                     tp);
107                         show_update = 1;
108                 }
109         } else if (progress_update) {
110                 strbuf_reset(counters_sb);
111                 strbuf_addf(counters_sb, "%"PRIuMAX"%s", (uintmax_t)n, tp);
112                 show_update = 1;
113         }
114
115         if (show_update) {
116                 if (is_foreground_fd(fileno(stderr)) || done) {
117                         const char *eol = done ? done : "\r";
118                         size_t clear_len = counters_sb->len < last_count_len ?
119                                         last_count_len - counters_sb->len + 1 :
120                                         0;
121                         size_t progress_line_len = progress->title_len +
122                                                 counters_sb->len + 2;
123                         int cols = term_columns();
124
125                         if (progress->split) {
126                                 fprintf(stderr, "  %s%*s", counters_sb->buf,
127                                         (int) clear_len, eol);
128                         } else if (!done && cols < progress_line_len) {
129                                 clear_len = progress->title_len + 1 < cols ?
130                                             cols - progress->title_len - 1 : 0;
131                                 fprintf(stderr, "%s:%*s\n  %s%s",
132                                         progress->title, (int) clear_len, "",
133                                         counters_sb->buf, eol);
134                                 progress->split = 1;
135                         } else {
136                                 fprintf(stderr, "%s: %s%*s", progress->title,
137                                         counters_sb->buf, (int) clear_len, eol);
138                         }
139                         fflush(stderr);
140                 }
141                 progress_update = 0;
142         }
143 }
144
145 static void throughput_string(struct strbuf *buf, uint64_t total,
146                               unsigned int rate)
147 {
148         strbuf_reset(buf);
149         strbuf_addstr(buf, ", ");
150         strbuf_humanise_bytes(buf, total);
151         strbuf_addstr(buf, " | ");
152         strbuf_humanise_bytes(buf, rate * 1024);
153         strbuf_addstr(buf, "/s");
154 }
155
156 void display_throughput(struct progress *progress, uint64_t total)
157 {
158         struct throughput *tp;
159         uint64_t now_ns;
160         unsigned int misecs, count, rate;
161
162         if (!progress)
163                 return;
164         tp = progress->throughput;
165
166         now_ns = getnanotime();
167
168         if (!tp) {
169                 progress->throughput = tp = calloc(1, sizeof(*tp));
170                 if (tp) {
171                         tp->prev_total = tp->curr_total = total;
172                         tp->prev_ns = now_ns;
173                         strbuf_init(&tp->display, 0);
174                 }
175                 return;
176         }
177         tp->curr_total = total;
178
179         /* only update throughput every 0.5 s */
180         if (now_ns - tp->prev_ns <= 500000000)
181                 return;
182
183         /*
184          * We have x = bytes and y = nanosecs.  We want z = KiB/s:
185          *
186          *      z = (x / 1024) / (y / 1000000000)
187          *      z = x / y * 1000000000 / 1024
188          *      z = x / (y * 1024 / 1000000000)
189          *      z = x / y'
190          *
191          * To simplify things we'll keep track of misecs, or 1024th of a sec
192          * obtained with:
193          *
194          *      y' = y * 1024 / 1000000000
195          *      y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
196          *      y' = y / 2^32 * 4398
197          *      y' = (y * 4398) >> 32
198          */
199         misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
200
201         count = total - tp->prev_total;
202         tp->prev_total = total;
203         tp->prev_ns = now_ns;
204         tp->avg_bytes += count;
205         tp->avg_misecs += misecs;
206         rate = tp->avg_bytes / tp->avg_misecs;
207         tp->avg_bytes -= tp->last_bytes[tp->idx];
208         tp->avg_misecs -= tp->last_misecs[tp->idx];
209         tp->last_bytes[tp->idx] = count;
210         tp->last_misecs[tp->idx] = misecs;
211         tp->idx = (tp->idx + 1) % TP_IDX_MAX;
212
213         throughput_string(&tp->display, total, rate);
214         if (progress->last_value != -1 && progress_update)
215                 display(progress, progress->last_value, NULL);
216 }
217
218 void display_progress(struct progress *progress, uint64_t n)
219 {
220         if (progress)
221                 display(progress, n, NULL);
222 }
223
224 static struct progress *start_progress_delay(const char *title, uint64_t total,
225                                              unsigned delay)
226 {
227         struct progress *progress = malloc(sizeof(*progress));
228         if (!progress) {
229                 /* unlikely, but here's a good fallback */
230                 fprintf(stderr, "%s...\n", title);
231                 fflush(stderr);
232                 return NULL;
233         }
234         progress->title = title;
235         progress->total = total;
236         progress->last_value = -1;
237         progress->last_percent = -1;
238         progress->delay = delay;
239         progress->throughput = NULL;
240         progress->start_ns = getnanotime();
241         strbuf_init(&progress->counters_sb, 0);
242         progress->title_len = utf8_strwidth(title);
243         progress->split = 0;
244         set_progress_signal();
245         return progress;
246 }
247
248 struct progress *start_delayed_progress(const char *title, uint64_t total)
249 {
250         return start_progress_delay(title, total, 2);
251 }
252
253 struct progress *start_progress(const char *title, uint64_t total)
254 {
255         return start_progress_delay(title, total, 0);
256 }
257
258 void stop_progress(struct progress **p_progress)
259 {
260         stop_progress_msg(p_progress, _("done"));
261 }
262
263 void stop_progress_msg(struct progress **p_progress, const char *msg)
264 {
265         struct progress *progress = *p_progress;
266         if (!progress)
267                 return;
268         *p_progress = NULL;
269         if (progress->last_value != -1) {
270                 /* Force the last update */
271                 char *buf;
272                 struct throughput *tp = progress->throughput;
273
274                 if (tp) {
275                         uint64_t now_ns = getnanotime();
276                         unsigned int misecs, rate;
277                         misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
278                         rate = tp->curr_total / (misecs ? misecs : 1);
279                         throughput_string(&tp->display, tp->curr_total, rate);
280                 }
281                 progress_update = 1;
282                 buf = xstrfmt(", %s.\n", msg);
283                 display(progress, progress->last_value, buf);
284                 free(buf);
285         }
286         clear_progress_signal();
287         strbuf_release(&progress->counters_sb);
288         if (progress->throughput)
289                 strbuf_release(&progress->throughput->display);
290         free(progress->throughput);
291         free(progress);
292 }