progress: clear previous progress update dynamically
[git/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
96a02f8f 11#include "git-compat-util.h"
754dbc43 12#include "gettext.h"
96a02f8f 13#include "progress.h"
079b546a 14#include "strbuf.h"
83d26fa7 15#include "trace.h"
96a02f8f 16
cf84d51c
NP
17#define TP_IDX_MAX 8
18
19struct throughput {
53ed7b5a 20 off_t curr_total;
218558af 21 off_t prev_total;
83d26fa7 22 uint64_t prev_ns;
218558af 23 unsigned int avg_bytes;
cf84d51c 24 unsigned int avg_misecs;
53ed7b5a 25 unsigned int last_bytes[TP_IDX_MAX];
cf84d51c
NP
26 unsigned int last_misecs[TP_IDX_MAX];
27 unsigned int idx;
3131977d 28 struct strbuf display;
cf84d51c
NP
29};
30
dc6a0757
NP
31struct progress {
32 const char *title;
d6861d02
EN
33 uint64_t last_value;
34 uint64_t total;
dc6a0757
NP
35 unsigned last_percent;
36 unsigned delay;
cf84d51c 37 struct throughput *throughput;
0fae1e07 38 uint64_t start_ns;
d53ba841 39 struct strbuf counters_sb;
dc6a0757
NP
40};
41
96a02f8f
NP
42static volatile sig_atomic_t progress_update;
43
44static void progress_interval(int signum)
45{
46 progress_update = 1;
47}
48
49static void set_progress_signal(void)
50{
51 struct sigaction sa;
52 struct itimerval v;
53
180a9f22
NP
54 progress_update = 0;
55
96a02f8f
NP
56 memset(&sa, 0, sizeof(sa));
57 sa.sa_handler = progress_interval;
58 sigemptyset(&sa.sa_mask);
59 sa.sa_flags = SA_RESTART;
60 sigaction(SIGALRM, &sa, NULL);
61
62 v.it_interval.tv_sec = 1;
63 v.it_interval.tv_usec = 0;
64 v.it_value = v.it_interval;
65 setitimer(ITIMER_REAL, &v, NULL);
66}
67
68static void clear_progress_signal(void)
69{
70 struct itimerval v = {{0,},};
71 setitimer(ITIMER_REAL, &v, NULL);
72 signal(SIGALRM, SIG_IGN);
73 progress_update = 0;
74}
75
85cb8906
LM
76static int is_foreground_fd(int fd)
77{
a4fb76ce
JK
78 int tpgrp = tcgetpgrp(fd);
79 return tpgrp < 0 || tpgrp == getpgid(0);
85cb8906
LM
80}
81
9219d127 82static void display(struct progress *progress, uint64_t n, const char *done)
96a02f8f 83{
d53ba841
SG
84 const char *tp;
85 struct strbuf *counters_sb = &progress->counters_sb;
86 int show_update = 0;
9f1fd84e 87 int last_count_len = counters_sb->len;
42e18fbf 88
9c5951ca 89 if (progress->delay && (!progress_update || --progress->delay))
9219d127 90 return;
42e18fbf
NP
91
92 progress->last_value = n;
3131977d 93 tp = (progress->throughput) ? progress->throughput->display.buf : "";
96a02f8f
NP
94 if (progress->total) {
95 unsigned percent = n * 100 / progress->total;
96 if (percent != progress->last_percent || progress_update) {
97 progress->last_percent = percent;
d53ba841
SG
98
99 strbuf_reset(counters_sb);
100 strbuf_addf(counters_sb,
101 "%3u%% (%"PRIuMAX"/%"PRIuMAX")%s", percent,
102 (uintmax_t)n, (uintmax_t)progress->total,
103 tp);
104 show_update = 1;
96a02f8f
NP
105 }
106 } else if (progress_update) {
d53ba841
SG
107 strbuf_reset(counters_sb);
108 strbuf_addf(counters_sb, "%"PRIuMAX"%s", (uintmax_t)n, tp);
109 show_update = 1;
110 }
111
112 if (show_update) {
85cb8906 113 if (is_foreground_fd(fileno(stderr)) || done) {
9f1fd84e
SG
114 const char *eol = done ? done : "\r";
115 size_t clear_len = counters_sb->len < last_count_len ?
116 last_count_len - counters_sb->len + 1 :
117 0;
118 fprintf(stderr, "%s: %s%*s", progress->title,
119 counters_sb->buf, (int) clear_len, eol);
85cb8906
LM
120 fflush(stderr);
121 }
96a02f8f 122 progress_update = 0;
96a02f8f 123 }
96a02f8f
NP
124}
125
d6861d02 126static void throughput_string(struct strbuf *buf, uint64_t total,
53ed7b5a
NP
127 unsigned int rate)
128{
3131977d 129 strbuf_reset(buf);
079b546a
AP
130 strbuf_addstr(buf, ", ");
131 strbuf_humanise_bytes(buf, total);
132 strbuf_addstr(buf, " | ");
133 strbuf_humanise_bytes(buf, rate * 1024);
134 strbuf_addstr(buf, "/s");
53ed7b5a
NP
135}
136
d6861d02 137void display_throughput(struct progress *progress, uint64_t total)
cf84d51c
NP
138{
139 struct throughput *tp;
83d26fa7
KB
140 uint64_t now_ns;
141 unsigned int misecs, count, rate;
cf84d51c
NP
142
143 if (!progress)
144 return;
145 tp = progress->throughput;
146
83d26fa7 147 now_ns = getnanotime();
cf84d51c
NP
148
149 if (!tp) {
150 progress->throughput = tp = calloc(1, sizeof(*tp));
218558af 151 if (tp) {
53ed7b5a 152 tp->prev_total = tp->curr_total = total;
83d26fa7 153 tp->prev_ns = now_ns;
3131977d 154 strbuf_init(&tp->display, 0);
218558af 155 }
cf84d51c
NP
156 return;
157 }
53ed7b5a 158 tp->curr_total = total;
cf84d51c 159
83d26fa7
KB
160 /* only update throughput every 0.5 s */
161 if (now_ns - tp->prev_ns <= 500000000)
162 return;
163
cf84d51c 164 /*
83d26fa7 165 * We have x = bytes and y = nanosecs. We want z = KiB/s:
cf84d51c 166 *
83d26fa7
KB
167 * z = (x / 1024) / (y / 1000000000)
168 * z = x / y * 1000000000 / 1024
169 * z = x / (y * 1024 / 1000000000)
cf84d51c
NP
170 * z = x / y'
171 *
172 * To simplify things we'll keep track of misecs, or 1024th of a sec
173 * obtained with:
174 *
83d26fa7
KB
175 * y' = y * 1024 / 1000000000
176 * y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
177 * y' = y / 2^32 * 4398
178 * y' = (y * 4398) >> 32
cf84d51c 179 */
83d26fa7
KB
180 misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
181
182 count = total - tp->prev_total;
183 tp->prev_total = total;
184 tp->prev_ns = now_ns;
185 tp->avg_bytes += count;
186 tp->avg_misecs += misecs;
187 rate = tp->avg_bytes / tp->avg_misecs;
188 tp->avg_bytes -= tp->last_bytes[tp->idx];
189 tp->avg_misecs -= tp->last_misecs[tp->idx];
190 tp->last_bytes[tp->idx] = count;
191 tp->last_misecs[tp->idx] = misecs;
192 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
193
3131977d 194 throughput_string(&tp->display, total, rate);
83d26fa7
KB
195 if (progress->last_value != -1 && progress_update)
196 display(progress, progress->last_value, NULL);
cf84d51c
NP
197}
198
9219d127 199void display_progress(struct progress *progress, uint64_t n)
96a02f8f 200{
9219d127
SG
201 if (progress)
202 display(progress, n, NULL);
96a02f8f
NP
203}
204
d6861d02 205static struct progress *start_progress_delay(const char *title, uint64_t total,
9c5951ca 206 unsigned delay)
180a9f22 207{
dc6a0757
NP
208 struct progress *progress = malloc(sizeof(*progress));
209 if (!progress) {
210 /* unlikely, but here's a good fallback */
211 fprintf(stderr, "%s...\n", title);
137a0d0e 212 fflush(stderr);
dc6a0757
NP
213 return NULL;
214 }
42e18fbf 215 progress->title = title;
180a9f22 216 progress->total = total;
42e18fbf 217 progress->last_value = -1;
180a9f22 218 progress->last_percent = -1;
180a9f22 219 progress->delay = delay;
cf84d51c 220 progress->throughput = NULL;
0fae1e07 221 progress->start_ns = getnanotime();
d53ba841 222 strbuf_init(&progress->counters_sb, 0);
180a9f22 223 set_progress_signal();
dc6a0757 224 return progress;
180a9f22
NP
225}
226
d6861d02 227struct progress *start_delayed_progress(const char *title, uint64_t total)
8aade107 228{
9c5951ca 229 return start_progress_delay(title, total, 2);
8aade107
JH
230}
231
d6861d02 232struct progress *start_progress(const char *title, uint64_t total)
42e18fbf 233{
9c5951ca 234 return start_progress_delay(title, total, 0);
42e18fbf
NP
235}
236
dc6a0757 237void stop_progress(struct progress **p_progress)
a984a06a 238{
754dbc43 239 stop_progress_msg(p_progress, _("done"));
a984a06a
NP
240}
241
242void stop_progress_msg(struct progress **p_progress, const char *msg)
96a02f8f 243{
dc6a0757
NP
244 struct progress *progress = *p_progress;
245 if (!progress)
246 return;
247 *p_progress = NULL;
42e18fbf
NP
248 if (progress->last_value != -1) {
249 /* Force the last update */
fbd09439 250 char *buf;
53ed7b5a 251 struct throughput *tp = progress->throughput;
d4c44443 252
53ed7b5a 253 if (tp) {
0fae1e07
RS
254 uint64_t now_ns = getnanotime();
255 unsigned int misecs, rate;
256 misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
257 rate = tp->curr_total / (misecs ? misecs : 1);
3131977d 258 throughput_string(&tp->display, tp->curr_total, rate);
53ed7b5a 259 }
42e18fbf 260 progress_update = 1;
fbd09439
MM
261 buf = xstrfmt(", %s.\n", msg);
262 display(progress, progress->last_value, buf);
263 free(buf);
42e18fbf 264 }
96a02f8f 265 clear_progress_signal();
d53ba841 266 strbuf_release(&progress->counters_sb);
3131977d
JK
267 if (progress->throughput)
268 strbuf_release(&progress->throughput->display);
cf84d51c 269 free(progress->throughput);
dc6a0757 270 free(progress);
96a02f8f 271}