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