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