1 /* $OpenBSD: progressmeter.c,v 1.54 2024/09/22 12:56:21 jsg Exp $ */
3 * Copyright (c) 2003 Nils Nordman. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/types.h>
29 #include <sys/ioctl.h>
42 #include "progressmeter.h"
47 #define DEFAULT_WINSIZE 80
48 #define MAX_WINSIZE 512
49 #define UPDATE_INTERVAL 1 /* update the progress meter every second */
50 #define STALL_TIME 5 /* we're stalled after this many seconds */
52 /* determines whether we can output to the terminal */
53 static int can_output(void);
56 static void sig_winch(int);
57 static void setscreensize(void);
59 /* signal handler for updating the progress meter */
60 static void sig_alarm(int);
62 static double start
; /* start progress */
63 static double last_update
; /* last progress update */
64 static const char *file
; /* name of the file being transferred */
65 static off_t start_pos
; /* initial position of transfer */
66 static off_t end_pos
; /* ending position of transfer */
67 static off_t cur_pos
; /* transfer position as of last refresh */
68 static volatile off_t
*counter
; /* progress counter */
69 static long stalled
; /* how long we have been stalled */
70 static int bytes_per_second
; /* current speed in bytes per second */
71 static int win_size
; /* terminal window size */
72 static volatile sig_atomic_t win_resized
; /* for window resizing */
73 static volatile sig_atomic_t alarm_fired
;
75 /* units for format_size */
76 static const char unit
[] = " KMGT";
81 return (getpgrp() == tcgetpgrp(STDOUT_FILENO
));
84 /* size needed to format integer type v, using (nbits(v) * log2(10) / 10) */
85 #define STRING_SIZE(v) (((sizeof(v) * 8 * 4) / 10) + 1)
88 format_rate(off_t bytes
)
91 static char buf
[STRING_SIZE(bytes
) * 2 + 16];
94 for (i
= 0; bytes
>= 100*1000 && unit
[i
] != 'T'; i
++)
95 bytes
= (bytes
+ 512) / 1024;
98 bytes
= (bytes
+ 512) / 1024;
100 snprintf(buf
, sizeof(buf
), "%3lld.%1lld%c%s",
101 (long long) (bytes
+ 5) / 100,
102 (long long) (bytes
+ 5) / 10 % 10,
109 format_size(off_t bytes
)
112 static char buf
[STRING_SIZE(bytes
) + 16];
114 for (i
= 0; bytes
>= 10000 && unit
[i
] != 'T'; i
++)
115 bytes
= (bytes
+ 512) / 1024;
116 snprintf(buf
, sizeof(buf
), "%4lld%c%s",
124 refresh_progress_meter(int force_update
)
126 char *buf
= NULL
, *obuf
= NULL
;
132 int hours
, minutes
, seconds
;
135 if ((!force_update
&& !alarm_fired
&& !win_resized
) || !can_output())
144 transferred
= *counter
- (cur_pos
? cur_pos
: start_pos
);
146 now
= monotime_double();
147 bytes_left
= end_pos
- cur_pos
;
150 elapsed
= now
- last_update
;
152 elapsed
= now
- start
;
153 /* Calculate true total speed when done */
154 transferred
= end_pos
- start_pos
;
155 bytes_per_second
= 0;
158 /* calculate speed */
160 cur_speed
= (transferred
/ elapsed
);
162 cur_speed
= transferred
;
164 #define AGE_FACTOR 0.9
165 if (bytes_per_second
!= 0) {
166 bytes_per_second
= (bytes_per_second
* AGE_FACTOR
) +
167 (cur_speed
* (1.0 - AGE_FACTOR
));
169 bytes_per_second
= cur_speed
;
173 /* Don't bother if we can't even display the completion percentage */
178 file_len
= cols
= win_size
- 36;
180 asmprintf(&buf
, INT_MAX
, &cols
, "%-*s", file_len
, file
);
181 /* If we used fewer columns than expected then pad */
183 xextendf(&buf
, NULL
, "%*s", file_len
- cols
, "");
185 /* percent of transfer done */
186 if (end_pos
== 0 || cur_pos
== end_pos
)
189 percent
= ((float)cur_pos
/ end_pos
) * 100;
191 /* percent / amount transferred / bandwidth usage */
192 xextendf(&buf
, NULL
, " %3d%% %s %s/s ", percent
, format_size(cur_pos
),
193 format_rate((off_t
)bytes_per_second
));
201 if (stalled
>= STALL_TIME
)
202 xextendf(&buf
, NULL
, "- stalled -");
203 else if (bytes_per_second
== 0 && bytes_left
)
204 xextendf(&buf
, NULL
, " --:-- ETA");
207 seconds
= bytes_left
/ bytes_per_second
;
211 hours
= seconds
/ 3600;
212 seconds
-= hours
* 3600;
213 minutes
= seconds
/ 60;
214 seconds
-= minutes
* 60;
217 xextendf(&buf
, NULL
, "%d:%02d:%02d",
218 hours
, minutes
, seconds
);
220 xextendf(&buf
, NULL
, " %02d:%02d", minutes
, seconds
);
223 xextendf(&buf
, NULL
, " ETA");
225 xextendf(&buf
, NULL
, " ");
228 /* Finally, truncate string at window width */
230 asmprintf(&obuf
, INT_MAX
, &cols
, " %s", buf
);
232 *obuf
= '\r'; /* must insert as asmprintf() would escape it */
233 atomicio(vwrite
, STDOUT_FILENO
, obuf
, strlen(obuf
));
240 sig_alarm(int ignore
)
243 alarm(UPDATE_INTERVAL
);
247 start_progress_meter(const char *f
, off_t filesize
, off_t
*ctr
)
249 start
= last_update
= monotime_double();
256 bytes_per_second
= 0;
259 refresh_progress_meter(1);
261 ssh_signal(SIGALRM
, sig_alarm
);
262 ssh_signal(SIGWINCH
, sig_winch
);
263 alarm(UPDATE_INTERVAL
);
267 stop_progress_meter(void)
274 /* Ensure we complete the progress */
275 if (cur_pos
!= end_pos
)
276 refresh_progress_meter(1);
278 atomicio(vwrite
, STDOUT_FILENO
, "\n", 1);
290 struct winsize winsize
;
292 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &winsize
) != -1 &&
293 winsize
.ws_col
!= 0) {
294 if (winsize
.ws_col
> MAX_WINSIZE
)
295 win_size
= MAX_WINSIZE
;
297 win_size
= winsize
.ws_col
;
299 win_size
= DEFAULT_WINSIZE
;
300 win_size
+= 1; /* trailing \0 */