Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl-7.19.0 / lib / progress.c
blob45adb426d7789ac39f629d20ca46b2aca48b822a
1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
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.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * $Id: progress.c,v 1.1.1.1 2008-09-23 16:32:05 hoffman Exp $
22 ***************************************************************************/
24 #include "setup.h"
26 #include "urldata.h"
27 #include "sendf.h"
28 #include "progress.h"
30 #define _MPRINTF_REPLACE /* use our functions only */
31 #include <curl/mprintf.h>
33 /* Provide a string that is 2 + 1 + 2 + 1 + 2 = 8 letters long (plus the zero
34 byte) */
35 static void time2str(char *r, long t)
37 long h;
38 if(!t) {
39 strcpy(r, "--:--:--");
40 return;
42 h = (t/3600);
43 if(h <= 99) {
44 long m = (t-(h*3600))/60;
45 long s = (t-(h*3600)-(m*60));
46 snprintf(r, 9, "%2ld:%02ld:%02ld",h,m,s);
48 else {
49 /* this equals to more than 99 hours, switch to a more suitable output
50 format to fit within the limits. */
51 if(h/24 <= 999)
52 snprintf(r, 9, "%3ldd %02ldh", h/24, h-(h/24)*24);
53 else
54 snprintf(r, 9, "%7ldd", h/24);
58 /* The point of this function would be to return a string of the input data,
59 but never longer than 5 columns (+ one zero byte).
60 Add suffix k, M, G when suitable... */
61 static char *max5data(curl_off_t bytes, char *max5)
63 #define ONE_KILOBYTE CURL_OFF_T_C(1024)
64 #define ONE_MEGABYTE (CURL_OFF_T_C(1024) * ONE_KILOBYTE)
65 #define ONE_GIGABYTE (CURL_OFF_T_C(1024) * ONE_MEGABYTE)
66 #define ONE_TERABYTE (CURL_OFF_T_C(1024) * ONE_GIGABYTE)
67 #define ONE_PETABYTE (CURL_OFF_T_C(1024) * ONE_TERABYTE)
69 if(bytes < CURL_OFF_T_C(100000))
70 snprintf(max5, 6, "%5" FORMAT_OFF_T, bytes);
72 else if(bytes < CURL_OFF_T_C(10000) * ONE_KILOBYTE)
73 snprintf(max5, 6, "%4" FORMAT_OFF_T "k", bytes/ONE_KILOBYTE);
75 else if(bytes < CURL_OFF_T_C(100) * ONE_MEGABYTE)
76 /* 'XX.XM' is good as long as we're less than 100 megs */
77 snprintf(max5, 6, "%2" FORMAT_OFF_T ".%0" FORMAT_OFF_T "M",
78 bytes/ONE_MEGABYTE,
79 (bytes%ONE_MEGABYTE) / (ONE_MEGABYTE/CURL_OFF_T_C(10)) );
81 #if (CURL_SIZEOF_CURL_OFF_T > 4)
83 else if(bytes < CURL_OFF_T_C(10000) * ONE_MEGABYTE)
84 /* 'XXXXM' is good until we're at 10000MB or above */
85 snprintf(max5, 6, "%4" FORMAT_OFF_T "M", bytes/ONE_MEGABYTE);
87 else if(bytes < CURL_OFF_T_C(100) * ONE_GIGABYTE)
88 /* 10000 MB - 100 GB, we show it as XX.XG */
89 snprintf(max5, 6, "%2" FORMAT_OFF_T ".%0" FORMAT_OFF_T "G",
90 bytes/ONE_GIGABYTE,
91 (bytes%ONE_GIGABYTE) / (ONE_GIGABYTE/CURL_OFF_T_C(10)) );
93 else if(bytes < CURL_OFF_T_C(10000) * ONE_GIGABYTE)
94 /* up to 10000GB, display without decimal: XXXXG */
95 snprintf(max5, 6, "%4" FORMAT_OFF_T "G", bytes/ONE_GIGABYTE);
97 else if(bytes < CURL_OFF_T_C(10000) * ONE_TERABYTE)
98 /* up to 10000TB, display without decimal: XXXXT */
99 snprintf(max5, 6, "%4" FORMAT_OFF_T "T", bytes/ONE_TERABYTE);
101 else
102 /* up to 10000PB, display without decimal: XXXXP */
103 snprintf(max5, 6, "%4" FORMAT_OFF_T "P", bytes/ONE_PETABYTE);
105 /* 16384 petabytes (16 exabytes) is the maximum a 64 bit unsigned number
106 can hold, but our data type is signed so 8192PB will be the maximum. */
108 #else
110 else
111 snprintf(max5, 6, "%4" FORMAT_OFF_T "M", bytes/ONE_MEGABYTE);
113 #endif
115 return max5;
120 New proposed interface, 9th of February 2000:
122 pgrsStartNow() - sets start time
123 pgrsSetDownloadSize(x) - known expected download size
124 pgrsSetUploadSize(x) - known expected upload size
125 pgrsSetDownloadCounter() - amount of data currently downloaded
126 pgrsSetUploadCounter() - amount of data currently uploaded
127 pgrsUpdate() - show progress
128 pgrsDone() - transfer complete
132 void Curl_pgrsDone(struct connectdata *conn)
134 struct SessionHandle *data = conn->data;
135 data->progress.lastshow=0;
136 Curl_pgrsUpdate(conn); /* the final (forced) update */
138 data->progress.speeder_c = 0; /* reset the progress meter display */
141 /* reset all times except redirect */
142 void Curl_pgrsResetTimes(struct SessionHandle *data)
144 data->progress.t_nslookup = 0.0;
145 data->progress.t_connect = 0.0;
146 data->progress.t_pretransfer = 0.0;
147 data->progress.t_starttransfer = 0.0;
150 void Curl_pgrsTime(struct SessionHandle *data, timerid timer)
152 switch(timer) {
153 default:
154 case TIMER_NONE:
155 /* mistake filter */
156 break;
157 case TIMER_STARTSINGLE:
158 /* This is set at the start of a single fetch */
159 data->progress.t_startsingle = Curl_tvnow();
160 break;
162 case TIMER_NAMELOOKUP:
163 data->progress.t_nslookup =
164 Curl_tvdiff_secs(Curl_tvnow(), data->progress.t_startsingle);
165 break;
166 case TIMER_CONNECT:
167 data->progress.t_connect =
168 Curl_tvdiff_secs(Curl_tvnow(), data->progress.t_startsingle);
169 break;
170 case TIMER_APPCONNECT:
171 data->progress.t_appconnect =
172 Curl_tvdiff_secs(Curl_tvnow(), data->progress.t_startsingle);
173 break;
174 case TIMER_PRETRANSFER:
175 data->progress.t_pretransfer =
176 Curl_tvdiff_secs(Curl_tvnow(), data->progress.t_startsingle);
177 break;
178 case TIMER_STARTTRANSFER:
179 data->progress.t_starttransfer =
180 Curl_tvdiff_secs(Curl_tvnow(), data->progress.t_startsingle);
181 break;
182 case TIMER_POSTRANSFER:
183 /* this is the normal end-of-transfer thing */
184 break;
185 case TIMER_REDIRECT:
186 data->progress.t_redirect =
187 Curl_tvdiff_secs(Curl_tvnow(), data->progress.start);
188 break;
192 void Curl_pgrsStartNow(struct SessionHandle *data)
194 data->progress.speeder_c = 0; /* reset the progress meter display */
195 data->progress.start = Curl_tvnow();
198 void Curl_pgrsSetDownloadCounter(struct SessionHandle *data, curl_off_t size)
200 data->progress.downloaded = size;
203 void Curl_pgrsSetUploadCounter(struct SessionHandle *data, curl_off_t size)
205 data->progress.uploaded = size;
208 void Curl_pgrsSetDownloadSize(struct SessionHandle *data, curl_off_t size)
210 data->progress.size_dl = size;
211 if(size > 0)
212 data->progress.flags |= PGRS_DL_SIZE_KNOWN;
213 else
214 data->progress.flags &= ~PGRS_DL_SIZE_KNOWN;
217 void Curl_pgrsSetUploadSize(struct SessionHandle *data, curl_off_t size)
219 data->progress.size_ul = size;
220 if(size > 0)
221 data->progress.flags |= PGRS_UL_SIZE_KNOWN;
222 else
223 data->progress.flags &= ~PGRS_UL_SIZE_KNOWN;
226 int Curl_pgrsUpdate(struct connectdata *conn)
228 struct timeval now;
229 int result;
230 char max5[6][10];
231 int dlpercen=0;
232 int ulpercen=0;
233 int total_percen=0;
234 curl_off_t total_transfer;
235 curl_off_t total_expected_transfer;
236 long timespent;
237 struct SessionHandle *data = conn->data;
238 int nowindex = data->progress.speeder_c% CURR_TIME;
239 int checkindex;
240 int countindex; /* amount of seconds stored in the speeder array */
241 char time_left[10];
242 char time_total[10];
243 char time_spent[10];
244 long ulestimate=0;
245 long dlestimate=0;
246 long total_estimate;
247 bool shownow=FALSE;
249 now = Curl_tvnow(); /* what time is it */
251 /* The time spent so far (from the start) */
252 data->progress.timespent =
253 (double)(now.tv_sec - data->progress.start.tv_sec) +
254 (double)(now.tv_usec - data->progress.start.tv_usec)/1000000.0;
255 timespent = (long)data->progress.timespent;
257 /* The average download speed this far */
258 data->progress.dlspeed = (curl_off_t)
259 ((double)data->progress.downloaded/
260 (data->progress.timespent>0?data->progress.timespent:1));
262 /* The average upload speed this far */
263 data->progress.ulspeed = (curl_off_t)
264 ((double)data->progress.uploaded/
265 (data->progress.timespent>0?data->progress.timespent:1));
267 /* Calculations done at most once a second, unless end is reached */
268 if(data->progress.lastshow != (long)now.tv_sec) {
269 shownow = TRUE;
271 data->progress.lastshow = now.tv_sec;
273 /* Let's do the "current speed" thing, which should use the fastest
274 of the dl/ul speeds. Store the faster speed at entry 'nowindex'. */
275 data->progress.speeder[ nowindex ] =
276 data->progress.downloaded>data->progress.uploaded?
277 data->progress.downloaded:data->progress.uploaded;
279 /* remember the exact time for this moment */
280 data->progress.speeder_time [ nowindex ] = now;
282 /* advance our speeder_c counter, which is increased every time we get
283 here and we expect it to never wrap as 2^32 is a lot of seconds! */
284 data->progress.speeder_c++;
286 /* figure out how many index entries of data we have stored in our speeder
287 array. With N_ENTRIES filled in, we have about N_ENTRIES-1 seconds of
288 transfer. Imagine, after one second we have filled in two entries,
289 after two seconds we've filled in three entries etc. */
290 countindex = ((data->progress.speeder_c>=CURR_TIME)?
291 CURR_TIME:data->progress.speeder_c) - 1;
293 /* first of all, we don't do this if there's no counted seconds yet */
294 if(countindex) {
295 long span_ms;
297 /* Get the index position to compare with the 'nowindex' position.
298 Get the oldest entry possible. While we have less than CURR_TIME
299 entries, the first entry will remain the oldest. */
300 checkindex = (data->progress.speeder_c>=CURR_TIME)?
301 data->progress.speeder_c%CURR_TIME:0;
303 /* Figure out the exact time for the time span */
304 span_ms = Curl_tvdiff(now,
305 data->progress.speeder_time[checkindex]);
306 if(0 == span_ms)
307 span_ms=1; /* at least one millisecond MUST have passed */
309 /* Calculate the average speed the last 'span_ms' milliseconds */
311 curl_off_t amount = data->progress.speeder[nowindex]-
312 data->progress.speeder[checkindex];
314 if(amount > CURL_OFF_T_C(4294967) /* 0xffffffff/1000 */)
315 /* the 'amount' value is bigger than would fit in 32 bits if
316 multiplied with 1000, so we use the double math for this */
317 data->progress.current_speed = (curl_off_t)
318 ((double)amount/((double)span_ms/1000.0));
319 else
320 /* the 'amount' value is small enough to fit within 32 bits even
321 when multiplied with 1000 */
322 data->progress.current_speed = amount*CURL_OFF_T_C(1000)/span_ms;
325 else
326 /* the first second we use the main average */
327 data->progress.current_speed =
328 (data->progress.ulspeed>data->progress.dlspeed)?
329 data->progress.ulspeed:data->progress.dlspeed;
331 } /* Calculations end */
333 if(!(data->progress.flags & PGRS_HIDE)) {
335 /* progress meter has not been shut off */
337 if(data->set.fprogress) {
338 /* There's a callback set, so we call that instead of writing
339 anything ourselves. This really is the way to go. */
340 result= data->set.fprogress(data->set.progress_client,
341 (double)data->progress.size_dl,
342 (double)data->progress.downloaded,
343 (double)data->progress.size_ul,
344 (double)data->progress.uploaded);
345 if(result)
346 failf(data, "Callback aborted");
347 return result;
350 if(!shownow)
351 /* only show the internal progress meter once per second */
352 return 0;
354 /* If there's no external callback set, use internal code to show
355 progress */
357 if(!(data->progress.flags & PGRS_HEADERS_OUT)) {
358 if(data->state.resume_from) {
359 fprintf(data->set.err,
360 "** Resuming transfer from byte position %" FORMAT_OFF_T "\n",
361 data->state.resume_from);
363 fprintf(data->set.err,
364 " %% Total %% Received %% Xferd Average Speed Time Time Time Current\n"
365 " Dload Upload Total Spent Left Speed\n");
366 data->progress.flags |= PGRS_HEADERS_OUT; /* headers are shown */
369 /* Figure out the estimated time of arrival for the upload */
370 if((data->progress.flags & PGRS_UL_SIZE_KNOWN) &&
371 (data->progress.ulspeed>0) &&
372 (data->progress.size_ul > 100) ) {
373 ulestimate = (long)(data->progress.size_ul / data->progress.ulspeed);
374 ulpercen = (int)(100*(data->progress.uploaded/100) /
375 (data->progress.size_ul/100) );
378 /* ... and the download */
379 if((data->progress.flags & PGRS_DL_SIZE_KNOWN) &&
380 (data->progress.dlspeed>0) &&
381 (data->progress.size_dl>100)) {
382 dlestimate = (long)(data->progress.size_dl / data->progress.dlspeed);
383 dlpercen = (int)(100*(data->progress.downloaded/100) /
384 (data->progress.size_dl/100));
387 /* Now figure out which of them is slower and use that one for the
388 total estimate! */
389 total_estimate = ulestimate>dlestimate?ulestimate:dlestimate;
391 /* create the three time strings */
392 time2str(time_left, total_estimate > 0?(total_estimate - timespent):0);
393 time2str(time_total, total_estimate);
394 time2str(time_spent, timespent);
396 /* Get the total amount of data expected to get transfered */
397 total_expected_transfer =
398 (data->progress.flags & PGRS_UL_SIZE_KNOWN?
399 data->progress.size_ul:data->progress.uploaded)+
400 (data->progress.flags & PGRS_DL_SIZE_KNOWN?
401 data->progress.size_dl:data->progress.downloaded);
403 /* We have transfered this much so far */
404 total_transfer = data->progress.downloaded + data->progress.uploaded;
406 /* Get the percentage of data transfered so far */
407 if(total_expected_transfer > 100)
408 total_percen=(int)(100*(total_transfer/100) /
409 (total_expected_transfer/100) );
411 fprintf(data->set.err,
412 "\r%3d %s %3d %s %3d %s %s %s %s %s %s %s",
413 total_percen, /* 3 letters */ /* total % */
414 max5data(total_expected_transfer, max5[2]), /* total size */
415 dlpercen, /* 3 letters */ /* rcvd % */
416 max5data(data->progress.downloaded, max5[0]), /* rcvd size */
417 ulpercen, /* 3 letters */ /* xfer % */
418 max5data(data->progress.uploaded, max5[1]), /* xfer size */
419 max5data(data->progress.dlspeed, max5[3]), /* avrg dl speed */
420 max5data(data->progress.ulspeed, max5[4]), /* avrg ul speed */
421 time_total, /* 8 letters */ /* total time */
422 time_spent, /* 8 letters */ /* time spent */
423 time_left, /* 8 letters */ /* time left */
424 max5data(data->progress.current_speed, max5[5]) /* current speed */
427 /* we flush the output stream to make it appear as soon as possible */
428 fflush(data->set.err);
430 } /* !(data->progress.flags & PGRS_HIDE) */
432 return 0;