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