progressmeter.c revision 124287
12311Sjkh/* 22311Sjkh * Copyright (c) 2003 Nils Nordman. All rights reserved. 32311Sjkh * 42311Sjkh * Redistribution and use in source and binary forms, with or without 52311Sjkh * modification, are permitted provided that the following conditions 62311Sjkh * are met: 72311Sjkh * 1. Redistributions of source code must retain the above copyright 82311Sjkh * notice, this list of conditions and the following disclaimer. 92311Sjkh * 2. Redistributions in binary form must reproduce the above copyright 102311Sjkh * notice, this list of conditions and the following disclaimer in the 112311Sjkh * documentation and/or other materials provided with the distribution. 122311Sjkh * 132311Sjkh * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 142311Sjkh * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 152311Sjkh * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 165176Sache * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 172311Sjkh * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 182311Sjkh * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 192311Sjkh * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 208857Srgrimes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 212311Sjkh * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 222311Sjkh * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 232311Sjkh */ 242311Sjkh 252311Sjkh#include "includes.h" 262311SjkhRCSID("$OpenBSD: progressmeter.c,v 1.15 2003/08/31 12:14:22 markus Exp $"); 272311Sjkh 282311Sjkh#include "progressmeter.h" 292311Sjkh#include "atomicio.h" 302311Sjkh#include "misc.h" 312311Sjkh 322311Sjkh#define DEFAULT_WINSIZE 80 332311Sjkh#define MAX_WINSIZE 512 342311Sjkh#define PADDING 1 /* padding between the progress indicators */ 352311Sjkh#define UPDATE_INTERVAL 1 /* update the progress meter every second */ 362311Sjkh#define STALL_TIME 5 /* we're stalled after this many seconds */ 372311Sjkh 382311Sjkh/* determines whether we can output to the terminal */ 392311Sjkhstatic int can_output(void); 402311Sjkh 412311Sjkh/* formats and inserts the specified size into the given buffer */ 422311Sjkhstatic void format_size(char *, int, off_t); 432311Sjkhstatic void format_rate(char *, int, off_t); 442311Sjkh 452311Sjkh/* updates the progressmeter to reflect the current state of the transfer */ 462311Sjkhvoid refresh_progress_meter(void); 472311Sjkh 482311Sjkh/* signal handler for updating the progress meter */ 492311Sjkhstatic void update_progress_meter(int); 502311Sjkh 512311Sjkhstatic time_t start; /* start progress */ 522311Sjkhstatic time_t last_update; /* last progress update */ 532311Sjkhstatic char *file; /* name of the file being transferred */ 542311Sjkhstatic off_t end_pos; /* ending position of transfer */ 552311Sjkhstatic off_t cur_pos; /* transfer position as of last refresh */ 562311Sjkhstatic volatile off_t *counter; /* progress counter */ 572311Sjkhstatic long stalled; /* how long we have been stalled */ 582311Sjkhstatic int bytes_per_second; /* current speed in bytes per second */ 592311Sjkhstatic int win_size; /* terminal window size */ 602311Sjkh 612311Sjkh/* units for format_size */ 622311Sjkhstatic const char unit[] = " KMGT"; 632311Sjkh 642311Sjkhstatic int 652311Sjkhcan_output(void) 662311Sjkh{ 672311Sjkh return (getpgrp() == tcgetpgrp(STDOUT_FILENO)); 682311Sjkh} 692311Sjkh 702311Sjkhstatic void 712311Sjkhformat_rate(char *buf, int size, off_t bytes) 722311Sjkh{ 732311Sjkh int i; 742311Sjkh 752311Sjkh bytes *= 100; 762311Sjkh for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++) 772311Sjkh bytes = (bytes + 512) / 1024; 782311Sjkh if (i == 0) { 792311Sjkh i++; 802311Sjkh bytes = (bytes + 512) / 1024; 812311Sjkh } 822311Sjkh snprintf(buf, size, "%3lld.%1lld%c%s", 832311Sjkh (int64_t) bytes / 100, 842311Sjkh (int64_t) (bytes + 5) / 10 % 10, 852311Sjkh unit[i], 862311Sjkh i ? "B" : " "); 872311Sjkh} 882311Sjkh 892311Sjkhstatic void 902311Sjkhformat_size(char *buf, int size, off_t bytes) 912311Sjkh{ 922311Sjkh int i; 932311Sjkh 942311Sjkh for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++) 952311Sjkh bytes = (bytes + 512) / 1024; 962311Sjkh snprintf(buf, size, "%4lld%c%s", 972311Sjkh (int64_t) bytes, 982311Sjkh unit[i], 992311Sjkh i ? "B" : " "); 1002311Sjkh} 1012311Sjkh 1022311Sjkhvoid 1032311Sjkhrefresh_progress_meter(void) 1042311Sjkh{ 1052311Sjkh char buf[MAX_WINSIZE + 1]; 1062311Sjkh time_t now; 1072311Sjkh off_t transferred; 1082311Sjkh double elapsed; 1092311Sjkh int percent; 1102311Sjkh int bytes_left; 1112311Sjkh int cur_speed; 1122311Sjkh int hours, minutes, seconds; 1132311Sjkh int i, len; 1142311Sjkh int file_len; 1152311Sjkh 1162311Sjkh transferred = *counter - cur_pos; 1172311Sjkh cur_pos = *counter; 1182311Sjkh now = time(NULL); 1192311Sjkh bytes_left = end_pos - cur_pos; 1202311Sjkh 1212311Sjkh if (bytes_left > 0) 1222311Sjkh elapsed = now - last_update; 1232311Sjkh else 1242311Sjkh elapsed = now - start; 1252311Sjkh 1262311Sjkh /* calculate speed */ 1272311Sjkh if (elapsed != 0) 1282311Sjkh cur_speed = (transferred / elapsed); 1292311Sjkh else 1302311Sjkh cur_speed = 0; 1312311Sjkh 1322311Sjkh#define AGE_FACTOR 0.9 1338857Srgrimes if (bytes_per_second != 0) { 1342311Sjkh bytes_per_second = (bytes_per_second * AGE_FACTOR) + 1352311Sjkh (cur_speed * (1.0 - AGE_FACTOR)); 1362311Sjkh } else 1372311Sjkh bytes_per_second = cur_speed; 1382311Sjkh 1392311Sjkh /* filename */ 1402311Sjkh buf[0] = '\0'; 1412311Sjkh file_len = win_size - 35; 1422311Sjkh if (file_len > 0) { 1432311Sjkh len = snprintf(buf, file_len + 1, "\r%s", file); 1442311Sjkh if (len < 0) 1452311Sjkh len = 0; 1462311Sjkh for (i = len; i < file_len; i++ ) 1472311Sjkh buf[i] = ' '; 1482311Sjkh buf[file_len] = '\0'; 1492311Sjkh } 1502311Sjkh 1512311Sjkh /* percent of transfer done */ 1522311Sjkh if (end_pos != 0) 1532311Sjkh percent = ((float)cur_pos / end_pos) * 100; 1542311Sjkh else 1552311Sjkh percent = 100; 1562311Sjkh snprintf(buf + strlen(buf), win_size - strlen(buf), 1572311Sjkh " %3d%% ", percent); 1582311Sjkh 1592311Sjkh /* amount transferred */ 1602311Sjkh format_size(buf + strlen(buf), win_size - strlen(buf), 1612311Sjkh cur_pos); 1622311Sjkh strlcat(buf, " ", win_size); 1632311Sjkh 1642311Sjkh /* bandwidth usage */ 1652311Sjkh format_rate(buf + strlen(buf), win_size - strlen(buf), 1662311Sjkh bytes_per_second); 1672311Sjkh strlcat(buf, "/s ", win_size); 1682311Sjkh 1692311Sjkh /* ETA */ 1702311Sjkh if (!transferred) 1712311Sjkh stalled += elapsed; 1722311Sjkh else 1732311Sjkh stalled = 0; 1742311Sjkh 1752311Sjkh if (stalled >= STALL_TIME) 1762311Sjkh strlcat(buf, "- stalled -", win_size); 1772311Sjkh else if (bytes_per_second == 0 && bytes_left) 1782311Sjkh strlcat(buf, " --:-- ETA", win_size); 1792311Sjkh else { 1802311Sjkh if (bytes_left > 0) 1812311Sjkh seconds = bytes_left / bytes_per_second; 1822311Sjkh else 1832311Sjkh seconds = elapsed; 1842311Sjkh 1852311Sjkh hours = seconds / 3600; 1862311Sjkh seconds -= hours * 3600; 1872311Sjkh minutes = seconds / 60; 1882311Sjkh seconds -= minutes * 60; 1892311Sjkh 1902311Sjkh if (hours != 0) 1912311Sjkh snprintf(buf + strlen(buf), win_size - strlen(buf), 1922311Sjkh "%d:%02d:%02d", hours, minutes, seconds); 1932311Sjkh else 1942311Sjkh snprintf(buf + strlen(buf), win_size - strlen(buf), 1952311Sjkh " %02d:%02d", minutes, seconds); 1962311Sjkh 1972311Sjkh if (bytes_left > 0) 1982311Sjkh strlcat(buf, " ETA", win_size); 1992311Sjkh else 2002311Sjkh strlcat(buf, " ", win_size); 2012311Sjkh } 2022311Sjkh 2032311Sjkh atomicio(vwrite, STDOUT_FILENO, buf, win_size - 1); 2042311Sjkh last_update = now; 2052311Sjkh} 2062311Sjkh 2072311Sjkhstatic void 2082311Sjkhupdate_progress_meter(int ignore) 2092311Sjkh{ 2102311Sjkh int save_errno; 2112311Sjkh 2122311Sjkh save_errno = errno; 2132311Sjkh 2142311Sjkh if (can_output()) 2152311Sjkh refresh_progress_meter(); 2162311Sjkh 2172311Sjkh signal(SIGALRM, update_progress_meter); 2182311Sjkh alarm(UPDATE_INTERVAL); 2192311Sjkh errno = save_errno; 2202311Sjkh} 2212311Sjkh 2222311Sjkhvoid 2232311Sjkhstart_progress_meter(char *f, off_t filesize, off_t *stat) 2242311Sjkh{ 2252311Sjkh struct winsize winsize; 2262311Sjkh 2272311Sjkh start = last_update = time(NULL); 2282311Sjkh file = f; 2292311Sjkh end_pos = filesize; 2302311Sjkh cur_pos = 0; 2312311Sjkh counter = stat; 2322311Sjkh stalled = 0; 2332311Sjkh bytes_per_second = 0; 2342311Sjkh 2352311Sjkh if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 && 2362311Sjkh winsize.ws_col != 0) { 2372311Sjkh if (winsize.ws_col > MAX_WINSIZE) 2382311Sjkh win_size = MAX_WINSIZE; 2392311Sjkh else 2402311Sjkh win_size = winsize.ws_col; 2412311Sjkh } else 2422311Sjkh win_size = DEFAULT_WINSIZE; 2432311Sjkh win_size += 1; /* trailing \0 */ 2442311Sjkh 2452311Sjkh if (can_output()) 2462311Sjkh refresh_progress_meter(); 2472311Sjkh 2482311Sjkh signal(SIGALRM, update_progress_meter); 2492311Sjkh alarm(UPDATE_INTERVAL); 2502311Sjkh} 2512311Sjkh 2522311Sjkhvoid 2532311Sjkhstop_progress_meter(void) 2542311Sjkh{ 2552311Sjkh alarm(0); 2562311Sjkh 2572311Sjkh if (!can_output()) 2582311Sjkh return; 2592311Sjkh 2602311Sjkh /* Ensure we complete the progress */ 2612311Sjkh if (cur_pos != end_pos) 2622311Sjkh refresh_progress_meter(); 2632311Sjkh 2642311Sjkh atomicio(vwrite, STDOUT_FILENO, "\n", 1); 2652311Sjkh} 2662311Sjkh