progress.c 15 KB

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