blob: 5a99c9fbf00bfb98d3a51cc7a7948b4e963b3517 [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
Nicolas Pitre96a02f82007-04-18 14:27:45 -040011#include "git-compat-util.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"
Nicolas Pitre96a02f82007-04-18 14:27:45 -040016
Nicolas Pitrecf84d512007-10-30 14:57:34 -040017#define TP_IDX_MAX 8
18
19struct throughput {
Nicolas Pitre53ed7b52007-11-06 16:30:28 -050020 off_t curr_total;
Nicolas Pitre218558a2007-11-04 22:15:41 -050021 off_t prev_total;
Karsten Blees83d26fa2014-07-12 02:08:11 +020022 uint64_t prev_ns;
Nicolas Pitre218558a2007-11-04 22:15:41 -050023 unsigned int avg_bytes;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040024 unsigned int avg_misecs;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -050025 unsigned int last_bytes[TP_IDX_MAX];
Nicolas Pitrecf84d512007-10-30 14:57:34 -040026 unsigned int last_misecs[TP_IDX_MAX];
27 unsigned int idx;
Jeff King31319772015-09-24 17:05:57 -040028 struct strbuf display;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040029};
30
Nicolas Pitredc6a0752007-10-30 14:57:32 -040031struct progress {
32 const char *title;
Elijah Newrend6861d02017-11-13 12:15:58 -080033 uint64_t last_value;
34 uint64_t total;
Nicolas Pitredc6a0752007-10-30 14:57:32 -040035 unsigned last_percent;
36 unsigned delay;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040037 struct throughput *throughput;
René Scharfe0fae1e02017-07-08 18:43:42 +020038 uint64_t start_ns;
Nicolas Pitredc6a0752007-10-30 14:57:32 -040039};
40
Nicolas Pitre96a02f82007-04-18 14:27:45 -040041static volatile sig_atomic_t progress_update;
42
43static void progress_interval(int signum)
44{
45 progress_update = 1;
46}
47
48static void set_progress_signal(void)
49{
50 struct sigaction sa;
51 struct itimerval v;
52
Nicolas Pitre180a9f22007-04-20 15:05:27 -040053 progress_update = 0;
54
Nicolas Pitre96a02f82007-04-18 14:27:45 -040055 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
67static 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
Luke Mewburn85cb8902015-04-13 23:30:51 +100075static int is_foreground_fd(int fd)
76{
Jeff Kinga4fb76c2015-05-19 01:24:57 -040077 int tpgrp = tcgetpgrp(fd);
78 return tpgrp < 0 || tpgrp == getpgid(0);
Luke Mewburn85cb8902015-04-13 23:30:51 +100079}
80
Elijah Newrend6861d02017-11-13 12:15:58 -080081static int display(struct progress *progress, uint64_t n, const char *done)
Nicolas Pitre96a02f82007-04-18 14:27:45 -040082{
Nicolas Pitrea984a062007-11-08 15:45:41 -050083 const char *eol, *tp;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -040084
Lars Schneider9c5951c2017-12-04 17:07:00 -050085 if (progress->delay && (!progress_update || --progress->delay))
86 return 0;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -040087
88 progress->last_value = n;
Jeff King31319772015-09-24 17:05:57 -040089 tp = (progress->throughput) ? progress->throughput->display.buf : "";
Nicolas Pitrea984a062007-11-08 15:45:41 -050090 eol = done ? done : " \r";
Nicolas Pitre96a02f82007-04-18 14:27:45 -040091 if (progress->total) {
92 unsigned percent = n * 100 / progress->total;
93 if (percent != progress->last_percent || progress_update) {
94 progress->last_percent = percent;
Luke Mewburn85cb8902015-04-13 23:30:51 +100095 if (is_foreground_fd(fileno(stderr)) || done) {
Elijah Newrend6861d02017-11-13 12:15:58 -080096 fprintf(stderr, "%s: %3u%% (%"PRIuMAX"/%"PRIuMAX")%s%s",
97 progress->title, percent,
98 (uintmax_t)n, (uintmax_t)progress->total,
99 tp, eol);
Luke Mewburn85cb8902015-04-13 23:30:51 +1000100 fflush(stderr);
101 }
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400102 progress_update = 0;
103 return 1;
104 }
105 } else if (progress_update) {
Luke Mewburn85cb8902015-04-13 23:30:51 +1000106 if (is_foreground_fd(fileno(stderr)) || done) {
Elijah Newrend6861d02017-11-13 12:15:58 -0800107 fprintf(stderr, "%s: %"PRIuMAX"%s%s",
108 progress->title, (uintmax_t)n, tp, eol);
Luke Mewburn85cb8902015-04-13 23:30:51 +1000109 fflush(stderr);
110 }
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400111 progress_update = 0;
112 return 1;
113 }
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400114
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400115 return 0;
116}
117
Elijah Newrend6861d02017-11-13 12:15:58 -0800118static void throughput_string(struct strbuf *buf, uint64_t total,
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500119 unsigned int rate)
120{
Jeff King31319772015-09-24 17:05:57 -0400121 strbuf_reset(buf);
Antoine Pelisse079b5462013-04-10 21:03:23 +0200122 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");
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500127}
128
Elijah Newrend6861d02017-11-13 12:15:58 -0800129void display_throughput(struct progress *progress, uint64_t total)
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400130{
131 struct throughput *tp;
Karsten Blees83d26fa2014-07-12 02:08:11 +0200132 uint64_t now_ns;
133 unsigned int misecs, count, rate;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400134
135 if (!progress)
136 return;
137 tp = progress->throughput;
138
Karsten Blees83d26fa2014-07-12 02:08:11 +0200139 now_ns = getnanotime();
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400140
141 if (!tp) {
142 progress->throughput = tp = calloc(1, sizeof(*tp));
Nicolas Pitre218558a2007-11-04 22:15:41 -0500143 if (tp) {
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500144 tp->prev_total = tp->curr_total = total;
Karsten Blees83d26fa2014-07-12 02:08:11 +0200145 tp->prev_ns = now_ns;
Jeff King31319772015-09-24 17:05:57 -0400146 strbuf_init(&tp->display, 0);
Nicolas Pitre218558a2007-11-04 22:15:41 -0500147 }
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400148 return;
149 }
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500150 tp->curr_total = total;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400151
Karsten Blees83d26fa2014-07-12 02:08:11 +0200152 /* only update throughput every 0.5 s */
153 if (now_ns - tp->prev_ns <= 500000000)
154 return;
155
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400156 /*
Karsten Blees83d26fa2014-07-12 02:08:11 +0200157 * We have x = bytes and y = nanosecs. We want z = KiB/s:
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400158 *
Karsten Blees83d26fa2014-07-12 02:08:11 +0200159 * z = (x / 1024) / (y / 1000000000)
160 * z = x / y * 1000000000 / 1024
161 * z = x / (y * 1024 / 1000000000)
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400162 * z = x / y'
163 *
164 * To simplify things we'll keep track of misecs, or 1024th of a sec
165 * obtained with:
166 *
Karsten Blees83d26fa2014-07-12 02:08:11 +0200167 * 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
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400171 */
Karsten Blees83d26fa2014-07-12 02:08:11 +0200172 misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400173
Karsten Blees83d26fa2014-07-12 02:08:11 +0200174 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;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500185
Jeff King31319772015-09-24 17:05:57 -0400186 throughput_string(&tp->display, total, rate);
Karsten Blees83d26fa2014-07-12 02:08:11 +0200187 if (progress->last_value != -1 && progress_update)
188 display(progress, progress->last_value, NULL);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400189}
190
Elijah Newrend6861d02017-11-13 12:15:58 -0800191int display_progress(struct progress *progress, uint64_t n)
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400192{
Nicolas Pitrea984a062007-11-08 15:45:41 -0500193 return progress ? display(progress, n, NULL) : 0;
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400194}
195
Elijah Newrend6861d02017-11-13 12:15:58 -0800196static struct progress *start_progress_delay(const char *title, uint64_t total,
Lars Schneider9c5951c2017-12-04 17:07:00 -0500197 unsigned delay)
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400198{
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400199 struct progress *progress = malloc(sizeof(*progress));
200 if (!progress) {
201 /* unlikely, but here's a good fallback */
202 fprintf(stderr, "%s...\n", title);
Johannes Sixt137a0d02007-11-19 20:48:58 +0100203 fflush(stderr);
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400204 return NULL;
205 }
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400206 progress->title = title;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400207 progress->total = total;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400208 progress->last_value = -1;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400209 progress->last_percent = -1;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400210 progress->delay = delay;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400211 progress->throughput = NULL;
René Scharfe0fae1e02017-07-08 18:43:42 +0200212 progress->start_ns = getnanotime();
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400213 set_progress_signal();
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400214 return progress;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400215}
216
Elijah Newrend6861d02017-11-13 12:15:58 -0800217struct progress *start_delayed_progress(const char *title, uint64_t total)
Junio C Hamano8aade102017-08-19 10:39:41 -0700218{
Lars Schneider9c5951c2017-12-04 17:07:00 -0500219 return start_progress_delay(title, total, 2);
Junio C Hamano8aade102017-08-19 10:39:41 -0700220}
221
Elijah Newrend6861d02017-11-13 12:15:58 -0800222struct progress *start_progress(const char *title, uint64_t total)
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400223{
Lars Schneider9c5951c2017-12-04 17:07:00 -0500224 return start_progress_delay(title, total, 0);
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400225}
226
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400227void stop_progress(struct progress **p_progress)
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400228{
Nguyễn Thái Ngọc Duy754dbc42014-02-21 19:50:18 +0700229 stop_progress_msg(p_progress, _("done"));
Nicolas Pitrea984a062007-11-08 15:45:41 -0500230}
231
232void stop_progress_msg(struct progress **p_progress, const char *msg)
233{
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400234 struct progress *progress = *p_progress;
235 if (!progress)
236 return;
237 *p_progress = NULL;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400238 if (progress->last_value != -1) {
239 /* Force the last update */
Maxim Moseychukfbd09432017-02-16 20:07:13 +0300240 char *buf;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500241 struct throughput *tp = progress->throughput;
Boyd Lynn Gerberd4c44442008-06-08 09:26:15 -0600242
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500243 if (tp) {
René Scharfe0fae1e02017-07-08 18:43:42 +0200244 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);
Jeff King31319772015-09-24 17:05:57 -0400248 throughput_string(&tp->display, tp->curr_total, rate);
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500249 }
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400250 progress_update = 1;
Maxim Moseychukfbd09432017-02-16 20:07:13 +0300251 buf = xstrfmt(", %s.\n", msg);
252 display(progress, progress->last_value, buf);
253 free(buf);
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400254 }
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400255 clear_progress_signal();
Jeff King31319772015-09-24 17:05:57 -0400256 if (progress->throughput)
257 strbuf_release(&progress->throughput->display);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400258 free(progress->throughput);
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400259 free(progress);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400260}