progress.c 16 KB

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