blob: 3971f49f4ddaa774806bd1717379a1edd22ba17c [file] [log] [blame]
Nicolas Pitre74b67922007-10-30 15:41:13 -04001/*
2 * Simple text-based progress display module for GIT
3 *
Nicolas Pitre03aa8ff2009-09-14 02:41:16 -04004 * Copyright (c) 2007 by Nicolas Pitre <nico@fluxnic.net>
Nicolas Pitre74b67922007-10-30 15:41:13 -04005 *
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
Nicolas Pitre96a02f82007-04-18 14:27:45 -040011#include "git-compat-util.h"
12#include "progress.h"
13
Nicolas Pitrecf84d512007-10-30 14:57:34 -040014#define TP_IDX_MAX 8
15
16struct throughput {
Nicolas Pitre53ed7b52007-11-06 16:30:28 -050017 off_t curr_total;
Nicolas Pitre218558a2007-11-04 22:15:41 -050018 off_t prev_total;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040019 struct timeval prev_tv;
Nicolas Pitre218558a2007-11-04 22:15:41 -050020 unsigned int avg_bytes;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040021 unsigned int avg_misecs;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -050022 unsigned int last_bytes[TP_IDX_MAX];
Nicolas Pitrecf84d512007-10-30 14:57:34 -040023 unsigned int last_misecs[TP_IDX_MAX];
24 unsigned int idx;
Nicolas Pitre81f66542007-11-01 16:59:57 -040025 char display[32];
Nicolas Pitrecf84d512007-10-30 14:57:34 -040026};
27
Nicolas Pitredc6a0752007-10-30 14:57:32 -040028struct progress {
29 const char *title;
30 int last_value;
31 unsigned total;
32 unsigned last_percent;
33 unsigned delay;
34 unsigned delayed_percent_treshold;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040035 struct throughput *throughput;
Nicolas Pitredc6a0752007-10-30 14:57:32 -040036};
37
Nicolas Pitre96a02f82007-04-18 14:27:45 -040038static 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
Nicolas Pitre180a9f22007-04-20 15:05:27 -040050 progress_update = 0;
51
Nicolas Pitre96a02f82007-04-18 14:27:45 -040052 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
Nicolas Pitrea984a062007-11-08 15:45:41 -050072static int display(struct progress *progress, unsigned n, const char *done)
Nicolas Pitre96a02f82007-04-18 14:27:45 -040073{
Nicolas Pitrea984a062007-11-08 15:45:41 -050074 const char *eol, *tp;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -040075
Nicolas Pitre180a9f22007-04-20 15:05:27 -040076 if (progress->delay) {
Nicolas Pitre180a9f22007-04-20 15:05:27 -040077 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 }
Nicolas Pitre180a9f22007-04-20 15:05:27 -040089 }
Nicolas Pitre42e18fb2007-10-16 21:55:45 -040090
91 progress->last_value = n;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040092 tp = (progress->throughput) ? progress->throughput->display : "";
Nicolas Pitrea984a062007-11-08 15:45:41 -050093 eol = done ? done : " \r";
Nicolas Pitre96a02f82007-04-18 14:27:45 -040094 if (progress->total) {
95 unsigned percent = n * 100 / progress->total;
96 if (percent != progress->last_percent || progress_update) {
97 progress->last_percent = percent;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040098 fprintf(stderr, "%s: %3u%% (%u/%u)%s%s",
99 progress->title, percent, n,
100 progress->total, tp, eol);
Johannes Sixt137a0d02007-11-19 20:48:58 +0100101 fflush(stderr);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400102 progress_update = 0;
103 return 1;
104 }
105 } else if (progress_update) {
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400106 fprintf(stderr, "%s: %u%s%s", progress->title, n, tp, eol);
Johannes Sixt137a0d02007-11-19 20:48:58 +0100107 fflush(stderr);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400108 progress_update = 0;
109 return 1;
110 }
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400111
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400112 return 0;
113}
114
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500115static 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) {
Nicolas Pitre66913282009-04-24 17:46:15 -0400124 int x = total + 5243; /* for rounding */
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500125 l -= snprintf(tp->display, l, ", %u.%2.2u MiB",
Nicolas Pitre66913282009-04-24 17:46:15 -0400126 x >> 20, ((x & ((1 << 20) - 1)) * 100) >> 20);
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500127 } else if (total > 1 << 10) {
Nicolas Pitre66913282009-04-24 17:46:15 -0400128 int x = total + 5; /* for rounding */
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500129 l -= snprintf(tp->display, l, ", %u.%2.2u KiB",
Nicolas Pitre66913282009-04-24 17:46:15 -0400130 x >> 10, ((x & ((1 << 10) - 1)) * 100) >> 10);
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500131 } else {
132 l -= snprintf(tp->display, l, ", %u bytes", (int)total);
133 }
Nicolas Pitre583371a2009-10-13 23:02:04 -0400134
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)
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500141 snprintf(tp->display + sizeof(tp->display) - l, l,
142 " | %u KiB/s", rate);
143}
144
Nicolas Pitre218558a2007-11-04 22:15:41 -0500145void display_throughput(struct progress *progress, off_t total)
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400146{
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));
Nicolas Pitre218558a2007-11-04 22:15:41 -0500159 if (tp) {
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500160 tp->prev_total = tp->curr_total = total;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400161 tp->prev_tv = tv;
Nicolas Pitre218558a2007-11-04 22:15:41 -0500162 }
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400163 return;
164 }
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500165 tp->curr_total = total;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400166
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400167 /*
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) {
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500186 unsigned int count, rate;
187
188 count = total - tp->prev_total;
Nicolas Pitre218558a2007-11-04 22:15:41 -0500189 tp->prev_total = total;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400190 tp->prev_tv = tv;
Nicolas Pitre218558a2007-11-04 22:15:41 -0500191 tp->avg_bytes += count;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400192 tp->avg_misecs += misecs;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500193 rate = tp->avg_bytes / tp->avg_misecs;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400194 tp->avg_bytes -= tp->last_bytes[tp->idx];
195 tp->avg_misecs -= tp->last_misecs[tp->idx];
Nicolas Pitre218558a2007-11-04 22:15:41 -0500196 tp->last_bytes[tp->idx] = count;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400197 tp->last_misecs[tp->idx] = misecs;
198 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
Nicolas Pitre3e935d12007-11-01 16:59:56 -0400199
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500200 throughput_string(tp, total, rate);
Nicolas Pitre3e935d12007-11-01 16:59:56 -0400201 if (progress->last_value != -1 && progress_update)
Nicolas Pitrea984a062007-11-08 15:45:41 -0500202 display(progress, progress->last_value, NULL);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400203 }
204}
205
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400206int display_progress(struct progress *progress, unsigned n)
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400207{
Nicolas Pitrea984a062007-11-08 15:45:41 -0500208 return progress ? display(progress, n, NULL) : 0;
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400209}
210
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400211struct progress *start_progress_delay(const char *title, unsigned total,
212 unsigned percent_treshold, unsigned delay)
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400213{
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400214 struct progress *progress = malloc(sizeof(*progress));
215 if (!progress) {
216 /* unlikely, but here's a good fallback */
217 fprintf(stderr, "%s...\n", title);
Johannes Sixt137a0d02007-11-19 20:48:58 +0100218 fflush(stderr);
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400219 return NULL;
220 }
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400221 progress->title = title;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400222 progress->total = total;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400223 progress->last_value = -1;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400224 progress->last_percent = -1;
225 progress->delayed_percent_treshold = percent_treshold;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400226 progress->delay = delay;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400227 progress->throughput = NULL;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400228 set_progress_signal();
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400229 return progress;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400230}
231
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400232struct progress *start_progress(const char *title, unsigned total)
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400233{
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400234 return start_progress_delay(title, total, 0, 0);
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400235}
236
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400237void stop_progress(struct progress **p_progress)
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400238{
Nicolas Pitrea984a062007-11-08 15:45:41 -0500239 stop_progress_msg(p_progress, "done");
240}
241
242void stop_progress_msg(struct progress **p_progress, const char *msg)
243{
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400244 struct progress *progress = *p_progress;
245 if (!progress)
246 return;
247 *p_progress = NULL;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400248 if (progress->last_value != -1) {
249 /* Force the last update */
Boyd Lynn Gerberd4c44442008-06-08 09:26:15 -0600250 char buf[128], *bufp;
251 size_t len = strlen(msg) + 5;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500252 struct throughput *tp = progress->throughput;
Boyd Lynn Gerberd4c44442008-06-08 09:26:15 -0600253
254 bufp = (len < sizeof(buf)) ? buf : xmalloc(len + 1);
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500255 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 }
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400260 progress_update = 1;
Boyd Lynn Gerberd4c44442008-06-08 09:26:15 -0600261 sprintf(bufp, ", %s.\n", msg);
262 display(progress, progress->last_value, bufp);
263 if (buf != bufp)
264 free(bufp);
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400265 }
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400266 clear_progress_signal();
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400267 free(progress->throughput);
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400268 free(progress);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400269}