]>
Commit | Line | Data |
---|---|---|
74b6792f NP |
1 | /* |
2 | * Simple text-based progress display module for GIT | |
3 | * | |
4 | * Copyright (c) 2007 by Nicolas Pitre <nico@cam.org> | |
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 | ||
16 | struct throughput { | |
218558af | 17 | off_t prev_total; |
cf84d51c | 18 | struct timeval prev_tv; |
218558af NP |
19 | unsigned int avg_bytes; |
20 | unsigned int last_bytes[TP_IDX_MAX]; | |
cf84d51c NP |
21 | unsigned int avg_misecs; |
22 | unsigned int last_misecs[TP_IDX_MAX]; | |
23 | unsigned int idx; | |
81f6654a | 24 | char display[32]; |
cf84d51c NP |
25 | }; |
26 | ||
dc6a0757 NP |
27 | struct progress { |
28 | const char *title; | |
29 | int last_value; | |
30 | unsigned total; | |
31 | unsigned last_percent; | |
32 | unsigned delay; | |
33 | unsigned delayed_percent_treshold; | |
cf84d51c | 34 | struct throughput *throughput; |
dc6a0757 NP |
35 | }; |
36 | ||
96a02f8f NP |
37 | static volatile sig_atomic_t progress_update; |
38 | ||
39 | static void progress_interval(int signum) | |
40 | { | |
41 | progress_update = 1; | |
42 | } | |
43 | ||
44 | static void set_progress_signal(void) | |
45 | { | |
46 | struct sigaction sa; | |
47 | struct itimerval v; | |
48 | ||
180a9f22 NP |
49 | progress_update = 0; |
50 | ||
96a02f8f NP |
51 | memset(&sa, 0, sizeof(sa)); |
52 | sa.sa_handler = progress_interval; | |
53 | sigemptyset(&sa.sa_mask); | |
54 | sa.sa_flags = SA_RESTART; | |
55 | sigaction(SIGALRM, &sa, NULL); | |
56 | ||
57 | v.it_interval.tv_sec = 1; | |
58 | v.it_interval.tv_usec = 0; | |
59 | v.it_value = v.it_interval; | |
60 | setitimer(ITIMER_REAL, &v, NULL); | |
61 | } | |
62 | ||
63 | static void clear_progress_signal(void) | |
64 | { | |
65 | struct itimerval v = {{0,},}; | |
66 | setitimer(ITIMER_REAL, &v, NULL); | |
67 | signal(SIGALRM, SIG_IGN); | |
68 | progress_update = 0; | |
69 | } | |
70 | ||
42e18fbf | 71 | static int display(struct progress *progress, unsigned n, int done) |
96a02f8f | 72 | { |
cf84d51c | 73 | char *eol, *tp; |
42e18fbf | 74 | |
180a9f22 | 75 | if (progress->delay) { |
180a9f22 NP |
76 | if (!progress_update || --progress->delay) |
77 | return 0; | |
78 | if (progress->total) { | |
79 | unsigned percent = n * 100 / progress->total; | |
80 | if (percent > progress->delayed_percent_treshold) { | |
81 | /* inhibit this progress report entirely */ | |
82 | clear_progress_signal(); | |
83 | progress->delay = -1; | |
84 | progress->total = 0; | |
85 | return 0; | |
86 | } | |
87 | } | |
180a9f22 | 88 | } |
42e18fbf NP |
89 | |
90 | progress->last_value = n; | |
cf84d51c | 91 | tp = (progress->throughput) ? progress->throughput->display : ""; |
42e18fbf | 92 | eol = done ? ", done. \n" : " \r"; |
96a02f8f NP |
93 | if (progress->total) { |
94 | unsigned percent = n * 100 / progress->total; | |
95 | if (percent != progress->last_percent || progress_update) { | |
96 | progress->last_percent = percent; | |
cf84d51c NP |
97 | fprintf(stderr, "%s: %3u%% (%u/%u)%s%s", |
98 | progress->title, percent, n, | |
99 | progress->total, tp, eol); | |
96a02f8f NP |
100 | progress_update = 0; |
101 | return 1; | |
102 | } | |
103 | } else if (progress_update) { | |
cf84d51c | 104 | fprintf(stderr, "%s: %u%s%s", progress->title, n, tp, eol); |
96a02f8f NP |
105 | progress_update = 0; |
106 | return 1; | |
107 | } | |
42e18fbf | 108 | |
96a02f8f NP |
109 | return 0; |
110 | } | |
111 | ||
218558af | 112 | void display_throughput(struct progress *progress, off_t total) |
cf84d51c NP |
113 | { |
114 | struct throughput *tp; | |
115 | struct timeval tv; | |
116 | unsigned int misecs; | |
117 | ||
118 | if (!progress) | |
119 | return; | |
120 | tp = progress->throughput; | |
121 | ||
122 | gettimeofday(&tv, NULL); | |
123 | ||
124 | if (!tp) { | |
125 | progress->throughput = tp = calloc(1, sizeof(*tp)); | |
218558af NP |
126 | if (tp) { |
127 | tp->prev_total = total; | |
cf84d51c | 128 | tp->prev_tv = tv; |
218558af | 129 | } |
cf84d51c NP |
130 | return; |
131 | } | |
132 | ||
cf84d51c NP |
133 | /* |
134 | * We have x = bytes and y = microsecs. We want z = KiB/s: | |
135 | * | |
136 | * z = (x / 1024) / (y / 1000000) | |
137 | * z = x / y * 1000000 / 1024 | |
138 | * z = x / (y * 1024 / 1000000) | |
139 | * z = x / y' | |
140 | * | |
141 | * To simplify things we'll keep track of misecs, or 1024th of a sec | |
142 | * obtained with: | |
143 | * | |
144 | * y' = y * 1024 / 1000000 | |
145 | * y' = y / (1000000 / 1024) | |
146 | * y' = y / 977 | |
147 | */ | |
148 | misecs = (tv.tv_sec - tp->prev_tv.tv_sec) * 1024; | |
149 | misecs += (int)(tv.tv_usec - tp->prev_tv.tv_usec) / 977; | |
150 | ||
151 | if (misecs > 512) { | |
81f6654a | 152 | int l = sizeof(tp->display); |
218558af NP |
153 | unsigned int count = total - tp->prev_total; |
154 | tp->prev_total = total; | |
cf84d51c | 155 | tp->prev_tv = tv; |
218558af | 156 | tp->avg_bytes += count; |
cf84d51c | 157 | tp->avg_misecs += misecs; |
81f6654a | 158 | |
218558af | 159 | if (total > 1 << 30) { |
81f6654a | 160 | l -= snprintf(tp->display, l, ", %u.%2.2u GiB", |
218558af NP |
161 | (int)(total >> 30), |
162 | (int)(total & ((1 << 30) - 1)) / 10737419); | |
163 | } else if (total > 1 << 20) { | |
81f6654a | 164 | l -= snprintf(tp->display, l, ", %u.%2.2u MiB", |
218558af NP |
165 | (int)(total >> 20), |
166 | ((int)(total & ((1 << 20) - 1)) | |
81f6654a | 167 | * 100) >> 20); |
218558af | 168 | } else if (total > 1 << 10) { |
81f6654a | 169 | l -= snprintf(tp->display, l, ", %u.%2.2u KiB", |
218558af NP |
170 | (int)(total >> 10), |
171 | ((int)(total & ((1 << 10) - 1)) | |
81f6654a NP |
172 | * 100) >> 10); |
173 | } else { | |
218558af | 174 | l -= snprintf(tp->display, l, ", %u bytes", (int)total); |
81f6654a NP |
175 | } |
176 | snprintf(tp->display + sizeof(tp->display) - l, l, | |
218558af | 177 | " | %u KiB/s", tp->avg_bytes / tp->avg_misecs); |
81f6654a | 178 | |
cf84d51c NP |
179 | tp->avg_bytes -= tp->last_bytes[tp->idx]; |
180 | tp->avg_misecs -= tp->last_misecs[tp->idx]; | |
218558af | 181 | tp->last_bytes[tp->idx] = count; |
cf84d51c NP |
182 | tp->last_misecs[tp->idx] = misecs; |
183 | tp->idx = (tp->idx + 1) % TP_IDX_MAX; | |
3e935d19 NP |
184 | |
185 | if (progress->last_value != -1 && progress_update) | |
186 | display(progress, progress->last_value, 0); | |
cf84d51c NP |
187 | } |
188 | } | |
189 | ||
42e18fbf | 190 | int display_progress(struct progress *progress, unsigned n) |
96a02f8f | 191 | { |
dc6a0757 | 192 | return progress ? display(progress, n, 0) : 0; |
96a02f8f NP |
193 | } |
194 | ||
dc6a0757 NP |
195 | struct progress *start_progress_delay(const char *title, unsigned total, |
196 | unsigned percent_treshold, unsigned delay) | |
180a9f22 | 197 | { |
dc6a0757 NP |
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 | return NULL; | |
203 | } | |
42e18fbf | 204 | progress->title = title; |
180a9f22 | 205 | progress->total = total; |
42e18fbf | 206 | progress->last_value = -1; |
180a9f22 NP |
207 | progress->last_percent = -1; |
208 | progress->delayed_percent_treshold = percent_treshold; | |
180a9f22 | 209 | progress->delay = delay; |
cf84d51c | 210 | progress->throughput = NULL; |
180a9f22 | 211 | set_progress_signal(); |
dc6a0757 | 212 | return progress; |
180a9f22 NP |
213 | } |
214 | ||
dc6a0757 | 215 | struct progress *start_progress(const char *title, unsigned total) |
42e18fbf | 216 | { |
dc6a0757 | 217 | return start_progress_delay(title, total, 0, 0); |
42e18fbf NP |
218 | } |
219 | ||
dc6a0757 | 220 | void stop_progress(struct progress **p_progress) |
96a02f8f | 221 | { |
dc6a0757 NP |
222 | struct progress *progress = *p_progress; |
223 | if (!progress) | |
224 | return; | |
225 | *p_progress = NULL; | |
42e18fbf NP |
226 | if (progress->last_value != -1) { |
227 | /* Force the last update */ | |
228 | progress_update = 1; | |
229 | display(progress, progress->last_value, 1); | |
230 | } | |
96a02f8f | 231 | clear_progress_signal(); |
cf84d51c | 232 | free(progress->throughput); |
dc6a0757 | 233 | free(progress); |
96a02f8f | 234 | } |