]> git.ipfire.org Git - thirdparty/git.git/blame - progress.c
i18n: avoid parenthesized string as array initializer
[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
NP
11#include "git-compat-util.h"
12#include "progress.h"
13
cf84d51c
NP
14#define TP_IDX_MAX 8
15
16struct throughput {
53ed7b5a 17 off_t curr_total;
218558af 18 off_t prev_total;
cf84d51c 19 struct timeval prev_tv;
218558af 20 unsigned int avg_bytes;
cf84d51c 21 unsigned int avg_misecs;
53ed7b5a 22 unsigned int last_bytes[TP_IDX_MAX];
cf84d51c
NP
23 unsigned int last_misecs[TP_IDX_MAX];
24 unsigned int idx;
81f6654a 25 char display[32];
cf84d51c
NP
26};
27
dc6a0757
NP
28struct progress {
29 const char *title;
30 int last_value;
31 unsigned total;
32 unsigned last_percent;
33 unsigned delay;
34 unsigned delayed_percent_treshold;
cf84d51c 35 struct throughput *throughput;
dc6a0757
NP
36};
37
96a02f8f
NP
38static volatile sig_atomic_t progress_update;
39
40static void progress_interval(int signum)
41{
42 progress_update = 1;
43}
44
45static void set_progress_signal(void)
46{
47 struct sigaction sa;
48 struct itimerval v;
49
180a9f22
NP
50 progress_update = 0;
51
96a02f8f
NP
52 memset(&sa, 0, sizeof(sa));
53 sa.sa_handler = progress_interval;
54 sigemptyset(&sa.sa_mask);
55 sa.sa_flags = SA_RESTART;
56 sigaction(SIGALRM, &sa, NULL);
57
58 v.it_interval.tv_sec = 1;
59 v.it_interval.tv_usec = 0;
60 v.it_value = v.it_interval;
61 setitimer(ITIMER_REAL, &v, NULL);
62}
63
64static void clear_progress_signal(void)
65{
66 struct itimerval v = {{0,},};
67 setitimer(ITIMER_REAL, &v, NULL);
68 signal(SIGALRM, SIG_IGN);
69 progress_update = 0;
70}
71
a984a06a 72static int display(struct progress *progress, unsigned n, const char *done)
96a02f8f 73{
a984a06a 74 const char *eol, *tp;
42e18fbf 75
180a9f22 76 if (progress->delay) {
180a9f22
NP
77 if (!progress_update || --progress->delay)
78 return 0;
79 if (progress->total) {
80 unsigned percent = n * 100 / progress->total;
81 if (percent > progress->delayed_percent_treshold) {
82 /* inhibit this progress report entirely */
83 clear_progress_signal();
84 progress->delay = -1;
85 progress->total = 0;
86 return 0;
87 }
88 }
180a9f22 89 }
42e18fbf
NP
90
91 progress->last_value = n;
cf84d51c 92 tp = (progress->throughput) ? progress->throughput->display : "";
a984a06a 93 eol = done ? done : " \r";
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;
cf84d51c
NP
98 fprintf(stderr, "%s: %3u%% (%u/%u)%s%s",
99 progress->title, percent, n,
100 progress->total, tp, eol);
137a0d0e 101 fflush(stderr);
96a02f8f
NP
102 progress_update = 0;
103 return 1;
104 }
105 } else if (progress_update) {
cf84d51c 106 fprintf(stderr, "%s: %u%s%s", progress->title, n, tp, eol);
137a0d0e 107 fflush(stderr);
96a02f8f
NP
108 progress_update = 0;
109 return 1;
110 }
42e18fbf 111
96a02f8f
NP
112 return 0;
113}
114
53ed7b5a
NP
115static void throughput_string(struct throughput *tp, off_t total,
116 unsigned int rate)
117{
118 int l = sizeof(tp->display);
119 if (total > 1 << 30) {
120 l -= snprintf(tp->display, l, ", %u.%2.2u GiB",
121 (int)(total >> 30),
122 (int)(total & ((1 << 30) - 1)) / 10737419);
123 } else if (total > 1 << 20) {
66913284 124 int x = total + 5243; /* for rounding */
53ed7b5a 125 l -= snprintf(tp->display, l, ", %u.%2.2u MiB",
66913284 126 x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20);
53ed7b5a 127 } else if (total > 1 << 10) {
66913284 128 int x = total + 5; /* for rounding */
53ed7b5a 129 l -= snprintf(tp->display, l, ", %u.%2.2u KiB",
66913284 130 x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10);
53ed7b5a
NP
131 } else {
132 l -= snprintf(tp->display, l, ", %u bytes", (int)total);
133 }
583371af
NP
134
135 if (rate > 1 << 10) {
136 int x = rate + 5; /* for rounding */
137 snprintf(tp->display + sizeof(tp->display) - l, l,
138 " | %u.%2.2u MiB/s",
139 x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10);
140 } else if (rate)
53ed7b5a
NP
141 snprintf(tp->display + sizeof(tp->display) - l, l,
142 " | %u KiB/s", rate);
143}
144
218558af 145void display_throughput(struct progress *progress, off_t total)
cf84d51c
NP
146{
147 struct throughput *tp;
148 struct timeval tv;
149 unsigned int misecs;
150
151 if (!progress)
152 return;
153 tp = progress->throughput;
154
155 gettimeofday(&tv, NULL);
156
157 if (!tp) {
158 progress->throughput = tp = calloc(1, sizeof(*tp));
218558af 159 if (tp) {
53ed7b5a 160 tp->prev_total = tp->curr_total = total;
cf84d51c 161 tp->prev_tv = tv;
218558af 162 }
cf84d51c
NP
163 return;
164 }
53ed7b5a 165 tp->curr_total = total;
cf84d51c 166
cf84d51c
NP
167 /*
168 * We have x = bytes and y = microsecs. We want z = KiB/s:
169 *
170 * z = (x / 1024) / (y / 1000000)
171 * z = x / y * 1000000 / 1024
172 * z = x / (y * 1024 / 1000000)
173 * z = x / y'
174 *
175 * To simplify things we'll keep track of misecs, or 1024th of a sec
176 * obtained with:
177 *
178 * y' = y * 1024 / 1000000
179 * y' = y / (1000000 / 1024)
180 * y' = y / 977
181 */
182 misecs = (tv.tv_sec - tp->prev_tv.tv_sec) * 1024;
183 misecs += (int)(tv.tv_usec - tp->prev_tv.tv_usec) / 977;
184
185 if (misecs > 512) {
53ed7b5a
NP
186 unsigned int count, rate;
187
188 count = total - tp->prev_total;
218558af 189 tp->prev_total = total;
cf84d51c 190 tp->prev_tv = tv;
218558af 191 tp->avg_bytes += count;
cf84d51c 192 tp->avg_misecs += misecs;
53ed7b5a 193 rate = tp->avg_bytes / tp->avg_misecs;
cf84d51c
NP
194 tp->avg_bytes -= tp->last_bytes[tp->idx];
195 tp->avg_misecs -= tp->last_misecs[tp->idx];
218558af 196 tp->last_bytes[tp->idx] = count;
cf84d51c
NP
197 tp->last_misecs[tp->idx] = misecs;
198 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
3e935d19 199
53ed7b5a 200 throughput_string(tp, total, rate);
3e935d19 201 if (progress->last_value != -1 && progress_update)
a984a06a 202 display(progress, progress->last_value, NULL);
cf84d51c
NP
203 }
204}
205
42e18fbf 206int display_progress(struct progress *progress, unsigned n)
96a02f8f 207{
a984a06a 208 return progress ? display(progress, n, NULL) : 0;
96a02f8f
NP
209}
210
dc6a0757
NP
211struct progress *start_progress_delay(const char *title, unsigned total,
212 unsigned percent_treshold, unsigned delay)
180a9f22 213{
dc6a0757
NP
214 struct progress *progress = malloc(sizeof(*progress));
215 if (!progress) {
216 /* unlikely, but here's a good fallback */
217 fprintf(stderr, "%s...\n", title);
137a0d0e 218 fflush(stderr);
dc6a0757
NP
219 return NULL;
220 }
42e18fbf 221 progress->title = title;
180a9f22 222 progress->total = total;
42e18fbf 223 progress->last_value = -1;
180a9f22
NP
224 progress->last_percent = -1;
225 progress->delayed_percent_treshold = percent_treshold;
180a9f22 226 progress->delay = delay;
cf84d51c 227 progress->throughput = NULL;
180a9f22 228 set_progress_signal();
dc6a0757 229 return progress;
180a9f22
NP
230}
231
dc6a0757 232struct progress *start_progress(const char *title, unsigned total)
42e18fbf 233{
dc6a0757 234 return start_progress_delay(title, total, 0, 0);
42e18fbf
NP
235}
236
dc6a0757 237void stop_progress(struct progress **p_progress)
a984a06a
NP
238{
239 stop_progress_msg(p_progress, "done");
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 */
d4c44443
BLG
250 char buf[128], *bufp;
251 size_t len = strlen(msg) + 5;
53ed7b5a 252 struct throughput *tp = progress->throughput;
d4c44443
BLG
253
254 bufp = (len < sizeof(buf)) ? buf : xmalloc(len + 1);
53ed7b5a
NP
255 if (tp) {
256 unsigned int rate = !tp->avg_misecs ? 0 :
257 tp->avg_bytes / tp->avg_misecs;
258 throughput_string(tp, tp->curr_total, rate);
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}