progressmeter.c 7.43 KB
Newer Older
1
/* $OpenBSD: progressmeter.c,v 1.40 2013/09/19 00:24:52 djm Exp $ */
2
/*
3
 * Copyright (c) 2003 Nils Nordman.  All rights reserved.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "includes.h"
27

28
#include <sys/types.h>
29
#include <sys/ioctl.h>
30
#include <sys/uio.h>
31

32
#include <errno.h>
33
#include <signal.h>
34
#include <stdio.h>
35
#include <string.h>
36
#include <time.h>
37
#include <unistd.h>
38

39
#include "progressmeter.h"
40
#include "atomicio.h"
41
#include "misc.h"
42

43 44 45 46 47
#define DEFAULT_WINSIZE 80
#define MAX_WINSIZE 512
#define PADDING 1		/* padding between the progress indicators */
#define UPDATE_INTERVAL 1	/* update the progress meter every second */
#define STALL_TIME 5		/* we're stalled after this many seconds */
48

49 50
/* determines whether we can output to the terminal */
static int can_output(void);
51

52 53 54
/* formats and inserts the specified size into the given buffer */
static void format_size(char *, int, off_t);
static void format_rate(char *, int, off_t);
55

56 57 58 59
/* window resizing */
static void sig_winch(int);
static void setscreensize(void);

60 61
/* updates the progressmeter to reflect the current state of the transfer */
void refresh_progress_meter(void);
62

63 64
/* signal handler for updating the progress meter */
static void update_progress_meter(int);
65

66 67 68
static time_t start;		/* start progress */
static time_t last_update;	/* last progress update */
static char *file;		/* name of the file being transferred */
69
static off_t start_pos;		/* initial position of transfer */
70 71
static off_t end_pos;		/* ending position of transfer */
static off_t cur_pos;		/* transfer position as of last refresh */
72
static volatile off_t *counter;	/* progress counter */
73 74 75
static long stalled;		/* how long we have been stalled */
static int bytes_per_second;	/* current speed in bytes per second */
static int win_size;		/* terminal window size */
76
static volatile sig_atomic_t win_resized; /* for window resizing */
77

78 79
/* units for format_size */
static const char unit[] = " KMGT";
80

81 82 83 84 85
static int
can_output(void)
{
	return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
}
86

87 88 89 90 91 92 93 94 95 96 97 98 99
static void
format_rate(char *buf, int size, off_t bytes)
{
	int i;

	bytes *= 100;
	for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++)
		bytes = (bytes + 512) / 1024;
	if (i == 0) {
		i++;
		bytes = (bytes + 512) / 1024;
	}
	snprintf(buf, size, "%3lld.%1lld%c%s",
100 101
	    (long long) (bytes + 5) / 100,
	    (long long) (bytes + 5) / 10 % 10,
102 103 104
	    unit[i],
	    i ? "B" : " ");
}
105

106 107 108 109 110 111 112 113
static void
format_size(char *buf, int size, off_t bytes)
{
	int i;

	for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++)
		bytes = (bytes + 512) / 1024;
	snprintf(buf, size, "%4lld%c%s",
114
	    (long long) bytes,
115 116 117
	    unit[i],
	    i ? "B" : " ");
}
118 119

