progressmeter.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* $OpenBSD: progressmeter.c,v 1.50 2020/01/23 07:10:22 dtucker Exp $ */
  2. /*
  3. * Copyright (c) 2003 Nils Nordman. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  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.
  13. *
  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.
  24. */
  25. #include "includes.h"
  26. #include <sys/types.h>
  27. #include <sys/ioctl.h>
  28. #include <sys/uio.h>
  29. #include <errno.h>
  30. #include <signal.h>
  31. #include <stdarg.h>
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <time.h>
  35. #include <unistd.h>
  36. #include "progressmeter.h"
  37. #include "atomicio.h"
  38. #include "misc.h"
  39. #include "utf8.h"
  40. #define DEFAULT_WINSIZE 80
  41. #define MAX_WINSIZE 512
  42. #define PADDING 1 /* padding between the progress indicators */
  43. #define UPDATE_INTERVAL 1 /* update the progress meter every second */
  44. #define STALL_TIME 5 /* we're stalled after this many seconds */
  45. /* determines whether we can output to the terminal */
  46. static int can_output(void);
  47. /* formats and inserts the specified size into the given buffer */
  48. static void format_size(char *, int, off_t);
  49. static void format_rate(char *, int, off_t);
  50. /* window resizing */
  51. static void sig_winch(int);
  52. static void setscreensize(void);
  53. /* signal handler for updating the progress meter */
  54. static void sig_alarm(int);
  55. static double start; /* start progress */
  56. static double last_update; /* last progress update */
  57. static const char *file; /* name of the file being transferred */
  58. static off_t start_pos; /* initial position of transfer */
  59. static off_t end_pos; /* ending position of transfer */
  60. static off_t cur_pos; /* transfer position as of last refresh */
  61. static off_t last_pos;
  62. static off_t max_delta_pos = 0;
  63. static volatile off_t *counter; /* progress counter */
  64. static long stalled; /* how long we have been stalled */
  65. static int bytes_per_second; /* current speed in bytes per second */
  66. static int win_size; /* terminal window size */
  67. static volatile sig_atomic_t win_resized; /* for window resizing */
  68. static volatile sig_atomic_t alarm_fired;
  69. /* units for format_size */
  70. static const char unit[] = " KMGT";
  71. static int
  72. can_output(void)
  73. {
  74. return (getpgrp() == tcgetpgrp(STDOUT_FILENO));
  75. }
  76. static void
  77. format_rate(char *buf, int size, off_t bytes)
  78. {
  79. int i;
  80. bytes *= 100;
  81. for (i = 0; bytes >= 100*1000 && unit[i] != 'T'; i++)
  82. bytes = (bytes + 512) / 1024;
  83. if (i == 0) {
  84. i++;
  85. bytes = (bytes + 512) / 1024;
  86. }
  87. snprintf(buf, size, "%3lld.%1lld%c%s",
  88. (long long) (bytes + 5) / 100,
  89. (long long) (bytes + 5) / 10 % 10,
  90. unit[i],
  91. i ? "B" : " ");
  92. }
  93. static void
  94. format_size(char *buf, int size, off_t bytes)
  95. {
  96. int i;
  97. for (i = 0; bytes >= 10000 && unit[i] != 'T'; i++)
  98. bytes = (bytes + 512) / 1024;
  99. snprintf(buf, size, "%4lld%c%s",
  100. (long long) bytes,
  101. unit[i],
  102. i ? "B" : " ");
  103. }
  104. void
  105. refresh_progress_meter(int force_update)
  106. {
  107. char buf[MAX_WINSIZE + 1];
  108. off_t transferred;
  109. double elapsed, now;
  110. int percent;
  111. off_t bytes_left;
  112. int cur_speed;
  113. int hours, minutes, seconds;
  114. int file_len;
  115. off_t delta_pos;
  116. if ((!force_update && !alarm_fired && !win_resized) || !can_output())
  117. return;
  118. alarm_fired = 0;
  119. if (win_resized) {
  120. setscreensize();
  121. win_resized = 0;
  122. }
  123. transferred = *counter - (cur_pos ? cur_pos : start_pos);
  124. cur_pos = *counter;
  125. now = monotime_double();
  126. bytes_left = end_pos - cur_pos;
  127. delta_pos = cur_pos - last_pos;
  128. if (delta_pos > max_delta_pos)
  129. max_delta_pos = delta_pos;
  130. if (bytes_left > 0)
  131. elapsed = now - last_update;
  132. else {
  133. elapsed = now - start;
  134. /* Calculate true total speed when done */
  135. transferred = end_pos - start_pos;
  136. bytes_per_second = 0;
  137. }
  138. /* calculate speed */
  139. if (elapsed != 0)
  140. cur_speed = (transferred / elapsed);
  141. else
  142. cur_speed = transferred;
  143. #define AGE_FACTOR 0.9
  144. if (bytes_per_second != 0) {
  145. bytes_per_second = (bytes_per_second * AGE_FACTOR) +
  146. (cur_speed * (1.0 - AGE_FACTOR));
  147. } else
  148. bytes_per_second = cur_speed;
  149. /* filename */
  150. buf[0] = '\0';
  151. file_len = win_size - 45;
  152. if (file_len > 0) {
  153. buf[0] = '\r';
  154. snmprintf(buf+1, sizeof(buf)-1, &file_len, "%-*s",
  155. file_len, file);
  156. }
  157. /* percent of transfer done */
  158. if (end_pos == 0 || cur_pos == end_pos)
  159. percent = 100;
  160. else
  161. percent = ((float)cur_pos / end_pos) * 100;
  162. snprintf(buf + strlen(buf), win_size - strlen(buf),
  163. " %3d%% ", percent);
  164. /* amount transferred */
  165. format_size(buf + strlen(buf), win_size - strlen(buf),
  166. cur_pos);
  167. strlcat(buf, " ", win_size);
  168. /* bandwidth usage */
  169. format_rate(buf + strlen(buf), win_size - strlen(buf),
  170. (off_t)bytes_per_second);
  171. strlcat(buf, "/s ", win_size);
  172. /* instantaneous rate */
  173. if (bytes_left > 0)
  174. format_rate(buf + strlen(buf), win_size - strlen(buf),
  175. delta_pos);
  176. else
  177. format_rate(buf + strlen(buf), win_size - strlen(buf),
  178. max_delta_pos);
  179. strlcat(buf, "/s ", win_size);
  180. /* ETA */
  181. if (!transferred)
  182. stalled += elapsed;
  183. else
  184. stalled = 0;
  185. if (stalled >= STALL_TIME)
  186. strlcat(buf, "- stalled -", win_size);
  187. else if (bytes_per_second == 0 && bytes_left)
  188. strlcat(buf, " --:-- ETA", win_size);
  189. else {
  190. if (bytes_left > 0)
  191. seconds = bytes_left / bytes_per_second;
  192. else
  193. seconds = elapsed;
  194. hours = seconds / 3600;
  195. seconds -= hours * 3600;
  196. minutes = seconds / 60;
  197. seconds -= minutes * 60;
  198. if (hours != 0)
  199. snprintf(buf + strlen(buf), win_size - strlen(buf),
  200. "%d:%02d:%02d", hours, minutes, seconds);
  201. else
  202. snprintf(buf + strlen(buf), win_size - strlen(buf),
  203. " %02d:%02d", minutes, seconds);
  204. if (bytes_left > 0)
  205. strlcat(buf, " ETA", win_size);
  206. else
  207. strlcat(buf, " ", win_size);
  208. }
  209. atomicio(vwrite, STDOUT_FILENO, buf, win_size - 1);
  210. last_update = now;
  211. last_pos = cur_pos;
  212. }
  213. /*ARGSUSED*/
  214. static void
  215. sig_alarm(int ignore)
  216. {
  217. alarm_fired = 1;
  218. alarm(UPDATE_INTERVAL);
  219. }
  220. void
  221. start_progress_meter(const char *f, off_t filesize, off_t *ctr)
  222. {
  223. start = last_update = monotime_double();
  224. file = f;
  225. start_pos = *ctr;
  226. end_pos = filesize;
  227. cur_pos = 0;
  228. counter = ctr;
  229. stalled = 0;
  230. bytes_per_second = 0;
  231. setscreensize();
  232. refresh_progress_meter(1);
  233. ssh_signal(SIGALRM, sig_alarm);
  234. ssh_signal(SIGWINCH, sig_winch);
  235. alarm(UPDATE_INTERVAL);
  236. }
  237. void
  238. stop_progress_meter(void)
  239. {
  240. alarm(0);
  241. if (!can_output())
  242. return;
  243. /* Ensure we complete the progress */
  244. if (cur_pos != end_pos)
  245. refresh_progress_meter(1);
  246. atomicio(vwrite, STDOUT_FILENO, "\n", 1);
  247. }
  248. /*ARGSUSED*/
  249. static void
  250. sig_winch(int sig)
  251. {
  252. win_resized = 1;
  253. }
  254. static void
  255. setscreensize(void)
  256. {
  257. struct winsize winsize;
  258. if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winsize) != -1 &&
  259. winsize.ws_col != 0) {
  260. if (winsize.ws_col > MAX_WINSIZE)
  261. win_size = MAX_WINSIZE;
  262. else
  263. win_size = winsize.ws_col;
  264. } else
  265. win_size = DEFAULT_WINSIZE;
  266. win_size += 1; /* trailing \0 */
  267. }