Imported Upstream version 7.40.0
[platform/upstream/curl.git] / lib / progress.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
9  *
10  * This software is licensed as described in the file COPYING, which
11  * you should have received as part of this distribution. The terms
12  * are also available at http://curl.haxx.se/docs/copyright.html.
13  *
14  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15  * copies of the Software, and permit persons to whom the Software is
16  * furnished to do so, under the terms of the COPYING file.
17  *
18  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19  * KIND, either express or implied.
20  *
21  ***************************************************************************/
22
23 #include "curl_setup.h"
24
25 #include "urldata.h"
26 #include "sendf.h"
27 #include "progress.h"
28
29 #define _MPRINTF_REPLACE /* use our functions only */
30 #include <curl/mprintf.h>
31
32 /* Provide a string that is 2 + 1 + 2 + 1 + 2 = 8 letters long (plus the zero
33    byte) */
34 static void time2str(char *r, curl_off_t seconds)
35 {
36   curl_off_t d, h, m, s;
37   if(seconds <= 0) {
38     strcpy(r, "--:--:--");
39     return;
40   }
41   h = seconds / CURL_OFF_T_C(3600);
42   if(h <= CURL_OFF_T_C(99)) {
43     m = (seconds - (h*CURL_OFF_T_C(3600))) / CURL_OFF_T_C(60);
44     s = (seconds - (h*CURL_OFF_T_C(3600))) - (m*CURL_OFF_T_C(60));
45     snprintf(r, 9, "%2" CURL_FORMAT_CURL_OFF_T ":%02" CURL_FORMAT_CURL_OFF_T
46              ":%02" CURL_FORMAT_CURL_OFF_T, h, m, s);
47   }
48   else {
49     /* this equals to more than 99 hours, switch to a more suitable output
50        format to fit within the limits. */
51     d = seconds / CURL_OFF_T_C(86400);
52     h = (seconds - (d*CURL_OFF_T_C(86400))) / CURL_OFF_T_C(3600);
53     if(d <= CURL_OFF_T_C(999))
54       snprintf(r, 9, "%3" CURL_FORMAT_CURL_OFF_T
55                "d %02" CURL_FORMAT_CURL_OFF_T "h", d, h);
56     else
57       snprintf(r, 9, "%7" CURL_FORMAT_CURL_OFF_T "d", d);
58   }
59 }
60
61 /* The point of this function would be to return a string of the input data,
62    but never longer than 5 columns (+ one zero byte).
63    Add suffix k, M, G when suitable... */
64 static char *max5data(curl_off_t bytes, char *max5)
65 {
66 #define ONE_KILOBYTE  CURL_OFF_T_C(1024)
67 #define ONE_MEGABYTE (CURL_OFF_T_C(1024) * ONE_KILOBYTE)
68 #define ONE_GIGABYTE (CURL_OFF_T_C(1024) * ONE_MEGABYTE)
69 #define ONE_TERABYTE (CURL_OFF_T_C(1024) * ONE_GIGABYTE)
70 #define ONE_PETABYTE (CURL_OFF_T_C(1024) * ONE_TERABYTE)
71
72   if(bytes < CURL_OFF_T_C(100000))
73     snprintf(max5, 6, "%5" CURL_FORMAT_CURL_OFF_T, bytes);
74
75   else if(bytes < CURL_OFF_T_C(10000) * ONE_KILOBYTE)
76     snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "k", bytes/ONE_KILOBYTE);
77
78   else if(bytes < CURL_OFF_T_C(100) * ONE_MEGABYTE)
79     /* 'XX.XM' is good as long as we're less than 100 megs */
80     snprintf(max5, 6, "%2" CURL_FORMAT_CURL_OFF_T ".%0"
81              CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE,
82              (bytes%ONE_MEGABYTE) / (ONE_MEGABYTE/CURL_OFF_T_C(10)) );
83
84 #if (CURL_SIZEOF_CURL_OFF_T > 4)
85
86   else if(bytes < CURL_OFF_T_C(10000) * ONE_MEGABYTE)
87     /* 'XXXXM' is good until we're at 10000MB or above */
88     snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE);
89
90   else if(bytes < CURL_OFF_T_C(100) * ONE_GIGABYTE)
91     /* 10000 MB - 100 GB, we show it as XX.XG */
92     snprintf(max5, 6, "%2" CURL_FORMAT_CURL_OFF_T ".%0"
93              CURL_FORMAT_CURL_OFF_T "G", bytes/ONE_GIGABYTE,
94              (bytes%ONE_GIGABYTE) / (ONE_GIGABYTE/CURL_OFF_T_C(10)) );
95
96   else if(bytes < CURL_OFF_T_C(10000) * ONE_GIGABYTE)
97     /* up to 10000GB, display without decimal: XXXXG */
98     snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "G", bytes/ONE_GIGABYTE);
99
100   else if(bytes < CURL_OFF_T_C(10000) * ONE_TERABYTE)
101     /* up to 10000TB, display without decimal: XXXXT */
102     snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "T", bytes/ONE_TERABYTE);
103
104   else
105     /* up to 10000PB, display without decimal: XXXXP */
106     snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "P", bytes/ONE_PETABYTE);
107
108     /* 16384 petabytes (16 exabytes) is the maximum a 64 bit unsigned number
109        can hold, but our data type is signed so 8192PB will be the maximum. */
110
111 #else
112
113   else
114     snprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE);
115
116 #endif
117
118   return max5;
119 }
120
121 /*
122
123    New proposed interface, 9th of February 2000:
124
125    pgrsStartNow() - sets start time
126    pgrsSetDownloadSize(x) - known expected download size
127    pgrsSetUploadSize(x) - known expected upload size
128    pgrsSetDownloadCounter() - amount of data currently downloaded
129    pgrsSetUploadCounter() - amount of data currently uploaded
130    pgrsUpdate() - show progress
131    pgrsDone() - transfer complete
132
133 */
134
135 int Curl_pgrsDone(struct connectdata *conn)
136 {
137   int rc;
138   struct SessionHandle *data = conn->data;
139   data->progress.lastshow=0;
140   rc = Curl_pgrsUpdate(conn); /* the final (forced) update */
141   if(rc)
142     return rc;
143
144   if(!(data->progress.flags & PGRS_HIDE) &&
145      !data->progress.callback)
146     /* only output if we don't use a progress callback and we're not
147      * hidden */
148     fprintf(data->set.err, "\n");
149
150   data->progress.speeder_c = 0; /* reset the progress meter display */
151   return 0;
152 }
153
154 /* reset all times except redirect, and reset the known transfer sizes */
155 void Curl_pgrsResetTimesSizes(struct SessionHandle *data)
156 {
157   data->progress.t_nslookup = 0.0;
158   data->progress.t_connect = 0.0;
159   data->progress.t_pretransfer = 0.0;
160   data->progress.t_starttransfer = 0.0;
161
162   Curl_pgrsSetDownloadSize(data, -1);
163   Curl_pgrsSetUploadSize(data, -1);
164 }
165
166 void Curl_pgrsTime(struct SessionHandle *data, timerid timer)
167 {
168   struct timeval now = Curl_tvnow();
169
170   switch(timer) {
171   default:
172   case TIMER_NONE:
173     /* mistake filter */
174     break;
175   case TIMER_STARTOP:
176     /* This is set at the start of a transfer */
177     data->progress.t_startop = now;
178     break;
179   case TIMER_STARTSINGLE:
180     /* This is set at the start of each single fetch */
181     data->progress.t_startsingle = now;
182     break;
183
184   case TIMER_STARTACCEPT:
185     data->progress.t_acceptdata = Curl_tvnow();
186     break;
187
188   case TIMER_NAMELOOKUP:
189     data->progress.t_nslookup =
190       Curl_tvdiff_secs(now, data->progress.t_startsingle);
191     break;
192   case TIMER_CONNECT:
193     data->progress.t_connect =
194       Curl_tvdiff_secs(now, data->progress.t_startsingle);
195     break;
196   case TIMER_APPCONNECT:
197     data->progress.t_appconnect =
198       Curl_tvdiff_secs(now, data->progress.t_startsingle);
199     break;
200   case TIMER_PRETRANSFER:
201     data->progress.t_pretransfer =
202       Curl_tvdiff_secs(now, data->progress.t_startsingle);
203     break;
204   case TIMER_STARTTRANSFER:
205     data->progress.t_starttransfer =
206       Curl_tvdiff_secs(now, data->progress.t_startsingle);
207     break;
208   case TIMER_POSTRANSFER:
209     /* this is the normal end-of-transfer thing */
210     break;
211   case TIMER_REDIRECT:
212     data->progress.t_redirect = Curl_tvdiff_secs(now, data->progress.start);
213     break;
214   }
215 }
216
217 void Curl_pgrsStartNow(struct SessionHandle *data)
218 {
219   data->progress.speeder_c = 0; /* reset the progress meter display */
220   data->progress.start = Curl_tvnow();
221   /* clear all bits except HIDE and HEADERS_OUT */
222   data->progress.flags &= PGRS_HIDE|PGRS_HEADERS_OUT;
223 }
224
225 void Curl_pgrsSetDownloadCounter(struct SessionHandle *data, curl_off_t size)
226 {
227   data->progress.downloaded = size;
228 }
229
230 void Curl_pgrsSetUploadCounter(struct SessionHandle *data, curl_off_t size)
231 {
232   data->progress.uploaded = size;
233 }
234
235 void Curl_pgrsSetDownloadSize(struct SessionHandle *data, curl_off_t size)
236 {
237   if(size >= 0) {
238     data->progress.size_dl = size;
239     data->progress.flags |= PGRS_DL_SIZE_KNOWN;
240   }
241   else {
242     data->progress.size_dl = 0;
243     data->progress.flags &= ~PGRS_DL_SIZE_KNOWN;
244   }
245 }
246
247 void Curl_pgrsSetUploadSize(struct SessionHandle *data, curl_off_t size)
248 {
249   if(size >= 0) {
250     data->progress.size_ul = size;
251     data->progress.flags |= PGRS_UL_SIZE_KNOWN;
252   }
253   else {
254     data->progress.size_ul = 0;
255     data->progress.flags &= ~PGRS_UL_SIZE_KNOWN;
256   }
257 }
258
259 /*
260  * Curl_pgrsUpdate() returns 0 for success or the value returned by the
261  * progress callback!
262  */
263 int Curl_pgrsUpdate(struct connectdata *conn)
264 {
265   struct timeval now;
266   int result;
267   char max5[6][10];
268   curl_off_t dlpercen=0;
269   curl_off_t ulpercen=0;
270   curl_off_t total_percen=0;
271   curl_off_t total_transfer;
272   curl_off_t total_expected_transfer;
273   curl_off_t timespent;
274   struct SessionHandle *data = conn->data;
275   int nowindex = data->progress.speeder_c% CURR_TIME;
276   int checkindex;
277   int countindex; /* amount of seconds stored in the speeder array */
278   char time_left[10];
279   char time_total[10];
280   char time_spent[10];
281   curl_off_t ulestimate=0;
282   curl_off_t dlestimate=0;
283   curl_off_t total_estimate;
284   bool shownow=FALSE;
285
286   now = Curl_tvnow(); /* what time is it */
287
288   /* The time spent so far (from the start) */
289   data->progress.timespent =
290     (double)(now.tv_sec - data->progress.start.tv_sec) +
291     (double)(now.tv_usec - data->progress.start.tv_usec)/1000000.0;
292   timespent = (curl_off_t)data->progress.timespent;
293
294   /* The average download speed this far */
295   data->progress.dlspeed = (curl_off_t)
296     ((double)data->progress.downloaded/
297      (data->progress.timespent>0?data->progress.timespent:1));
298
299   /* The average upload speed this far */
300   data->progress.ulspeed = (curl_off_t)
301     ((double)data->progress.uploaded/
302      (data->progress.timespent>0?data->progress.timespent:1));
303
304   /* Calculations done at most once a second, unless end is reached */
305   if(data->progress.lastshow != (long)now.tv_sec) {
306     shownow = TRUE;
307
308     data->progress.lastshow = now.tv_sec;
309
310     /* Let's do the "current speed" thing, which should use the fastest
311        of the dl/ul speeds. Store the faster speed at entry 'nowindex'. */
312     data->progress.speeder[ nowindex ] =
313       data->progress.downloaded>data->progress.uploaded?
314       data->progress.downloaded:data->progress.uploaded;
315
316     /* remember the exact time for this moment */
317     data->progress.speeder_time [ nowindex ] = now;
318
319     /* advance our speeder_c counter, which is increased every time we get
320        here and we expect it to never wrap as 2^32 is a lot of seconds! */
321     data->progress.speeder_c++;
322
323     /* figure out how many index entries of data we have stored in our speeder
324        array. With N_ENTRIES filled in, we have about N_ENTRIES-1 seconds of
325        transfer. Imagine, after one second we have filled in two entries,
326        after two seconds we've filled in three entries etc. */
327     countindex = ((data->progress.speeder_c>=CURR_TIME)?
328                   CURR_TIME:data->progress.speeder_c) - 1;
329
330     /* first of all, we don't do this if there's no counted seconds yet */
331     if(countindex) {
332       long span_ms;
333
334       /* Get the index position to compare with the 'nowindex' position.
335          Get the oldest entry possible. While we have less than CURR_TIME
336          entries, the first entry will remain the oldest. */
337       checkindex = (data->progress.speeder_c>=CURR_TIME)?
338         data->progress.speeder_c%CURR_TIME:0;
339
340       /* Figure out the exact time for the time span */
341       span_ms = Curl_tvdiff(now,
342                             data->progress.speeder_time[checkindex]);
343       if(0 == span_ms)
344         span_ms=1; /* at least one millisecond MUST have passed */
345
346       /* Calculate the average speed the last 'span_ms' milliseconds */
347       {
348         curl_off_t amount = data->progress.speeder[nowindex]-
349           data->progress.speeder[checkindex];
350
351         if(amount > CURL_OFF_T_C(4294967) /* 0xffffffff/1000 */)
352           /* the 'amount' value is bigger than would fit in 32 bits if
353              multiplied with 1000, so we use the double math for this */
354           data->progress.current_speed = (curl_off_t)
355             ((double)amount/((double)span_ms/1000.0));
356         else
357           /* the 'amount' value is small enough to fit within 32 bits even
358              when multiplied with 1000 */
359           data->progress.current_speed = amount*CURL_OFF_T_C(1000)/span_ms;
360       }
361     }
362     else
363       /* the first second we use the main average */
364       data->progress.current_speed =
365         (data->progress.ulspeed>data->progress.dlspeed)?
366         data->progress.ulspeed:data->progress.dlspeed;
367
368   } /* Calculations end */
369
370   if(!(data->progress.flags & PGRS_HIDE)) {
371     /* progress meter has not been shut off */
372
373     if(data->set.fxferinfo) {
374       /* There's a callback set, call that */
375       result= data->set.fxferinfo(data->set.progress_client,
376                                   data->progress.size_dl,
377                                   data->progress.downloaded,
378                                   data->progress.size_ul,
379                                   data->progress.uploaded);
380       if(result)
381         failf(data, "Callback aborted");
382       return result;
383     }
384     else if(data->set.fprogress) {
385       /* The older deprecated callback is set, call that */
386       result= data->set.fprogress(data->set.progress_client,
387                                   (double)data->progress.size_dl,
388                                   (double)data->progress.downloaded,
389                                   (double)data->progress.size_ul,
390                                   (double)data->progress.uploaded);
391       if(result)
392         failf(data, "Callback aborted");
393       return result;
394     }
395
396     if(!shownow)
397       /* only show the internal progress meter once per second */
398       return 0;
399
400     /* If there's no external callback set, use internal code to show
401        progress */
402
403     if(!(data->progress.flags & PGRS_HEADERS_OUT)) {
404       if(data->state.resume_from) {
405         fprintf(data->set.err,
406                 "** Resuming transfer from byte position %"
407                 CURL_FORMAT_CURL_OFF_T "\n", data->state.resume_from);
408       }
409       fprintf(data->set.err,
410               "  %% Total    %% Received %% Xferd  Average Speed   "
411               "Time    Time     Time  Current\n"
412               "                                 Dload  Upload   "
413               "Total   Spent    Left  Speed\n");
414       data->progress.flags |= PGRS_HEADERS_OUT; /* headers are shown */
415     }
416
417     /* Figure out the estimated time of arrival for the upload */
418     if((data->progress.flags & PGRS_UL_SIZE_KNOWN) &&
419        (data->progress.ulspeed > CURL_OFF_T_C(0))) {
420       ulestimate = data->progress.size_ul / data->progress.ulspeed;
421
422       if(data->progress.size_ul > CURL_OFF_T_C(10000))
423         ulpercen = data->progress.uploaded /
424           (data->progress.size_ul/CURL_OFF_T_C(100));
425       else if(data->progress.size_ul > CURL_OFF_T_C(0))
426         ulpercen = (data->progress.uploaded*100) /
427           data->progress.size_ul;
428     }
429
430     /* ... and the download */
431     if((data->progress.flags & PGRS_DL_SIZE_KNOWN) &&
432        (data->progress.dlspeed > CURL_OFF_T_C(0))) {
433       dlestimate = data->progress.size_dl / data->progress.dlspeed;
434
435       if(data->progress.size_dl > CURL_OFF_T_C(10000))
436         dlpercen = data->progress.downloaded /
437           (data->progress.size_dl/CURL_OFF_T_C(100));
438       else if(data->progress.size_dl > CURL_OFF_T_C(0))
439         dlpercen = (data->progress.downloaded*100) /
440           data->progress.size_dl;
441     }
442
443     /* Now figure out which of them is slower and use that one for the
444        total estimate! */
445     total_estimate = ulestimate>dlestimate?ulestimate:dlestimate;
446
447     /* create the three time strings */
448     time2str(time_left, total_estimate > 0?(total_estimate - timespent):0);
449     time2str(time_total, total_estimate);
450     time2str(time_spent, timespent);
451
452     /* Get the total amount of data expected to get transferred */
453     total_expected_transfer =
454       (data->progress.flags & PGRS_UL_SIZE_KNOWN?
455        data->progress.size_ul:data->progress.uploaded)+
456       (data->progress.flags & PGRS_DL_SIZE_KNOWN?
457        data->progress.size_dl:data->progress.downloaded);
458
459     /* We have transferred this much so far */
460     total_transfer = data->progress.downloaded + data->progress.uploaded;
461
462     /* Get the percentage of data transferred so far */
463     if(total_expected_transfer > CURL_OFF_T_C(10000))
464       total_percen = total_transfer /
465         (total_expected_transfer/CURL_OFF_T_C(100));
466     else if(total_expected_transfer > CURL_OFF_T_C(0))
467       total_percen = (total_transfer*100) / total_expected_transfer;
468
469     fprintf(data->set.err,
470             "\r"
471             "%3" CURL_FORMAT_CURL_OFF_T " %s  "
472             "%3" CURL_FORMAT_CURL_OFF_T " %s  "
473             "%3" CURL_FORMAT_CURL_OFF_T " %s  %s  %s %s %s %s %s",
474             total_percen,  /* 3 letters */                /* total % */
475             max5data(total_expected_transfer, max5[2]),   /* total size */
476             dlpercen,      /* 3 letters */                /* rcvd % */
477             max5data(data->progress.downloaded, max5[0]), /* rcvd size */
478             ulpercen,      /* 3 letters */                /* xfer % */
479             max5data(data->progress.uploaded, max5[1]),   /* xfer size */
480             max5data(data->progress.dlspeed, max5[3]),    /* avrg dl speed */
481             max5data(data->progress.ulspeed, max5[4]),    /* avrg ul speed */
482             time_total,    /* 8 letters */                /* total time */
483             time_spent,    /* 8 letters */                /* time spent */
484             time_left,     /* 8 letters */                /* time left */
485             max5data(data->progress.current_speed, max5[5]) /* current speed */
486             );
487
488     /* we flush the output stream to make it appear as soon as possible */
489     fflush(data->set.err);
490
491   } /* !(data->progress.flags & PGRS_HIDE) */
492
493   return 0;
494 }