Merge branch 'jk/repack-pack-keep-objects' into maint
[git/mjg.git] / progress.c
blob261314ef3cd60662300129df98afff7ea2a1673c
1 /*
2 * Simple text-based progress display module for GIT
4 * Copyright (c) 2007 by Nicolas Pitre <nico@fluxnic.net>
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 */
11 #include "git-compat-util.h"
12 #include "gettext.h"
13 #include "progress.h"
14 #include "strbuf.h"
16 #define TP_IDX_MAX 8
18 struct throughput {
19 off_t curr_total;
20 off_t prev_total;
21 struct timeval prev_tv;
22 unsigned int avg_bytes;
23 unsigned int avg_misecs;
24 unsigned int last_bytes[TP_IDX_MAX];
25 unsigned int last_misecs[TP_IDX_MAX];
26 unsigned int idx;
27 char display[32];
30 struct progress {
31 const char *title;
32 int last_value;
33 unsigned total;
34 unsigned last_percent;
35 unsigned delay;
36 unsigned delayed_percent_treshold;
37 struct throughput *throughput;
40 static volatile sig_atomic_t progress_update;
42 static void progress_interval(int signum)
44 progress_update = 1;
47 static void set_progress_signal(void)
49 struct sigaction sa;
50 struct itimerval v;
52 progress_update = 0;
54 memset(&sa, 0, sizeof(sa));
55 sa.sa_handler = progress_interval;
56 sigemptyset(&sa.sa_mask);
57 sa.sa_flags = SA_RESTART;
58 sigaction(SIGALRM, &sa, NULL);
60 v.it_interval.tv_sec = 1;
61 v.it_interval.tv_usec = 0;
62 v.it_value = v.it_interval;
63 setitimer(ITIMER_REAL, &v, NULL);
66 static void clear_progress_signal(void)
68 struct itimerval v = {{0,},};
69 setitimer(ITIMER_REAL, &v, NULL);
70 signal(SIGALRM, SIG_IGN);
71 progress_update = 0;
74 static int display(struct progress *progress, unsigned n, const char *done)
76 const char *eol, *tp;
78 if (progress->delay) {
79 if (!progress_update || --progress->delay)
80 return 0;
81 if (progress->total) {
82 unsigned percent = n * 100 / progress->total;
83 if (percent > progress->delayed_percent_treshold) {
84 /* inhibit this progress report entirely */
85 clear_progress_signal();
86 progress->delay = -1;
87 progress->total = 0;
88 return 0;
93 progress->last_value = n;
94 tp = (progress->throughput) ? progress->throughput->display : "";
95 eol = done ? done : " \r";
96 if (progress->total) {
97 unsigned percent = n * 100 / progress->total;
98 if (percent != progress->last_percent || progress_update) {
99 progress->last_percent = percent;
100 fprintf(stderr, "%s: %3u%% (%u/%u)%s%s",
101 progress->title, percent, n,
102 progress->total, tp, eol);
103 fflush(stderr);
104 progress_update = 0;
105 return 1;
107 } else if (progress_update) {
108 fprintf(stderr, "%s: %u%s%s", progress->title, n, tp, eol);
109 fflush(stderr);
110 progress_update = 0;
111 return 1;
114 return 0;
117 static void throughput_string(struct strbuf *buf, off_t total,
118 unsigned int rate)
120 strbuf_addstr(buf, ", ");
121 strbuf_humanise_bytes(buf, total);
122 strbuf_addstr(buf, " | ");
123 strbuf_humanise_bytes(buf, rate * 1024);
124 strbuf_addstr(buf, "/s");
127 void display_throughput(struct progress *progress, off_t total)
129 struct throughput *tp;
130 struct timeval tv;
131 unsigned int misecs;
133 if (!progress)
134 return;
135 tp = progress->throughput;
137 gettimeofday(&tv, NULL);
139 if (!tp) {
140 progress->throughput = tp = calloc(1, sizeof(*tp));
141 if (tp) {
142 tp->prev_total = tp->curr_total = total;
143 tp->prev_tv = tv;
145 return;
147 tp->curr_total = total;
150 * We have x = bytes and y = microsecs. We want z = KiB/s:
152 * z = (x / 1024) / (y / 1000000)
153 * z = x / y * 1000000 / 1024
154 * z = x / (y * 1024 / 1000000)
155 * z = x / y'
157 * To simplify things we'll keep track of misecs, or 1024th of a sec
158 * obtained with:
160 * y' = y * 1024 / 1000000
161 * y' = y / (1000000 / 1024)
162 * y' = y / 977
164 misecs = (tv.tv_sec - tp->prev_tv.tv_sec) * 1024;
165 misecs += (int)(tv.tv_usec - tp->prev_tv.tv_usec) / 977;
167 if (misecs > 512) {
168 struct strbuf buf = STRBUF_INIT;
169 unsigned int count, rate;
171 count = total - tp->prev_total;
172 tp->prev_total = total;
173 tp->prev_tv = tv;
174 tp->avg_bytes += count;
175 tp->avg_misecs += misecs;
176 rate = tp->avg_bytes / tp->avg_misecs;
177 tp->avg_bytes -= tp->last_bytes[tp->idx];
178 tp->avg_misecs -= tp->last_misecs[tp->idx];
179 tp->last_bytes[tp->idx] = count;
180 tp->last_misecs[tp->idx] = misecs;
181 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
183 throughput_string(&buf, total, rate);
184 strncpy(tp->display, buf.buf, sizeof(tp->display));
185 strbuf_release(&buf);
186 if (progress->last_value != -1 && progress_update)
187 display(progress, progress->last_value, NULL);
191 int display_progress(struct progress *progress, unsigned n)
193 return progress ? display(progress, n, NULL) : 0;
196 struct progress *start_progress_delay(const char *title, unsigned total,
197 unsigned percent_treshold, unsigned delay)
199 struct progress *progress = malloc(sizeof(*progress));
200 if (!progress) {
201 /* unlikely, but here's a good fallback */
202 fprintf(stderr, "%s...\n", title);
203 fflush(stderr);
204 return NULL;
206 progress->title = title;
207 progress->total = total;
208 progress->last_value = -1;
209 progress->last_percent = -1;
210 progress->delayed_percent_treshold = percent_treshold;
211 progress->delay = delay;
212 progress->throughput = NULL;
213 set_progress_signal();
214 return progress;
217 struct progress *start_progress(const char *title, unsigned total)
219 return start_progress_delay(title, total, 0, 0);
222 void stop_progress(struct progress **p_progress)
224 stop_progress_msg(p_progress, _("done"));
227 void stop_progress_msg(struct progress **p_progress, const char *msg)
229 struct progress *progress = *p_progress;
230 if (!progress)
231 return;
232 *p_progress = NULL;
233 if (progress->last_value != -1) {
234 /* Force the last update */
235 char buf[128], *bufp;
236 size_t len = strlen(msg) + 5;
237 struct throughput *tp = progress->throughput;
239 bufp = (len < sizeof(buf)) ? buf : xmalloc(len + 1);
240 if (tp) {
241 struct strbuf strbuf = STRBUF_INIT;
242 unsigned int rate = !tp->avg_misecs ? 0 :
243 tp->avg_bytes / tp->avg_misecs;
244 throughput_string(&strbuf, tp->curr_total, rate);
245 strncpy(tp->display, strbuf.buf, sizeof(tp->display));
246 strbuf_release(&strbuf);
248 progress_update = 1;
249 sprintf(bufp, ", %s.\n", msg);
250 display(progress, progress->last_value, bufp);
251 if (buf != bufp)
252 free(bufp);
254 clear_progress_signal();
255 free(progress->throughput);
256 free(progress);