progress.c 6.3 KB
Newer Older
1 2 3
/*
 * Simple text-based progress display module for GIT
 *
4
 * Copyright (c) 2007 by Nicolas Pitre <nico@fluxnic.net>
5 6 7 8 9 10
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

11
#include "git-compat-util.h"
12
#include "gettext.h"
13
#include "progress.h"
14
#include "strbuf.h"
15
#include "trace.h"
16

17 18 19
#define TP_IDX_MAX      8

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 27
	unsigned int last_misecs[TP_IDX_MAX];
	unsigned int idx;
28
	struct strbuf display;
29 30
};

31 32 33 34 35 36 37
struct progress {
	const char *title;
	int last_value;
	unsigned total;
	unsigned last_percent;
	unsigned delay;
	unsigned delayed_percent_treshold;
38
	struct throughput *throughput;
39 40
};

41 42 43 44 45 46 47 48 49 50 51 52
static volatile sig_atomic_t progress_update;

static void progress_interval(int signum)
{
	progress_update = 1;
}

static void set_progress_signal(void)
{
	struct sigaction sa;
	struct itimerval v;

53 54
	progress_update = 0;

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
	memset(&sa, 0, sizeof(sa));
	sa.sa_handler = progress_interval;
	sigemptyset(&sa.sa_mask);
	sa.sa_flags = SA_RESTART;
	sigaction(SIGALRM, &sa, NULL);

	v.it_interval.tv_sec = 1;
	v.it_interval.tv_usec = 0;
	v.it_value = v.it_interval;
	setitimer(ITIMER_REAL, &v, NULL);
}

static void clear_progress_signal(void)
{
	struct itimerval v = {{0,},};
	setitimer(ITIMER_REAL, &v, NULL);
	signal(SIGALRM, SIG_IGN);
	progress_update = 0;
}

75 76
static int is_foreground_fd(int fd)
{
77 78
	int tpgrp = tcgetpgrp(fd);
	return tpgrp < 0 || tpgrp == getpgid(0);
79 80
}

81
static int display(struct progress *progress, unsigned n, const char *done)
82
{
83
	const char *eol, *tp;
84

85 86 87 88 89 90 91 92 93 94 95 96 97 98
	if (progress->delay) {
		if (!progress_update || --progress->delay)
			return 0;
		if (progress->total) {
			unsigned percent = n * 100 / progress->total;
			if (percent > progress->delayed_percent_treshold) {
				/* inhibit this progress report entirely */
				clear_progress_signal();
				progress->delay = -1;
				progress->total = 0;
				return 0;
			}
		}
	}
99 100

	progress->last_value = n;
101
	tp = (progress->throughput) ? progress->throughput->display.buf : "";
102
	eol = done ? done : "   \r";
103 104 105 106
	if (progress->total) {
		unsigned percent = n * 100 / progress->total;
		if (percent != progress->last_percent || progress_update) {
			progress->last_percent = percent;
107 108 109 110 111 112
			if (is_foreground_fd(fileno(stderr)) || done) {
				fprintf(stderr, "%s: %3u%% (%u/%u)%s%s",
					progress->title, percent, n,
					progress->total, tp, eol);
				fflush(stderr);
			}
113 114 115 116
			progress_update = 0;
			return 1;
		}
	} else if (progress_update) {
117 118 119 120 121
		if (is_foreground_fd(fileno(stderr)) || done) {
			fprintf(stderr, "%s: %u%s%s",
				progress->title, n, tp, eol);
			fflush(stderr);
		}
122 123 124
		progress_update = 0;
		return 1;
	}
125

126 127 128
	return 0;
}

129
static void throughput_string(struct strbuf *buf, off_t total,
130 131
			      unsigned int rate)
{
132
	strbuf_reset(buf);
133 134 135 136 137
	strbuf_addstr(buf, ", ");
	strbuf_humanise_bytes(buf, total);
	strbuf_addstr(buf, " | ");
	strbuf_humanise_bytes(buf, rate * 1024);
	strbuf_addstr(buf, "/s");
138 139
}

