]> git.ipfire.org Git - thirdparty/git.git/blame - progress.c
compat/mingw: stubs for getpgid() and tcgetpgrp()
[thirdparty/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;
81f6654a 28 char display[32];
cf84d51c
NP
29};
30
dc6a0757
NP
31struct progress {
32 const char *title;
33 int last_value;
34 unsigned total;
35 unsigned last_percent;
36 unsigned delay;
37 unsigned delayed_percent_treshold;
cf84d51c 38 struct throughput *throughput;
dc6a0757
NP
39};
40
96a02f8f
NP
41static 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
180a9f22
NP
53 progress_update = 0;
54
96a02f8f
NP
55 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
85cb8906
LM
75static int is_foreground_fd(int fd)
76{
77 return getpgid(0) == tcgetpgrp(fd);
78}
79
a984a06a 80static int display(struct progress *progress, unsigned n, const char *done)
96a02f8f 81{
a984a06a 82 const char *eol, *tp;
42e18fbf 83
180a9f22 84 if (progress->delay) {
180a9f22
NP
85 if (!progress_update || --progress->delay)
86 return 0;
87 if (progress->total) {
88 unsigned percent = n * 100 / progress->total;
89 if (percent > progress->delayed_percent_treshold) {
90 /* inhibit this progress report entirely */
91 clear_progress_signal();
92 progress->delay = -1;
93 progress->total = 0;
94 return 0;
95 }
96 }
180a9f22 97 }
42e18fbf
NP
98
99 progress->last_value = n;
cf84d51c 100 tp = (progress->throughput) ? progress->throughput->display : "";
a984a06a 101 eol = done ? done : " \r";
96a02f8f
NP
102 if (progress->total) {
103 unsigned percent = n * 100 / progress->total;
104 if (percent != progress->last_percent || progress_update) {
105 progress->last_percent = percent;
85cb8906
LM
106 if (is_foreground_fd(fileno(stderr)) || done) {
107 fprintf(stderr, "%s: %3u%% (%u/%u)%s%s",
108 progress->title, percent, n,
109 progress->total, tp, eol);
110 fflush(stderr);
111 }
96a02f8f
NP
112 progress_update = 0;
113 return 1;
114 }
115 } else if (progress_update) {
85cb8906
LM
116 if (is_foreground_fd(fileno(stderr)) || done) {
117 fprintf(stderr, "%s: %u%s%s",
118 progress->title, n, tp, eol);
119 fflush(stderr);
120 }
96a02f8f
NP
121 progress_update = 0;
122 return 1;
123 }
42e18fbf 124
96a02f8f
NP
125 return 0;
126}
127
079b546a 128static void throughput_string(struct strbuf *buf, off_t total,
53ed7b5a
NP
129 unsigned int rate)
130{
079b546a
AP
131 strbuf_addstr(buf, ", ");
132 strbuf_humanise_bytes(buf, total);
133 strbuf_addstr(buf, " | ");
134 strbuf_humanise_bytes(buf, rate * 1024);
135 strbuf_addstr(buf, "/s");
53ed7b5a
NP
136}
137
218558af 138void display_throughput(struct progress *progress, off_t total)
cf84d51c
NP
139{
140 struct throughput *tp;
83d26fa7
KB
141 uint64_t now_ns;
142 unsigned int misecs, count, rate;
143 struct strbuf buf = STRBUF_INIT;
cf84d51c
NP
144
145 if (!progress)
146 return;
147 tp = progress->throughput;
148
83d26fa7 149 now_ns = getnanotime();
cf84d51c
NP
150
151 if (!tp) {
152 progress->throughput = tp = calloc(1, sizeof(*tp));
218558af 153 if (tp) {
53ed7b5a 154 tp->prev_total = tp->curr_total = total;
83d26fa7 155 tp->prev_ns = now_ns;
218558af 156 }
cf84d51c
NP
157 return;
158 }
53ed7b5a 159 tp->curr_total = total;
cf84d51c 160
83d26fa7
KB
161 /* only update throughput every 0.5 s */
162 if (now_ns - tp->prev_ns <= 500000000)
163 return;
164
cf84d51c 165 /*
83d26fa7 166 * We have x = bytes and y = nanosecs. We want z = KiB/s:
cf84d51c 167 *
83d26fa7
KB
168 * z = (x / 1024) / (y / 1000000000)
169 * z = x / y * 1000000000 / 1024
170 * z = x / (y * 1024 / 1000000000)
cf84d51c
NP
171 * z = x / y'
172 *
173 * To simplify things we'll keep track of misecs, or 1024th of a sec
174 * obtained with:
175 *
83d26fa7
KB
176 * y' = y * 1024 / 1000000000
177 * y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
178 * y' = y / 2^32 * 4398
179 * y' = (y * 4398) >> 32
cf84d51c 180 */
83d26fa7
KB
181 misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
182
183 count = total - tp->prev_total;
184 tp->prev_total = total;
185 tp->prev_ns = now_ns;
186 tp->avg_bytes += count;
187 tp->avg_misecs += misecs;
188 rate = tp->avg_bytes / tp->avg_misecs;
189 tp->avg_bytes -= tp->last_bytes[tp->idx];
190 tp->avg_misecs -= tp->last_misecs[tp->idx];
191 tp->last_bytes[tp->idx] = count;
192 tp->last_misecs[tp->idx] = misecs;
193 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
194
195 throughput_string(&buf, total, rate);
196 strncpy(tp->display, buf.buf, sizeof(tp->display));
197 strbuf_release(&buf);
198 if (progress->last_value != -1 && progress_update)
199 display(progress, progress->last_value, NULL);
cf84d51c
NP
200}
201
42e18fbf 202int display_progress(struct progress *progress, unsigned n)
96a02f8f 203{
a984a06a 204 return progress ? display(progress, n, NULL) : 0;
96a02f8f
NP
205}
206
dc6a0757
NP
207struct progress *start_progress_delay(const char *title, unsigned total,
208 unsigned percent_treshold, unsigned delay)
180a9f22 209{
dc6a0757
NP
210 struct progress *progress = malloc(sizeof(*progress));
211 if (!progress) {
212 /* unlikely, but here's a good fallback */
213 fprintf(stderr, "%s...\n", title);
137a0d0e 214 fflush(stderr);
dc6a0757
NP
215 return NULL;
216 }
42e18fbf 217 progress->title = title;
180a9f22 218 progress->total = total;
42e18fbf 219 progress->last_value = -1;
180a9f22
NP
220 progress->last_percent = -1;
221 progress->delayed_percent_treshold = percent_treshold;
180a9f22 222 progress->delay = delay;
cf84d51c 223 progress->throughput = NULL;
180a9f22 224 set_progress_signal();
dc6a0757 225 return progress;
180a9f22
NP
226}
227
dc6a0757 228struct progress *start_progress(const char *title, unsigned total)
42e18fbf 229{
dc6a0757 230 return start_progress_delay(title, total, 0, 0);
42e18fbf
NP
231}
232
dc6a0757 233void stop_progress(struct progress **p_progress)
a984a06a 234{
754dbc43 235 stop_progress_msg(p_progress, _("done"));
a984a06a
NP
236}
237
238void stop_progress_msg(struct progress **p_progress, const char *msg)
96a02f8f 239{
dc6a0757
NP
240 struct progress *progress = *p_progress;
241 if (!progress)
242 return;
243 *p_progress = NULL;
42e18fbf
NP
244 if (progress->last_value != -1) {
245 /* Force the last update */
d4c44443
BLG
246 char buf[128], *bufp;
247 size_t len = strlen(msg) + 5;
53ed7b5a 248 struct throughput *tp = progress->throughput;
d4c44443
BLG
249
250 bufp = (len < sizeof(buf)) ? buf : xmalloc(len + 1);
53ed7b5a 251 if (tp) {
079b546a 252 struct strbuf strbuf = STRBUF_INIT;
53ed7b5a
NP
253 unsigned int rate = !tp->avg_misecs ? 0 :
254 tp->avg_bytes / tp->avg_misecs;
079b546a
AP
255 throughput_string(&strbuf, tp->curr_total, rate);
256 strncpy(tp->display, strbuf.buf, sizeof(tp->display));
257 strbuf_release(&strbuf);
53ed7b5a 258 }
42e18fbf 259 progress_update = 1;
d4c44443
BLG
260 sprintf(bufp, ", %s.\n", msg);
261 display(progress, progress->last_value, bufp);
262 if (buf != bufp)
263 free(bufp);
42e18fbf 264 }
96a02f8f 265 clear_progress_signal();
cf84d51c 266 free(progress->throughput);
dc6a0757 267 free(progress);
96a02f8f 268}