progress.c 16 KB

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