Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 1 | /* |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 2 | * Copyright (c) 2003 Nils Nordman. All rights reserved. |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 14 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 15 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 16 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 17 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 18 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 19 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 20 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 22 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | */ |
| 24 | |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 25 | #include "includes.h" |
Damien Miller | 8c5e91c | 2003-11-21 23:09:10 +1100 | [diff] [blame] | 26 | RCSID("$OpenBSD: progressmeter.c,v 1.17 2003/11/20 11:39:28 markus Exp $"); |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 27 | |
Damien Miller | 71a5141 | 2003-01-14 22:23:23 +1100 | [diff] [blame] | 28 | #include "progressmeter.h" |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 29 | #include "atomicio.h" |
Damien Miller | 0cbb9de | 2003-06-04 22:56:15 +1000 | [diff] [blame] | 30 | #include "misc.h" |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 31 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 32 | #define DEFAULT_WINSIZE 80 |
| 33 | #define MAX_WINSIZE 512 |
| 34 | #define PADDING 1 /* padding between the progress indicators */ |
| 35 | #define UPDATE_INTERVAL 1 /* update the progress meter every second */ |
| 36 | #define STALL_TIME 5 /* we're stalled after this many seconds */ |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 37 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 38 | /* determines whether we can output to the terminal */ |
| 39 | static int can_output(void); |
| 40 | |
| 41 | /* formats and inserts the specified size into the given buffer */ |
| 42 | static void format_size(char *, int, off_t); |
| 43 | static void format_rate(char *, int, off_t); |
| 44 | |
| 45 | /* updates the progressmeter to reflect the current state of the transfer */ |
| 46 | void refresh_progress_meter(void); |
| 47 | |
| 48 | /* signal handler for updating the progress meter */ |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 49 | static void update_progress_meter(int); |
| 50 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 51 | static time_t start; /* start progress */ |
| 52 | static time_t last_update; /* last progress update */ |
| 53 | static char *file; /* name of the file being transferred */ |
| 54 | static off_t end_pos; /* ending position of transfer */ |
| 55 | static off_t cur_pos; /* transfer position as of last refresh */ |
| 56 | static volatile off_t *counter; /* progress counter */ |
| 57 | static long stalled; /* how long we have been stalled */ |
| 58 | static int bytes_per_second; /* current speed in bytes per second */ |
| 59 | static int win_size; /* terminal window size */ |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 60 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 61 | /* units for format_size */ |
| 62 | static const char unit[] = " KMGT"; |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 63 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 64 | static int |
| 65 | can_output(void) |
| 66 | { |
| 67 | return (getpgrp() == tcgetpgrp(STDOUT_FILENO)); |
| 68 | } |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 69 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 70 | static void |
| 71 | format_rate(char *buf, int size, off_t bytes) |
| 72 | { |
| 73 | int i; |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 74 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 75 | bytes *= 100; |
| 76 | for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++) |
| 77 | bytes = (bytes + 512) / 1024; |
| 78 | if (i == 0) { |
| 79 | i++; |
| 80 | bytes = (bytes + 512) / 1024; |
| 81 | } |
| 82 | snprintf(buf, size, "%3lld.%1lld%c%s", |
Damien Miller | 8c5e91c | 2003-11-21 23:09:10 +1100 | [diff] [blame] | 83 | (int64_t) (bytes + 5) / 100, |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 84 | (int64_t) (bytes + 5) / 10 % 10, |
| 85 | unit[i], |
| 86 | i ? "B" : " "); |
| 87 | } |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 88 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 89 | static void |
| 90 | format_size(char *buf, int size, off_t bytes) |
| 91 | { |
| 92 | int i; |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 93 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 94 | for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++) |
| 95 | bytes = (bytes + 512) / 1024; |
| 96 | snprintf(buf, size, "%4lld%c%s", |
| 97 | (int64_t) bytes, |
| 98 | unit[i], |
| 99 | i ? "B" : " "); |
| 100 | } |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 101 | |
| 102 | void |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 103 | refresh_progress_meter(void) |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 104 | { |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 105 | char buf[MAX_WINSIZE + 1]; |
| 106 | time_t now; |
| 107 | off_t transferred; |
| 108 | double elapsed; |
| 109 | int percent; |
| 110 | int bytes_left; |
| 111 | int cur_speed; |
| 112 | int hours, minutes, seconds; |
| 113 | int i, len; |
| 114 | int file_len; |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 115 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 116 | transferred = *counter - cur_pos; |
| 117 | cur_pos = *counter; |
| 118 | now = time(NULL); |
| 119 | bytes_left = end_pos - cur_pos; |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 120 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 121 | if (bytes_left > 0) |
| 122 | elapsed = now - last_update; |
| 123 | else |
| 124 | elapsed = now - start; |
| 125 | |
| 126 | /* calculate speed */ |
| 127 | if (elapsed != 0) |
| 128 | cur_speed = (transferred / elapsed); |
| 129 | else |
| 130 | cur_speed = 0; |
| 131 | |
| 132 | #define AGE_FACTOR 0.9 |
| 133 | if (bytes_per_second != 0) { |
| 134 | bytes_per_second = (bytes_per_second * AGE_FACTOR) + |
| 135 | (cur_speed * (1.0 - AGE_FACTOR)); |
| 136 | } else |
| 137 | bytes_per_second = cur_speed; |
| 138 | |
| 139 | /* filename */ |
| 140 | buf[0] = '\0'; |
| 141 | file_len = win_size - 35; |
| 142 | if (file_len > 0) { |
Damien Miller | 17af176 | 2003-09-02 22:53:01 +1000 | [diff] [blame] | 143 | len = snprintf(buf, file_len + 1, "\r%s", file); |
Damien Miller | b5829f5 | 2003-09-02 22:53:32 +1000 | [diff] [blame] | 144 | if (len < 0) |
| 145 | len = 0; |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 146 | for (i = len; i < file_len; i++ ) |
| 147 | buf[i] = ' '; |
| 148 | buf[file_len] = '\0'; |
| 149 | } |
| 150 | |
| 151 | /* percent of transfer done */ |
| 152 | if (end_pos != 0) |
| 153 | percent = ((float)cur_pos / end_pos) * 100; |
| 154 | else |
| 155 | percent = 100; |
| 156 | snprintf(buf + strlen(buf), win_size - strlen(buf), |
| 157 | " %3d%% ", percent); |
| 158 | |
| 159 | /* amount transferred */ |
| 160 | format_size(buf + strlen(buf), win_size - strlen(buf), |
| 161 | cur_pos); |
| 162 | strlcat(buf, " ", win_size); |
| 163 | |
| 164 | /* bandwidth usage */ |
| 165 | format_rate(buf + strlen(buf), win_size - strlen(buf), |
| 166 | bytes_per_second); |
| 167 | strlcat(buf, "/s ", win_size); |
| 168 | |
| 169 | /* ETA */ |
| 170 | if (!transferred) |
| 171 | stalled += elapsed; |
| 172 | else |
| 173 | stalled = 0; |
| 174 | |
| 175 | if (stalled >= STALL_TIME) |
| 176 | strlcat(buf, "- stalled -", win_size); |
| 177 | else if (bytes_per_second == 0 && bytes_left) |
| 178 | strlcat(buf, " --:-- ETA", win_size); |
| 179 | else { |
| 180 | if (bytes_left > 0) |
| 181 | seconds = bytes_left / bytes_per_second; |
| 182 | else |
| 183 | seconds = elapsed; |
| 184 | |
| 185 | hours = seconds / 3600; |
| 186 | seconds -= hours * 3600; |
| 187 | minutes = seconds / 60; |
| 188 | seconds -= minutes * 60; |
| 189 | |
| 190 | if (hours != 0) |
| 191 | snprintf(buf + strlen(buf), win_size - strlen(buf), |
| 192 | "%d:%02d:%02d", hours, minutes, seconds); |
| 193 | else |
| 194 | snprintf(buf + strlen(buf), win_size - strlen(buf), |
| 195 | " %02d:%02d", minutes, seconds); |
| 196 | |
| 197 | if (bytes_left > 0) |
| 198 | strlcat(buf, " ETA", win_size); |
| 199 | else |
| 200 | strlcat(buf, " ", win_size); |
| 201 | } |
| 202 | |
Darren Tucker | 6cc310b | 2003-10-02 16:15:15 +1000 | [diff] [blame] | 203 | atomicio(vwrite, STDOUT_FILENO, buf, win_size - 1); |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 204 | last_update = now; |
| 205 | } |
| 206 | |
| 207 | static void |
| 208 | update_progress_meter(int ignore) |
| 209 | { |
| 210 | int save_errno; |
| 211 | |
| 212 | save_errno = errno; |
| 213 | |
| 214 | if (can_output()) |
| 215 | refresh_progress_meter(); |
| 216 | |
Ben Lindstrom | 5ade9ab | 2003-08-25 01:16:21 +0000 | [diff] [blame] | 217 | signal(SIGALRM, update_progress_meter); |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 218 | alarm(UPDATE_INTERVAL); |
| 219 | errno = save_errno; |
| 220 | } |
| 221 | |
| 222 | void |
| 223 | start_progress_meter(char *f, off_t filesize, off_t *stat) |
| 224 | { |
| 225 | struct winsize winsize; |
| 226 | |
| 227 | start = last_update = time(NULL); |
| 228 | file = f; |
| 229 | end_pos = filesize; |
| 230 | cur_pos = 0; |
| 231 | counter = stat; |
| 232 | stalled = 0; |
| 233 | bytes_per_second = 0; |
| 234 | |
| 235 | if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 && |
| 236 | winsize.ws_col != 0) { |
| 237 | if (winsize.ws_col > MAX_WINSIZE) |
| 238 | win_size = MAX_WINSIZE; |
| 239 | else |
| 240 | win_size = winsize.ws_col; |
| 241 | } else |
| 242 | win_size = DEFAULT_WINSIZE; |
| 243 | win_size += 1; /* trailing \0 */ |
| 244 | |
| 245 | if (can_output()) |
| 246 | refresh_progress_meter(); |
| 247 | |
Ben Lindstrom | 5ade9ab | 2003-08-25 01:16:21 +0000 | [diff] [blame] | 248 | signal(SIGALRM, update_progress_meter); |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 249 | alarm(UPDATE_INTERVAL); |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void |
Damien Miller | 2b92d32 | 2003-06-11 22:05:06 +1000 | [diff] [blame] | 253 | stop_progress_meter(void) |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 254 | { |
| 255 | alarm(0); |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 256 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 257 | if (!can_output()) |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 258 | return; |
| 259 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 260 | /* Ensure we complete the progress */ |
| 261 | if (cur_pos != end_pos) |
| 262 | refresh_progress_meter(); |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 263 | |
Darren Tucker | 06ef75b | 2003-08-02 23:28:38 +1000 | [diff] [blame] | 264 | atomicio(vwrite, STDOUT_FILENO, "\n", 1); |
Damien Miller | 6fd00e0 | 2003-01-10 21:46:02 +1100 | [diff] [blame] | 265 | } |