]>
Commit | Line | Data |
---|---|---|
74b6792f NP |
1 | /* |
2 | * Simple text-based progress display module for GIT | |
3 | * | |
03aa8ff3 | 4 | * Copyright (c) 2007 by Nicolas Pitre <nico@fluxnic.net> |
74b6792f NP |
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 | ||
96a02f8f | 11 | #include "git-compat-util.h" |
754dbc43 | 12 | #include "gettext.h" |
96a02f8f | 13 | #include "progress.h" |
079b546a | 14 | #include "strbuf.h" |
83d26fa7 | 15 | #include "trace.h" |
96a02f8f | 16 | |
cf84d51c NP |
17 | #define TP_IDX_MAX 8 |
18 | ||
19 | struct throughput { | |
53ed7b5a | 20 | off_t curr_total; |
218558af | 21 | off_t prev_total; |
83d26fa7 | 22 | uint64_t prev_ns; |
218558af | 23 | unsigned int avg_bytes; |
cf84d51c | 24 | unsigned int avg_misecs; |
53ed7b5a | 25 | unsigned int last_bytes[TP_IDX_MAX]; |
cf84d51c NP |
26 | unsigned int last_misecs[TP_IDX_MAX]; |
27 | unsigned int idx; | |
3131977d | 28 | struct strbuf display; |
cf84d51c NP |
29 | }; |
30 | ||
dc6a0757 NP |
31 | struct progress { |
32 | const char *title; | |
d6861d02 EN |
33 | uint64_t last_value; |
34 | uint64_t total; | |
dc6a0757 NP |
35 | unsigned last_percent; |
36 | unsigned delay; | |
cf84d51c | 37 | struct throughput *throughput; |
0fae1e07 | 38 | uint64_t start_ns; |
dc6a0757 NP |
39 | }; |
40 | ||
96a02f8f NP |
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 | ||
180a9f22 NP |
53 | progress_update = 0; |
54 | ||
96a02f8f NP |
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 | ||
85cb8906 LM |
75 | static int is_foreground_fd(int fd) |
76 | { | |
a4fb76ce JK |
77 | int tpgrp = tcgetpgrp(fd); |
78 | return tpgrp < 0 || tpgrp == getpgid(0); | |
85cb8906 LM |
79 | } |
80 | ||
d6861d02 | 81 | static int display(struct progress *progress, uint64_t n, const char *done) |
96a02f8f | 82 | { |
a984a06a | 83 | const char *eol, *tp; |
42e18fbf | 84 | |
9c5951ca LS |
85 | if (progress->delay && (!progress_update || --progress->delay)) |
86 | return 0; | |
42e18fbf NP |
87 | |
88 | progress->last_value = n; | |
3131977d | 89 | tp = (progress->throughput) ? progress->throughput->display.buf : ""; |
a984a06a | 90 | eol = done ? done : " \r"; |
96a02f8f NP |
91 | if (progress->total) { |
92 | unsigned percent = n * 100 / progress->total; | |
93 | if (percent != progress->last_percent || progress_update) { | |
94 | progress->last_percent = percent; | |
85cb8906 | 95 | if (is_foreground_fd(fileno(stderr)) || done) { |
d6861d02 EN |
96 | fprintf(stderr, "%s: %3u%% (%"PRIuMAX"/%"PRIuMAX")%s%s", |
97 | progress->title, percent, | |
98 | (uintmax_t)n, (uintmax_t)progress->total, | |
99 | tp, eol); | |
85cb8906 LM |
100 | fflush(stderr); |
101 | } | |
96a02f8f NP |
102 | progress_update = 0; |
103 | return 1; | |
104 | } | |
105 | } else if (progress_update) { | |
85cb8906 | 106 | if (is_foreground_fd(fileno(stderr)) || done) { |
d6861d02 EN |
107 | fprintf(stderr, "%s: %"PRIuMAX"%s%s", |
108 | progress->title, (uintmax_t)n, tp, eol); | |
85cb8906 LM |
109 | fflush(stderr); |
110 | } | |
96a02f8f NP |
111 | progress_update = 0; |
112 | return 1; | |
113 | } | |
42e18fbf | 114 | |
96a02f8f NP |
115 | return 0; |
116 | } | |
117 | ||
d6861d02 | 118 | static void throughput_string(struct strbuf *buf, uint64_t total, |
53ed7b5a NP |
119 | unsigned int rate) |
120 | { | |
3131977d | 121 | strbuf_reset(buf); |
079b546a AP |
122 | strbuf_addstr(buf, ", "); |
123 | strbuf_humanise_bytes(buf, total); | |
124 | strbuf_addstr(buf, " | "); | |
125 | strbuf_humanise_bytes(buf, rate * 1024); | |
126 | strbuf_addstr(buf, "/s"); | |
53ed7b5a NP |
127 | } |
128 | ||
d6861d02 | 129 | void display_throughput(struct progress *progress, uint64_t total) |
cf84d51c NP |
130 | { |
131 | struct throughput *tp; | |
83d26fa7 KB |
132 | uint64_t now_ns; |
133 | unsigned int misecs, count, rate; | |
cf84d51c NP |
134 | |
135 | if (!progress) | |
136 | return; | |
137 | tp = progress->throughput; | |
138 | ||
83d26fa7 | 139 | now_ns = getnanotime(); |
cf84d51c NP |
140 | |
141 | if (!tp) { | |
142 | progress->throughput = tp = calloc(1, sizeof(*tp)); | |
218558af | 143 | if (tp) { |
53ed7b5a | 144 | tp->prev_total = tp->curr_total = total; |
83d26fa7 | 145 | tp->prev_ns = now_ns; |
3131977d | 146 | strbuf_init(&tp->display, 0); |
218558af | 147 | } |
cf84d51c NP |
148 | return; |
149 | } | |
53ed7b5a | 150 | tp->curr_total = total; |
cf84d51c | 151 | |
83d26fa7 KB |
152 | /* only update throughput every 0.5 s */ |
153 | if (now_ns - tp->prev_ns <= 500000000) | |
154 | return; | |
155 | ||
cf84d51c | 156 | /* |
83d26fa7 | 157 | * We have x = bytes and y = nanosecs. We want z = KiB/s: |
cf84d51c | 158 | * |
83d26fa7 KB |
159 | * z = (x / 1024) / (y / 1000000000) |
160 | * z = x / y * 1000000000 / 1024 | |
161 | * z = x / (y * 1024 / 1000000000) | |
cf84d51c NP |
162 | * z = x / y' |
163 | * | |
164 | * To simplify things we'll keep track of misecs, or 1024th of a sec | |
165 | * obtained with: | |
166 | * | |
83d26fa7 KB |
167 | * y' = y * 1024 / 1000000000 |
168 | * y' = y * (2^10 / 2^42) * (2^42 / 1000000000) | |
169 | * y' = y / 2^32 * 4398 | |
170 | * y' = (y * 4398) >> 32 | |
cf84d51c | 171 | */ |
83d26fa7 KB |
172 | misecs = ((now_ns - tp->prev_ns) * 4398) >> 32; |
173 | ||
174 | count = total - tp->prev_total; | |
175 | tp->prev_total = total; | |
176 | tp->prev_ns = now_ns; | |
177 | tp->avg_bytes += count; | |
178 | tp->avg_misecs += misecs; | |
179 | rate = tp->avg_bytes / tp->avg_misecs; | |
180 | tp->avg_bytes -= tp->last_bytes[tp->idx]; | |
181 | tp->avg_misecs -= tp->last_misecs[tp->idx]; | |
182 | tp->last_bytes[tp->idx] = count; | |
183 | tp->last_misecs[tp->idx] = misecs; | |
184 | tp->idx = (tp->idx + 1) % TP_IDX_MAX; | |
185 | ||
3131977d | 186 | throughput_string(&tp->display, total, rate); |
83d26fa7 KB |
187 | if (progress->last_value != -1 && progress_update) |
188 | display(progress, progress->last_value, NULL); | |
cf84d51c NP |
189 | } |
190 | ||
d6861d02 | 191 | int display_progress(struct progress *progress, uint64_t n) |
96a02f8f | 192 | { |
a984a06a | 193 | return progress ? display(progress, n, NULL) : 0; |
96a02f8f NP |
194 | } |
195 | ||
d6861d02 | 196 | static struct progress *start_progress_delay(const char *title, uint64_t total, |
9c5951ca | 197 | unsigned delay) |
180a9f22 | 198 | { |
dc6a0757 NP |
199 | struct progress *progress = malloc(sizeof(*progress)); |
200 | if (!progress) { | |
201 | /* unlikely, but here's a good fallback */ | |
202 | fprintf(stderr, "%s...\n", title); | |
137a0d0e | 203 | fflush(stderr); |
dc6a0757 NP |
204 | return NULL; |
205 | } | |
42e18fbf | 206 | progress->title = title; |
180a9f22 | 207 | progress->total = total; |
42e18fbf | 208 | progress->last_value = -1; |
180a9f22 | 209 | progress->last_percent = -1; |
180a9f22 | 210 | progress->delay = delay; |
cf84d51c | 211 | progress->throughput = NULL; |
0fae1e07 | 212 | progress->start_ns = getnanotime(); |
180a9f22 | 213 | set_progress_signal(); |
dc6a0757 | 214 | return progress; |
180a9f22 NP |
215 | } |
216 | ||
d6861d02 | 217 | struct progress *start_delayed_progress(const char *title, uint64_t total) |
8aade107 | 218 | { |
9c5951ca | 219 | return start_progress_delay(title, total, 2); |
8aade107 JH |
220 | } |
221 | ||
d6861d02 | 222 | struct progress *start_progress(const char *title, uint64_t total) |
42e18fbf | 223 | { |
9c5951ca | 224 | return start_progress_delay(title, total, 0); |
42e18fbf NP |
225 | } |
226 | ||
dc6a0757 | 227 | void stop_progress(struct progress **p_progress) |
a984a06a | 228 | { |
754dbc43 | 229 | stop_progress_msg(p_progress, _("done")); |
a984a06a NP |
230 | } |
231 | ||
232 | void stop_progress_msg(struct progress **p_progress, const char *msg) | |
96a02f8f | 233 | { |
dc6a0757 NP |
234 | struct progress *progress = *p_progress; |
235 | if (!progress) | |
236 | return; | |
237 | *p_progress = NULL; | |
42e18fbf NP |
238 | if (progress->last_value != -1) { |
239 | /* Force the last update */ | |
fbd09439 | 240 | char *buf; |
53ed7b5a | 241 | struct throughput *tp = progress->throughput; |
d4c44443 | 242 | |
53ed7b5a | 243 | if (tp) { |
0fae1e07 RS |
244 | uint64_t now_ns = getnanotime(); |
245 | unsigned int misecs, rate; | |
246 | misecs = ((now_ns - progress->start_ns) * 4398) >> 32; | |
247 | rate = tp->curr_total / (misecs ? misecs : 1); | |
3131977d | 248 | throughput_string(&tp->display, tp->curr_total, rate); |
53ed7b5a | 249 | } |
42e18fbf | 250 | progress_update = 1; |
fbd09439 MM |
251 | buf = xstrfmt(", %s.\n", msg); |
252 | display(progress, progress->last_value, buf); | |
253 | free(buf); | |
42e18fbf | 254 | } |
96a02f8f | 255 | clear_progress_signal(); |
3131977d JK |
256 | if (progress->throughput) |
257 | strbuf_release(&progress->throughput->display); | |
cf84d51c | 258 | free(progress->throughput); |
dc6a0757 | 259 | free(progress); |
96a02f8f | 260 | } |