🌐 AI搜索 & 代理 主页
blob: 412e6b1ecc36e8bd8b7f090b7da3dcf0c7e4e5bd [file] [log] [blame]
Nicolas Pitre74b67922007-10-30 19:41:131/*
2 * Simple text-based progress display module for GIT
3 *
Nicolas Pitre03aa8ff2009-09-14 06:41:164 * Copyright (c) 2007 by Nicolas Pitre <nico@fluxnic.net>
Nicolas Pitre74b67922007-10-30 19:41:135 *
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 18:27:4511#include "git-compat-util.h"
Nguyễn Thái Ngọc Duy754dbc42014-02-21 12:50:1812#include "gettext.h"
Nicolas Pitre96a02f82007-04-18 18:27:4513#include "progress.h"
Antoine Pelisse079b5462013-04-10 19:03:2314#include "strbuf.h"
Karsten Blees83d26fa2014-07-12 00:08:1115#include "trace.h"
Nicolas Pitre96a02f82007-04-18 18:27:4516
Nicolas Pitrecf84d512007-10-30 18:57:3417#define TP_IDX_MAX 8
18
19struct throughput {
Nicolas Pitre53ed7b52007-11-06 21:30:2820 off_t curr_total;
Nicolas Pitre218558a2007-11-05 03:15:4121 off_t prev_total;
Karsten Blees83d26fa2014-07-12 00:08:1122 uint64_t prev_ns;
Nicolas Pitre218558a2007-11-05 03:15:4123 unsigned int avg_bytes;
Nicolas Pitrecf84d512007-10-30 18:57:3424 unsigned int avg_misecs;
Nicolas Pitre53ed7b52007-11-06 21:30:2825 unsigned int last_bytes[TP_IDX_MAX];
Nicolas Pitrecf84d512007-10-30 18:57:3426 unsigned int last_misecs[TP_IDX_MAX];
27 unsigned int idx;
Nicolas Pitre81f66542007-11-01 20:59:5728 char display[32];
Nicolas Pitrecf84d512007-10-30 18:57:3429};
30
Nicolas Pitredc6a0752007-10-30 18:57:3231struct progress {
32 const char *title;
33 int last_value;
34 unsigned total;
35 unsigned last_percent;
36 unsigned delay;
37 unsigned delayed_percent_treshold;
Nicolas Pitrecf84d512007-10-30 18:57:3438 struct throughput *throughput;
Nicolas Pitredc6a0752007-10-30 18:57:3239};
40
Nicolas Pitre96a02f82007-04-18 18:27:4541static volatile sig_atomic_t progress_update;
42
43static void progress_interval(int signum)
44{
45 progress_update = 1;
46}
47
48static void set_progress_signal(void)
49{
50 struct sigaction sa;
51 struct itimerval v;
52
Nicolas Pitre180a9f22007-04-20 19:05:2753 progress_update = 0;
54
Nicolas Pitre96a02f82007-04-18 18:27:4555 memset(&sa, 0, sizeof(sa));
56 sa.sa_handler = progress_interval;
57 sigemptyset(&sa.sa_mask);
58 sa.sa_flags = SA_RESTART;
59 sigaction(SIGALRM, &sa, NULL);
60
61 v.it_interval.tv_sec = 1;
62 v.it_interval.tv_usec = 0;
63 v.it_value = v.it_interval;
64 setitimer(ITIMER_REAL, &v, NULL);
65}
66
67static void clear_progress_signal(void)
68{
69 struct itimerval v = {{0,},};
70 setitimer(ITIMER_REAL, &v, NULL);
71 signal(SIGALRM, SIG_IGN);
72 progress_update = 0;
73}
74
Nicolas Pitrea984a062007-11-08 20:45:4175static int display(struct progress *progress, unsigned n, const char *done)
Nicolas Pitre96a02f82007-04-18 18:27:4576{
Nicolas Pitrea984a062007-11-08 20:45:4177 const char *eol, *tp;
Nicolas Pitre42e18fb2007-10-17 01:55:4578
Nicolas Pitre180a9f22007-04-20 19:05:2779 if (progress->delay) {
Nicolas Pitre180a9f22007-04-20 19:05:2780 if (!progress_update || --progress->delay)
81 return 0;
82 if (progress->total) {
83 unsigned percent = n * 100 / progress->total;
84 if (percent > progress->delayed_percent_treshold) {
85 /* inhibit this progress report entirely */
86 clear_progress_signal();
87 progress->delay = -1;
88 progress->total = 0;
89 return 0;
90 }
91 }
Nicolas Pitre180a9f22007-04-20 19:05:2792 }
Nicolas Pitre42e18fb2007-10-17 01:55:4593
94 progress->last_value = n;
Nicolas Pitrecf84d512007-10-30 18:57:3495 tp = (progress->throughput) ? progress->throughput->display : "";
Nicolas Pitrea984a062007-11-08 20:45:4196 eol = done ? done : " \r";
Nicolas Pitre96a02f82007-04-18 18:27:4597 if (progress->total) {
98 unsigned percent = n * 100 / progress->total;
99 if (percent != progress->last_percent || progress_update) {
100 progress->last_percent = percent;
Nicolas Pitrecf84d512007-10-30 18:57:34101 fprintf(stderr, "%s: %3u%% (%u/%u)%s%s",
102 progress->title, percent, n,
103 progress->total, tp, eol);
Johannes Sixt137a0d02007-11-19 19:48:58104 fflush(stderr);
Nicolas Pitre96a02f82007-04-18 18:27:45105 progress_update = 0;
106 return 1;
107 }
108 } else if (progress_update) {
Nicolas Pitrecf84d512007-10-30 18:57:34109 fprintf(stderr, "%s: %u%s%s", progress->title, n, tp, eol);
Johannes Sixt137a0d02007-11-19 19:48:58110 fflush(stderr);
Nicolas Pitre96a02f82007-04-18 18:27:45111 progress_update = 0;
112 return 1;
113 }
Nicolas Pitre42e18fb2007-10-17 01:55:45114
Nicolas Pitre96a02f82007-04-18 18:27:45115 return 0;
116}
117
Antoine Pelisse079b5462013-04-10 19:03:23118static void throughput_string(struct strbuf *buf, off_t total,
Nicolas Pitre53ed7b52007-11-06 21:30:28119 unsigned int rate)
120{
Antoine Pelisse079b5462013-04-10 19:03:23121 strbuf_addstr(buf, ", ");
122 strbuf_humanise_bytes(buf, total);
123 strbuf_addstr(buf, " | ");
124 strbuf_humanise_bytes(buf, rate * 1024);
125 strbuf_addstr(buf, "/s");
Nicolas Pitre53ed7b52007-11-06 21:30:28126}
127
Nicolas Pitre218558a2007-11-05 03:15:41128void display_throughput(struct progress *progress, off_t total)
Nicolas Pitrecf84d512007-10-30 18:57:34129{
130 struct throughput *tp;
Karsten Blees83d26fa2014-07-12 00:08:11131 uint64_t now_ns;
132 unsigned int misecs, count, rate;
133 struct strbuf buf = STRBUF_INIT;
Nicolas Pitrecf84d512007-10-30 18:57:34134
135 if (!progress)
136 return;
137 tp = progress->throughput;
138
Karsten Blees83d26fa2014-07-12 00:08:11139 now_ns = getnanotime();
Nicolas Pitrecf84d512007-10-30 18:57:34140
141 if (!tp) {
142 progress->throughput = tp = calloc(1, sizeof(*tp));
Nicolas Pitre218558a2007-11-05 03:15:41143 if (tp) {
Nicolas Pitre53ed7b52007-11-06 21:30:28144 tp->prev_total = tp->curr_total = total;
Karsten Blees83d26fa2014-07-12 00:08:11145 tp->prev_ns = now_ns;
Nicolas Pitre218558a2007-11-05 03:15:41146 }
Nicolas Pitrecf84d512007-10-30 18:57:34147 return;
148 }
Nicolas Pitre53ed7b52007-11-06 21:30:28149 tp->curr_total = total;
Nicolas Pitrecf84d512007-10-30 18:57:34150
Karsten Blees83d26fa2014-07-12 00:08:11151 /* only update throughput every 0.5 s */
152 if (now_ns - tp->prev_ns <= 500000000)
153 return;
154
Nicolas Pitrecf84d512007-10-30 18:57:34155 /*
Karsten Blees83d26fa2014-07-12 00:08:11156 * We have x = bytes and y = nanosecs. We want z = KiB/s:
Nicolas Pitrecf84d512007-10-30 18:57:34157 *
Karsten Blees83d26fa2014-07-12 00:08:11158 * z = (x / 1024) / (y / 1000000000)
159 * z = x / y * 1000000000 / 1024
160 * z = x / (y * 1024 / 1000000000)
Nicolas Pitrecf84d512007-10-30 18:57:34161 * z = x / y'
162 *
163 * To simplify things we'll keep track of misecs, or 1024th of a sec
164 * obtained with:
165 *
Karsten Blees83d26fa2014-07-12 00:08:11166 * y' = y * 1024 / 1000000000
167 * y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
168 * y' = y / 2^32 * 4398
169 * y' = (y * 4398) >> 32
Nicolas Pitrecf84d512007-10-30 18:57:34170 */
Karsten Blees83d26fa2014-07-12 00:08:11171 misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
Nicolas Pitrecf84d512007-10-30 18:57:34172
Karsten Blees83d26fa2014-07-12 00:08:11173 count = total - tp->prev_total;
174 tp->prev_total = total;
175 tp->prev_ns = now_ns;
176 tp->avg_bytes += count;
177 tp->avg_misecs += misecs;
178 rate = tp->avg_bytes / tp->avg_misecs;
179 tp->avg_bytes -= tp->last_bytes[tp->idx];
180 tp->avg_misecs -= tp->last_misecs[tp->idx];
181 tp->last_bytes[tp->idx] = count;
182 tp->last_misecs[tp->idx] = misecs;
183 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
Nicolas Pitre53ed7b52007-11-06 21:30:28184
Karsten Blees83d26fa2014-07-12 00:08:11185 throughput_string(&buf, total, rate);
186 strncpy(tp->display, buf.buf, sizeof(tp->display));
187 strbuf_release(&buf);
188 if (progress->last_value != -1 && progress_update)
189 display(progress, progress->last_value, NULL);
Nicolas Pitrecf84d512007-10-30 18:57:34190}
191
Nicolas Pitre42e18fb2007-10-17 01:55:45192int display_progress(struct progress *progress, unsigned n)
Nicolas Pitre96a02f82007-04-18 18:27:45193{
Nicolas Pitrea984a062007-11-08 20:45:41194 return progress ? display(progress, n, NULL) : 0;
Nicolas Pitre96a02f82007-04-18 18:27:45195}
196
Nicolas Pitredc6a0752007-10-30 18:57:32197struct progress *start_progress_delay(const char *title, unsigned total,
198 unsigned percent_treshold, unsigned delay)
Nicolas Pitre180a9f22007-04-20 19:05:27199{
Nicolas Pitredc6a0752007-10-30 18:57:32200 struct progress *progress = malloc(sizeof(*progress));
201 if (!progress) {
202 /* unlikely, but here's a good fallback */
203 fprintf(stderr, "%s...\n", title);
Johannes Sixt137a0d02007-11-19 19:48:58204 fflush(stderr);
Nicolas Pitredc6a0752007-10-30 18:57:32205 return NULL;
206 }
Nicolas Pitre42e18fb2007-10-17 01:55:45207 progress->title = title;
Nicolas Pitre180a9f22007-04-20 19:05:27208 progress->total = total;
Nicolas Pitre42e18fb2007-10-17 01:55:45209 progress->last_value = -1;
Nicolas Pitre180a9f22007-04-20 19:05:27210 progress->last_percent = -1;
211 progress->delayed_percent_treshold = percent_treshold;
Nicolas Pitre180a9f22007-04-20 19:05:27212 progress->delay = delay;
Nicolas Pitrecf84d512007-10-30 18:57:34213 progress->throughput = NULL;
Nicolas Pitre180a9f22007-04-20 19:05:27214 set_progress_signal();
Nicolas Pitredc6a0752007-10-30 18:57:32215 return progress;
Nicolas Pitre180a9f22007-04-20 19:05:27216}
217
Nicolas Pitredc6a0752007-10-30 18:57:32218struct progress *start_progress(const char *title, unsigned total)
Nicolas Pitre42e18fb2007-10-17 01:55:45219{
Nicolas Pitredc6a0752007-10-30 18:57:32220 return start_progress_delay(title, total, 0, 0);
Nicolas Pitre42e18fb2007-10-17 01:55:45221}
222
Nicolas Pitredc6a0752007-10-30 18:57:32223void stop_progress(struct progress **p_progress)
Nicolas Pitre96a02f82007-04-18 18:27:45224{
Nguyễn Thái Ngọc Duy754dbc42014-02-21 12:50:18225 stop_progress_msg(p_progress, _("done"));
Nicolas Pitrea984a062007-11-08 20:45:41226}
227
228void stop_progress_msg(struct progress **p_progress, const char *msg)
229{
Nicolas Pitredc6a0752007-10-30 18:57:32230 struct progress *progress = *p_progress;
231 if (!progress)
232 return;
233 *p_progress = NULL;
Nicolas Pitre42e18fb2007-10-17 01:55:45234 if (progress->last_value != -1) {
235 /* Force the last update */
Boyd Lynn Gerberd4c44442008-06-08 15:26:15236 char buf[128], *bufp;
237 size_t len = strlen(msg) + 5;
Nicolas Pitre53ed7b52007-11-06 21:30:28238 struct throughput *tp = progress->throughput;
Boyd Lynn Gerberd4c44442008-06-08 15:26:15239
240 bufp = (len < sizeof(buf)) ? buf : xmalloc(len + 1);
Nicolas Pitre53ed7b52007-11-06 21:30:28241 if (tp) {
Antoine Pelisse079b5462013-04-10 19:03:23242 struct strbuf strbuf = STRBUF_INIT;
Nicolas Pitre53ed7b52007-11-06 21:30:28243 unsigned int rate = !tp->avg_misecs ? 0 :
244 tp->avg_bytes / tp->avg_misecs;
Antoine Pelisse079b5462013-04-10 19:03:23245 throughput_string(&strbuf, tp->curr_total, rate);
246 strncpy(tp->display, strbuf.buf, sizeof(tp->display));
247 strbuf_release(&strbuf);
Nicolas Pitre53ed7b52007-11-06 21:30:28248 }
Nicolas Pitre42e18fb2007-10-17 01:55:45249 progress_update = 1;
Boyd Lynn Gerberd4c44442008-06-08 15:26:15250 sprintf(bufp, ", %s.\n", msg);
251 display(progress, progress->last_value, bufp);
252 if (buf != bufp)
253 free(bufp);
Nicolas Pitre42e18fb2007-10-17 01:55:45254 }
Nicolas Pitre96a02f82007-04-18 18:27:45255 clear_progress_signal();
Nicolas Pitrecf84d512007-10-30 18:57:34256 free(progress->throughput);
Nicolas Pitredc6a0752007-10-30 18:57:32257 free(progress);
Nicolas Pitre96a02f82007-04-18 18:27:45258}