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.
11 #include "git-compat-util.h"
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
];
33 unsigned last_percent
;
35 unsigned delayed_percent_treshold
;
36 struct throughput
*throughput
;
39 static volatile sig_atomic_t progress_update
;
41 static void progress_interval(int signum
)
46 static void set_progress_signal(void)
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
);
73 static int display(struct progress
*progress
, unsigned n
, const char *done
)
77 if (progress
->delay
) {
78 if (!progress_update
|| --progress
->delay
)
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();
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
);
106 } else if (progress_update
) {
107 fprintf(stderr
, "%s: %u%s%s", progress
->title
, n
, tp
, eol
);
116 static void throughput_string(struct strbuf
*buf
, off_t total
,
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
;
134 tp
= progress
->throughput
;
136 gettimeofday(&tv
, NULL
);
139 progress
->throughput
= tp
= calloc(1, sizeof(*tp
));
141 tp
->prev_total
= tp
->curr_total
= total
;
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)
156 * To simplify things we'll keep track of misecs, or 1024th of a sec
159 * y' = y * 1024 / 1000000
160 * y' = y / (1000000 / 1024)
163 misecs
= (tv
.tv_sec
- tp
->prev_tv
.tv_sec
) * 1024;
164 misecs
+= (int)(tv
.tv_usec
- tp
->prev_tv
.tv_usec
) / 977;
167 struct strbuf buf
= STRBUF_INIT
;
168 unsigned int count
, rate
;
170 count
= total
- tp
->prev_total
;
171 tp
->prev_total
= total
;
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
));
200 /* unlikely, but here's a good fallback */
201 fprintf(stderr
, "%s...\n", title
);
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();
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
;
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);
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
);
248 sprintf(bufp
, ", %s.\n", msg
);
249 display(progress
, progress
->last_value
, bufp
);
253 clear_progress_signal();
254 free(progress
->throughput
);