progress: fix progress meters when dealing with lots of work
[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         unsigned delayed_percent_threshold;
38         struct throughput *throughput;
39         uint64_t start_ns;
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 int display(struct progress *progress, uint64_t n, const char *done)
83 {
84         const char *eol, *tp;
85
86         if (progress->delay) {
87                 if (!progress_update || --progress->delay)
88                         return 0;
89                 if (progress->total) {
90                         unsigned percent = n * 100 / progress->total;
91                         if (percent > progress->delayed_percent_threshold) {
92                                 /* inhibit this progress report entirely */
93                                 clear_progress_signal();
94                                 progress->delay = -1;
95                                 progress->total = 0;
96                                 return 0;
97                         }
98                 }
99         }
100
101         progress->last_value = n;
102         tp = (progress->throughput) ? progress->throughput->display.buf : "";
103         eol = done ? done : "   \r";
104         if (progress->total) {
105                 unsigned percent = n * 100 / progress->total;
106                 if (percent != progress->last_percent || progress_update) {
107                         progress->last_percent = percent;
108                         if (is_foreground_fd(fileno(stderr)) || done) {
109                                 fprintf(stderr, "%s: %3u%% (%"PRIuMAX"/%"PRIuMAX")%s%s",
110                                         progress->title, percent,
111                                         (uintmax_t)n, (uintmax_t)progress->total,
112                                         tp, eol);
113                                 fflush(stderr);
114                         }
115                         progress_update = 0;
116                         return 1;
117                 }
118         } else if (progress_update) {
119                 if (is_foreground_fd(fileno(stderr)) || done) {
120                         fprintf(stderr, "%s: %"PRIuMAX"%s%s",
121                                 progress->title, (uintmax_t)n, tp, eol);
122                         fflush(stderr);
123                 }
124                 progress_update = 0;
125                 return 1;
126         }
127
128         return 0;
129 }
130
131 static void throughput_string(struct strbuf *buf, uint64_t total,
132                               unsigned int rate)
133 {
134         strbuf_reset(buf);
135         strbuf_addstr(buf, ", ");
136         strbuf_humanise_bytes(buf, total);
137         strbuf_addstr(buf, " | ");
138         strbuf_humanise_bytes(buf, rate * 1024);
139         strbuf_addstr(buf, "/s");
140 }
141
142 void display_throughput(struct progress *progress, uint64_t total)
143 {
144         struct throughput *tp;
145         uint64_t now_ns;
146         unsigned int misecs, count, rate;
147
148         if (!progress)
149                 return;
150         tp = progress->throughput;
151
152         now_ns = getnanotime();
153
154         if (!tp) {
155                 progress->throughput = tp = calloc(1, sizeof(*tp));
156                 if (tp) {
157                         tp->prev_total = tp->curr_total = total;
158                         tp->prev_ns = now_ns;
159                         strbuf_init(&tp->display, 0);
160                 }
161                 return;
162         }
163         tp->curr_total = total;
164
165         /* only update throughput every 0.5 s */
166         if (now_ns - tp->prev_ns <= 500000000)
167                 return;
168
169         /*
170          * We have x = bytes and y = nanosecs.  We want z = KiB/s:
171          *
172          *      z = (x / 1024) / (y / 1000000000)
173          *      z = x / y * 1000000000 / 1024
174          *      z = x / (y * 1024 / 1000000000)
175          *      z = x / y'
176          *
177          * To simplify things we'll keep track of misecs, or 1024th of a sec
178          * obtained with:
179          *
180          *      y' = y * 1024 / 1000000000
181          *      y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
182          *      y' = y / 2^32 * 4398
183          *      y' = (y * 4398) >> 32
184          */
185         misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
186
187         count = total - tp->prev_total;
188         tp->prev_total = total;
189         tp->prev_ns = now_ns;
190         tp->avg_bytes += count;
191         tp->avg_misecs += misecs;
192         rate = tp->avg_bytes / tp->avg_misecs;
193         tp->avg_bytes -= tp->last_bytes[tp->idx];
194         tp->avg_misecs -= tp->last_misecs[tp->idx];
195         tp->last_bytes[tp->idx] = count;
196         tp->last_misecs[tp->idx] = misecs;
197         tp->idx = (tp->idx + 1) % TP_IDX_MAX;
198
199         throughput_string(&tp->display, total, rate);
200         if (progress->last_value != -1 && progress_update)
201                 display(progress, progress->last_value, NULL);
202 }
203
204 int display_progress(struct progress *progress, uint64_t n)
205 {
206         return progress ? display(progress, n, NULL) : 0;
207 }
208
209 static struct progress *start_progress_delay(const char *title, uint64_t total,
210                                              unsigned percent_threshold, unsigned delay)
211 {
212         struct progress *progress = malloc(sizeof(*progress));
213         if (!progress) {
214                 /* unlikely, but here's a good fallback */
215                 fprintf(stderr, "%s...\n", title);
216                 fflush(stderr);
217                 return NULL;
218         }
219         progress->title = title;
220         progress->total = total;
221         progress->last_value = -1;
222         progress->last_percent = -1;
223         progress->delayed_percent_threshold = percent_threshold;
224         progress->delay = delay;
225         progress->throughput = NULL;
226         progress->start_ns = getnanotime();
227         set_progress_signal();
228         return progress;
229 }
230
231 struct progress *start_delayed_progress(const char *title, uint64_t total)
232 {
233         return start_progress_delay(title, total, 0, 2);
234 }
235
236 struct progress *start_progress(const char *title, uint64_t total)
237 {
238         return start_progress_delay(title, total, 0, 0);
239 }
240
241 void stop_progress(struct progress **p_progress)
242 {
243         stop_progress_msg(p_progress, _("done"));
244 }
245
246 void stop_progress_msg(struct progress **p_progress, const char *msg)
247 {
248         struct progress *progress = *p_progress;
249         if (!progress)
250                 return;
251         *p_progress = NULL;
252         if (progress->last_value != -1) {
253                 /* Force the last update */
254                 char *buf;
255                 struct throughput *tp = progress->throughput;
256
257                 if (tp) {
258                         uint64_t now_ns = getnanotime();
259                         unsigned int misecs, rate;
260                         misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
261                         rate = tp->curr_total / (misecs ? misecs : 1);
262                         throughput_string(&tp->display, tp->curr_total, rate);
263                 }
264                 progress_update = 1;
265                 buf = xstrfmt(", %s.\n", msg);
266                 display(progress, progress->last_value, buf);
267                 free(buf);
268         }
269         clear_progress_signal();
270         if (progress->throughput)
271                 strbuf_release(&progress->throughput->display);
272         free(progress->throughput);
273         free(progress);
274 }