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