source: asp3_tinet_ecnl_rx/trunk/curl-7.57.0/lib/progress.c@ 337

Last change on this file since 337 was 337, checked in by coas-nagasima, 6 years ago

ASP3版ECNLを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 20.1 KB
Line 
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2017, 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 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) */
32static 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... */
62static 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
133int 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 */
153void Curl_pgrsResetTransferSizes(struct Curl_easy *data)
154{
155 Curl_pgrsSetDownloadSize(data, -1);
156 Curl_pgrsSetUploadSize(data, -1);
157}
158
159/*
160 * @unittest: 1399
161 */
162void 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
226void 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 */
258long 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
286void 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
304void 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
322void 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
334void 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 */
350int 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 struct Curl_easy *data = conn->data;
362 int nowindex = data->progress.speeder_c% CURR_TIME;
363 int checkindex;
364 int countindex; /* amount of seconds stored in the speeder array */
365 char time_left[10];
366 char time_total[10];
367 char time_spent[10];
368 curl_off_t ulestimate = 0;
369 curl_off_t dlestimate = 0;
370 curl_off_t total_estimate;
371 bool shownow = FALSE;
372
373 now = Curl_now(); /* what time is it */
374
375 /* The time spent so far (from the start) */
376 data->progress.timespent = Curl_timediff_us(now, data->progress.start);
377 timespent = (curl_off_t)data->progress.timespent/1000000; /* seconds */
378
379 /* The average download speed this far */
380 data->progress.dlspeed = (curl_off_t)
381 (data->progress.downloaded/
382 (timespent>0?timespent:1));
383
384 /* The average upload speed this far */
385 data->progress.ulspeed = (curl_off_t)
386 (data->progress.uploaded/
387 (timespent>0?timespent:1));
388
389 /* Calculations done at most once a second, unless end is reached */
390 if(data->progress.lastshow != now.tv_sec) {
391 shownow = TRUE;
392
393 data->progress.lastshow = now.tv_sec;
394
395 /* Let's do the "current speed" thing, with the dl + ul speeds
396 combined. Store the speed at entry 'nowindex'. */
397 data->progress.speeder[ nowindex ] =
398 data->progress.downloaded + data->progress.uploaded;
399
400 /* remember the exact time for this moment */
401 data->progress.speeder_time [ nowindex ] = now;
402
403 /* advance our speeder_c counter, which is increased every time we get
404 here and we expect it to never wrap as 2^32 is a lot of seconds! */
405 data->progress.speeder_c++;
406
407 /* figure out how many index entries of data we have stored in our speeder
408 array. With N_ENTRIES filled in, we have about N_ENTRIES-1 seconds of
409 transfer. Imagine, after one second we have filled in two entries,
410 after two seconds we've filled in three entries etc. */
411 countindex = ((data->progress.speeder_c >= CURR_TIME)?
412 CURR_TIME:data->progress.speeder_c) - 1;
413
414 /* first of all, we don't do this if there's no counted seconds yet */
415 if(countindex) {
416 timediff_t span_ms;
417
418 /* Get the index position to compare with the 'nowindex' position.
419 Get the oldest entry possible. While we have less than CURR_TIME
420 entries, the first entry will remain the oldest. */
421 checkindex = (data->progress.speeder_c >= CURR_TIME)?
422 data->progress.speeder_c%CURR_TIME:0;
423
424 /* Figure out the exact time for the time span */
425 span_ms = Curl_timediff(now,
426 data->progress.speeder_time[checkindex]);
427 if(0 == span_ms)
428 span_ms = 1; /* at least one millisecond MUST have passed */
429
430 /* Calculate the average speed the last 'span_ms' milliseconds */
431 {
432 curl_off_t amount = data->progress.speeder[nowindex]-
433 data->progress.speeder[checkindex];
434
435 if(amount > CURL_OFF_T_C(4294967) /* 0xffffffff/1000 */)
436 /* the 'amount' value is bigger than would fit in 32 bits if
437 multiplied with 1000, so we use the double math for this */
438 data->progress.current_speed = (curl_off_t)
439 ((double)amount/((double)span_ms/1000.0));
440 else
441 /* the 'amount' value is small enough to fit within 32 bits even
442 when multiplied with 1000 */
443 data->progress.current_speed = amount*CURL_OFF_T_C(1000)/span_ms;
444 }
445 }
446 else
447 /* the first second we use the average */
448 data->progress.current_speed =
449 data->progress.ulspeed + data->progress.dlspeed;
450
451 } /* Calculations end */
452
453 if(!(data->progress.flags & PGRS_HIDE)) {
454 /* progress meter has not been shut off */
455
456 if(data->set.fxferinfo) {
457 /* There's a callback set, call that */
458 result = data->set.fxferinfo(data->set.progress_client,
459 data->progress.size_dl,
460 data->progress.downloaded,
461 data->progress.size_ul,
462 data->progress.uploaded);
463 if(result)
464 failf(data, "Callback aborted");
465 return result;
466 }
467 if(data->set.fprogress) {
468 /* The older deprecated callback is set, call that */
469 result = data->set.fprogress(data->set.progress_client,
470 (double)data->progress.size_dl,
471 (double)data->progress.downloaded,
472 (double)data->progress.size_ul,
473 (double)data->progress.uploaded);
474 if(result)
475 failf(data, "Callback aborted");
476 return result;
477 }
478
479 if(!shownow)
480 /* only show the internal progress meter once per second */
481 return 0;
482
483 /* If there's no external callback set, use internal code to show
484 progress */
485
486 if(!(data->progress.flags & PGRS_HEADERS_OUT)) {
487 if(data->state.resume_from) {
488 fprintf(data->set.err,
489 "** Resuming transfer from byte position %"
490 CURL_FORMAT_CURL_OFF_T "\n", data->state.resume_from);
491 }
492 fprintf(data->set.err,
493 " %% Total %% Received %% Xferd Average Speed "
494 "Time Time Time Current\n"
495 " Dload Upload "
496 "Total Spent Left Speed\n");
497 data->progress.flags |= PGRS_HEADERS_OUT; /* headers are shown */
498 }
499
500 /* Figure out the estimated time of arrival for the upload */
501 if((data->progress.flags & PGRS_UL_SIZE_KNOWN) &&
502 (data->progress.ulspeed > CURL_OFF_T_C(0))) {
503 ulestimate = data->progress.size_ul / data->progress.ulspeed;
504
505 if(data->progress.size_ul > CURL_OFF_T_C(10000))
506 ulpercen = data->progress.uploaded /
507 (data->progress.size_ul/CURL_OFF_T_C(100));
508 else if(data->progress.size_ul > CURL_OFF_T_C(0))
509 ulpercen = (data->progress.uploaded*100) /
510 data->progress.size_ul;
511 }
512
513 /* ... and the download */
514 if((data->progress.flags & PGRS_DL_SIZE_KNOWN) &&
515 (data->progress.dlspeed > CURL_OFF_T_C(0))) {
516 dlestimate = data->progress.size_dl / data->progress.dlspeed;
517
518 if(data->progress.size_dl > CURL_OFF_T_C(10000))
519 dlpercen = data->progress.downloaded /
520 (data->progress.size_dl/CURL_OFF_T_C(100));
521 else if(data->progress.size_dl > CURL_OFF_T_C(0))
522 dlpercen = (data->progress.downloaded*100) /
523 data->progress.size_dl;
524 }
525
526 /* Now figure out which of them is slower and use that one for the
527 total estimate! */
528 total_estimate = ulestimate>dlestimate?ulestimate:dlestimate;
529
530 /* create the three time strings */
531 time2str(time_left, total_estimate > 0?(total_estimate - timespent):0);
532 time2str(time_total, total_estimate);
533 time2str(time_spent, timespent);
534
535 /* Get the total amount of data expected to get transferred */
536 total_expected_transfer =
537 (data->progress.flags & PGRS_UL_SIZE_KNOWN?
538 data->progress.size_ul:data->progress.uploaded)+
539 (data->progress.flags & PGRS_DL_SIZE_KNOWN?
540 data->progress.size_dl:data->progress.downloaded);
541
542 /* We have transferred this much so far */
543 total_transfer = data->progress.downloaded + data->progress.uploaded;
544
545 /* Get the percentage of data transferred so far */
546 if(total_expected_transfer > CURL_OFF_T_C(10000))
547 total_percen = total_transfer /
548 (total_expected_transfer/CURL_OFF_T_C(100));
549 else if(total_expected_transfer > CURL_OFF_T_C(0))
550 total_percen = (total_transfer*100) / total_expected_transfer;
551
552 fprintf(data->set.err,
553 "\r"
554 "%3" CURL_FORMAT_CURL_OFF_T " %s "
555 "%3" CURL_FORMAT_CURL_OFF_T " %s "
556 "%3" CURL_FORMAT_CURL_OFF_T " %s %s %s %s %s %s %s",
557 total_percen, /* 3 letters */ /* total % */
558 max5data(total_expected_transfer, max5[2]), /* total size */
559 dlpercen, /* 3 letters */ /* rcvd % */
560 max5data(data->progress.downloaded, max5[0]), /* rcvd size */
561 ulpercen, /* 3 letters */ /* xfer % */
562 max5data(data->progress.uploaded, max5[1]), /* xfer size */
563 max5data(data->progress.dlspeed, max5[3]), /* avrg dl speed */
564 max5data(data->progress.ulspeed, max5[4]), /* avrg ul speed */
565 time_total, /* 8 letters */ /* total time */
566 time_spent, /* 8 letters */ /* time spent */
567 time_left, /* 8 letters */ /* time left */
568 max5data(data->progress.current_speed, max5[5]) /* current speed */
569 );
570
571 /* we flush the output stream to make it appear as soon as possible */
572 fflush(data->set.err);
573
574 } /* !(data->progress.flags & PGRS_HIDE) */
575
576 return 0;
577}
Note: See TracBrowser for help on using the repository browser.