blob: 76a88c573f7895bbf388ab4335faa1b86c3975d3 [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;
33 int last_value;
34 unsigned total;
35 unsigned last_percent;
36 unsigned delay;
37 unsigned delayed_percent_treshold;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040038 struct throughput *throughput;
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
Nicolas Pitrea984a062007-11-08 15:45:41 -050081static int display(struct progress *progress, unsigned 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
Nicolas Pitre180a9f22007-04-20 15:05:27 -040085 if (progress->delay) {
Nicolas Pitre180a9f22007-04-20 15:05:27 -040086 if (!progress_update || --progress->delay)
87 return 0;
88 if (progress->total) {
89 unsigned percent = n * 100 / progress->total;
90 if (percent > progress->delayed_percent_treshold) {
91 /* inhibit this progress report entirely */
92 clear_progress_signal();
93 progress->delay = -1;
94 progress->total = 0;
95 return 0;
96 }
97 }
Nicolas Pitre180a9f22007-04-20 15:05:27 -040098 }
Nicolas Pitre42e18fb2007-10-16 21:55:45 -040099
100 progress->last_value = n;
Jeff King31319772015-09-24 17:05:57 -0400101 tp = (progress->throughput) ? progress->throughput->display.buf : "";
Nicolas Pitrea984a062007-11-08 15:45:41 -0500102 eol = done ? done : " \r";
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400103 if (progress->total) {
104 unsigned percent = n * 100 / progress->total;
105 if (percent != progress->last_percent || progress_update) {
106 progress->last_percent = percent;
Luke Mewburn85cb8902015-04-13 23:30:51 +1000107 if (is_foreground_fd(fileno(stderr)) || done) {
108 fprintf(stderr, "%s: %3u%% (%u/%u)%s%s",
109 progress->title, percent, n,
110 progress->total, tp, eol);
111 fflush(stderr);
112 }
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400113 progress_update = 0;
114 return 1;
115 }
116 } else if (progress_update) {
Luke Mewburn85cb8902015-04-13 23:30:51 +1000117 if (is_foreground_fd(fileno(stderr)) || done) {
118 fprintf(stderr, "%s: %u%s%s",
119 progress->title, n, tp, eol);
120 fflush(stderr);
121 }
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400122 progress_update = 0;
123 return 1;
124 }
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400125
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400126 return 0;
127}
128
Antoine Pelisse079b5462013-04-10 21:03:23 +0200129static void throughput_string(struct strbuf *buf, off_t total,
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500130 unsigned int rate)
131{
Jeff King31319772015-09-24 17:05:57 -0400132 strbuf_reset(buf);
Antoine Pelisse079b5462013-04-10 21:03:23 +0200133 strbuf_addstr(buf, ", ");
134 strbuf_humanise_bytes(buf, total);
135 strbuf_addstr(buf, " | ");
136 strbuf_humanise_bytes(buf, rate * 1024);
137 strbuf_addstr(buf, "/s");
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500138}
139
Nicolas Pitre218558a2007-11-04 22:15:41 -0500140void display_throughput(struct progress *progress, off_t total)
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400141{
142 struct throughput *tp;
Karsten Blees83d26fa2014-07-12 02:08:11 +0200143 uint64_t now_ns;
144 unsigned int misecs, count, rate;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400145
146 if (!progress)
147 return;
148 tp = progress->throughput;
149
Karsten Blees83d26fa2014-07-12 02:08:11 +0200150 now_ns = getnanotime();
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400151
152 if (!tp) {
153 progress->throughput = tp = calloc(1, sizeof(*tp));
Nicolas Pitre218558a2007-11-04 22:15:41 -0500154 if (tp) {
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500155 tp->prev_total = tp->curr_total = total;
Karsten Blees83d26fa2014-07-12 02:08:11 +0200156 tp->prev_ns = now_ns;
Jeff King31319772015-09-24 17:05:57 -0400157 strbuf_init(&tp->display, 0);
Nicolas Pitre218558a2007-11-04 22:15:41 -0500158 }
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400159 return;
160 }
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500161 tp->curr_total = total;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400162
Karsten Blees83d26fa2014-07-12 02:08:11 +0200163 /* only update throughput every 0.5 s */
164 if (now_ns - tp->prev_ns <= 500000000)
165 return;
166
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400167 /*
Karsten Blees83d26fa2014-07-12 02:08:11 +0200168 * We have x = bytes and y = nanosecs. We want z = KiB/s:
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400169 *
Karsten Blees83d26fa2014-07-12 02:08:11 +0200170 * z = (x / 1024) / (y / 1000000000)
171 * z = x / y * 1000000000 / 1024
172 * z = x / (y * 1024 / 1000000000)
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400173 * z = x / y'
174 *
175 * To simplify things we'll keep track of misecs, or 1024th of a sec
176 * obtained with:
177 *
Karsten Blees83d26fa2014-07-12 02:08:11 +0200178 * y' = y * 1024 / 1000000000
179 * y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
180 * y' = y / 2^32 * 4398
181 * y' = (y * 4398) >> 32
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400182 */
Karsten Blees83d26fa2014-07-12 02:08:11 +0200183 misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400184
Karsten Blees83d26fa2014-07-12 02:08:11 +0200185 count = total - tp->prev_total;
186 tp->prev_total = total;
187 tp->prev_ns = now_ns;
188 tp->avg_bytes += count;
189 tp->avg_misecs += misecs;
190 rate = tp->avg_bytes / tp->avg_misecs;
191 tp->avg_bytes -= tp->last_bytes[tp->idx];
192 tp->avg_misecs -= tp->last_misecs[tp->idx];
193 tp->last_bytes[tp->idx] = count;
194 tp->last_misecs[tp->idx] = misecs;
195 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500196
Jeff King31319772015-09-24 17:05:57 -0400197 throughput_string(&tp->display, total, rate);
Karsten Blees83d26fa2014-07-12 02:08:11 +0200198 if (progress->last_value != -1 && progress_update)
199 display(progress, progress->last_value, NULL);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400200}
201
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400202int display_progress(struct progress *progress, unsigned n)
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400203{
Nicolas Pitrea984a062007-11-08 15:45:41 -0500204 return progress ? display(progress, n, NULL) : 0;
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400205}
206
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400207struct progress *start_progress_delay(const char *title, unsigned total,
208 unsigned percent_treshold, unsigned delay)
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400209{
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400210 struct progress *progress = malloc(sizeof(*progress));
211 if (!progress) {
212 /* unlikely, but here's a good fallback */
213 fprintf(stderr, "%s...\n", title);
Johannes Sixt137a0d02007-11-19 20:48:58 +0100214 fflush(stderr);
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400215 return NULL;
216 }
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400217 progress->title = title;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400218 progress->total = total;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400219 progress->last_value = -1;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400220 progress->last_percent = -1;
221 progress->delayed_percent_treshold = percent_treshold;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400222 progress->delay = delay;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400223 progress->throughput = NULL;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400224 set_progress_signal();
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400225 return progress;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400226}
227
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400228struct progress *start_progress(const char *title, unsigned total)
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400229{
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400230 return start_progress_delay(title, total, 0, 0);
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400231}
232
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400233void stop_progress(struct progress **p_progress)
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400234{
Nguyễn Thái Ngọc Duy754dbc42014-02-21 19:50:18 +0700235 stop_progress_msg(p_progress, _("done"));
Nicolas Pitrea984a062007-11-08 15:45:41 -0500236}
237
238void stop_progress_msg(struct progress **p_progress, const char *msg)
239{
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400240 struct progress *progress = *p_progress;
241 if (!progress)
242 return;
243 *p_progress = NULL;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400244 if (progress->last_value != -1) {
245 /* Force the last update */
Boyd Lynn Gerberd4c44442008-06-08 09:26:15 -0600246 char buf[128], *bufp;
247 size_t len = strlen(msg) + 5;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500248 struct throughput *tp = progress->throughput;
Boyd Lynn Gerberd4c44442008-06-08 09:26:15 -0600249
Jeff King3733e692016-02-22 17:44:28 -0500250 bufp = (len < sizeof(buf)) ? buf : xmallocz(len);
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500251 if (tp) {
252 unsigned int rate = !tp->avg_misecs ? 0 :
253 tp->avg_bytes / tp->avg_misecs;
Jeff King31319772015-09-24 17:05:57 -0400254 throughput_string(&tp->display, tp->curr_total, rate);
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500255 }
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400256 progress_update = 1;
Jeff Kingf5691aa2015-09-24 17:06:46 -0400257 xsnprintf(bufp, len + 1, ", %s.\n", msg);
Boyd Lynn Gerberd4c44442008-06-08 09:26:15 -0600258 display(progress, progress->last_value, bufp);
259 if (buf != bufp)
260 free(bufp);
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400261 }
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400262 clear_progress_signal();
Jeff King31319772015-09-24 17:05:57 -0400263 if (progress->throughput)
264 strbuf_release(&progress->throughput->display);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400265 free(progress->throughput);
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400266 free(progress);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400267}