progress.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Progress bar code.
  4. */
  5. /* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff,
  6. * much of which was blatantly stolen from openssh.
  7. */
  8. /*-
  9. * Copyright (c) 1992, 1993
  10. * The Regents of the University of California. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. *
  21. * 3. BSD Advertising Clause omitted per the July 22, 1999 licensing change
  22. * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
  23. *
  24. * 4. Neither the name of the University nor the names of its contributors
  25. * may be used to endorse or promote products derived from this software
  26. * without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  29. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  32. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  37. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  38. * SUCH DAMAGE.
  39. */
  40. #include "libbb.h"
  41. #include "unicode.h"
  42. enum {
  43. /* Seconds when xfer considered "stalled" */
  44. STALLTIME = 5
  45. };
  46. static unsigned int get_tty2_width(void)
  47. {
  48. unsigned width;
  49. get_terminal_width_height(2, &width, NULL);
  50. return width;
  51. }
  52. void FAST_FUNC bb_progress_init(bb_progress_t *p, const char *curfile)
  53. {
  54. #if ENABLE_UNICODE_SUPPORT
  55. init_unicode();
  56. p->curfile = unicode_conv_to_printable_fixedwidth(/*NULL,*/ curfile, 20);
  57. #else
  58. p->curfile = curfile;
  59. #endif
  60. p->start_sec = monotonic_sec();
  61. p->last_update_sec = p->start_sec;
  62. p->last_change_sec = p->start_sec;
  63. p->last_size = 0;
  64. }
  65. /* File already had beg_size bytes.
  66. * Then we started downloading.
  67. * We downloaded "transferred" bytes so far.
  68. * Download is expected to stop when total size (beg_size + transferred)
  69. * will be "totalsize" bytes.
  70. * If totalsize == 0, then it is unknown.
  71. */
  72. void FAST_FUNC bb_progress_update(bb_progress_t *p,
  73. uoff_t beg_size,
  74. uoff_t transferred,
  75. uoff_t totalsize)
  76. {
  77. uoff_t beg_and_transferred;
  78. unsigned since_last_update, elapsed;
  79. int barlength;
  80. int kiloscale;
  81. //transferred = 1234; /* use for stall detection testing */
  82. //totalsize = 0; /* use for unknown size download testing */
  83. elapsed = monotonic_sec();
  84. since_last_update = elapsed - p->last_update_sec;
  85. p->last_update_sec = elapsed;
  86. if (totalsize != 0 && transferred >= totalsize - beg_size) {
  87. /* Last call. Do not skip this update */
  88. transferred = totalsize - beg_size; /* sanitize just in case */
  89. }
  90. else if (since_last_update == 0) {
  91. /*
  92. * Do not update on every call
  93. * (we can be called on every network read!)
  94. */
  95. return;
  96. }
  97. kiloscale = 0;
  98. /*
  99. * Scale sizes down if they are close to overflowing.
  100. * This allows calculations like (100 * transferred / totalsize)
  101. * without risking overflow: we guarantee 10 highest bits to be 0.
  102. * Introduced error is less than 1 / 2^12 ~= 0.025%
  103. */
  104. if (ULONG_MAX > 0xffffffff || sizeof(off_t) == 4 || sizeof(off_t) != 8) {
  105. /*
  106. * 64-bit CPU || small off_t: in either case,
  107. * >> is cheap, single-word operation.
  108. * ... || strange off_t: also use this code
  109. * (it is safe, just suboptimal wrt code size),
  110. * because 32/64 optimized one works only for 64-bit off_t.
  111. */
  112. if (totalsize >= (1 << 22)) {
  113. totalsize >>= 10;
  114. beg_size >>= 10;
  115. transferred >>= 10;
  116. kiloscale = 1;
  117. }
  118. } else {
  119. /* 32-bit CPU and 64-bit off_t.
  120. * Use a 40-bit shift, it is easier to do on 32-bit CPU.
  121. */
  122. /* ONE suppresses "warning: shift count >= width of type" */
  123. #define ONE (sizeof(off_t) > 4)
  124. if (totalsize >= (uoff_t)(1ULL << 54*ONE)) {
  125. totalsize = (uint32_t)(totalsize >> 32*ONE) >> 8;
  126. beg_size = (uint32_t)(beg_size >> 32*ONE) >> 8;
  127. transferred = (uint32_t)(transferred >> 32*ONE) >> 8;
  128. kiloscale = 4;
  129. }
  130. }
  131. if (ENABLE_UNICODE_SUPPORT)
  132. fprintf(stderr, "\r%s", p->curfile);
  133. else
  134. fprintf(stderr, "\r%-20.20s", p->curfile);
  135. beg_and_transferred = beg_size + transferred;
  136. if (totalsize != 0) {
  137. unsigned ratio = 100 * beg_and_transferred / totalsize;
  138. fprintf(stderr, "%4u%%", ratio);
  139. barlength = get_tty2_width() - 49;
  140. if (barlength > 0) {
  141. /* god bless gcc for variable arrays :) */
  142. char buf[barlength + 1];
  143. unsigned stars = (unsigned)barlength * beg_and_transferred / totalsize;
  144. memset(buf, ' ', barlength);
  145. buf[barlength] = '\0';
  146. memset(buf, '*', stars);
  147. fprintf(stderr, " |%s|", buf);
  148. }
  149. }
  150. while (beg_and_transferred >= 100000) {
  151. beg_and_transferred >>= 10;
  152. kiloscale++;
  153. }
  154. /* see http://en.wikipedia.org/wiki/Tera */
  155. fprintf(stderr, "%6u%c", (unsigned)beg_and_transferred, " kMGTPEZY"[kiloscale]);
  156. #define beg_and_transferred dont_use_beg_and_transferred_below()
  157. since_last_update = elapsed - p->last_change_sec;
  158. if ((unsigned)transferred != p->last_size) {
  159. p->last_change_sec = elapsed;
  160. p->last_size = (unsigned)transferred;
  161. if (since_last_update >= STALLTIME) {
  162. /* We "cut out" these seconds from elapsed time
  163. * by adjusting start time */
  164. p->start_sec += since_last_update;
  165. }
  166. since_last_update = 0; /* we are un-stalled now */
  167. }
  168. elapsed -= p->start_sec; /* now it's "elapsed since start" */
  169. if (since_last_update >= STALLTIME) {
  170. fprintf(stderr, " - stalled -");
  171. } else if (!totalsize || !transferred || (int)elapsed < 0) {
  172. fprintf(stderr, " --:--:-- ETA");
  173. } else {
  174. unsigned eta, secs, hours;
  175. totalsize -= beg_size; /* now it's "total to upload" */
  176. /* Estimated remaining time =
  177. * estimated_sec_to_dl_totalsize_bytes - elapsed_sec =
  178. * totalsize / average_bytes_sec_so_far - elapsed =
  179. * totalsize / (transferred/elapsed) - elapsed =
  180. * totalsize * elapsed / transferred - elapsed
  181. */
  182. eta = totalsize * elapsed / transferred - elapsed;
  183. if (eta >= 1000*60*60)
  184. eta = 1000*60*60 - 1;
  185. secs = eta % 3600;
  186. hours = eta / 3600;
  187. fprintf(stderr, "%3u:%02u:%02u ETA", hours, secs / 60, secs % 60);
  188. }
  189. }