void
120
refresh_progress_meter(void)
121
{
122 123 124 125 126
	char buf[MAX_WINSIZE + 1];
	time_t now;
	off_t transferred;
	double elapsed;
	int percent;
127
	off_t bytes_left;
128 129 130 131 132
	int cur_speed;
	int hours, minutes, seconds;
	int i, len;
	int file_len;

133
	transferred = *counter - (cur_pos ? cur_pos : start_pos);
134
	cur_pos = *counter;
135
	now = monotime();
136 137 138 139
	bytes_left = end_pos - cur_pos;

	if (bytes_left > 0)
		elapsed = now - last_update;
140
	else {
141
		elapsed = now - start;
142
		/* Calculate true total speed when done */
143
		transferred = end_pos - start_pos;
144 145
		bytes_per_second = 0;
	}
146

147 148 149 150
	/* calculate speed */
	if (elapsed != 0)
		cur_speed = (transferred / elapsed);
	else
151
		cur_speed = transferred;
152

153 154 155 156 157 158 159 160 161 162 163
#define AGE_FACTOR 0.9
	if (bytes_per_second != 0) {
		bytes_per_second = (bytes_per_second * AGE_FACTOR) +
		    (cur_speed * (1.0 - AGE_FACTOR));
	} else
		bytes_per_second = cur_speed;

	/* filename */
	buf[0] = '\0';
	file_len = win_size - 35;
	if (file_len > 0) {
164
		len = snprintf(buf, file_len + 1, "\r%s", file);
165 166
		if (len < 0)
			len = 0;
167 168
		if (len >= file_len + 1)
			len = file_len;
169
		for (i = len; i < file_len; i++)
170 171 172
			buf[i] = ' ';
		buf[file_len] = '\0';
	}
173

174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
	/* percent of transfer done */
	if (end_pos != 0)
		percent = ((float)cur_pos / end_pos) * 100;
	else
		percent = 100;
	snprintf(buf + strlen(buf), win_size - strlen(buf),
	    " %3d%% ", percent);

	/* amount transferred */
	format_size(buf + strlen(buf), win_size - strlen(buf),
	    cur_pos);
	strlcat(buf, " ", win_size);

	/* bandwidth usage */
	format_rate(buf + strlen(buf), win_size - strlen(buf),
189
	    (off_t)bytes_per_second);
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
	strlcat(buf, "/s ", win_size);

	/* ETA */
	if (!transferred)
		stalled += elapsed;
	else
		stalled = 0;

	if (stalled >= STALL_TIME)
		strlcat(buf, "- stalled -", win_size);
	else if (bytes_per_second == 0 && bytes_left)
		strlcat(buf, "  --:-- ETA", win_size);
	else {
		if (bytes_left > 0)
			seconds = bytes_left / bytes_per_second;
		else
			seconds = elapsed;

		hours = seconds / 3600;
		seconds -= hours * 3600;
		minutes = seconds / 60;
		seconds -= minutes * 60;

		if (hours != 0)
			snprintf(buf + strlen(buf), win_size - strlen(buf),
			    "%d:%02d:%02d", hours, minutes, seconds);
		else
			snprintf(buf + strlen(buf), win_size - strlen(buf),
			    "  %02d:%02d", minutes, seconds);

		if (bytes_left > 0)
			strlcat(buf, " ETA", win_size);
		else
			strlcat(buf, "    ", win_size);
	}

226
	atomicio(vwrite, STDOUT_FILENO, buf, win_size - 1);
227
	last_update = now;
228 229
}

230
/*ARGSUSED*/
231 232 233
static void
update_progress_meter(int ignore)
{
234 235 236 237
	int save_errno;

	save_errno = errno;

238 239 240 241
	if (win_resized) {
		setscreensize();
		win_resized = 0;
	}
242 243
	if (can_output())
		refresh_progress_meter();
244

245
	signal(SIGALRM, update_progress_meter);
246
	alarm(UPDATE_INTERVAL);
247 248 249
	errno = save_errno;
}

250
void
251
start_progress_meter(char *f, off_t filesize, off_t *ctr)
252
{
253
	start = last_update = monotime();
254
	file = f;
255
	start_pos = *ctr;
256 257
	end_pos = filesize;
	cur_pos = 0;
258
	counter = ctr;
259 260 261
	stalled = 0;
	bytes_per_second = 0;

262
	setscreensize();
263 264
	if (can_output())
		refresh_progress_meter();
265

266
	signal(SIGALRM, update_progress_meter);
267
	signal(SIGWINCH, sig_winch);
268
	alarm(UPDATE_INTERVAL);
269 270
}

271 272
void
stop_progress_meter(void)
273
{
274
	alarm(0);
275

276 277 278 279 280 281 282 283
	if (!can_output())
		return;

	/* Ensure we complete the progress */
	if (cur_pos != end_pos)
		refresh_progress_meter();

	atomicio(vwrite, STDOUT_FILENO, "\n", 1);
284
}
285

286
/*ARGSUSED*/
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
static void
sig_winch(int sig)
{
	win_resized = 1;
}

static void
setscreensize(void)
{
	struct winsize winsize;

	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
	    winsize.ws_col != 0) {
		if (winsize.ws_col > MAX_WINSIZE)
			win_size = MAX_WINSIZE;
		else
			win_size = winsize.ws_col;
	} else
		win_size = DEFAULT_WINSIZE;
	win_size += 1;					/* trailing \0 */
}