progress meter: display >999 hours ETA correctly
[oweals/busybox.git] / libbb / progress.c
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
43 enum {
44         /* Seconds when xfer considered "stalled" */
45         STALLTIME = 5
46 };
47
48 static unsigned int get_tty2_width(void)
49 {
50         unsigned width;
51         get_terminal_width_height(2, &width, NULL);
52         return width;
53 }
54
55 void FAST_FUNC bb_progress_init(bb_progress_t *p)
56 {
57         p->start_sec = monotonic_sec();
58         p->lastupdate_sec = p->start_sec;
59         p->lastsize = 0;
60         p->inited = 1;
61 }
62
63 /* File already had beg_size bytes.
64  * Then we started downloading.
65  * We downloaded "transferred" bytes so far.
66  * Download is expected to stop when total size (beg_size + transferred)
67  * will be "totalsize" bytes.
68  * If totalsize == 0, then it is unknown.
69  */
70 void FAST_FUNC bb_progress_update(bb_progress_t *p,
71                 const char *curfile,
72                 uoff_t beg_size,
73                 uoff_t transferred,
74                 uoff_t totalsize)
75 {
76         uoff_t beg_and_transferred;
77         unsigned since_last_update, elapsed;
78         unsigned ratio;
79         int barlength;
80         int kiloscale;
81
82         beg_and_transferred = beg_size + transferred;
83
84         elapsed = monotonic_sec();
85         since_last_update = elapsed - p->lastupdate_sec;
86         /*
87          * Do not update on every call
88          * (we can be called on every network read!)
89          */
90         if (since_last_update == 0 && beg_and_transferred < totalsize)
91                 return;
92
93         kiloscale = 0;
94         /*
95          * Scale sizes down if they are close to overflowing.
96          * This allows calculations like (100 * transferred / totalsize)
97          * without risking overflow: we guarantee 10 highest bits to be 0.
98          * Introduced error is less than 1 / 2^12 ~= 0.025%
99          */
100         if (ULONG_MAX > 0xffffffff || sizeof(off_t) == 4 || sizeof(off_t) != 8) {
101                 /*
102                  * 64-bit CPU || small off_t: in either case,
103                  * >> is cheap, single-word operation.
104                  * ... || strange off_t: also use this code (it is safe,
105                  * even if suboptimal), because 32/64 optimized one
106                  * works only for 64-bit off_t.
107                  */
108                 if (totalsize >= (1 << 22)) {
109                         totalsize >>= 10;
110                         beg_size >>= 10;
111                         transferred >>= 10;
112                         beg_and_transferred >>= 10;
113                         kiloscale = 1;
114                 }
115         } else {
116                 /* 32-bit CPU and 64-bit off_t.
117                  * Pick a shift (40 bits) which is easier to do on 32-bit CPU.
118                  */
119                 if (totalsize >= (uoff_t)(1ULL << 54)) {
120                         totalsize = (uint32_t)(totalsize >> 32) >> 8;
121                         beg_size = (uint32_t)(beg_size >> 32) >> 8;
122                         transferred = (uint32_t)(transferred >> 32) >> 8;
123                         beg_and_transferred = (uint32_t)(beg_and_transferred >> 32) >> 8;
124                         kiloscale = 4;
125                 }
126         }
127
128         if (beg_and_transferred > totalsize)
129                 beg_and_transferred = totalsize;
130
131         ratio = 100 * beg_and_transferred / totalsize;
132 #if ENABLE_UNICODE_SUPPORT
133         init_unicode();
134         {
135                 char *buf = unicode_conv_to_printable_fixedwidth(/*NULL,*/ curfile, 20);
136                 fprintf(stderr, "\r%s%4u%% ", buf, ratio);
137                 free(buf);
138         }
139 #else
140         fprintf(stderr, "\r%-20.20s%4u%% ", curfile, ratio);
141 #endif
142
143         barlength = get_tty2_width() - 49;
144         if (barlength > 0) {
145                 /* god bless gcc for variable arrays :) */
146                 char buf[barlength + 1];
147                 unsigned stars = (unsigned)barlength * beg_and_transferred / totalsize;
148                 memset(buf, ' ', barlength);
149                 buf[barlength] = '\0';
150                 memset(buf, '*', stars);
151                 fprintf(stderr, "|%s|", buf);
152         }
153
154         while (beg_and_transferred >= 100000) {
155                 beg_and_transferred >>= 10;
156                 kiloscale++;
157         }
158         /* see http://en.wikipedia.org/wiki/Tera */
159         fprintf(stderr, "%6u%c", (unsigned)beg_and_transferred, " kMGTPEZY"[kiloscale]);
160 #define beg_and_transferred dont_use_beg_and_transferred_below()
161
162         if (transferred != p->lastsize) {
163                 p->lastupdate_sec = elapsed;
164                 p->lastsize = transferred;
165                 if (since_last_update >= STALLTIME) {
166                         /* We "cut off" these seconds from elapsed time
167                          * by adjusting start time */
168                         p->start_sec += since_last_update;
169                 }
170                 since_last_update = 0; /* we are un-stalled now */
171         }
172
173         elapsed -= p->start_sec; /* now it's "elapsed since start" */
174
175         if (since_last_update >= STALLTIME) {
176                 fprintf(stderr, "  - stalled -");
177         } else if (!totalsize || !transferred || (int)elapsed <= 0) {
178                 fprintf(stderr, " --:--:-- ETA");
179         } else {
180                 unsigned eta, secs, hours;
181
182                 totalsize -= beg_size; /* now it's "total to upload" */
183
184                 /* Estimated remaining time =
185                  * estimated_sec_to_dl_totalsize_bytes - elapsed_sec =
186                  * totalsize / average_bytes_sec_so_far - elapsed =
187                  * totalsize / (transferred/elapsed) - elapsed =
188                  * totalsize * elapsed / transferred - elapsed
189                  */
190                 eta = totalsize * elapsed / transferred - elapsed;
191                 if (eta >= 1000*60*60)
192                         eta = 1000*60*60 - 1;
193                 secs = eta % 3600;
194                 hours = eta / 3600;
195                 fprintf(stderr, "%3u:%02u:%02u ETA", hours, secs / 60, secs % 60);
196         }
197 }