]> git.ipfire.org Git - thirdparty/git.git/blame - progress.c
tree-diff.c: move S_DIFFTREE_IFXMIN_NEQ define from cache.h
[thirdparty/git.git] / progress.c
CommitLineData
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
3cacb9aa 11#define GIT_TEST_PROGRESS_ONLY
77f091ed 12#include "git-compat-util.h"
ca4eed70 13#include "pager.h"
96a02f8f 14#include "progress.h"
079b546a 15#include "strbuf.h"
83d26fa7 16#include "trace.h"
74ea5c95 17#include "trace2.h"
545dc345 18#include "utf8.h"
44a4693b 19#include "config.h"
96a02f8f 20
cf84d51c
NP
21#define TP_IDX_MAX 8
22
23struct throughput {
53ed7b5a 24 off_t curr_total;
218558af 25 off_t prev_total;
83d26fa7 26 uint64_t prev_ns;
218558af 27 unsigned int avg_bytes;
cf84d51c 28 unsigned int avg_misecs;
53ed7b5a 29 unsigned int last_bytes[TP_IDX_MAX];
cf84d51c
NP
30 unsigned int last_misecs[TP_IDX_MAX];
31 unsigned int idx;
3131977d 32 struct strbuf display;
cf84d51c
NP
33};
34
dc6a0757
NP
35struct progress {
36 const char *title;
d6861d02
EN
37 uint64_t last_value;
38 uint64_t total;
dc6a0757
NP
39 unsigned last_percent;
40 unsigned delay;
9d81ecb5 41 unsigned sparse;
cf84d51c 42 struct throughput *throughput;
0fae1e07 43 uint64_t start_ns;
d53ba841 44 struct strbuf counters_sb;
545dc345
SG
45 int title_len;
46 int split;
dc6a0757
NP
47};
48
96a02f8f
NP
49static volatile sig_atomic_t progress_update;
50
2bb74b53
SG
51/*
52 * These are only intended for testing the progress output, i.e. exclusively
53 * for 'test-tool progress'.
54 */
55int progress_testing;
56uint64_t progress_test_ns = 0;
2bb74b53
SG
57void progress_test_force_update(void)
58{
59 progress_update = 1;
60}
61
62
9ec03b59 63static void progress_interval(int signum UNUSED)
96a02f8f
NP
64{
65 progress_update = 1;
66}
67
68static void set_progress_signal(void)
69{
70 struct sigaction sa;
71 struct itimerval v;
72
2bb74b53
SG
73 if (progress_testing)
74 return;
75
180a9f22
NP
76 progress_update = 0;
77
96a02f8f
NP
78 memset(&sa, 0, sizeof(sa));
79 sa.sa_handler = progress_interval;
80 sigemptyset(&sa.sa_mask);
81 sa.sa_flags = SA_RESTART;
82 sigaction(SIGALRM, &sa, NULL);
83
84 v.it_interval.tv_sec = 1;
85 v.it_interval.tv_usec = 0;
86 v.it_value = v.it_interval;
87 setitimer(ITIMER_REAL, &v, NULL);
88}
89
90static void clear_progress_signal(void)
91{
92 struct itimerval v = {{0,},};
2bb74b53
SG
93
94 if (progress_testing)
95 return;
96
96a02f8f
NP
97 setitimer(ITIMER_REAL, &v, NULL);
98 signal(SIGALRM, SIG_IGN);
99 progress_update = 0;
100}
101
85cb8906
LM
102static int is_foreground_fd(int fd)
103{
a4fb76ce
JK
104 int tpgrp = tcgetpgrp(fd);
105 return tpgrp < 0 || tpgrp == getpgid(0);
85cb8906
LM
106}
107
9219d127 108static void display(struct progress *progress, uint64_t n, const char *done)
96a02f8f 109{
d53ba841
SG
110 const char *tp;
111 struct strbuf *counters_sb = &progress->counters_sb;
112 int show_update = 0;
bbf47568 113 int last_count_len = counters_sb->len;
42e18fbf 114
9c5951ca 115 if (progress->delay && (!progress_update || --progress->delay))
9219d127 116 return;
42e18fbf
NP
117
118 progress->last_value = n;
3131977d 119 tp = (progress->throughput) ? progress->throughput->display.buf : "";
96a02f8f
NP
120 if (progress->total) {
121 unsigned percent = n * 100 / progress->total;
122 if (percent != progress->last_percent || progress_update) {
123 progress->last_percent = percent;
d53ba841
SG
124
125 strbuf_reset(counters_sb);
126 strbuf_addf(counters_sb,
127 "%3u%% (%"PRIuMAX"/%"PRIuMAX")%s", percent,
128 (uintmax_t)n, (uintmax_t)progress->total,
129 tp);
130 show_update = 1;
96a02f8f
NP
131 }
132 } else if (progress_update) {
d53ba841
SG
133 strbuf_reset(counters_sb);
134 strbuf_addf(counters_sb, "%"PRIuMAX"%s", (uintmax_t)n, tp);
135 show_update = 1;
136 }
137
138 if (show_update) {
85cb8906 139 if (is_foreground_fd(fileno(stderr)) || done) {
9f1fd84e 140 const char *eol = done ? done : "\r";
bbf47568
SG
141 size_t clear_len = counters_sb->len < last_count_len ?
142 last_count_len - counters_sb->len + 1 :
143 0;
144 /* The "+ 2" accounts for the ": ". */
145 size_t progress_line_len = progress->title_len +
146 counters_sb->len + 2;
147 int cols = term_columns();
545dc345
SG
148
149 if (progress->split) {
bbf47568
SG
150 fprintf(stderr, " %s%*s", counters_sb->buf,
151 (int) clear_len, eol);
152 } else if (!done && cols < progress_line_len) {
153 clear_len = progress->title_len + 1 < cols ?
154 cols - progress->title_len - 1 : 0;
155 fprintf(stderr, "%s:%*s\n %s%s",
156 progress->title, (int) clear_len, "",
157 counters_sb->buf, eol);
545dc345
SG
158 progress->split = 1;
159 } else {
bbf47568
SG
160 fprintf(stderr, "%s: %s%*s", progress->title,
161 counters_sb->buf, (int) clear_len, eol);
545dc345 162 }
85cb8906
LM
163 fflush(stderr);
164 }
96a02f8f 165 progress_update = 0;
96a02f8f 166 }
96a02f8f
NP
167}
168
d6861d02 169static void throughput_string(struct strbuf *buf, uint64_t total,
53ed7b5a
NP
170 unsigned int rate)
171{
3131977d 172 strbuf_reset(buf);
079b546a
AP
173 strbuf_addstr(buf, ", ");
174 strbuf_humanise_bytes(buf, total);
175 strbuf_addstr(buf, " | ");
8f354a1f 176 strbuf_humanise_rate(buf, rate * 1024);
53ed7b5a
NP
177}
178
2bb74b53
SG
179static uint64_t progress_getnanotime(struct progress *progress)
180{
181 if (progress_testing)
182 return progress->start_ns + progress_test_ns;
183 else
184 return getnanotime();
185}
186
d6861d02 187void display_throughput(struct progress *progress, uint64_t total)
cf84d51c
NP
188{
189 struct throughput *tp;
83d26fa7
KB
190 uint64_t now_ns;
191 unsigned int misecs, count, rate;
cf84d51c
NP
192
193 if (!progress)
194 return;
195 tp = progress->throughput;
196
2bb74b53 197 now_ns = progress_getnanotime(progress);
cf84d51c
NP
198
199 if (!tp) {
ca56dadb 200 progress->throughput = CALLOC_ARRAY(tp, 1);
999b951b
JK
201 tp->prev_total = tp->curr_total = total;
202 tp->prev_ns = now_ns;
203 strbuf_init(&tp->display, 0);
cf84d51c
NP
204 return;
205 }
53ed7b5a 206 tp->curr_total = total;
cf84d51c 207
83d26fa7
KB
208 /* only update throughput every 0.5 s */
209 if (now_ns - tp->prev_ns <= 500000000)
210 return;
211
cf84d51c 212 /*
83d26fa7 213 * We have x = bytes and y = nanosecs. We want z = KiB/s:
cf84d51c 214 *
83d26fa7
KB
215 * z = (x / 1024) / (y / 1000000000)
216 * z = x / y * 1000000000 / 1024
217 * z = x / (y * 1024 / 1000000000)
cf84d51c
NP
218 * z = x / y'
219 *
220 * To simplify things we'll keep track of misecs, or 1024th of a sec
221 * obtained with:
222 *
83d26fa7
KB
223 * y' = y * 1024 / 1000000000
224 * y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
225 * y' = y / 2^32 * 4398
226 * y' = (y * 4398) >> 32
cf84d51c 227 */
83d26fa7
KB
228 misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
229
230 count = total - tp->prev_total;
231 tp->prev_total = total;
232 tp->prev_ns = now_ns;
233 tp->avg_bytes += count;
234 tp->avg_misecs += misecs;
235 rate = tp->avg_bytes / tp->avg_misecs;
236 tp->avg_bytes -= tp->last_bytes[tp->idx];
237 tp->avg_misecs -= tp->last_misecs[tp->idx];
238 tp->last_bytes[tp->idx] = count;
239 tp->last_misecs[tp->idx] = misecs;
240 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
241
3131977d 242 throughput_string(&tp->display, total, rate);
83d26fa7
KB
243 if (progress->last_value != -1 && progress_update)
244 display(progress, progress->last_value, NULL);
cf84d51c
NP
245}
246
9219d127 247void display_progress(struct progress *progress, uint64_t n)
96a02f8f 248{
9219d127
SG
249 if (progress)
250 display(progress, n, NULL);
96a02f8f
NP
251}
252
d6861d02 253static struct progress *start_progress_delay(const char *title, uint64_t total,
9d81ecb5 254 unsigned delay, unsigned sparse)
180a9f22 255{
999b951b 256 struct progress *progress = xmalloc(sizeof(*progress));
42e18fbf 257 progress->title = title;
180a9f22 258 progress->total = total;
42e18fbf 259 progress->last_value = -1;
180a9f22 260 progress->last_percent = -1;
180a9f22 261 progress->delay = delay;
9d81ecb5 262 progress->sparse = sparse;
cf84d51c 263 progress->throughput = NULL;
0fae1e07 264 progress->start_ns = getnanotime();
d53ba841 265 strbuf_init(&progress->counters_sb, 0);
545dc345
SG
266 progress->title_len = utf8_strwidth(title);
267 progress->split = 0;
180a9f22 268 set_progress_signal();
98a13647 269 trace2_region_enter("progress", title, the_repository);
dc6a0757 270 return progress;
180a9f22
NP
271}
272
44a4693b
DS
273static int get_default_delay(void)
274{
275 static int delay_in_secs = -1;
276
277 if (delay_in_secs < 0)
278 delay_in_secs = git_env_ulong("GIT_PROGRESS_DELAY", 2);
279
280 return delay_in_secs;
281}
282
d6861d02 283struct progress *start_delayed_progress(const char *title, uint64_t total)
8aade107 284{
44a4693b 285 return start_progress_delay(title, total, get_default_delay(), 0);
8aade107
JH
286}
287
d6861d02 288struct progress *start_progress(const char *title, uint64_t total)
42e18fbf 289{
9d81ecb5
JH
290 return start_progress_delay(title, total, 0, 0);
291}
292
293/*
294 * Here "sparse" means that the caller might use some sampling criteria to
295 * decide when to call display_progress() rather than calling it for every
296 * integer value in[0 .. total). In particular, the caller might not call
297 * display_progress() for the last value in the range.
298 *
299 * When "sparse" is set, stop_progress() will automatically force the done
300 * message to show 100%.
301 */
302struct progress *start_sparse_progress(const char *title, uint64_t total)
303{
304 return start_progress_delay(title, total, 0, 1);
305}
306
307struct progress *start_delayed_sparse_progress(const char *title,
308 uint64_t total)
309{
44a4693b 310 return start_progress_delay(title, total, get_default_delay(), 1);
9d81ecb5
JH
311}
312
313static void finish_if_sparse(struct progress *progress)
314{
74900a6b 315 if (progress->sparse &&
9d81ecb5
JH
316 progress->last_value != progress->total)
317 display_progress(progress, progress->total);
42e18fbf
NP
318}
319
accf1eb1
ÆAB
320static void force_last_update(struct progress *progress, const char *msg)
321{
322 char *buf;
323 struct throughput *tp = progress->throughput;
324
325 if (tp) {
326 uint64_t now_ns = progress_getnanotime(progress);
327 unsigned int misecs, rate;
328 misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
329 rate = tp->curr_total / (misecs ? misecs : 1);
330 throughput_string(&tp->display, tp->curr_total, rate);
331 }
332 progress_update = 1;
333 buf = xstrfmt(", %s.\n", msg);
334 display(progress, progress->last_value, buf);
335 free(buf);
336}
337
338static void log_trace2(struct progress *progress)
339{
340 trace2_data_intmax("progress", the_repository, "total_objects",
341 progress->total);
342
343 if (progress->throughput)
344 trace2_data_intmax("progress", the_repository, "total_bytes",
345 progress->throughput->curr_total);
346
347 trace2_region_leave("progress", progress->title, the_repository);
348}
349
a984a06a 350void stop_progress_msg(struct progress **p_progress, const char *msg)
96a02f8f 351{
ac900fdd
352 struct progress *progress;
353
354 if (!p_progress)
355 BUG("don't provide NULL to stop_progress_msg");
356
357 progress = *p_progress;
dc6a0757
NP
358 if (!progress)
359 return;
360 *p_progress = NULL;
accf1eb1 361
74900a6b 362 finish_if_sparse(progress);
accf1eb1
ÆAB
363 if (progress->last_value != -1)
364 force_last_update(progress, msg);
74900a6b 365 log_trace2(progress);
accf1eb1 366
96a02f8f 367 clear_progress_signal();
d53ba841 368 strbuf_release(&progress->counters_sb);
3131977d
JK
369 if (progress->throughput)
370 strbuf_release(&progress->throughput->display);
cf84d51c 371 free(progress->throughput);
dc6a0757 372 free(progress);
96a02f8f 373}