l10n: zh_CN: review for git v2.14.0 l10n
[git.git] / progress.c
blob73e36d4a423067756ccd52af8cdb4cf0f762684b
1 /*
2 * Simple text-based progress display module for GIT
4 * Copyright (c) 2007 by Nicolas Pitre <nico@fluxnic.net>
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 */
11 #include "git-compat-util.h"
12 #include "gettext.h"
13 #include "progress.h"
14 #include "strbuf.h"
15 #include "trace.h"
17 #define TP_IDX_MAX 8
19 struct throughput {
20 off_t curr_total;
21 off_t prev_total;
22 uint64_t prev_ns;
23 unsigned int avg_bytes;
24 unsigned int avg_misecs;
25 unsigned int last_bytes[TP_IDX_MAX];
26 unsigned int last_misecs[TP_IDX_MAX];
27 unsigned int idx;
28 struct strbuf display;
31 struct progress {
32 const char *title;
33 int last_value;
34 unsigned total;
35 unsigned last_percent;
36 unsigned delay;
37 unsigned delayed_percent_treshold;
38 struct throughput *throughput;
39 uint64_t start_ns;
42 static volatile sig_atomic_t progress_update;
44 static void progress_interval(int signum)
46 progress_update = 1;
49 static void set_progress_signal(void)
51 struct sigaction sa;
52 struct itimerval v;
54 progress_update = 0;
56 memset(&sa, 0, sizeof(sa));
57 sa.sa_handler = progress_interval;
58 sigemptyset(&sa.sa_mask);
59 sa.sa_flags = SA_RESTART;
60 sigaction(SIGALRM, &sa, NULL);
62 v.it_interval.tv_sec = 1;
63 v.it_interval.tv_usec = 0;
64 v.it_value = v.it_interval;
65 setitimer(ITIMER_REAL, &v, NULL);
68 static void clear_progress_signal(void)
70 struct itimerval v = {{0,},};
71 setitimer(ITIMER_REAL, &v, NULL);
72 signal(SIGALRM, SIG_IGN);
73 progress_update = 0;
76 static int is_foreground_fd(int fd)
78 int tpgrp = tcgetpgrp(fd);
79 return tpgrp < 0 || tpgrp == getpgid(0);
82 static int display(struct progress *progress, unsigned n, const char *done)
84 const char *eol, *tp;
86 if (progress->delay) {
87 if (!progress_update || --progress->delay)
88 return 0;
89 if (progress->total) {
90 unsigned percent = n * 100 / progress->total;
91 if (percent > progress->delayed_percent_treshold) {
92 /* inhibit this progress report entirely */
93 clear_progress_signal();
94 progress->delay = -1;
95 progress->total = 0;
96 return 0;
101 progress->last_value = n;
102 tp = (progress->throughput) ? progress->throughput->display.buf : "";
103 eol = done ? done : " \r";
104 if (progress->total) {
105 unsigned percent = n * 100 / progress->total;
106 if (percent != progress->last_percent || progress_update) {
107 progress->last_percent = percent;
108 if (is_foreground_fd(fileno(stderr)) || done) {
109 fprintf(stderr, "%s: %3u%% (%u/%u)%s%s",
110 progress->title, percent, n,
111 progress->total, tp, eol);
112 fflush(stderr);
114 progress_update = 0;
115 return 1;
117 } else if (progress_update) {
118 if (is_foreground_fd(fileno(stderr)) || done) {
119 fprintf(stderr, "%s: %u%s%s",
120 progress->title, n, tp, eol);
121 fflush(stderr);
123 progress_update = 0;
124 return 1;
127 return 0;
130 static void throughput_string(struct strbuf *buf, off_t total,
131 unsigned int rate)
133 strbuf_reset(buf);
134 strbuf_addstr(buf, ", ");
135 strbuf_humanise_bytes(buf, total);
136 strbuf_addstr(buf, " | ");
137 strbuf_humanise_bytes(buf, rate * 1024);
138 strbuf_addstr(buf, "/s");
141 void display_throughput(struct progress *progress, off_t total)
143 struct throughput *tp;
144 uint64_t now_ns;
145 unsigned int misecs, count, rate;
147 if (!progress)
148 return;
149 tp = progress->throughput;
151 now_ns = getnanotime();
153 if (!tp) {
154 progress->throughput = tp = calloc(1, sizeof(*tp));
155 if (tp) {
156 tp->prev_total = tp->curr_total = total;
157 tp->prev_ns = now_ns;
158 strbuf_init(&tp->display, 0);
160 return;
162 tp->curr_total = total;
164 /* only update throughput every 0.5 s */
165 if (now_ns - tp->prev_ns <= 500000000)
166 return;
169 * We have x = bytes and y = nanosecs. We want z = KiB/s:
171 * z = (x / 1024) / (y / 1000000000)
172 * z = x / y * 1000000000 / 1024
173 * z = x / (y * 1024 / 1000000000)
174 * z = x / y'
176 * To simplify things we'll keep track of misecs, or 1024th of a sec
177 * obtained with:
179 * y' = y * 1024 / 1000000000
180 * y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
181 * y' = y / 2^32 * 4398
182 * y' = (y * 4398) >> 32
184 misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
186 count = total - tp->prev_total;
187 tp->prev_total = total;
188 tp->prev_ns = now_ns;
189 tp->avg_bytes += count;
190 tp->avg_misecs += misecs;
191 rate = tp->avg_bytes / tp->avg_misecs;
192 tp->avg_bytes -= tp->last_bytes[tp->idx];
193 tp->avg_misecs -= tp->last_misecs[tp->idx];
194 tp->last_bytes[tp->idx] = count;
195 tp->last_misecs[tp->idx] = misecs;
196 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
198 throughput_string(&tp->display, total, rate);
199 if (progress->last_value != -1 && progress_update)
200 display(progress, progress->last_value, NULL);
203 int display_progress(struct progress *progress, unsigned n)
205 return progress ? display(progress, n, NULL) : 0;
208 struct progress *start_progress_delay(const char *title, unsigned total,
209 unsigned percent_treshold, unsigned delay)
211 struct progress *progress = malloc(sizeof(*progress));
212 if (!progress) {
213 /* unlikely, but here's a good fallback */
214 fprintf(stderr, "%s...\n", title);
215 fflush(stderr);
216 return NULL;
218 progress->title = title;
219 progress->total = total;
220 progress->last_value = -1;
221 progress->last_percent = -1;
222 progress->delayed_percent_treshold = percent_treshold;
223 progress->delay = delay;
224 progress->throughput = NULL;
225 progress->start_ns = getnanotime();
226 set_progress_signal();
227 return progress;
230 struct progress *start_progress(const char *title, unsigned total)
232 return start_progress_delay(title, total, 0, 0);
235 void stop_progress(struct progress **p_progress)
237 stop_progress_msg(p_progress, _("done"));
240 void stop_progress_msg(struct progress **p_progress, const char *msg)
242 struct progress *progress = *p_progress;
243 if (!progress)
244 return;
245 *p_progress = NULL;
246 if (progress->last_value != -1) {
247 /* Force the last update */
248 char *buf;
249 struct throughput *tp = progress->throughput;
251 if (tp) {
252 uint64_t now_ns = getnanotime();
253 unsigned int misecs, rate;
254 misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
255 rate = tp->curr_total / (misecs ? misecs : 1);
256 throughput_string(&tp->display, tp->curr_total, rate);
258 progress_update = 1;
259 buf = xstrfmt(", %s.\n", msg);
260 display(progress, progress->last_value, buf);
261 free(buf);
263 clear_progress_signal();
264 if (progress->throughput)
265 strbuf_release(&progress->throughput->display);
266 free(progress->throughput);
267 free(progress);