progress.c 14 KB

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