blob: a2e8cf64a8d1aa2f94c745bbb5098b9c771f3a05 [file] [log] [blame]
Nicolas Pitre74b67922007-10-30 15:41:13 -04001/*
2 * Simple text-based progress display module for GIT
3 *
Nicolas Pitre03aa8ff2009-09-14 02:41:16 -04004 * Copyright (c) 2007 by Nicolas Pitre <nico@fluxnic.net>
Nicolas Pitre74b67922007-10-30 15:41:13 -04005 *
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
SZEDER Gábor545dc342019-04-12 21:45:15 +020011#include "cache.h"
Nguyễn Thái Ngọc Duy754dbc42014-02-21 19:50:18 +070012#include "gettext.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -040013#include "progress.h"
Antoine Pelisse079b5462013-04-10 21:03:23 +020014#include "strbuf.h"
Karsten Blees83d26fa2014-07-12 02:08:11 +020015#include "trace.h"
SZEDER Gábor545dc342019-04-12 21:45:15 +020016#include "utf8.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -040017
Nicolas Pitrecf84d512007-10-30 14:57:34 -040018#define TP_IDX_MAX 8
19
20struct throughput {
Nicolas Pitre53ed7b52007-11-06 16:30:28 -050021 off_t curr_total;
Nicolas Pitre218558a2007-11-04 22:15:41 -050022 off_t prev_total;
Karsten Blees83d26fa2014-07-12 02:08:11 +020023 uint64_t prev_ns;
Nicolas Pitre218558a2007-11-04 22:15:41 -050024 unsigned int avg_bytes;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040025 unsigned int avg_misecs;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -050026 unsigned int last_bytes[TP_IDX_MAX];
Nicolas Pitrecf84d512007-10-30 14:57:34 -040027 unsigned int last_misecs[TP_IDX_MAX];
28 unsigned int idx;
Jeff King31319772015-09-24 17:05:57 -040029 struct strbuf display;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040030};
31
Nicolas Pitredc6a0752007-10-30 14:57:32 -040032struct progress {
33 const char *title;
Elijah Newrend6861d02017-11-13 12:15:58 -080034 uint64_t last_value;
35 uint64_t total;
Nicolas Pitredc6a0752007-10-30 14:57:32 -040036 unsigned last_percent;
37 unsigned delay;
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -070038 unsigned sparse;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040039 struct throughput *throughput;
René Scharfe0fae1e02017-07-08 18:43:42 +020040 uint64_t start_ns;
SZEDER Gábord53ba842019-04-05 02:45:37 +020041 struct strbuf counters_sb;
SZEDER Gábor545dc342019-04-12 21:45:15 +020042 int title_len;
43 int split;
Nicolas Pitredc6a0752007-10-30 14:57:32 -040044};
45
Nicolas Pitre96a02f82007-04-18 14:27:45 -040046static volatile sig_atomic_t progress_update;
47
48static void progress_interval(int signum)
49{
50 progress_update = 1;
51}
52
53static void set_progress_signal(void)
54{
55 struct sigaction sa;
56 struct itimerval v;
57
Nicolas Pitre180a9f22007-04-20 15:05:27 -040058 progress_update = 0;
59
Nicolas Pitre96a02f82007-04-18 14:27:45 -040060 memset(&sa, 0, sizeof(sa));
61 sa.sa_handler = progress_interval;
62 sigemptyset(&sa.sa_mask);
63 sa.sa_flags = SA_RESTART;
64 sigaction(SIGALRM, &sa, NULL);
65
66 v.it_interval.tv_sec = 1;
67 v.it_interval.tv_usec = 0;
68 v.it_value = v.it_interval;
69 setitimer(ITIMER_REAL, &v, NULL);
70}
71
72static void clear_progress_signal(void)
73{
74 struct itimerval v = {{0,},};
75 setitimer(ITIMER_REAL, &v, NULL);
76 signal(SIGALRM, SIG_IGN);
77 progress_update = 0;
78}
79
Luke Mewburn85cb8902015-04-13 23:30:51 +100080static int is_foreground_fd(int fd)
81{
Jeff Kinga4fb76c2015-05-19 01:24:57 -040082 int tpgrp = tcgetpgrp(fd);
83 return tpgrp < 0 || tpgrp == getpgid(0);
Luke Mewburn85cb8902015-04-13 23:30:51 +100084}
85
SZEDER Gábor9219d122019-04-05 02:45:36 +020086static void display(struct progress *progress, uint64_t n, const char *done)
Nicolas Pitre96a02f82007-04-18 14:27:45 -040087{
SZEDER Gábord53ba842019-04-05 02:45:37 +020088 const char *tp;
89 struct strbuf *counters_sb = &progress->counters_sb;
90 int show_update = 0;
SZEDER Gábor9f1fd842019-04-12 21:45:14 +020091 int last_count_len = counters_sb->len;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -040092
Lars Schneider9c5951c2017-12-04 17:07:00 -050093 if (progress->delay && (!progress_update || --progress->delay))
SZEDER Gábor9219d122019-04-05 02:45:36 +020094 return;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -040095
96 progress->last_value = n;
Jeff King31319772015-09-24 17:05:57 -040097 tp = (progress->throughput) ? progress->throughput->display.buf : "";
Nicolas Pitre96a02f82007-04-18 14:27:45 -040098 if (progress->total) {
99 unsigned percent = n * 100 / progress->total;
100 if (percent != progress->last_percent || progress_update) {
101 progress->last_percent = percent;
SZEDER Gábord53ba842019-04-05 02:45:37 +0200102
103 strbuf_reset(counters_sb);
104 strbuf_addf(counters_sb,
105 "%3u%% (%"PRIuMAX"/%"PRIuMAX")%s", percent,
106 (uintmax_t)n, (uintmax_t)progress->total,
107 tp);
108 show_update = 1;
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400109 }
110 } else if (progress_update) {
SZEDER Gábord53ba842019-04-05 02:45:37 +0200111 strbuf_reset(counters_sb);
112 strbuf_addf(counters_sb, "%"PRIuMAX"%s", (uintmax_t)n, tp);
113 show_update = 1;
114 }
115
116 if (show_update) {
Luke Mewburn85cb8902015-04-13 23:30:51 +1000117 if (is_foreground_fd(fileno(stderr)) || done) {
SZEDER Gábor9f1fd842019-04-12 21:45:14 +0200118 const char *eol = done ? done : "\r";
119 size_t clear_len = counters_sb->len < last_count_len ?
120 last_count_len - counters_sb->len + 1 :
121 0;
SZEDER Gábor545dc342019-04-12 21:45:15 +0200122 size_t progress_line_len = progress->title_len +
123 counters_sb->len + 2;
124 int cols = term_columns();
125
126 if (progress->split) {
127 fprintf(stderr, " %s%*s", counters_sb->buf,
128 (int) clear_len, eol);
129 } else if (!done && cols < progress_line_len) {
130 clear_len = progress->title_len + 1 < cols ?
SZEDER Gábor1aed1a52019-05-19 16:45:46 +0200131 cols - progress->title_len - 1 : 0;
SZEDER Gábor545dc342019-04-12 21:45:15 +0200132 fprintf(stderr, "%s:%*s\n %s%s",
133 progress->title, (int) clear_len, "",
134 counters_sb->buf, eol);
135 progress->split = 1;
136 } else {
137 fprintf(stderr, "%s: %s%*s", progress->title,
138 counters_sb->buf, (int) clear_len, eol);
139 }
Luke Mewburn85cb8902015-04-13 23:30:51 +1000140 fflush(stderr);
141 }
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400142 progress_update = 0;
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400143 }
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400144}
145
Elijah Newrend6861d02017-11-13 12:15:58 -0800146static void throughput_string(struct strbuf *buf, uint64_t total,
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500147 unsigned int rate)
148{
Jeff King31319772015-09-24 17:05:57 -0400149 strbuf_reset(buf);
Antoine Pelisse079b5462013-04-10 21:03:23 +0200150 strbuf_addstr(buf, ", ");
151 strbuf_humanise_bytes(buf, total);
152 strbuf_addstr(buf, " | ");
153 strbuf_humanise_bytes(buf, rate * 1024);
154 strbuf_addstr(buf, "/s");
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500155}
156
Elijah Newrend6861d02017-11-13 12:15:58 -0800157void display_throughput(struct progress *progress, uint64_t total)
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400158{
159 struct throughput *tp;
Karsten Blees83d26fa2014-07-12 02:08:11 +0200160 uint64_t now_ns;
161 unsigned int misecs, count, rate;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400162
163 if (!progress)
164 return;
165 tp = progress->throughput;
166
Karsten Blees83d26fa2014-07-12 02:08:11 +0200167 now_ns = getnanotime();
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400168
169 if (!tp) {
Jeff King999b9512019-04-11 09:49:57 -0400170 progress->throughput = tp = xcalloc(1, sizeof(*tp));
171 tp->prev_total = tp->curr_total = total;
172 tp->prev_ns = now_ns;
173 strbuf_init(&tp->display, 0);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400174 return;
175 }
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500176 tp->curr_total = total;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400177
Karsten Blees83d26fa2014-07-12 02:08:11 +0200178 /* only update throughput every 0.5 s */
179 if (now_ns - tp->prev_ns <= 500000000)
180 return;
181
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400182 /*
Karsten Blees83d26fa2014-07-12 02:08:11 +0200183 * We have x = bytes and y = nanosecs. We want z = KiB/s:
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400184 *
Karsten Blees83d26fa2014-07-12 02:08:11 +0200185 * z = (x / 1024) / (y / 1000000000)
186 * z = x / y * 1000000000 / 1024
187 * z = x / (y * 1024 / 1000000000)
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400188 * z = x / y'
189 *
190 * To simplify things we'll keep track of misecs, or 1024th of a sec
191 * obtained with:
192 *
Karsten Blees83d26fa2014-07-12 02:08:11 +0200193 * y' = y * 1024 / 1000000000
194 * y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
195 * y' = y / 2^32 * 4398
196 * y' = (y * 4398) >> 32
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400197 */
Karsten Blees83d26fa2014-07-12 02:08:11 +0200198 misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400199
Karsten Blees83d26fa2014-07-12 02:08:11 +0200200 count = total - tp->prev_total;
201 tp->prev_total = total;
202 tp->prev_ns = now_ns;
203 tp->avg_bytes += count;
204 tp->avg_misecs += misecs;
205 rate = tp->avg_bytes / tp->avg_misecs;
206 tp->avg_bytes -= tp->last_bytes[tp->idx];
207 tp->avg_misecs -= tp->last_misecs[tp->idx];
208 tp->last_bytes[tp->idx] = count;
209 tp->last_misecs[tp->idx] = misecs;
210 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500211
Jeff King31319772015-09-24 17:05:57 -0400212 throughput_string(&tp->display, total, rate);
Karsten Blees83d26fa2014-07-12 02:08:11 +0200213 if (progress->last_value != -1 && progress_update)
214 display(progress, progress->last_value, NULL);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400215}
216
SZEDER Gábor9219d122019-04-05 02:45:36 +0200217void display_progress(struct progress *progress, uint64_t n)
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400218{
SZEDER Gábor9219d122019-04-05 02:45:36 +0200219 if (progress)
220 display(progress, n, NULL);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400221}
222
Elijah Newrend6861d02017-11-13 12:15:58 -0800223static struct progress *start_progress_delay(const char *title, uint64_t total,
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -0700224 unsigned delay, unsigned sparse)
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400225{
Jeff King999b9512019-04-11 09:49:57 -0400226 struct progress *progress = xmalloc(sizeof(*progress));
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400227 progress->title = title;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400228 progress->total = total;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400229 progress->last_value = -1;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400230 progress->last_percent = -1;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400231 progress->delay = delay;
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -0700232 progress->sparse = sparse;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400233 progress->throughput = NULL;
René Scharfe0fae1e02017-07-08 18:43:42 +0200234 progress->start_ns = getnanotime();
SZEDER Gábord53ba842019-04-05 02:45:37 +0200235 strbuf_init(&progress->counters_sb, 0);
SZEDER Gábor545dc342019-04-12 21:45:15 +0200236 progress->title_len = utf8_strwidth(title);
237 progress->split = 0;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400238 set_progress_signal();
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400239 return progress;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400240}
241
Elijah Newrend6861d02017-11-13 12:15:58 -0800242struct progress *start_delayed_progress(const char *title, uint64_t total)
Junio C Hamano8aade102017-08-19 10:39:41 -0700243{
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -0700244 return start_progress_delay(title, total, 2, 0);
Junio C Hamano8aade102017-08-19 10:39:41 -0700245}
246
Elijah Newrend6861d02017-11-13 12:15:58 -0800247struct progress *start_progress(const char *title, uint64_t total)
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400248{
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -0700249 return start_progress_delay(title, total, 0, 0);
250}
251
252/*
253 * Here "sparse" means that the caller might use some sampling criteria to
254 * decide when to call display_progress() rather than calling it for every
255 * integer value in[0 .. total). In particular, the caller might not call
256 * display_progress() for the last value in the range.
257 *
258 * When "sparse" is set, stop_progress() will automatically force the done
259 * message to show 100%.
260 */
261struct progress *start_sparse_progress(const char *title, uint64_t total)
262{
263 return start_progress_delay(title, total, 0, 1);
264}
265
266struct progress *start_delayed_sparse_progress(const char *title,
267 uint64_t total)
268{
269 return start_progress_delay(title, total, 2, 1);
270}
271
272static void finish_if_sparse(struct progress *progress)
273{
274 if (progress &&
275 progress->sparse &&
276 progress->last_value != progress->total)
277 display_progress(progress, progress->total);
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400278}
279
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400280void stop_progress(struct progress **p_progress)
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400281{
Jeff Hostetler9d81ecb2019-03-21 12:36:11 -0700282 finish_if_sparse(*p_progress);
283
Nguyễn Thái Ngọc Duy754dbc42014-02-21 19:50:18 +0700284 stop_progress_msg(p_progress, _("done"));
Nicolas Pitrea984a062007-11-08 15:45:41 -0500285}
286
287void stop_progress_msg(struct progress **p_progress, const char *msg)
288{
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400289 struct progress *progress = *p_progress;
290 if (!progress)
291 return;
292 *p_progress = NULL;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400293 if (progress->last_value != -1) {
294 /* Force the last update */
Maxim Moseychukfbd09432017-02-16 20:07:13 +0300295 char *buf;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500296 struct throughput *tp = progress->throughput;
Boyd Lynn Gerberd4c44442008-06-08 09:26:15 -0600297
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500298 if (tp) {
René Scharfe0fae1e02017-07-08 18:43:42 +0200299 uint64_t now_ns = getnanotime();
300 unsigned int misecs, rate;
301 misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
302 rate = tp->curr_total / (misecs ? misecs : 1);
Jeff King31319772015-09-24 17:05:57 -0400303 throughput_string(&tp->display, tp->curr_total, rate);
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500304 }
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400305 progress_update = 1;
Maxim Moseychukfbd09432017-02-16 20:07:13 +0300306 buf = xstrfmt(", %s.\n", msg);
307 display(progress, progress->last_value, buf);
308 free(buf);
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400309 }
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400310 clear_progress_signal();
SZEDER Gábord53ba842019-04-05 02:45:37 +0200311 strbuf_release(&progress->counters_sb);
Jeff King31319772015-09-24 17:05:57 -0400312 if (progress->throughput)
313 strbuf_release(&progress->throughput->display);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400314 free(progress->throughput);
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400315 free(progress);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400316}