blob: 412e6b1ecc36e8bd8b7f090b7da3dcf0c7e4e5bd [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;
Nicolas Pitre81f66542007-11-01 16:59:57 -040028 char display[32];
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
Nicolas Pitrea984a062007-11-08 15:45:41 -050075static int display(struct progress *progress, unsigned n, const char *done)
Nicolas Pitre96a02f82007-04-18 14:27:45 -040076{
Nicolas Pitrea984a062007-11-08 15:45:41 -050077 const char *eol, *tp;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -040078
Nicolas Pitre180a9f22007-04-20 15:05:27 -040079 if (progress->delay) {
Nicolas Pitre180a9f22007-04-20 15:05:27 -040080 if (!progress_update || --progress->delay)
81 return 0;
82 if (progress->total) {
83 unsigned percent = n * 100 / progress->total;
84 if (percent > progress->delayed_percent_treshold) {
85 /* inhibit this progress report entirely */
86 clear_progress_signal();
87 progress->delay = -1;
88 progress->total = 0;
89 return 0;
90 }
91 }
Nicolas Pitre180a9f22007-04-20 15:05:27 -040092 }
Nicolas Pitre42e18fb2007-10-16 21:55:45 -040093
94 progress->last_value = n;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040095 tp = (progress->throughput) ? progress->throughput->display : "";
Nicolas Pitrea984a062007-11-08 15:45:41 -050096 eol = done ? done : " \r";
Nicolas Pitre96a02f82007-04-18 14:27:45 -040097 if (progress->total) {
98 unsigned percent = n * 100 / progress->total;
99 if (percent != progress->last_percent || progress_update) {
100 progress->last_percent = percent;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400101 fprintf(stderr, "%s: %3u%% (%u/%u)%s%s",
102 progress->title, percent, n,
103 progress->total, tp, eol);
Johannes Sixt137a0d02007-11-19 20:48:58 +0100104 fflush(stderr);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400105 progress_update = 0;
106 return 1;
107 }
108 } else if (progress_update) {
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400109 fprintf(stderr, "%s: %u%s%s", progress->title, n, tp, eol);
Johannes Sixt137a0d02007-11-19 20:48:58 +0100110 fflush(stderr);
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
Antoine Pelisse079b5462013-04-10 21:03:23 +0200118static void throughput_string(struct strbuf *buf, off_t total,
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500119 unsigned int rate)
120{
Antoine Pelisse079b5462013-04-10 21:03:23 +0200121 strbuf_addstr(buf, ", ");
122 strbuf_humanise_bytes(buf, total);
123 strbuf_addstr(buf, " | ");
124 strbuf_humanise_bytes(buf, rate * 1024);
125 strbuf_addstr(buf, "/s");
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500126}
127
Nicolas Pitre218558a2007-11-04 22:15:41 -0500128void display_throughput(struct progress *progress, off_t total)
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400129{
130 struct throughput *tp;
Karsten Blees83d26fa2014-07-12 02:08:11 +0200131 uint64_t now_ns;
132 unsigned int misecs, count, rate;
133 struct strbuf buf = STRBUF_INIT;
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;
Nicolas Pitre218558a2007-11-04 22:15:41 -0500146 }
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400147 return;
148 }
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500149 tp->curr_total = total;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400150
Karsten Blees83d26fa2014-07-12 02:08:11 +0200151 /* only update throughput every 0.5 s */
152 if (now_ns - tp->prev_ns <= 500000000)
153 return;
154
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400155 /*
Karsten Blees83d26fa2014-07-12 02:08:11 +0200156 * We have x = bytes and y = nanosecs. We want z = KiB/s:
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400157 *
Karsten Blees83d26fa2014-07-12 02:08:11 +0200158 * z = (x / 1024) / (y / 1000000000)
159 * z = x / y * 1000000000 / 1024
160 * z = x / (y * 1024 / 1000000000)
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400161 * z = x / y'
162 *
163 * To simplify things we'll keep track of misecs, or 1024th of a sec
164 * obtained with:
165 *
Karsten Blees83d26fa2014-07-12 02:08:11 +0200166 * y' = y * 1024 / 1000000000
167 * y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
168 * y' = y / 2^32 * 4398
169 * y' = (y * 4398) >> 32
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400170 */
Karsten Blees83d26fa2014-07-12 02:08:11 +0200171 misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400172
Karsten Blees83d26fa2014-07-12 02:08:11 +0200173 count = total - tp->prev_total;
174 tp->prev_total = total;
175 tp->prev_ns = now_ns;
176 tp->avg_bytes += count;
177 tp->avg_misecs += misecs;
178 rate = tp->avg_bytes / tp->avg_misecs;
179 tp->avg_bytes -= tp->last_bytes[tp->idx];
180 tp->avg_misecs -= tp->last_misecs[tp->idx];
181 tp->last_bytes[tp->idx] = count;
182 tp->last_misecs[tp->idx] = misecs;
183 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500184
Karsten Blees83d26fa2014-07-12 02:08:11 +0200185 throughput_string(&buf, total, rate);
186 strncpy(tp->display, buf.buf, sizeof(tp->display));
187 strbuf_release(&buf);
188 if (progress->last_value != -1 && progress_update)
189 display(progress, progress->last_value, NULL);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400190}
191
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400192int display_progress(struct progress *progress, unsigned n)
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400193{
Nicolas Pitrea984a062007-11-08 15:45:41 -0500194 return progress ? display(progress, n, NULL) : 0;
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400195}
196
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400197struct progress *start_progress_delay(const char *title, unsigned total,
198 unsigned percent_treshold, unsigned delay)
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400199{
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400200 struct progress *progress = malloc(sizeof(*progress));
201 if (!progress) {
202 /* unlikely, but here's a good fallback */
203 fprintf(stderr, "%s...\n", title);
Johannes Sixt137a0d02007-11-19 20:48:58 +0100204 fflush(stderr);
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400205 return NULL;
206 }
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400207 progress->title = title;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400208 progress->total = total;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400209 progress->last_value = -1;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400210 progress->last_percent = -1;
211 progress->delayed_percent_treshold = percent_treshold;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400212 progress->delay = delay;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400213 progress->throughput = NULL;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400214 set_progress_signal();
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400215 return progress;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400216}
217
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400218struct progress *start_progress(const char *title, unsigned total)
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400219{
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400220 return start_progress_delay(title, total, 0, 0);
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400221}
222
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400223void stop_progress(struct progress **p_progress)
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400224{
Nguyễn Thái Ngọc Duy754dbc42014-02-21 19:50:18 +0700225 stop_progress_msg(p_progress, _("done"));
Nicolas Pitrea984a062007-11-08 15:45:41 -0500226}
227
228void stop_progress_msg(struct progress **p_progress, const char *msg)
229{
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400230 struct progress *progress = *p_progress;
231 if (!progress)
232 return;
233 *p_progress = NULL;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400234 if (progress->last_value != -1) {
235 /* Force the last update */
Boyd Lynn Gerberd4c44442008-06-08 09:26:15 -0600236 char buf[128], *bufp;
237 size_t len = strlen(msg) + 5;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500238 struct throughput *tp = progress->throughput;
Boyd Lynn Gerberd4c44442008-06-08 09:26:15 -0600239
240 bufp = (len < sizeof(buf)) ? buf : xmalloc(len + 1);
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500241 if (tp) {
Antoine Pelisse079b5462013-04-10 21:03:23 +0200242 struct strbuf strbuf = STRBUF_INIT;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500243 unsigned int rate = !tp->avg_misecs ? 0 :
244 tp->avg_bytes / tp->avg_misecs;
Antoine Pelisse079b5462013-04-10 21:03:23 +0200245 throughput_string(&strbuf, tp->curr_total, rate);
246 strncpy(tp->display, strbuf.buf, sizeof(tp->display));
247 strbuf_release(&strbuf);
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500248 }
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400249 progress_update = 1;
Boyd Lynn Gerberd4c44442008-06-08 09:26:15 -0600250 sprintf(bufp, ", %s.\n", msg);
251 display(progress, progress->last_value, bufp);
252 if (buf != bufp)
253 free(bufp);
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400254 }
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400255 clear_progress_signal();
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400256 free(progress->throughput);
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400257 free(progress);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400258}