140
void display_throughput(struct progress *progress, off_t total)
141 142
{
	struct throughput *tp;
143 144
	uint64_t now_ns;
	unsigned int misecs, count, rate;
145 146 147 148 149

	if (!progress)
		return;
	tp = progress->throughput;

150
	now_ns = getnanotime();
151 152 153

	if (!tp) {
		progress->throughput = tp = calloc(1, sizeof(*tp));
154
		if (tp) {
155
			tp->prev_total = tp->curr_total = total;
156
			tp->prev_ns = now_ns;
157
			strbuf_init(&tp->display, 0);
158
		}
159 160
		return;
	}
161
	tp->curr_total = total;
162

163 164 165 166
	/* only update throughput every 0.5 s */
	if (now_ns - tp->prev_ns <= 500000000)
		return;

167
	/*
168
	 * We have x = bytes and y = nanosecs.  We want z = KiB/s:
169
	 *
170 171 172
	 *	z = (x / 1024) / (y / 1000000000)
	 *	z = x / y * 1000000000 / 1024
	 *	z = x / (y * 1024 / 1000000000)
173 174 175 176 177
	 *	z = x / y'
	 *
	 * To simplify things we'll keep track of misecs, or 1024th of a sec
	 * obtained with:
	 *
178 179 180 181
	 *	y' = y * 1024 / 1000000000
	 *	y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
	 *	y' = y / 2^32 * 4398
	 *	y' = (y * 4398) >> 32
182
	 */
183 184 185 186 187 188 189 190 191 192 193 194 195 196
	misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;

	count = total - tp->prev_total;
	tp->prev_total = total;
	tp->prev_ns = now_ns;
	tp->avg_bytes += count;
	tp->avg_misecs += misecs;
	rate = tp->avg_bytes / tp->avg_misecs;
	tp->avg_bytes -= tp->last_bytes[tp->idx];
	tp->avg_misecs -= tp->last_misecs[tp->idx];
	tp->last_bytes[tp->idx] = count;
	tp->last_misecs[tp->idx] = misecs;
	tp->idx = (tp->idx + 1) % TP_IDX_MAX;

197
	throughput_string(&tp->display, total, rate);
198 199
	if (progress->last_value != -1 && progress_update)
		display(progress, progress->last_value, NULL);
200 201
}

202
int display_progress(struct progress *progress, unsigned n)
203
{
204
	return progress ? display(progress, n, NULL) : 0;
205 206
}

207 208
struct progress *start_progress_delay(const char *title, unsigned total,
				       unsigned percent_treshold, unsigned delay)
209
{
210 211 212 213
	struct progress *progress = malloc(sizeof(*progress));
	if (!progress) {
		/* unlikely, but here's a good fallback */
		fprintf(stderr, "%s...\n", title);
214
		fflush(stderr);
215 216
		return NULL;
	}
217
	progress->title = title;
218
	progress->total = total;
219
	progress->last_value = -1;
220 221 222
	progress->last_percent = -1;
	progress->delayed_percent_treshold = percent_treshold;
	progress->delay = delay;
223
	progress->throughput = NULL;
224
	set_progress_signal();
225
	return progress;
226 227
}

228
struct progress *start_progress(const char *title, unsigned total)
229
{
230
	return start_progress_delay(title, total, 0, 0);
231 232
}

233
void stop_progress(struct progress **p_progress)
234
{
235
	stop_progress_msg(p_progress, _("done"));
236 237 238
}

void stop_progress_msg(struct progress **p_progress, const char *msg)
239
{
240 241 242 243
	struct progress *progress = *p_progress;
	if (!progress)
		return;
	*p_progress = NULL;
244 245
	if (progress->last_value != -1) {
		/* Force the last update */
246 247
		char buf[128], *bufp;
		size_t len = strlen(msg) + 5;
248
		struct throughput *tp = progress->throughput;
249

250
		bufp = (len < sizeof(buf)) ? buf : xmallocz(len);
251 252 253
		if (tp) {
			unsigned int rate = !tp->avg_misecs ? 0 :
					tp->avg_bytes / tp->avg_misecs;
254
			throughput_string(&tp->display, tp->curr_total, rate);
255
		}
256
		progress_update = 1;
257
		xsnprintf(bufp, len + 1, ", %s.\n", msg);
258 259 260
		display(progress, progress->last_value, bufp);
		if (buf != bufp)
			free(bufp);
261
	}
262
	clear_progress_signal();
263 264
	if (progress->throughput)
		strbuf_release(&progress->throughput->display);
265
	free(progress->throughput);
266
	free(progress);
267
}