Nicolas Pitre | 74b6792 | 2007-10-30 15:41:13 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Simple text-based progress display module for GIT |
| 3 | * |
Nicolas Pitre | 03aa8ff | 2009-09-14 02:41:16 -0400 | [diff] [blame] | 4 | * Copyright (c) 2007 by Nicolas Pitre <nico@fluxnic.net> |
Nicolas Pitre | 74b6792 | 2007-10-30 15:41:13 -0400 | [diff] [blame] | 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 | |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 11 | #include "git-compat-util.h" |
Nguyễn Thái Ngọc Duy | 754dbc4 | 2014-02-21 19:50:18 +0700 | [diff] [blame] | 12 | #include "gettext.h" |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 13 | #include "progress.h" |
Antoine Pelisse | 079b546 | 2013-04-10 21:03:23 +0200 | [diff] [blame] | 14 | #include "strbuf.h" |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 15 | |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 16 | #define TP_IDX_MAX 8 |
| 17 | |
| 18 | struct throughput { |
Nicolas Pitre | 53ed7b5 | 2007-11-06 16:30:28 -0500 | [diff] [blame] | 19 | off_t curr_total; |
Nicolas Pitre | 218558a | 2007-11-04 22:15:41 -0500 | [diff] [blame] | 20 | off_t prev_total; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 21 | struct timeval prev_tv; |
Nicolas Pitre | 218558a | 2007-11-04 22:15:41 -0500 | [diff] [blame] | 22 | unsigned int avg_bytes; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 23 | unsigned int avg_misecs; |
Nicolas Pitre | 53ed7b5 | 2007-11-06 16:30:28 -0500 | [diff] [blame] | 24 | unsigned int last_bytes[TP_IDX_MAX]; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 25 | unsigned int last_misecs[TP_IDX_MAX]; |
| 26 | unsigned int idx; |
Nicolas Pitre | 81f6654 | 2007-11-01 16:59:57 -0400 | [diff] [blame] | 27 | char display[32]; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 28 | }; |
| 29 | |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 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; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 37 | struct throughput *throughput; |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 38 | }; |
| 39 | |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 40 | static volatile sig_atomic_t progress_update; |
| 41 | |
| 42 | static void progress_interval(int signum) |
| 43 | { |
| 44 | progress_update = 1; |
| 45 | } |
| 46 | |
| 47 | static void set_progress_signal(void) |
| 48 | { |
| 49 | struct sigaction sa; |
| 50 | struct itimerval v; |
| 51 | |
Nicolas Pitre | 180a9f2 | 2007-04-20 15:05:27 -0400 | [diff] [blame] | 52 | progress_update = 0; |
| 53 | |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 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); |
| 59 | |
| 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); |
| 64 | } |
| 65 | |
| 66 | static void clear_progress_signal(void) |
| 67 | { |
| 68 | struct itimerval v = {{0,},}; |
| 69 | setitimer(ITIMER_REAL, &v, NULL); |
| 70 | signal(SIGALRM, SIG_IGN); |
| 71 | progress_update = 0; |
| 72 | } |
| 73 | |
Nicolas Pitre | a984a06 | 2007-11-08 15:45:41 -0500 | [diff] [blame] | 74 | static int display(struct progress *progress, unsigned n, const char *done) |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 75 | { |
Nicolas Pitre | a984a06 | 2007-11-08 15:45:41 -0500 | [diff] [blame] | 76 | const char *eol, *tp; |
Nicolas Pitre | 42e18fb | 2007-10-16 21:55:45 -0400 | [diff] [blame] | 77 | |
Nicolas Pitre | 180a9f2 | 2007-04-20 15:05:27 -0400 | [diff] [blame] | 78 | if (progress->delay) { |
Nicolas Pitre | 180a9f2 | 2007-04-20 15:05:27 -0400 | [diff] [blame] | 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; |
| 89 | } |
| 90 | } |
Nicolas Pitre | 180a9f2 | 2007-04-20 15:05:27 -0400 | [diff] [blame] | 91 | } |
Nicolas Pitre | 42e18fb | 2007-10-16 21:55:45 -0400 | [diff] [blame] | 92 | |
| 93 | progress->last_value = n; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 94 | tp = (progress->throughput) ? progress->throughput->display : ""; |
Nicolas Pitre | a984a06 | 2007-11-08 15:45:41 -0500 | [diff] [blame] | 95 | eol = done ? done : " \r"; |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 96 | if (progress->total) { |
| 97 | unsigned percent = n * 100 / progress->total; |
| 98 | if (percent != progress->last_percent || progress_update) { |
| 99 | progress->last_percent = percent; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 100 | fprintf(stderr, "%s: %3u%% (%u/%u)%s%s", |
| 101 | progress->title, percent, n, |
| 102 | progress->total, tp, eol); |
Johannes Sixt | 137a0d0 | 2007-11-19 20:48:58 +0100 | [diff] [blame] | 103 | fflush(stderr); |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 104 | progress_update = 0; |
| 105 | return 1; |
| 106 | } |
| 107 | } else if (progress_update) { |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 108 | fprintf(stderr, "%s: %u%s%s", progress->title, n, tp, eol); |
Johannes Sixt | 137a0d0 | 2007-11-19 20:48:58 +0100 | [diff] [blame] | 109 | fflush(stderr); |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 110 | progress_update = 0; |
| 111 | return 1; |
| 112 | } |
Nicolas Pitre | 42e18fb | 2007-10-16 21:55:45 -0400 | [diff] [blame] | 113 | |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
Antoine Pelisse | 079b546 | 2013-04-10 21:03:23 +0200 | [diff] [blame] | 117 | static void throughput_string(struct strbuf *buf, off_t total, |
Nicolas Pitre | 53ed7b5 | 2007-11-06 16:30:28 -0500 | [diff] [blame] | 118 | unsigned int rate) |
| 119 | { |
Antoine Pelisse | 079b546 | 2013-04-10 21:03:23 +0200 | [diff] [blame] | 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"); |
Nicolas Pitre | 53ed7b5 | 2007-11-06 16:30:28 -0500 | [diff] [blame] | 125 | } |
| 126 | |
Nicolas Pitre | 218558a | 2007-11-04 22:15:41 -0500 | [diff] [blame] | 127 | void display_throughput(struct progress *progress, off_t total) |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 128 | { |
| 129 | struct throughput *tp; |
| 130 | struct timeval tv; |
| 131 | unsigned int misecs; |
| 132 | |
| 133 | if (!progress) |
| 134 | return; |
| 135 | tp = progress->throughput; |
| 136 | |
| 137 | gettimeofday(&tv, NULL); |
| 138 | |
| 139 | if (!tp) { |
| 140 | progress->throughput = tp = calloc(1, sizeof(*tp)); |
Nicolas Pitre | 218558a | 2007-11-04 22:15:41 -0500 | [diff] [blame] | 141 | if (tp) { |
Nicolas Pitre | 53ed7b5 | 2007-11-06 16:30:28 -0500 | [diff] [blame] | 142 | tp->prev_total = tp->curr_total = total; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 143 | tp->prev_tv = tv; |
Nicolas Pitre | 218558a | 2007-11-04 22:15:41 -0500 | [diff] [blame] | 144 | } |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 145 | return; |
| 146 | } |
Nicolas Pitre | 53ed7b5 | 2007-11-06 16:30:28 -0500 | [diff] [blame] | 147 | tp->curr_total = total; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 148 | |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 149 | /* |
| 150 | * We have x = bytes and y = microsecs. We want z = KiB/s: |
| 151 | * |
| 152 | * z = (x / 1024) / (y / 1000000) |
| 153 | * z = x / y * 1000000 / 1024 |
| 154 | * z = x / (y * 1024 / 1000000) |
| 155 | * z = x / y' |
| 156 | * |
| 157 | * To simplify things we'll keep track of misecs, or 1024th of a sec |
| 158 | * obtained with: |
| 159 | * |
| 160 | * y' = y * 1024 / 1000000 |
| 161 | * y' = y / (1000000 / 1024) |
| 162 | * y' = y / 977 |
| 163 | */ |
| 164 | misecs = (tv.tv_sec - tp->prev_tv.tv_sec) * 1024; |
| 165 | misecs += (int)(tv.tv_usec - tp->prev_tv.tv_usec) / 977; |
| 166 | |
| 167 | if (misecs > 512) { |
Antoine Pelisse | 079b546 | 2013-04-10 21:03:23 +0200 | [diff] [blame] | 168 | struct strbuf buf = STRBUF_INIT; |
Nicolas Pitre | 53ed7b5 | 2007-11-06 16:30:28 -0500 | [diff] [blame] | 169 | unsigned int count, rate; |
| 170 | |
| 171 | count = total - tp->prev_total; |
Nicolas Pitre | 218558a | 2007-11-04 22:15:41 -0500 | [diff] [blame] | 172 | tp->prev_total = total; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 173 | tp->prev_tv = tv; |
Nicolas Pitre | 218558a | 2007-11-04 22:15:41 -0500 | [diff] [blame] | 174 | tp->avg_bytes += count; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 175 | tp->avg_misecs += misecs; |
Nicolas Pitre | 53ed7b5 | 2007-11-06 16:30:28 -0500 | [diff] [blame] | 176 | rate = tp->avg_bytes / tp->avg_misecs; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 177 | tp->avg_bytes -= tp->last_bytes[tp->idx]; |
| 178 | tp->avg_misecs -= tp->last_misecs[tp->idx]; |
Nicolas Pitre | 218558a | 2007-11-04 22:15:41 -0500 | [diff] [blame] | 179 | tp->last_bytes[tp->idx] = count; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 180 | tp->last_misecs[tp->idx] = misecs; |
| 181 | tp->idx = (tp->idx + 1) % TP_IDX_MAX; |
Nicolas Pitre | 3e935d1 | 2007-11-01 16:59:56 -0400 | [diff] [blame] | 182 | |
Antoine Pelisse | 079b546 | 2013-04-10 21:03:23 +0200 | [diff] [blame] | 183 | throughput_string(&buf, total, rate); |
| 184 | strncpy(tp->display, buf.buf, sizeof(tp->display)); |
| 185 | strbuf_release(&buf); |
Nicolas Pitre | 3e935d1 | 2007-11-01 16:59:56 -0400 | [diff] [blame] | 186 | if (progress->last_value != -1 && progress_update) |
Nicolas Pitre | a984a06 | 2007-11-08 15:45:41 -0500 | [diff] [blame] | 187 | display(progress, progress->last_value, NULL); |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Nicolas Pitre | 42e18fb | 2007-10-16 21:55:45 -0400 | [diff] [blame] | 191 | int display_progress(struct progress *progress, unsigned n) |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 192 | { |
Nicolas Pitre | a984a06 | 2007-11-08 15:45:41 -0500 | [diff] [blame] | 193 | return progress ? display(progress, n, NULL) : 0; |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 194 | } |
| 195 | |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 196 | struct progress *start_progress_delay(const char *title, unsigned total, |
| 197 | unsigned percent_treshold, unsigned delay) |
Nicolas Pitre | 180a9f2 | 2007-04-20 15:05:27 -0400 | [diff] [blame] | 198 | { |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 199 | struct progress *progress = malloc(sizeof(*progress)); |
| 200 | if (!progress) { |
| 201 | /* unlikely, but here's a good fallback */ |
| 202 | fprintf(stderr, "%s...\n", title); |
Johannes Sixt | 137a0d0 | 2007-11-19 20:48:58 +0100 | [diff] [blame] | 203 | fflush(stderr); |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 204 | return NULL; |
| 205 | } |
Nicolas Pitre | 42e18fb | 2007-10-16 21:55:45 -0400 | [diff] [blame] | 206 | progress->title = title; |
Nicolas Pitre | 180a9f2 | 2007-04-20 15:05:27 -0400 | [diff] [blame] | 207 | progress->total = total; |
Nicolas Pitre | 42e18fb | 2007-10-16 21:55:45 -0400 | [diff] [blame] | 208 | progress->last_value = -1; |
Nicolas Pitre | 180a9f2 | 2007-04-20 15:05:27 -0400 | [diff] [blame] | 209 | progress->last_percent = -1; |
| 210 | progress->delayed_percent_treshold = percent_treshold; |
Nicolas Pitre | 180a9f2 | 2007-04-20 15:05:27 -0400 | [diff] [blame] | 211 | progress->delay = delay; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 212 | progress->throughput = NULL; |
Nicolas Pitre | 180a9f2 | 2007-04-20 15:05:27 -0400 | [diff] [blame] | 213 | set_progress_signal(); |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 214 | return progress; |
Nicolas Pitre | 180a9f2 | 2007-04-20 15:05:27 -0400 | [diff] [blame] | 215 | } |
| 216 | |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 217 | struct progress *start_progress(const char *title, unsigned total) |
Nicolas Pitre | 42e18fb | 2007-10-16 21:55:45 -0400 | [diff] [blame] | 218 | { |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 219 | return start_progress_delay(title, total, 0, 0); |
Nicolas Pitre | 42e18fb | 2007-10-16 21:55:45 -0400 | [diff] [blame] | 220 | } |
| 221 | |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 222 | void stop_progress(struct progress **p_progress) |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 223 | { |
Nguyễn Thái Ngọc Duy | 754dbc4 | 2014-02-21 19:50:18 +0700 | [diff] [blame] | 224 | stop_progress_msg(p_progress, _("done")); |
Nicolas Pitre | a984a06 | 2007-11-08 15:45:41 -0500 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | void stop_progress_msg(struct progress **p_progress, const char *msg) |
| 228 | { |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 229 | struct progress *progress = *p_progress; |
| 230 | if (!progress) |
| 231 | return; |
| 232 | *p_progress = NULL; |
Nicolas Pitre | 42e18fb | 2007-10-16 21:55:45 -0400 | [diff] [blame] | 233 | if (progress->last_value != -1) { |
| 234 | /* Force the last update */ |
Boyd Lynn Gerber | d4c4444 | 2008-06-08 09:26:15 -0600 | [diff] [blame] | 235 | char buf[128], *bufp; |
| 236 | size_t len = strlen(msg) + 5; |
Nicolas Pitre | 53ed7b5 | 2007-11-06 16:30:28 -0500 | [diff] [blame] | 237 | struct throughput *tp = progress->throughput; |
Boyd Lynn Gerber | d4c4444 | 2008-06-08 09:26:15 -0600 | [diff] [blame] | 238 | |
| 239 | bufp = (len < sizeof(buf)) ? buf : xmalloc(len + 1); |
Nicolas Pitre | 53ed7b5 | 2007-11-06 16:30:28 -0500 | [diff] [blame] | 240 | if (tp) { |
Antoine Pelisse | 079b546 | 2013-04-10 21:03:23 +0200 | [diff] [blame] | 241 | struct strbuf strbuf = STRBUF_INIT; |
Nicolas Pitre | 53ed7b5 | 2007-11-06 16:30:28 -0500 | [diff] [blame] | 242 | unsigned int rate = !tp->avg_misecs ? 0 : |
| 243 | tp->avg_bytes / tp->avg_misecs; |
Antoine Pelisse | 079b546 | 2013-04-10 21:03:23 +0200 | [diff] [blame] | 244 | throughput_string(&strbuf, tp->curr_total, rate); |
| 245 | strncpy(tp->display, strbuf.buf, sizeof(tp->display)); |
| 246 | strbuf_release(&strbuf); |
Nicolas Pitre | 53ed7b5 | 2007-11-06 16:30:28 -0500 | [diff] [blame] | 247 | } |
Nicolas Pitre | 42e18fb | 2007-10-16 21:55:45 -0400 | [diff] [blame] | 248 | progress_update = 1; |
Boyd Lynn Gerber | d4c4444 | 2008-06-08 09:26:15 -0600 | [diff] [blame] | 249 | sprintf(bufp, ", %s.\n", msg); |
| 250 | display(progress, progress->last_value, bufp); |
| 251 | if (buf != bufp) |
| 252 | free(bufp); |
Nicolas Pitre | 42e18fb | 2007-10-16 21:55:45 -0400 | [diff] [blame] | 253 | } |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 254 | clear_progress_signal(); |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 255 | free(progress->throughput); |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 256 | free(progress); |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 257 | } |