progress.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2002, 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. #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
  26. #if defined(__MINGW32__)
  27. #include <winsock.h>
  28. #endif
  29. #include <time.h>
  30. #endif
  31. /* 20000318 mgs
  32. * later we use _scrsize to determine the screen width, this emx library
  33. * function needs stdlib.h to be included */
  34. #if defined(__EMX__)
  35. #include <stdlib.h>
  36. #endif
  37. #include <curl/curl.h>
  38. #include "urldata.h"
  39. #include "sendf.h"
  40. #include "progress.h"
  41. #define _MPRINTF_REPLACE /* use our functions only */
  42. #include <curl/mprintf.h>
  43. static void time2str(char *r, int t)
  44. {
  45. int h = (t/3600);
  46. int m = (t-(h*3600))/60;
  47. int s = (t-(h*3600)-(m*60));
  48. sprintf(r,"%2d:%02d:%02d",h,m,s);
  49. }
  50. /* The point of this function would be to return a string of the input data,
  51. but never longer than 5 columns. Add suffix k, M, G when suitable... */
  52. static char *max5data(double bytes, char *max5)
  53. {
  54. #define ONE_KILOBYTE 1024
  55. #define ONE_MEGABYTE (1024*1024)
  56. if(bytes < 100000) {
  57. sprintf(max5, "%5d", (int)bytes);
  58. return max5;
  59. }
  60. if(bytes < (9999*ONE_KILOBYTE)) {
  61. sprintf(max5, "%4dk", (int)bytes/ONE_KILOBYTE);
  62. return max5;
  63. }
  64. if(bytes < (100*ONE_MEGABYTE)) {
  65. /* 'XX.XM' is good as long as we're less than 100 megs */
  66. sprintf(max5, "%4.1fM", bytes/ONE_MEGABYTE);
  67. return max5;
  68. }
  69. sprintf(max5, "%4dM", (int)bytes/ONE_MEGABYTE);
  70. return max5;
  71. }
  72. /*
  73. New proposed interface, 9th of February 2000:
  74. pgrsStartNow() - sets start time
  75. pgrsSetDownloadSize(x) - known expected download size
  76. pgrsSetUploadSize(x) - known expected upload size
  77. pgrsSetDownloadCounter() - amount of data currently downloaded
  78. pgrsSetUploadCounter() - amount of data currently uploaded
  79. pgrsUpdate() - show progress
  80. pgrsDone() - transfer complete
  81. */
  82. void Curl_pgrsDone(struct connectdata *conn)
  83. {
  84. struct SessionHandle *data = conn->data;
  85. if(!(data->progress.flags & PGRS_HIDE)) {
  86. data->progress.lastshow=0;
  87. Curl_pgrsUpdate(conn); /* the final (forced) update */
  88. if(!data->progress.callback)
  89. /* only output if we don't use progress callback */
  90. fprintf(data->set.err, "\n");
  91. }
  92. }
  93. /* reset all times except redirect */
  94. void Curl_pgrsResetTimes(struct SessionHandle *data)
  95. {
  96. data->progress.t_nslookup = 0.0;
  97. data->progress.t_connect = 0.0;
  98. data->progress.t_pretransfer = 0.0;
  99. data->progress.t_starttransfer = 0.0;
  100. }
  101. void Curl_pgrsTime(struct SessionHandle *data, timerid timer)
  102. {
  103. switch(timer) {
  104. default:
  105. case TIMER_NONE:
  106. /* mistake filter */
  107. break;
  108. case TIMER_STARTSINGLE:
  109. /* This is set at the start of a single fetch */
  110. data->progress.t_startsingle = Curl_tvnow();
  111. break;
  112. case TIMER_NAMELOOKUP:
  113. data->progress.t_nslookup =
  114. (double)Curl_tvdiff(Curl_tvnow(), data->progress.t_startsingle)/1000.0;
  115. break;
  116. case TIMER_CONNECT:
  117. data->progress.t_connect =
  118. (double)Curl_tvdiff(Curl_tvnow(), data->progress.t_startsingle)/1000.0;
  119. break;
  120. case TIMER_PRETRANSFER:
  121. data->progress.t_pretransfer =
  122. (double)Curl_tvdiff(Curl_tvnow(), data->progress.t_startsingle)/1000.0;
  123. break;
  124. case TIMER_STARTTRANSFER:
  125. data->progress.t_starttransfer =
  126. (double)Curl_tvdiff(Curl_tvnow(), data->progress.t_startsingle)/1000.0;
  127. break;
  128. case TIMER_POSTRANSFER:
  129. /* this is the normal end-of-transfer thing */
  130. break;
  131. case TIMER_REDIRECT:
  132. data->progress.t_redirect =
  133. (double)Curl_tvdiff(Curl_tvnow(), data->progress.start)/1000.0;
  134. break;
  135. }
  136. }
  137. void Curl_pgrsStartNow(struct SessionHandle *data)
  138. {
  139. data->progress.speeder_c = 0; /* reset the progress meter display */
  140. data->progress.start = Curl_tvnow();
  141. }
  142. void Curl_pgrsSetDownloadCounter(struct SessionHandle *data, double size)
  143. {
  144. data->progress.downloaded = size;
  145. }
  146. void Curl_pgrsSetUploadCounter(struct SessionHandle *data, double size)
  147. {
  148. data->progress.uploaded = size;
  149. }
  150. void Curl_pgrsSetDownloadSize(struct SessionHandle *data, double size)
  151. {
  152. if(size > 0) {
  153. data->progress.size_dl = size;
  154. data->progress.flags |= PGRS_DL_SIZE_KNOWN;
  155. }
  156. }
  157. void Curl_pgrsSetUploadSize(struct SessionHandle *data, double size)
  158. {
  159. if(size > 0) {
  160. data->progress.size_ul = size;
  161. data->progress.flags |= PGRS_UL_SIZE_KNOWN;
  162. }
  163. }
  164. /* EXAMPLE OUTPUT to follow:
  165. % Total % Received % Xferd Average Speed Time Curr.
  166. Dload Upload Total Current Left Speed
  167. 100 12345 100 12345 100 12345 12345 12345 12:12:12 12:12:12 12:12:12 12345
  168. */
  169. int Curl_pgrsUpdate(struct connectdata *conn)
  170. {
  171. struct timeval now;
  172. int result;
  173. char max5[6][10];
  174. double dlpercen=0;
  175. double ulpercen=0;
  176. double total_percen=0;
  177. double total_transfer;
  178. double total_expected_transfer;
  179. double timespent;
  180. struct SessionHandle *data = conn->data;
  181. int nowindex = data->progress.speeder_c% CURR_TIME;
  182. int checkindex;
  183. int countindex; /* amount of seconds stored in the speeder array */
  184. char time_left[10];
  185. char time_total[10];
  186. char time_current[10];
  187. double ulestimate=0;
  188. double dlestimate=0;
  189. double total_estimate;
  190. if(data->progress.flags & PGRS_HIDE)
  191. ; /* We do enter this function even if we don't wanna see anything, since
  192. this is were lots of the calculations are being made that will be used
  193. even when not displayed! */
  194. else if(!(data->progress.flags & PGRS_HEADERS_OUT)) {
  195. if (!data->progress.callback) {
  196. if(conn->resume_from)
  197. fprintf(data->set.err, "** Resuming transfer from byte position %d\n",
  198. conn->resume_from);
  199. fprintf(data->set.err,
  200. " %% Total %% Received %% Xferd Average Speed Time Curr.\n"
  201. " Dload Upload Total Current Left Speed\n");
  202. }
  203. data->progress.flags |= PGRS_HEADERS_OUT; /* headers are shown */
  204. }
  205. now = Curl_tvnow(); /* what time is it */
  206. /* The exact time spent so far (from the start) */
  207. timespent = (double)Curl_tvdiff (now, data->progress.start)/1000;
  208. data->progress.timespent = timespent;
  209. /* The average download speed this far */
  210. data->progress.dlspeed =
  211. data->progress.downloaded/(timespent>0.01?timespent:1);
  212. /* The average upload speed this far */
  213. data->progress.ulspeed =
  214. data->progress.uploaded/(timespent>0.01?timespent:1);
  215. if(data->progress.lastshow == Curl_tvlong(now))
  216. return 0; /* never update this more than once a second if the end isn't
  217. reached */
  218. data->progress.lastshow = now.tv_sec;
  219. /* Let's do the "current speed" thing, which should use the fastest
  220. of the dl/ul speeds. Store the fasted speed at entry 'nowindex'. */
  221. data->progress.speeder[ nowindex ] =
  222. data->progress.downloaded>data->progress.uploaded?
  223. data->progress.downloaded:data->progress.uploaded;
  224. /* remember the exact time for this moment */
  225. data->progress.speeder_time [ nowindex ] = now;
  226. /* advance our speeder_c counter, which is increased every time we get
  227. here and we expect it to never wrap as 2^32 is a lot of seconds! */
  228. data->progress.speeder_c++;
  229. /* figure out how many index entries of data we have stored in our speeder
  230. array. With N_ENTRIES filled in, we have about N_ENTRIES-1 seconds of
  231. transfer. Imagine, after one second we have filled in two entries,
  232. after two seconds we've filled in three entries etc. */
  233. countindex = ((data->progress.speeder_c>=CURR_TIME)?
  234. CURR_TIME:data->progress.speeder_c) - 1;
  235. /* first of all, we don't do this if there's no counted seconds yet */
  236. if(countindex) {
  237. long span_ms;
  238. /* Get the index position to compare with the 'nowindex' position.
  239. Get the oldest entry possible. While we have less than CURR_TIME
  240. entries, the first entry will remain the oldest. */
  241. checkindex = (data->progress.speeder_c>=CURR_TIME)?
  242. data->progress.speeder_c%CURR_TIME:0;
  243. /* Figure out the exact time for the time span */
  244. span_ms = Curl_tvdiff(now,
  245. data->progress.speeder_time[checkindex]);
  246. if(0 == span_ms)
  247. span_ms=1; /* at least one millisecond MUST have passed */
  248. /* Calculate the average speed the last 'countindex' seconds */
  249. data->progress.current_speed =
  250. (data->progress.speeder[nowindex]-
  251. data->progress.speeder[checkindex])/((double)span_ms/1000);
  252. }
  253. else
  254. /* the first second we use the main average */
  255. data->progress.current_speed =
  256. (data->progress.ulspeed>data->progress.dlspeed)?
  257. data->progress.ulspeed:data->progress.dlspeed;
  258. if(data->progress.flags & PGRS_HIDE)
  259. return 0;
  260. else if(data->set.fprogress) {
  261. /* There's a callback set, so we call that instead of writing
  262. anything ourselves. This really is the way to go. */
  263. result= data->set.fprogress(data->set.progress_client,
  264. data->progress.size_dl,
  265. data->progress.downloaded,
  266. data->progress.size_ul,
  267. data->progress.uploaded);
  268. if(result)
  269. failf(data, "Callback aborted");
  270. return result;
  271. }
  272. /* Figure out the estimated time of arrival for the upload */
  273. if((data->progress.flags & PGRS_UL_SIZE_KNOWN) && data->progress.ulspeed){
  274. ulestimate = data->progress.size_ul / data->progress.ulspeed;
  275. ulpercen = (data->progress.uploaded / data->progress.size_ul)*100;
  276. }
  277. /* ... and the download */
  278. if((data->progress.flags & PGRS_DL_SIZE_KNOWN) && data->progress.dlspeed) {
  279. dlestimate = data->progress.size_dl / data->progress.dlspeed;
  280. dlpercen = (data->progress.downloaded / data->progress.size_dl)*100;
  281. }
  282. /* Now figure out which of them that is slower and use for the for
  283. total estimate! */
  284. total_estimate = ulestimate>dlestimate?ulestimate:dlestimate;
  285. /* If we have a total estimate, we can display that and the expected
  286. time left */
  287. if(total_estimate) {
  288. time2str(time_left, (int)(total_estimate - data->progress.timespent));
  289. time2str(time_total, (int)total_estimate);
  290. }
  291. else {
  292. /* otherwise we blank those times */
  293. strcpy(time_left, "--:--:--");
  294. strcpy(time_total, "--:--:--");
  295. }
  296. /* The time spent so far is always known */
  297. time2str(time_current, (int)data->progress.timespent);
  298. /* Get the total amount of data expected to get transfered */
  299. total_expected_transfer =
  300. (data->progress.flags & PGRS_UL_SIZE_KNOWN?
  301. data->progress.size_ul:data->progress.uploaded)+
  302. (data->progress.flags & PGRS_DL_SIZE_KNOWN?
  303. data->progress.size_dl:data->progress.downloaded);
  304. /* We have transfered this much so far */
  305. total_transfer = data->progress.downloaded + data->progress.uploaded;
  306. /* Get the percentage of data transfered so far */
  307. if(total_expected_transfer)
  308. total_percen=(double)(total_transfer/total_expected_transfer)*100;
  309. fprintf(data->set.err,
  310. "\r%3d %s %3d %s %3d %s %s %s %s %s %s %s",
  311. (int)total_percen, /* total % */
  312. max5data(total_expected_transfer, max5[2]), /* total size */
  313. (int)dlpercen, /* rcvd % */
  314. max5data(data->progress.downloaded, max5[0]), /* rcvd size */
  315. (int)ulpercen, /* xfer % */
  316. max5data(data->progress.uploaded, max5[1]), /* xfer size */
  317. max5data(data->progress.dlspeed, max5[3]), /* avrg dl speed */
  318. max5data(data->progress.ulspeed, max5[4]), /* avrg ul speed */
  319. time_total, /* total time */
  320. time_current, /* current time */
  321. time_left, /* time left */
  322. max5data(data->progress.current_speed, max5[5]) /* current speed */
  323. );
  324. /* we flush the output stream to make it appear as soon as possible */
  325. fflush(data->set.err);
  326. return 0;
  327. }
  328. /*
  329. * local variables:
  330. * eval: (load-file "../curl-mode.el")
  331. * end:
  332. * vim600: fdm=marker
  333. * vim: et sw=2 ts=2 sts=2 tw=78
  334. */