1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) 1998 - 2018, 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 the known transfer sizes */ 153 void Curl_pgrsResetTransferSizes(struct Curl_easy *data) 154 { 155 Curl_pgrsSetDownloadSize(data, -1); 156 Curl_pgrsSetUploadSize(data, -1); 157 } 158 159 /* 160 * @unittest: 1399 161 */ 162 void Curl_pgrsTime(struct Curl_easy *data, timerid timer) 163 { 164 struct curltime now = Curl_now(); 165 time_t *delta = NULL; 166 167 switch(timer) { 168 default: 169 case TIMER_NONE: 170 /* mistake filter */ 171 break; 172 case TIMER_STARTOP: 173 /* This is set at the start of a transfer */ 174 data->progress.t_startop = now; 175 break; 176 case TIMER_STARTSINGLE: 177 /* This is set at the start of each single fetch */ 178 data->progress.t_startsingle = now; 179 data->progress.is_t_startransfer_set = false; 180 break; 181 case TIMER_STARTACCEPT: 182 data->progress.t_acceptdata = now; 183 break; 184 case TIMER_NAMELOOKUP: 185 delta = &data->progress.t_nslookup; 186 break; 187 case TIMER_CONNECT: 188 delta = &data->progress.t_connect; 189 break; 190 case TIMER_APPCONNECT: 191 delta = &data->progress.t_appconnect; 192 break; 193 case TIMER_PRETRANSFER: 194 delta = &data->progress.t_pretransfer; 195 break; 196 case TIMER_STARTTRANSFER: 197 delta = &data->progress.t_starttransfer; 198 /* prevent updating t_starttransfer unless: 199 * 1) this is the first time we're setting t_starttransfer 200 * 2) a redirect has occurred since the last time t_starttransfer was set 201 * This prevents repeated invocations of the function from incorrectly 202 * changing the t_starttransfer time. 203 */ 204 if(data->progress.is_t_startransfer_set) { 205 return; 206 } 207 else { 208 data->progress.is_t_startransfer_set = true; 209 break; 210 } 211 case TIMER_POSTRANSFER: 212 /* this is the normal end-of-transfer thing */ 213 break; 214 case TIMER_REDIRECT: 215 data->progress.t_redirect = Curl_timediff_us(now, data->progress.start); 216 break; 217 } 218 if(delta) { 219 timediff_t us = Curl_timediff_us(now, data->progress.t_startsingle); 220 if(us < 1) 221 us = 1; /* make sure at least one microsecond passed */ 222 *delta += us; 223 } 224 } 225 226 void Curl_pgrsStartNow(struct Curl_easy *data) 227 { 228 data->progress.speeder_c = 0; /* reset the progress meter display */ 229 data->progress.start = Curl_now(); 230 data->progress.is_t_startransfer_set = false; 231 data->progress.ul_limit_start.tv_sec = 0; 232 data->progress.ul_limit_start.tv_usec = 0; 233 data->progress.dl_limit_start.tv_sec = 0; 234 data->progress.dl_limit_start.tv_usec = 0; 235 /* clear all bits except HIDE and HEADERS_OUT */ 236 data->progress.flags &= PGRS_HIDE|PGRS_HEADERS_OUT; 237 } 238 239 /* 240 * This is used to handle speed limits, calculating how much milliseconds we 241 * need to wait until we're back under the speed limit, if needed. 242 * 243 * The way it works is by having a "starting point" (time & amount of data 244 * transferred by then) used in the speed computation, to be used instead of 245 * the start of the transfer. This starting point is regularly moved as 246 * transfer goes on, to keep getting accurate values (instead of average over 247 * the entire transfer). 248 * 249 * This function takes the current amount of data transferred, the amount at 250 * the starting point, the limit (in bytes/s), the time of the starting point 251 * and the current time. 252 * 253 * Returns -1 if no waiting is needed (not enough data transferred since 254 * starting point yet), 0 when no waiting is needed but the starting point 255 * should be reset (to current), or the number of milliseconds to wait to get 256 * back under the speed limit. 257 */ 258 long Curl_pgrsLimitWaitTime(curl_off_t cursize, 259 curl_off_t startsize, 260 curl_off_t limit, 261 struct curltime start, 262 struct curltime now) 263 { 264 curl_off_t size = cursize - startsize; 265 time_t minimum; 266 time_t actual; 267 268 /* we don't have a starting point yet -- return 0 so it gets (re)set */ 269 if(start.tv_sec == 0 && start.tv_usec == 0) 270 return 0; 271 272 /* not enough data yet */ 273 if(size < limit) 274 return -1; 275 276 minimum = (time_t) (CURL_OFF_T_C(1000) * size / limit); 277 actual = Curl_timediff(now, start); 278 279 if(actual < minimum) 280 /* this is a conversion on some systems (64bit time_t => 32bit long) */ 281 return (long)(minimum - actual); 282 283 return 0; 284 } 285 286 void Curl_pgrsSetDownloadCounter(struct Curl_easy *data, curl_off_t size) 287 { 288 struct curltime now = Curl_now(); 289 290 data->progress.downloaded = size; 291 292 /* download speed limit */ 293 if((data->set.max_recv_speed > 0) && 294 (Curl_pgrsLimitWaitTime(data->progress.downloaded, 295 data->progress.dl_limit_size, 296 data->set.max_recv_speed, 297 data->progress.dl_limit_start, 298 now) == 0)) { 299 data->progress.dl_limit_start = now; 300 data->progress.dl_limit_size = size; 301 } 302 } 303 304 void Curl_pgrsSetUploadCounter(struct Curl_easy *data, curl_off_t size) 305 { 306 struct curltime now = Curl_now(); 307 308 data->progress.uploaded = size; 309 310 /* upload speed limit */ 311 if((data->set.max_send_speed > 0) && 312 (Curl_pgrsLimitWaitTime(data->progress.uploaded, 313 data->progress.ul_limit_size, 314 data->set.max_send_speed, 315 data->progress.ul_limit_start, 316 now) == 0)) { 317 data->progress.ul_limit_start = now; 318 data->progress.ul_limit_size = size; 319 } 320 } 321 322 void Curl_pgrsSetDownloadSize(struct Curl_easy *data, curl_off_t size) 323 { 324 if(size >= 0) { 325 data->progress.size_dl = size; 326 data->progress.flags |= PGRS_DL_SIZE_KNOWN; 327 } 328 else { 329 data->progress.size_dl = 0; 330 data->progress.flags &= ~PGRS_DL_SIZE_KNOWN; 331 } 332 } 333 334 void Curl_pgrsSetUploadSize(struct Curl_easy *data, curl_off_t size) 335 { 336 if(size >= 0) { 337 data->progress.size_ul = size; 338 data->progress.flags |= PGRS_UL_SIZE_KNOWN; 339 } 340 else { 341 data->progress.size_ul = 0; 342 data->progress.flags &= ~PGRS_UL_SIZE_KNOWN; 343 } 344 } 345 346 /* 347 * Curl_pgrsUpdate() returns 0 for success or the value returned by the 348 * progress callback! 349 */ 350 int Curl_pgrsUpdate(struct connectdata *conn) 351 { 352 struct curltime now; 353 int result; 354 char max5[6][10]; 355 curl_off_t dlpercen = 0; 356 curl_off_t ulpercen = 0; 357 curl_off_t total_percen = 0; 358 curl_off_t total_transfer; 359 curl_off_t total_expected_transfer; 360 curl_off_t timespent; 361 curl_off_t timespent_ms; /* milliseconds */ 362 struct Curl_easy *data = conn->data; 363 int nowindex = data->progress.speeder_c% CURR_TIME; 364 int checkindex; 365 int countindex; /* amount of seconds stored in the speeder array */ 366 char time_left[10]; 367 char time_total[10]; 368 char time_spent[10]; 369 curl_off_t ulestimate = 0; 370 curl_off_t dlestimate = 0; 371 curl_off_t total_estimate; 372 bool shownow = FALSE; 373 curl_off_t dl = data->progress.downloaded; 374 curl_off_t ul = data->progress.uploaded; 375 376 now = Curl_now(); /* what time is it */ 377 378 /* The time spent so far (from the start) */ 379 data->progress.timespent = Curl_timediff_us(now, data->progress.start); 380 timespent = (curl_off_t)data->progress.timespent/1000000; /* seconds */ 381 timespent_ms = (curl_off_t)data->progress.timespent/1000; /* ms */ 382 383 /* The average download speed this far */ 384 if(dl < CURL_OFF_T_MAX/1000) 385 data->progress.dlspeed = (dl * 1000 / (timespent_ms>0?timespent_ms:1)); 386 else 387 data->progress.dlspeed = (dl / (timespent>0?timespent:1)); 388 389 /* The average upload speed this far */ 390 if(ul < CURL_OFF_T_MAX/1000) 391 data->progress.ulspeed = (ul * 1000 / (timespent_ms>0?timespent_ms:1)); 392 else 393 data->progress.ulspeed = (ul / (timespent>0?timespent:1)); 394 395 /* Calculations done at most once a second, unless end is reached */ 396 if(data->progress.lastshow != now.tv_sec) { 397 shownow = TRUE; 398 399 data->progress.lastshow = now.tv_sec; 400 401 /* Let's do the "current speed" thing, with the dl + ul speeds 402 combined. Store the speed at entry 'nowindex'. */ 403 data->progress.speeder[ nowindex ] = 404 data->progress.downloaded + data->progress.uploaded; 405 406 /* remember the exact time for this moment */ 407 data->progress.speeder_time [ nowindex ] = now; 408 409 /* advance our speeder_c counter, which is increased every time we get 410 here and we expect it to never wrap as 2^32 is a lot of seconds! */ 411 data->progress.speeder_c++; 412 413 /* figure out how many index entries of data we have stored in our speeder 414 array. With N_ENTRIES filled in, we have about N_ENTRIES-1 seconds of 415 transfer. Imagine, after one second we have filled in two entries, 416 after two seconds we've filled in three entries etc. */ 417 countindex = ((data->progress.speeder_c >= CURR_TIME)? 418 CURR_TIME:data->progress.speeder_c) - 1; 419 420 /* first of all, we don't do this if there's no counted seconds yet */ 421 if(countindex) { 422 timediff_t span_ms; 423 424 /* Get the index position to compare with the 'nowindex' position. 425 Get the oldest entry possible. While we have less than CURR_TIME 426 entries, the first entry will remain the oldest. */ 427 checkindex = (data->progress.speeder_c >= CURR_TIME)? 428 data->progress.speeder_c%CURR_TIME:0; 429 430 /* Figure out the exact time for the time span */ 431 span_ms = Curl_timediff(now, 432 data->progress.speeder_time[checkindex]); 433 if(0 == span_ms) 434 span_ms = 1; /* at least one millisecond MUST have passed */ 435 436 /* Calculate the average speed the last 'span_ms' milliseconds */ 437 { 438 curl_off_t amount = data->progress.speeder[nowindex]- 439 data->progress.speeder[checkindex]; 440 441 if(amount > CURL_OFF_T_C(4294967) /* 0xffffffff/1000 */) 442 /* the 'amount' value is bigger than would fit in 32 bits if 443 multiplied with 1000, so we use the double math for this */ 444 data->progress.current_speed = (curl_off_t) 445 ((double)amount/((double)span_ms/1000.0)); 446 else 447 /* the 'amount' value is small enough to fit within 32 bits even 448 when multiplied with 1000 */ 449 data->progress.current_speed = amount*CURL_OFF_T_C(1000)/span_ms; 450 } 451 } 452 else 453 /* the first second we use the average */ 454 data->progress.current_speed = 455 data->progress.ulspeed + data->progress.dlspeed; 456 457 } /* Calculations end */ 458 459 if(!(data->progress.flags & PGRS_HIDE)) { 460 /* progress meter has not been shut off */ 461 462 if(data->set.fxferinfo) { 463 /* There's a callback set, call that */ 464 result = data->set.fxferinfo(data->set.progress_client, 465 data->progress.size_dl, 466 data->progress.downloaded, 467 data->progress.size_ul, 468 data->progress.uploaded); 469 if(result) 470 failf(data, "Callback aborted"); 471 return result; 472 } 473 if(data->set.fprogress) { 474 /* The older deprecated callback is set, call that */ 475 result = data->set.fprogress(data->set.progress_client, 476 (double)data->progress.size_dl, 477 (double)data->progress.downloaded, 478 (double)data->progress.size_ul, 479 (double)data->progress.uploaded); 480 if(result) 481 failf(data, "Callback aborted"); 482 return result; 483 } 484 485 if(!shownow) 486 /* only show the internal progress meter once per second */ 487 return 0; 488 489 /* If there's no external callback set, use internal code to show 490 progress */ 491 492 if(!(data->progress.flags & PGRS_HEADERS_OUT)) { 493 if(data->state.resume_from) { 494 fprintf(data->set.err, 495 "** Resuming transfer from byte position %" 496 CURL_FORMAT_CURL_OFF_T "\n", data->state.resume_from); 497 } 498 fprintf(data->set.err, 499 " %% Total %% Received %% Xferd Average Speed " 500 "Time Time Time Current\n" 501 " Dload Upload " 502 "Total Spent Left Speed\n"); 503 data->progress.flags |= PGRS_HEADERS_OUT; /* headers are shown */ 504 } 505 506 /* Figure out the estimated time of arrival for the upload */ 507 if((data->progress.flags & PGRS_UL_SIZE_KNOWN) && 508 (data->progress.ulspeed > CURL_OFF_T_C(0))) { 509 ulestimate = data->progress.size_ul / data->progress.ulspeed; 510 511 if(data->progress.size_ul > CURL_OFF_T_C(10000)) 512 ulpercen = data->progress.uploaded / 513 (data->progress.size_ul/CURL_OFF_T_C(100)); 514 else if(data->progress.size_ul > CURL_OFF_T_C(0)) 515 ulpercen = (data->progress.uploaded*100) / 516 data->progress.size_ul; 517 } 518 519 /* ... and the download */ 520 if((data->progress.flags & PGRS_DL_SIZE_KNOWN) && 521 (data->progress.dlspeed > CURL_OFF_T_C(0))) { 522 dlestimate = data->progress.size_dl / data->progress.dlspeed; 523 524 if(data->progress.size_dl > CURL_OFF_T_C(10000)) 525 dlpercen = data->progress.downloaded / 526 (data->progress.size_dl/CURL_OFF_T_C(100)); 527 else if(data->progress.size_dl > CURL_OFF_T_C(0)) 528 dlpercen = (data->progress.downloaded*100) / 529 data->progress.size_dl; 530 } 531 532 /* Now figure out which of them is slower and use that one for the 533 total estimate! */ 534 total_estimate = ulestimate>dlestimate?ulestimate:dlestimate; 535 536 /* create the three time strings */ 537 time2str(time_left, total_estimate > 0?(total_estimate - timespent):0); 538 time2str(time_total, total_estimate); 539 time2str(time_spent, timespent); 540 541 /* Get the total amount of data expected to get transferred */ 542 total_expected_transfer = 543 (data->progress.flags & PGRS_UL_SIZE_KNOWN? 544 data->progress.size_ul:data->progress.uploaded)+ 545 (data->progress.flags & PGRS_DL_SIZE_KNOWN? 546 data->progress.size_dl:data->progress.downloaded); 547 548 /* We have transferred this much so far */ 549 total_transfer = data->progress.downloaded + data->progress.uploaded; 550 551 /* Get the percentage of data transferred so far */ 552 if(total_expected_transfer > CURL_OFF_T_C(10000)) 553 total_percen = total_transfer / 554 (total_expected_transfer/CURL_OFF_T_C(100)); 555 else if(total_expected_transfer > CURL_OFF_T_C(0)) 556 total_percen = (total_transfer*100) / total_expected_transfer; 557 558 fprintf(data->set.err, 559 "\r" 560 "%3" CURL_FORMAT_CURL_OFF_T " %s " 561 "%3" CURL_FORMAT_CURL_OFF_T " %s " 562 "%3" CURL_FORMAT_CURL_OFF_T " %s %s %s %s %s %s %s", 563 total_percen, /* 3 letters */ /* total % */ 564 max5data(total_expected_transfer, max5[2]), /* total size */ 565 dlpercen, /* 3 letters */ /* rcvd % */ 566 max5data(data->progress.downloaded, max5[0]), /* rcvd size */ 567 ulpercen, /* 3 letters */ /* xfer % */ 568 max5data(data->progress.uploaded, max5[1]), /* xfer size */ 569 max5data(data->progress.dlspeed, max5[3]), /* avrg dl speed */ 570 max5data(data->progress.ulspeed, max5[4]), /* avrg ul speed */ 571 time_total, /* 8 letters */ /* total time */ 572 time_spent, /* 8 letters */ /* time spent */ 573 time_left, /* 8 letters */ /* time left */ 574 max5data(data->progress.current_speed, max5[5]) /* current speed */ 575 ); 576 577 /* we flush the output stream to make it appear as soon as possible */ 578 fflush(data->set.err); 579 580 } /* !(data->progress.flags & PGRS_HIDE) */ 581 582 return 0; 583 } 584