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 | |
Đoàn Trần Công Danh | 3cacb9a | 2020-04-27 21:22:37 +0700 | [diff] [blame] | 11 | #define GIT_TEST_PROGRESS_ONLY |
SZEDER Gábor | 545dc34 | 2019-04-12 21:45:15 +0200 | [diff] [blame] | 12 | #include "cache.h" |
Nguyễn Thái Ngọc Duy | 754dbc4 | 2014-02-21 19:50:18 +0700 | [diff] [blame] | 13 | #include "gettext.h" |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 14 | #include "progress.h" |
Antoine Pelisse | 079b546 | 2013-04-10 21:03:23 +0200 | [diff] [blame] | 15 | #include "strbuf.h" |
Karsten Blees | 83d26fa | 2014-07-12 02:08:11 +0200 | [diff] [blame] | 16 | #include "trace.h" |
SZEDER Gábor | 545dc34 | 2019-04-12 21:45:15 +0200 | [diff] [blame] | 17 | #include "utf8.h" |
Derrick Stolee | 44a4693 | 2019-11-25 21:28:22 +0000 | [diff] [blame] | 18 | #include "config.h" |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 19 | |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 20 | #define TP_IDX_MAX 8 |
| 21 | |
| 22 | struct throughput { |
Nicolas Pitre | 53ed7b5 | 2007-11-06 16:30:28 -0500 | [diff] [blame] | 23 | off_t curr_total; |
Nicolas Pitre | 218558a | 2007-11-04 22:15:41 -0500 | [diff] [blame] | 24 | off_t prev_total; |
Karsten Blees | 83d26fa | 2014-07-12 02:08:11 +0200 | [diff] [blame] | 25 | uint64_t prev_ns; |
Nicolas Pitre | 218558a | 2007-11-04 22:15:41 -0500 | [diff] [blame] | 26 | unsigned int avg_bytes; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 27 | unsigned int avg_misecs; |
Nicolas Pitre | 53ed7b5 | 2007-11-06 16:30:28 -0500 | [diff] [blame] | 28 | unsigned int last_bytes[TP_IDX_MAX]; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 29 | unsigned int last_misecs[TP_IDX_MAX]; |
| 30 | unsigned int idx; |
Jeff King | 3131977 | 2015-09-24 17:05:57 -0400 | [diff] [blame] | 31 | struct strbuf display; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 32 | }; |
| 33 | |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 34 | struct progress { |
| 35 | const char *title; |
Elijah Newren | d6861d0 | 2017-11-13 12:15:58 -0800 | [diff] [blame] | 36 | uint64_t last_value; |
| 37 | uint64_t total; |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 38 | unsigned last_percent; |
| 39 | unsigned delay; |
Jeff Hostetler | 9d81ecb | 2019-03-21 12:36:11 -0700 | [diff] [blame] | 40 | unsigned sparse; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 41 | struct throughput *throughput; |
René Scharfe | 0fae1e0 | 2017-07-08 18:43:42 +0200 | [diff] [blame] | 42 | uint64_t start_ns; |
SZEDER Gábor | d53ba84 | 2019-04-05 02:45:37 +0200 | [diff] [blame] | 43 | struct strbuf counters_sb; |
SZEDER Gábor | 545dc34 | 2019-04-12 21:45:15 +0200 | [diff] [blame] | 44 | int title_len; |
| 45 | int split; |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 46 | }; |
| 47 | |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 48 | static volatile sig_atomic_t progress_update; |
| 49 | |
SZEDER Gábor | 2bb74b5 | 2019-09-16 22:54:12 +0200 | [diff] [blame] | 50 | /* |
| 51 | * These are only intended for testing the progress output, i.e. exclusively |
| 52 | * for 'test-tool progress'. |
| 53 | */ |
| 54 | int progress_testing; |
| 55 | uint64_t progress_test_ns = 0; |
SZEDER Gábor | 2bb74b5 | 2019-09-16 22:54:12 +0200 | [diff] [blame] | 56 | void progress_test_force_update(void) |
| 57 | { |
| 58 | progress_update = 1; |
| 59 | } |
| 60 | |
| 61 | |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 62 | static void progress_interval(int signum) |
| 63 | { |
| 64 | progress_update = 1; |
| 65 | } |
| 66 | |
| 67 | static void set_progress_signal(void) |
| 68 | { |
| 69 | struct sigaction sa; |
| 70 | struct itimerval v; |
| 71 | |
SZEDER Gábor | 2bb74b5 | 2019-09-16 22:54:12 +0200 | [diff] [blame] | 72 | if (progress_testing) |
| 73 | return; |
| 74 | |
Nicolas Pitre | 180a9f2 | 2007-04-20 15:05:27 -0400 | [diff] [blame] | 75 | progress_update = 0; |
| 76 | |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 77 | memset(&sa, 0, sizeof(sa)); |
| 78 | sa.sa_handler = progress_interval; |
| 79 | sigemptyset(&sa.sa_mask); |
| 80 | sa.sa_flags = SA_RESTART; |
| 81 | sigaction(SIGALRM, &sa, NULL); |
| 82 | |
| 83 | v.it_interval.tv_sec = 1; |
| 84 | v.it_interval.tv_usec = 0; |
| 85 | v.it_value = v.it_interval; |
| 86 | setitimer(ITIMER_REAL, &v, NULL); |
| 87 | } |
| 88 | |
| 89 | static void clear_progress_signal(void) |
| 90 | { |
| 91 | struct itimerval v = {{0,},}; |
SZEDER Gábor | 2bb74b5 | 2019-09-16 22:54:12 +0200 | [diff] [blame] | 92 | |
| 93 | if (progress_testing) |
| 94 | return; |
| 95 | |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 96 | setitimer(ITIMER_REAL, &v, NULL); |
| 97 | signal(SIGALRM, SIG_IGN); |
| 98 | progress_update = 0; |
| 99 | } |
| 100 | |
Luke Mewburn | 85cb890 | 2015-04-13 23:30:51 +1000 | [diff] [blame] | 101 | static int is_foreground_fd(int fd) |
| 102 | { |
Jeff King | a4fb76c | 2015-05-19 01:24:57 -0400 | [diff] [blame] | 103 | int tpgrp = tcgetpgrp(fd); |
| 104 | return tpgrp < 0 || tpgrp == getpgid(0); |
Luke Mewburn | 85cb890 | 2015-04-13 23:30:51 +1000 | [diff] [blame] | 105 | } |
| 106 | |
SZEDER Gábor | 9219d12 | 2019-04-05 02:45:36 +0200 | [diff] [blame] | 107 | static void display(struct progress *progress, uint64_t n, const char *done) |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 108 | { |
SZEDER Gábor | d53ba84 | 2019-04-05 02:45:37 +0200 | [diff] [blame] | 109 | const char *tp; |
| 110 | struct strbuf *counters_sb = &progress->counters_sb; |
| 111 | int show_update = 0; |
SZEDER Gábor | bbf4756 | 2019-09-16 22:54:11 +0200 | [diff] [blame] | 112 | int last_count_len = counters_sb->len; |
Nicolas Pitre | 42e18fb | 2007-10-16 21:55:45 -0400 | [diff] [blame] | 113 | |
Lars Schneider | 9c5951c | 2017-12-04 17:07:00 -0500 | [diff] [blame] | 114 | if (progress->delay && (!progress_update || --progress->delay)) |
SZEDER Gábor | 9219d12 | 2019-04-05 02:45:36 +0200 | [diff] [blame] | 115 | return; |
Nicolas Pitre | 42e18fb | 2007-10-16 21:55:45 -0400 | [diff] [blame] | 116 | |
| 117 | progress->last_value = n; |
Jeff King | 3131977 | 2015-09-24 17:05:57 -0400 | [diff] [blame] | 118 | tp = (progress->throughput) ? progress->throughput->display.buf : ""; |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 119 | if (progress->total) { |
| 120 | unsigned percent = n * 100 / progress->total; |
| 121 | if (percent != progress->last_percent || progress_update) { |
| 122 | progress->last_percent = percent; |
SZEDER Gábor | d53ba84 | 2019-04-05 02:45:37 +0200 | [diff] [blame] | 123 | |
| 124 | strbuf_reset(counters_sb); |
| 125 | strbuf_addf(counters_sb, |
| 126 | "%3u%% (%"PRIuMAX"/%"PRIuMAX")%s", percent, |
| 127 | (uintmax_t)n, (uintmax_t)progress->total, |
| 128 | tp); |
| 129 | show_update = 1; |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 130 | } |
| 131 | } else if (progress_update) { |
SZEDER Gábor | d53ba84 | 2019-04-05 02:45:37 +0200 | [diff] [blame] | 132 | strbuf_reset(counters_sb); |
| 133 | strbuf_addf(counters_sb, "%"PRIuMAX"%s", (uintmax_t)n, tp); |
| 134 | show_update = 1; |
| 135 | } |
| 136 | |
| 137 | if (show_update) { |
Luke Mewburn | 85cb890 | 2015-04-13 23:30:51 +1000 | [diff] [blame] | 138 | if (is_foreground_fd(fileno(stderr)) || done) { |
SZEDER Gábor | 9f1fd84 | 2019-04-12 21:45:14 +0200 | [diff] [blame] | 139 | const char *eol = done ? done : "\r"; |
SZEDER Gábor | bbf4756 | 2019-09-16 22:54:11 +0200 | [diff] [blame] | 140 | size_t clear_len = counters_sb->len < last_count_len ? |
| 141 | last_count_len - counters_sb->len + 1 : |
| 142 | 0; |
| 143 | /* The "+ 2" accounts for the ": ". */ |
| 144 | size_t progress_line_len = progress->title_len + |
| 145 | counters_sb->len + 2; |
| 146 | int cols = term_columns(); |
SZEDER Gábor | 545dc34 | 2019-04-12 21:45:15 +0200 | [diff] [blame] | 147 | |
| 148 | if (progress->split) { |
SZEDER Gábor | bbf4756 | 2019-09-16 22:54:11 +0200 | [diff] [blame] | 149 | fprintf(stderr, " %s%*s", counters_sb->buf, |
| 150 | (int) clear_len, eol); |
| 151 | } else if (!done && cols < progress_line_len) { |
| 152 | clear_len = progress->title_len + 1 < cols ? |
| 153 | cols - progress->title_len - 1 : 0; |
| 154 | fprintf(stderr, "%s:%*s\n %s%s", |
| 155 | progress->title, (int) clear_len, "", |
| 156 | counters_sb->buf, eol); |
SZEDER Gábor | 545dc34 | 2019-04-12 21:45:15 +0200 | [diff] [blame] | 157 | progress->split = 1; |
| 158 | } else { |
SZEDER Gábor | bbf4756 | 2019-09-16 22:54:11 +0200 | [diff] [blame] | 159 | fprintf(stderr, "%s: %s%*s", progress->title, |
| 160 | counters_sb->buf, (int) clear_len, eol); |
SZEDER Gábor | 545dc34 | 2019-04-12 21:45:15 +0200 | [diff] [blame] | 161 | } |
Luke Mewburn | 85cb890 | 2015-04-13 23:30:51 +1000 | [diff] [blame] | 162 | fflush(stderr); |
| 163 | } |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 164 | progress_update = 0; |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 165 | } |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 166 | } |
| 167 | |
Elijah Newren | d6861d0 | 2017-11-13 12:15:58 -0800 | [diff] [blame] | 168 | static void throughput_string(struct strbuf *buf, uint64_t total, |
Nicolas Pitre | 53ed7b5 | 2007-11-06 16:30:28 -0500 | [diff] [blame] | 169 | unsigned int rate) |
| 170 | { |
Jeff King | 3131977 | 2015-09-24 17:05:57 -0400 | [diff] [blame] | 171 | strbuf_reset(buf); |
Antoine Pelisse | 079b546 | 2013-04-10 21:03:23 +0200 | [diff] [blame] | 172 | strbuf_addstr(buf, ", "); |
| 173 | strbuf_humanise_bytes(buf, total); |
| 174 | strbuf_addstr(buf, " | "); |
Dimitriy Ryazantcev | 8f354a1 | 2019-07-02 21:22:48 +0300 | [diff] [blame] | 175 | strbuf_humanise_rate(buf, rate * 1024); |
Nicolas Pitre | 53ed7b5 | 2007-11-06 16:30:28 -0500 | [diff] [blame] | 176 | } |
| 177 | |
SZEDER Gábor | 2bb74b5 | 2019-09-16 22:54:12 +0200 | [diff] [blame] | 178 | static uint64_t progress_getnanotime(struct progress *progress) |
| 179 | { |
| 180 | if (progress_testing) |
| 181 | return progress->start_ns + progress_test_ns; |
| 182 | else |
| 183 | return getnanotime(); |
| 184 | } |
| 185 | |
Elijah Newren | d6861d0 | 2017-11-13 12:15:58 -0800 | [diff] [blame] | 186 | void display_throughput(struct progress *progress, uint64_t total) |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 187 | { |
| 188 | struct throughput *tp; |
Karsten Blees | 83d26fa | 2014-07-12 02:08:11 +0200 | [diff] [blame] | 189 | uint64_t now_ns; |
| 190 | unsigned int misecs, count, rate; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 191 | |
| 192 | if (!progress) |
| 193 | return; |
| 194 | tp = progress->throughput; |
| 195 | |
SZEDER Gábor | 2bb74b5 | 2019-09-16 22:54:12 +0200 | [diff] [blame] | 196 | now_ns = progress_getnanotime(progress); |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 197 | |
| 198 | if (!tp) { |
René Scharfe | ca56dad | 2021-03-13 17:17:22 +0100 | [diff] [blame] | 199 | progress->throughput = CALLOC_ARRAY(tp, 1); |
Jeff King | 999b951 | 2019-04-11 09:49:57 -0400 | [diff] [blame] | 200 | tp->prev_total = tp->curr_total = total; |
| 201 | tp->prev_ns = now_ns; |
| 202 | strbuf_init(&tp->display, 0); |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 203 | return; |
| 204 | } |
Nicolas Pitre | 53ed7b5 | 2007-11-06 16:30:28 -0500 | [diff] [blame] | 205 | tp->curr_total = total; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 206 | |
Karsten Blees | 83d26fa | 2014-07-12 02:08:11 +0200 | [diff] [blame] | 207 | /* only update throughput every 0.5 s */ |
| 208 | if (now_ns - tp->prev_ns <= 500000000) |
| 209 | return; |
| 210 | |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 211 | /* |
Karsten Blees | 83d26fa | 2014-07-12 02:08:11 +0200 | [diff] [blame] | 212 | * We have x = bytes and y = nanosecs. We want z = KiB/s: |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 213 | * |
Karsten Blees | 83d26fa | 2014-07-12 02:08:11 +0200 | [diff] [blame] | 214 | * z = (x / 1024) / (y / 1000000000) |
| 215 | * z = x / y * 1000000000 / 1024 |
| 216 | * z = x / (y * 1024 / 1000000000) |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 217 | * z = x / y' |
| 218 | * |
| 219 | * To simplify things we'll keep track of misecs, or 1024th of a sec |
| 220 | * obtained with: |
| 221 | * |
Karsten Blees | 83d26fa | 2014-07-12 02:08:11 +0200 | [diff] [blame] | 222 | * y' = y * 1024 / 1000000000 |
| 223 | * y' = y * (2^10 / 2^42) * (2^42 / 1000000000) |
| 224 | * y' = y / 2^32 * 4398 |
| 225 | * y' = (y * 4398) >> 32 |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 226 | */ |
Karsten Blees | 83d26fa | 2014-07-12 02:08:11 +0200 | [diff] [blame] | 227 | misecs = ((now_ns - tp->prev_ns) * 4398) >> 32; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 228 | |
Karsten Blees | 83d26fa | 2014-07-12 02:08:11 +0200 | [diff] [blame] | 229 | count = total - tp->prev_total; |
| 230 | tp->prev_total = total; |
| 231 | tp->prev_ns = now_ns; |
| 232 | tp->avg_bytes += count; |
| 233 | tp->avg_misecs += misecs; |
| 234 | rate = tp->avg_bytes / tp->avg_misecs; |
| 235 | tp->avg_bytes -= tp->last_bytes[tp->idx]; |
| 236 | tp->avg_misecs -= tp->last_misecs[tp->idx]; |
| 237 | tp->last_bytes[tp->idx] = count; |
| 238 | tp->last_misecs[tp->idx] = misecs; |
| 239 | tp->idx = (tp->idx + 1) % TP_IDX_MAX; |
Nicolas Pitre | 53ed7b5 | 2007-11-06 16:30:28 -0500 | [diff] [blame] | 240 | |
Jeff King | 3131977 | 2015-09-24 17:05:57 -0400 | [diff] [blame] | 241 | throughput_string(&tp->display, total, rate); |
Karsten Blees | 83d26fa | 2014-07-12 02:08:11 +0200 | [diff] [blame] | 242 | if (progress->last_value != -1 && progress_update) |
| 243 | display(progress, progress->last_value, NULL); |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 244 | } |
| 245 | |
SZEDER Gábor | 9219d12 | 2019-04-05 02:45:36 +0200 | [diff] [blame] | 246 | void display_progress(struct progress *progress, uint64_t n) |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 247 | { |
SZEDER Gábor | 9219d12 | 2019-04-05 02:45:36 +0200 | [diff] [blame] | 248 | if (progress) |
| 249 | display(progress, n, NULL); |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 250 | } |
| 251 | |
Elijah Newren | d6861d0 | 2017-11-13 12:15:58 -0800 | [diff] [blame] | 252 | static struct progress *start_progress_delay(const char *title, uint64_t total, |
Jeff Hostetler | 9d81ecb | 2019-03-21 12:36:11 -0700 | [diff] [blame] | 253 | unsigned delay, unsigned sparse) |
Nicolas Pitre | 180a9f2 | 2007-04-20 15:05:27 -0400 | [diff] [blame] | 254 | { |
Jeff King | 999b951 | 2019-04-11 09:49:57 -0400 | [diff] [blame] | 255 | struct progress *progress = xmalloc(sizeof(*progress)); |
Nicolas Pitre | 42e18fb | 2007-10-16 21:55:45 -0400 | [diff] [blame] | 256 | progress->title = title; |
Nicolas Pitre | 180a9f2 | 2007-04-20 15:05:27 -0400 | [diff] [blame] | 257 | progress->total = total; |
Nicolas Pitre | 42e18fb | 2007-10-16 21:55:45 -0400 | [diff] [blame] | 258 | progress->last_value = -1; |
Nicolas Pitre | 180a9f2 | 2007-04-20 15:05:27 -0400 | [diff] [blame] | 259 | progress->last_percent = -1; |
Nicolas Pitre | 180a9f2 | 2007-04-20 15:05:27 -0400 | [diff] [blame] | 260 | progress->delay = delay; |
Jeff Hostetler | 9d81ecb | 2019-03-21 12:36:11 -0700 | [diff] [blame] | 261 | progress->sparse = sparse; |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 262 | progress->throughput = NULL; |
René Scharfe | 0fae1e0 | 2017-07-08 18:43:42 +0200 | [diff] [blame] | 263 | progress->start_ns = getnanotime(); |
SZEDER Gábor | d53ba84 | 2019-04-05 02:45:37 +0200 | [diff] [blame] | 264 | strbuf_init(&progress->counters_sb, 0); |
SZEDER Gábor | 545dc34 | 2019-04-12 21:45:15 +0200 | [diff] [blame] | 265 | progress->title_len = utf8_strwidth(title); |
| 266 | progress->split = 0; |
Nicolas Pitre | 180a9f2 | 2007-04-20 15:05:27 -0400 | [diff] [blame] | 267 | set_progress_signal(); |
Emily Shaffer | 98a1364 | 2020-05-12 14:44:20 -0700 | [diff] [blame] | 268 | trace2_region_enter("progress", title, the_repository); |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 269 | return progress; |
Nicolas Pitre | 180a9f2 | 2007-04-20 15:05:27 -0400 | [diff] [blame] | 270 | } |
| 271 | |
Derrick Stolee | 44a4693 | 2019-11-25 21:28:22 +0000 | [diff] [blame] | 272 | static int get_default_delay(void) |
| 273 | { |
| 274 | static int delay_in_secs = -1; |
| 275 | |
| 276 | if (delay_in_secs < 0) |
| 277 | delay_in_secs = git_env_ulong("GIT_PROGRESS_DELAY", 2); |
| 278 | |
| 279 | return delay_in_secs; |
| 280 | } |
| 281 | |
Elijah Newren | d6861d0 | 2017-11-13 12:15:58 -0800 | [diff] [blame] | 282 | struct progress *start_delayed_progress(const char *title, uint64_t total) |
Junio C Hamano | 8aade10 | 2017-08-19 10:39:41 -0700 | [diff] [blame] | 283 | { |
Derrick Stolee | 44a4693 | 2019-11-25 21:28:22 +0000 | [diff] [blame] | 284 | return start_progress_delay(title, total, get_default_delay(), 0); |
Junio C Hamano | 8aade10 | 2017-08-19 10:39:41 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Elijah Newren | d6861d0 | 2017-11-13 12:15:58 -0800 | [diff] [blame] | 287 | struct progress *start_progress(const char *title, uint64_t total) |
Nicolas Pitre | 42e18fb | 2007-10-16 21:55:45 -0400 | [diff] [blame] | 288 | { |
Jeff Hostetler | 9d81ecb | 2019-03-21 12:36:11 -0700 | [diff] [blame] | 289 | return start_progress_delay(title, total, 0, 0); |
| 290 | } |
| 291 | |
| 292 | /* |
| 293 | * Here "sparse" means that the caller might use some sampling criteria to |
| 294 | * decide when to call display_progress() rather than calling it for every |
| 295 | * integer value in[0 .. total). In particular, the caller might not call |
| 296 | * display_progress() for the last value in the range. |
| 297 | * |
| 298 | * When "sparse" is set, stop_progress() will automatically force the done |
| 299 | * message to show 100%. |
| 300 | */ |
| 301 | struct progress *start_sparse_progress(const char *title, uint64_t total) |
| 302 | { |
| 303 | return start_progress_delay(title, total, 0, 1); |
| 304 | } |
| 305 | |
| 306 | struct progress *start_delayed_sparse_progress(const char *title, |
| 307 | uint64_t total) |
| 308 | { |
Derrick Stolee | 44a4693 | 2019-11-25 21:28:22 +0000 | [diff] [blame] | 309 | return start_progress_delay(title, total, get_default_delay(), 1); |
Jeff Hostetler | 9d81ecb | 2019-03-21 12:36:11 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static void finish_if_sparse(struct progress *progress) |
| 313 | { |
Ævar Arnfjörð Bjarmason | 74900a6 | 2022-02-03 22:40:18 +0100 | [diff] [blame] | 314 | if (progress->sparse && |
Jeff Hostetler | 9d81ecb | 2019-03-21 12:36:11 -0700 | [diff] [blame] | 315 | progress->last_value != progress->total) |
| 316 | display_progress(progress, progress->total); |
Nicolas Pitre | 42e18fb | 2007-10-16 21:55:45 -0400 | [diff] [blame] | 317 | } |
| 318 | |
Ævar Arnfjörð Bjarmason | accf1eb | 2022-02-03 22:40:17 +0100 | [diff] [blame] | 319 | static void force_last_update(struct progress *progress, const char *msg) |
| 320 | { |
| 321 | char *buf; |
| 322 | struct throughput *tp = progress->throughput; |
| 323 | |
| 324 | if (tp) { |
| 325 | uint64_t now_ns = progress_getnanotime(progress); |
| 326 | unsigned int misecs, rate; |
| 327 | misecs = ((now_ns - progress->start_ns) * 4398) >> 32; |
| 328 | rate = tp->curr_total / (misecs ? misecs : 1); |
| 329 | throughput_string(&tp->display, tp->curr_total, rate); |
| 330 | } |
| 331 | progress_update = 1; |
| 332 | buf = xstrfmt(", %s.\n", msg); |
| 333 | display(progress, progress->last_value, buf); |
| 334 | free(buf); |
| 335 | } |
| 336 | |
| 337 | static void log_trace2(struct progress *progress) |
| 338 | { |
| 339 | trace2_data_intmax("progress", the_repository, "total_objects", |
| 340 | progress->total); |
| 341 | |
| 342 | if (progress->throughput) |
| 343 | trace2_data_intmax("progress", the_repository, "total_bytes", |
| 344 | progress->throughput->curr_total); |
| 345 | |
| 346 | trace2_region_leave("progress", progress->title, the_repository); |
| 347 | } |
| 348 | |
Nicolas Pitre | a984a06 | 2007-11-08 15:45:41 -0500 | [diff] [blame] | 349 | void stop_progress_msg(struct progress **p_progress, const char *msg) |
| 350 | { |
Martin Ågren | ac900fd | 2020-08-10 21:47:48 +0200 | [diff] [blame] | 351 | struct progress *progress; |
| 352 | |
| 353 | if (!p_progress) |
| 354 | BUG("don't provide NULL to stop_progress_msg"); |
| 355 | |
| 356 | progress = *p_progress; |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 357 | if (!progress) |
| 358 | return; |
| 359 | *p_progress = NULL; |
Boyd Lynn Gerber | d4c4444 | 2008-06-08 09:26:15 -0600 | [diff] [blame] | 360 | |
Ævar Arnfjörð Bjarmason | 74900a6 | 2022-02-03 22:40:18 +0100 | [diff] [blame] | 361 | finish_if_sparse(progress); |
Ævar Arnfjörð Bjarmason | accf1eb | 2022-02-03 22:40:17 +0100 | [diff] [blame] | 362 | if (progress->last_value != -1) |
| 363 | force_last_update(progress, msg); |
Ævar Arnfjörð Bjarmason | 74900a6 | 2022-02-03 22:40:18 +0100 | [diff] [blame] | 364 | log_trace2(progress); |
Ævar Arnfjörð Bjarmason | accf1eb | 2022-02-03 22:40:17 +0100 | [diff] [blame] | 365 | |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 366 | clear_progress_signal(); |
SZEDER Gábor | d53ba84 | 2019-04-05 02:45:37 +0200 | [diff] [blame] | 367 | strbuf_release(&progress->counters_sb); |
Jeff King | 3131977 | 2015-09-24 17:05:57 -0400 | [diff] [blame] | 368 | if (progress->throughput) |
| 369 | strbuf_release(&progress->throughput->display); |
Nicolas Pitre | cf84d51 | 2007-10-30 14:57:34 -0400 | [diff] [blame] | 370 | free(progress->throughput); |
Nicolas Pitre | dc6a075 | 2007-10-30 14:57:32 -0400 | [diff] [blame] | 371 | free(progress); |
Nicolas Pitre | 96a02f8 | 2007-04-18 14:27:45 -0400 | [diff] [blame] | 372 | } |