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