blob: 261314ef3cd60662300129df98afff7ea2a1673c [file] [log] [blame]
Nicolas Pitre74b67922007-10-30 15:41:13 -04001/*
2 * Simple text-based progress display module for GIT
3 *
Nicolas Pitre03aa8ff2009-09-14 02:41:16 -04004 * Copyright (c) 2007 by Nicolas Pitre <nico@fluxnic.net>
Nicolas Pitre74b67922007-10-30 15:41:13 -04005 *
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 14:27:45 -040011#include "git-compat-util.h"
Nguyễn Thái Ngọc Duy754dbc42014-02-21 19:50:18 +070012#include "gettext.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -040013#include "progress.h"
Antoine Pelisse079b5462013-04-10 21:03:23 +020014#include "strbuf.h"
Nicolas Pitre96a02f82007-04-18 14:27:45 -040015
Nicolas Pitrecf84d512007-10-30 14:57:34 -040016#define TP_IDX_MAX 8
17
18struct throughput {
Nicolas Pitre53ed7b52007-11-06 16:30:28 -050019 off_t curr_total;
Nicolas Pitre218558a2007-11-04 22:15:41 -050020 off_t prev_total;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040021 struct timeval prev_tv;
Nicolas Pitre218558a2007-11-04 22:15:41 -050022 unsigned int avg_bytes;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040023 unsigned int avg_misecs;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -050024 unsigned int last_bytes[TP_IDX_MAX];
Nicolas Pitrecf84d512007-10-30 14:57:34 -040025 unsigned int last_misecs[TP_IDX_MAX];
26 unsigned int idx;
Nicolas Pitre81f66542007-11-01 16:59:57 -040027 char display[32];
Nicolas Pitrecf84d512007-10-30 14:57:34 -040028};
29
Nicolas Pitredc6a0752007-10-30 14:57:32 -040030struct 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 Pitrecf84d512007-10-30 14:57:34 -040037 struct throughput *throughput;
Nicolas Pitredc6a0752007-10-30 14:57:32 -040038};
39
Nicolas Pitre96a02f82007-04-18 14:27:45 -040040static volatile sig_atomic_t progress_update;
41
42static void progress_interval(int signum)
43{
44 progress_update = 1;
45}
46
47static void set_progress_signal(void)
48{
49 struct sigaction sa;
50 struct itimerval v;
51
Nicolas Pitre180a9f22007-04-20 15:05:27 -040052 progress_update = 0;
53
Nicolas Pitre96a02f82007-04-18 14:27:45 -040054 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
66static 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 Pitrea984a062007-11-08 15:45:41 -050074static int display(struct progress *progress, unsigned n, const char *done)
Nicolas Pitre96a02f82007-04-18 14:27:45 -040075{
Nicolas Pitrea984a062007-11-08 15:45:41 -050076 const char *eol, *tp;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -040077
Nicolas Pitre180a9f22007-04-20 15:05:27 -040078 if (progress->delay) {
Nicolas Pitre180a9f22007-04-20 15:05:27 -040079 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 Pitre180a9f22007-04-20 15:05:27 -040091 }
Nicolas Pitre42e18fb2007-10-16 21:55:45 -040092
93 progress->last_value = n;
Nicolas Pitrecf84d512007-10-30 14:57:34 -040094 tp = (progress->throughput) ? progress->throughput->display : "";
Nicolas Pitrea984a062007-11-08 15:45:41 -050095 eol = done ? done : " \r";
Nicolas Pitre96a02f82007-04-18 14:27:45 -040096 if (progress->total) {
97 unsigned percent = n * 100 / progress->total;
98 if (percent != progress->last_percent || progress_update) {
99 progress->last_percent = percent;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400100 fprintf(stderr, "%s: %3u%% (%u/%u)%s%s",
101 progress->title, percent, n,
102 progress->total, tp, eol);
Johannes Sixt137a0d02007-11-19 20:48:58 +0100103 fflush(stderr);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400104 progress_update = 0;
105 return 1;
106 }
107 } else if (progress_update) {
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400108 fprintf(stderr, "%s: %u%s%s", progress->title, n, tp, eol);
Johannes Sixt137a0d02007-11-19 20:48:58 +0100109 fflush(stderr);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400110 progress_update = 0;
111 return 1;
112 }
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400113
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400114 return 0;
115}
116
Antoine Pelisse079b5462013-04-10 21:03:23 +0200117static void throughput_string(struct strbuf *buf, off_t total,
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500118 unsigned int rate)
119{
Antoine Pelisse079b5462013-04-10 21:03:23 +0200120 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 Pitre53ed7b52007-11-06 16:30:28 -0500125}
126
Nicolas Pitre218558a2007-11-04 22:15:41 -0500127void display_throughput(struct progress *progress, off_t total)
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400128{
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 Pitre218558a2007-11-04 22:15:41 -0500141 if (tp) {
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500142 tp->prev_total = tp->curr_total = total;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400143 tp->prev_tv = tv;
Nicolas Pitre218558a2007-11-04 22:15:41 -0500144 }
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400145 return;
146 }
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500147 tp->curr_total = total;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400148
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400149 /*
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 Pelisse079b5462013-04-10 21:03:23 +0200168 struct strbuf buf = STRBUF_INIT;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500169 unsigned int count, rate;
170
171 count = total - tp->prev_total;
Nicolas Pitre218558a2007-11-04 22:15:41 -0500172 tp->prev_total = total;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400173 tp->prev_tv = tv;
Nicolas Pitre218558a2007-11-04 22:15:41 -0500174 tp->avg_bytes += count;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400175 tp->avg_misecs += misecs;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500176 rate = tp->avg_bytes / tp->avg_misecs;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400177 tp->avg_bytes -= tp->last_bytes[tp->idx];
178 tp->avg_misecs -= tp->last_misecs[tp->idx];
Nicolas Pitre218558a2007-11-04 22:15:41 -0500179 tp->last_bytes[tp->idx] = count;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400180 tp->last_misecs[tp->idx] = misecs;
181 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
Nicolas Pitre3e935d12007-11-01 16:59:56 -0400182
Antoine Pelisse079b5462013-04-10 21:03:23 +0200183 throughput_string(&buf, total, rate);
184 strncpy(tp->display, buf.buf, sizeof(tp->display));
185 strbuf_release(&buf);
Nicolas Pitre3e935d12007-11-01 16:59:56 -0400186 if (progress->last_value != -1 && progress_update)
Nicolas Pitrea984a062007-11-08 15:45:41 -0500187 display(progress, progress->last_value, NULL);
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400188 }
189}
190
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400191int display_progress(struct progress *progress, unsigned n)
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400192{
Nicolas Pitrea984a062007-11-08 15:45:41 -0500193 return progress ? display(progress, n, NULL) : 0;
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400194}
195
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400196struct progress *start_progress_delay(const char *title, unsigned total,
197 unsigned percent_treshold, unsigned delay)
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400198{
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400199 struct progress *progress = malloc(sizeof(*progress));
200 if (!progress) {
201 /* unlikely, but here's a good fallback */
202 fprintf(stderr, "%s...\n", title);
Johannes Sixt137a0d02007-11-19 20:48:58 +0100203 fflush(stderr);
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400204 return NULL;
205 }
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400206 progress->title = title;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400207 progress->total = total;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400208 progress->last_value = -1;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400209 progress->last_percent = -1;
210 progress->delayed_percent_treshold = percent_treshold;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400211 progress->delay = delay;
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400212 progress->throughput = NULL;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400213 set_progress_signal();
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400214 return progress;
Nicolas Pitre180a9f22007-04-20 15:05:27 -0400215}
216
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400217struct progress *start_progress(const char *title, unsigned total)
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400218{
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400219 return start_progress_delay(title, total, 0, 0);
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400220}
221
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400222void stop_progress(struct progress **p_progress)
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400223{
Nguyễn Thái Ngọc Duy754dbc42014-02-21 19:50:18 +0700224 stop_progress_msg(p_progress, _("done"));
Nicolas Pitrea984a062007-11-08 15:45:41 -0500225}
226
227void stop_progress_msg(struct progress **p_progress, const char *msg)
228{
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400229 struct progress *progress = *p_progress;
230 if (!progress)
231 return;
232 *p_progress = NULL;
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400233 if (progress->last_value != -1) {
234 /* Force the last update */
Boyd Lynn Gerberd4c44442008-06-08 09:26:15 -0600235 char buf[128], *bufp;
236 size_t len = strlen(msg) + 5;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500237 struct throughput *tp = progress->throughput;
Boyd Lynn Gerberd4c44442008-06-08 09:26:15 -0600238
239 bufp = (len < sizeof(buf)) ? buf : xmalloc(len + 1);
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500240 if (tp) {
Antoine Pelisse079b5462013-04-10 21:03:23 +0200241 struct strbuf strbuf = STRBUF_INIT;
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500242 unsigned int rate = !tp->avg_misecs ? 0 :
243 tp->avg_bytes / tp->avg_misecs;
Antoine Pelisse079b5462013-04-10 21:03:23 +0200244 throughput_string(&strbuf, tp->curr_total, rate);
245 strncpy(tp->display, strbuf.buf, sizeof(tp->display));
246 strbuf_release(&strbuf);
Nicolas Pitre53ed7b52007-11-06 16:30:28 -0500247 }
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400248 progress_update = 1;
Boyd Lynn Gerberd4c44442008-06-08 09:26:15 -0600249 sprintf(bufp, ", %s.\n", msg);
250 display(progress, progress->last_value, bufp);
251 if (buf != bufp)
252 free(bufp);
Nicolas Pitre42e18fb2007-10-16 21:55:45 -0400253 }
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400254 clear_progress_signal();
Nicolas Pitrecf84d512007-10-30 14:57:34 -0400255 free(progress->throughput);
Nicolas Pitredc6a0752007-10-30 14:57:32 -0400256 free(progress);
Nicolas Pitre96a02f82007-04-18 14:27:45 -0400257}