rev-parse --parseopt: add the --stuck-long mode
[alt-git.git] / progress.c
blob10652b174d1ccfb941b3d4122a882f1af3323084
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 "progress.h"
13 #include "strbuf.h"
15 #define TP_IDX_MAX 8
17 struct throughput {
18 off_t curr_total;
19 off_t prev_total;
20 struct timeval prev_tv;
21 unsigned int avg_bytes;
22 unsigned int avg_misecs;
23 unsigned int last_bytes[TP_IDX_MAX];
24 unsigned int last_misecs[TP_IDX_MAX];
25 unsigned int idx;
26 char display[32];
29 struct progress {
30 const char *title;
31 int last_value;
32 unsigned total;
33 unsigned last_percent;
34 unsigned delay;
35 unsigned delayed_percent_treshold;
36 struct throughput *throughput;
39 static volatile sig_atomic_t progress_update;
41 static void progress_interval(int signum)
43 progress_update = 1;
46 static void set_progress_signal(void)
48 struct sigaction sa;
49 struct itimerval v;
51 progress_update = 0;
53 memset(&sa, 0, sizeof(sa));
54 sa.sa_handler = progress_interval;
55 sigemptyset(&sa.sa_mask);
56 sa.sa_flags = SA_RESTART;
57 sigaction(SIGALRM, &sa, NULL);
59 v.it_interval.tv_sec = 1;
60 v.it_interval.tv_usec = 0;
61 v.it_value = v.it_interval;
62 setitimer(ITIMER_REAL, &v, NULL);
65 static void clear_progress_signal(void)
67 struct itimerval v = {{0,},};
68 setitimer(ITIMER_REAL, &v, NULL);
69 signal(SIGALRM, SIG_IGN);
70 progress_update = 0;
73 static int display(struct progress *progress, unsigned n, const char *done)
75 const char *eol, *tp;
77 if (progress->delay) {
78 if (!progress_update || --progress->delay)
79 return 0;
80 if (progress->total) {
81 unsigned percent = n * 100 / progress->total;
82 if (percent > progress->delayed_percent_treshold) {
83 /* inhibit this progress report entirely */
84 clear_progress_signal();
85 progress->delay = -1;
86 progress->total = 0;
87 return 0;
92 progress->last_value = n;
93 tp = (progress->throughput) ? progress->throughput->display : "";
94 eol = done ? done : " \r";
95 if (progress->total) {
96 unsigned percent = n * 100 / progress->total;
97 if (percent != progress->last_percent || progress_update) {
98 progress->last_percent = percent;
99 fprintf(stderr, "%s: %3u%% (%u/%u)%s%s",
100 progress->title, percent, n,
101 progress->total, tp, eol);
102 fflush(stderr);
103 progress_update = 0;
104 return 1;
106 } else if (progress_update) {
107 fprintf(stderr, "%s: %u%s%s", progress->title, n, tp, eol);
108 fflush(stderr);
109 progress_update = 0;
110 return 1;
113 return 0;
116 static void throughput_string(struct strbuf *buf, off_t total,
117 unsigned int rate)
119 strbuf_addstr(buf, ", ");
120 strbuf_humanise_bytes(buf, total);
121 strbuf_addstr(buf, " | ");
122 strbuf_humanise_bytes(buf, rate * 1024);
123 strbuf_addstr(buf, "/s");
126 void display_throughput(struct progress *progress, off_t total)
128 struct throughput *tp;
129 struct timeval tv;
130 unsigned int misecs;
132 if (!progress)
133 return;
134 tp = progress->throughput;
136 gettimeofday(&tv, NULL);
138 if (!tp) {
139 progress->throughput = tp = calloc(1, sizeof(*tp));
140 if (tp) {
141 tp->prev_total = tp->curr_total = total;
142 tp->prev_tv = tv;
144 return;
146 tp->curr_total = total;
149 * We have x = bytes and y = microsecs. We want z = KiB/s:
151 * z = (x / 1024) / (y / 1000000)
152 * z = x / y * 1000000 / 1024
153 * z = x / (y * 1024 / 1000000)
154 * z = x / y'
156 * To simplify things we'll keep track of misecs, or 1024th of a sec
157 * obtained with:
159 * y' = y * 1024 / 1000000
160 * y' = y / (1000000 / 1024)
161 * y' = y / 977
163 misecs = (tv.tv_sec - tp->prev_tv.tv_sec) * 1024;
164 misecs += (int)(tv.tv_usec - tp->prev_tv.tv_usec) / 977;
166 if (misecs > 512) {
167 struct strbuf buf = STRBUF_INIT;
168 unsigned int count, rate;
170 count = total - tp->prev_total;
171 tp->prev_total = total;
172 tp->prev_tv = tv;
173 tp->avg_bytes += count;
174 tp->avg_misecs += misecs;
175 rate = tp->avg_bytes / tp->avg_misecs;
176 tp->avg_bytes -= tp->last_bytes[tp->idx];
177 tp->avg_misecs -= tp->last_misecs[tp->idx];
178 tp->last_bytes[tp->idx] = count;
179 tp->last_misecs[tp->idx] = misecs;
180 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
182 throughput_string(&buf, total, rate);
183 strncpy(tp->display, buf.buf, sizeof(tp->display));
184 strbuf_release(&buf);
185 if (progress->last_value != -1 && progress_update)
186 display(progress, progress->last_value, NULL);
190 int display_progress(struct progress *progress, unsigned n)
192 return progress ? display(progress, n, NULL) : 0;
195 struct progress *start_progress_delay(const char *title, unsigned total,
196 unsigned percent_treshold, unsigned delay)
198 struct progress *progress = malloc(sizeof(*progress));
199 if (!progress) {
200 /* unlikely, but here's a good fallback */
201 fprintf(stderr, "%s...\n", title);
202 fflush(stderr);
203 return NULL;
205 progress->title = title;
206 progress->total = total;
207 progress->last_value = -1;
208 progress->last_percent = -1;
209 progress->delayed_percent_treshold = percent_treshold;
210 progress->delay = delay;
211 progress->throughput = NULL;
212 set_progress_signal();
213 return progress;
216 struct progress *start_progress(const char *title, unsigned total)
218 return start_progress_delay(title, total, 0, 0);
221 void stop_progress(struct progress **p_progress)
223 stop_progress_msg(p_progress, "done");
226 void stop_progress_msg(struct progress **p_progress, const char *msg)
228 struct progress *progress = *p_progress;
229 if (!progress)
230 return;
231 *p_progress = NULL;
232 if (progress->last_value != -1) {
233 /* Force the last update */
234 char buf[128], *bufp;
235 size_t len = strlen(msg) + 5;
236 struct throughput *tp = progress->throughput;
238 bufp = (len < sizeof(buf)) ? buf : xmalloc(len + 1);
239 if (tp) {
240 struct strbuf strbuf = STRBUF_INIT;
241 unsigned int rate = !tp->avg_misecs ? 0 :
242 tp->avg_bytes / tp->avg_misecs;
243 throughput_string(&strbuf, tp->curr_total, rate);
244 strncpy(tp->display, strbuf.buf, sizeof(tp->display));
245 strbuf_release(&strbuf);
247 progress_update = 1;
248 sprintf(bufp, ", %s.\n", msg);
249 display(progress, progress->last_value, bufp);
250 if (buf != bufp)
251 free(bufp);
253 clear_progress_signal();
254 free(progress->throughput);
255 free(progress);