sideband.c: ESC is spelled '\033' not '\e' for portability.
[git/git.git] / progress.c
CommitLineData
74b6792f
NP
1/*
2 * Simple text-based progress display module for GIT
3 *
4 * Copyright (c) 2007 by Nicolas Pitre <nico@cam.org>
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
96a02f8f
NP
11#include "git-compat-util.h"
12#include "progress.h"
13
cf84d51c
NP
14#define TP_IDX_MAX 8
15
16struct throughput {
17 struct timeval prev_tv;
81f6654a 18 off_t total;
cf84d51c
NP
19 unsigned long count;
20 unsigned long avg_bytes;
21 unsigned long last_bytes[TP_IDX_MAX];
22 unsigned int avg_misecs;
23 unsigned int last_misecs[TP_IDX_MAX];
24 unsigned int idx;
81f6654a 25 char display[32];
cf84d51c
NP
26};
27
dc6a0757
NP
28struct progress {
29 const char *title;
30 int last_value;
31 unsigned total;
32 unsigned last_percent;
33 unsigned delay;
34 unsigned delayed_percent_treshold;
cf84d51c 35 struct throughput *throughput;
dc6a0757
NP
36};
37
96a02f8f
NP
38static volatile sig_atomic_t progress_update;
39
40static void progress_interval(int signum)
41{
42 progress_update = 1;
43}
44
45static void set_progress_signal(void)
46{
47 struct sigaction sa;
48 struct itimerval v;
49
180a9f22
NP
50 progress_update = 0;
51
96a02f8f
NP
52 memset(&sa, 0, sizeof(sa));
53 sa.sa_handler = progress_interval;
54 sigemptyset(&sa.sa_mask);
55 sa.sa_flags = SA_RESTART;
56 sigaction(SIGALRM, &sa, NULL);
57
58 v.it_interval.tv_sec = 1;
59 v.it_interval.tv_usec = 0;
60 v.it_value = v.it_interval;
61 setitimer(ITIMER_REAL, &v, NULL);
62}
63
64static void clear_progress_signal(void)
65{
66 struct itimerval v = {{0,},};
67 setitimer(ITIMER_REAL, &v, NULL);
68 signal(SIGALRM, SIG_IGN);
69 progress_update = 0;
70}
71
42e18fbf 72static int display(struct progress *progress, unsigned n, int done)
96a02f8f 73{
cf84d51c 74 char *eol, *tp;
42e18fbf 75
180a9f22 76 if (progress->delay) {
180a9f22
NP
77 if (!progress_update || --progress->delay)
78 return 0;
79 if (progress->total) {
80 unsigned percent = n * 100 / progress->total;
81 if (percent > progress->delayed_percent_treshold) {
82 /* inhibit this progress report entirely */
83 clear_progress_signal();
84 progress->delay = -1;
85 progress->total = 0;
86 return 0;
87 }
88 }
180a9f22 89 }
42e18fbf
NP
90
91 progress->last_value = n;
cf84d51c 92 tp = (progress->throughput) ? progress->throughput->display : "";
42e18fbf 93 eol = done ? ", done. \n" : " \r";
96a02f8f
NP
94 if (progress->total) {
95 unsigned percent = n * 100 / progress->total;
96 if (percent != progress->last_percent || progress_update) {
97 progress->last_percent = percent;
cf84d51c
NP
98 fprintf(stderr, "%s: %3u%% (%u/%u)%s%s",
99 progress->title, percent, n,
100 progress->total, tp, eol);
96a02f8f
NP
101 progress_update = 0;
102 return 1;
103 }
104 } else if (progress_update) {
cf84d51c 105 fprintf(stderr, "%s: %u%s%s", progress->title, n, tp, eol);
96a02f8f
NP
106 progress_update = 0;
107 return 1;
108 }
42e18fbf 109
96a02f8f
NP
110 return 0;
111}
112
cf84d51c
NP
113void display_throughput(struct progress *progress, unsigned long n)
114{
115 struct throughput *tp;
116 struct timeval tv;
117 unsigned int misecs;
118
119 if (!progress)
120 return;
121 tp = progress->throughput;
122
123 gettimeofday(&tv, NULL);
124
125 if (!tp) {
126 progress->throughput = tp = calloc(1, sizeof(*tp));
127 if (tp)
128 tp->prev_tv = tv;
129 return;
130 }
131
81f6654a 132 tp->total += n;
cf84d51c
NP
133 tp->count += n;
134
135 /*
136 * We have x = bytes and y = microsecs. We want z = KiB/s:
137 *
138 * z = (x / 1024) / (y / 1000000)
139 * z = x / y * 1000000 / 1024
140 * z = x / (y * 1024 / 1000000)
141 * z = x / y'
142 *
143 * To simplify things we'll keep track of misecs, or 1024th of a sec
144 * obtained with:
145 *
146 * y' = y * 1024 / 1000000
147 * y' = y / (1000000 / 1024)
148 * y' = y / 977
149 */
150 misecs = (tv.tv_sec - tp->prev_tv.tv_sec) * 1024;
151 misecs += (int)(tv.tv_usec - tp->prev_tv.tv_usec) / 977;
152
153 if (misecs > 512) {
81f6654a 154 int l = sizeof(tp->display);
cf84d51c
NP
155 tp->prev_tv = tv;
156 tp->avg_bytes += tp->count;
157 tp->avg_misecs += misecs;
81f6654a
NP
158
159 if (tp->total > 1 << 30) {
160 l -= snprintf(tp->display, l, ", %u.%2.2u GiB",
161 (int)(tp->total >> 30),
162 (int)(tp->total & ((1 << 30) - 1)) / 10737419);
163 } else if (tp->total > 1 << 20) {
164 l -= snprintf(tp->display, l, ", %u.%2.2u MiB",
165 (int)(tp->total >> 20),
166 ((int)(tp->total & ((1 << 20) - 1))
167 * 100) >> 20);
168 } else if (tp->total > 1 << 10) {
169 l -= snprintf(tp->display, l, ", %u.%2.2u KiB",
170 (int)(tp->total >> 10),
171 ((int)(tp->total & ((1 << 10) - 1))
172 * 100) >> 10);
173 } else {
174 l -= snprintf(tp->display, l, ", %u bytes",
175 (int)tp->total);
176 }
177 snprintf(tp->display + sizeof(tp->display) - l, l,
178 " | %lu KiB/s", tp->avg_bytes / tp->avg_misecs);
179
cf84d51c
NP
180 tp->avg_bytes -= tp->last_bytes[tp->idx];
181 tp->avg_misecs -= tp->last_misecs[tp->idx];
182 tp->last_bytes[tp->idx] = tp->count;
183 tp->last_misecs[tp->idx] = misecs;
184 tp->idx = (tp->idx + 1) % TP_IDX_MAX;
185 tp->count = 0;
3e935d19
NP
186
187 if (progress->last_value != -1 && progress_update)
188 display(progress, progress->last_value, 0);
cf84d51c
NP
189 }
190}
191
42e18fbf 192int display_progress(struct progress *progress, unsigned n)
96a02f8f 193{
dc6a0757 194 return progress ? display(progress, n, 0) : 0;
96a02f8f
NP
195}
196
dc6a0757
NP
197struct progress *start_progress_delay(const char *title, unsigned total,
198 unsigned percent_treshold, unsigned delay)
180a9f22 199{
dc6a0757
NP
200 struct progress *progress = malloc(sizeof(*progress));
201 if (!progress) {
202 /* unlikely, but here's a good fallback */
203 fprintf(stderr, "%s...\n", title);
204 return NULL;
205 }
42e18fbf 206 progress->title = title;
180a9f22 207 progress->total = total;
42e18fbf 208 progress->last_value = -1;
180a9f22
NP
209 progress->last_percent = -1;
210 progress->delayed_percent_treshold = percent_treshold;
180a9f22 211 progress->delay = delay;
cf84d51c 212 progress->throughput = NULL;
180a9f22 213 set_progress_signal();
dc6a0757 214 return progress;
180a9f22
NP
215}
216
dc6a0757 217struct progress *start_progress(const char *title, unsigned total)
42e18fbf 218{
dc6a0757 219 return start_progress_delay(title, total, 0, 0);
42e18fbf
NP
220}
221
dc6a0757 222void stop_progress(struct progress **p_progress)
96a02f8f 223{
dc6a0757
NP
224 struct progress *progress = *p_progress;
225 if (!progress)
226 return;
227 *p_progress = NULL;
42e18fbf
NP
228 if (progress->last_value != -1) {
229 /* Force the last update */
230 progress_update = 1;
231 display(progress, progress->last_value, 1);
232 }
96a02f8f 233 clear_progress_signal();
cf84d51c 234 free(progress->throughput);
dc6a0757 235 free(progress);
96a02f8f 236}