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