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