tool_progress.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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 https://curl.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 "tool_setup.h"
  23. #include "tool_operate.h"
  24. #include "tool_progress.h"
  25. #include "tool_util.h"
  26. #define ENABLE_CURLX_PRINTF
  27. /* use our own printf() functions */
  28. #include "curlx.h"
  29. /* The point of this function would be to return a string of the input data,
  30. but never longer than 5 columns (+ one zero byte).
  31. Add suffix k, M, G when suitable... */
  32. static char *max5data(curl_off_t bytes, char *max5)
  33. {
  34. #define ONE_KILOBYTE CURL_OFF_T_C(1024)
  35. #define ONE_MEGABYTE (CURL_OFF_T_C(1024) * ONE_KILOBYTE)
  36. #define ONE_GIGABYTE (CURL_OFF_T_C(1024) * ONE_MEGABYTE)
  37. #define ONE_TERABYTE (CURL_OFF_T_C(1024) * ONE_GIGABYTE)
  38. #define ONE_PETABYTE (CURL_OFF_T_C(1024) * ONE_TERABYTE)
  39. if(bytes < CURL_OFF_T_C(100000))
  40. msnprintf(max5, 6, "%5" CURL_FORMAT_CURL_OFF_T, bytes);
  41. else if(bytes < CURL_OFF_T_C(10000) * ONE_KILOBYTE)
  42. msnprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "k", bytes/ONE_KILOBYTE);
  43. else if(bytes < CURL_OFF_T_C(100) * ONE_MEGABYTE)
  44. /* 'XX.XM' is good as long as we're less than 100 megs */
  45. msnprintf(max5, 6, "%2" CURL_FORMAT_CURL_OFF_T ".%0"
  46. CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE,
  47. (bytes%ONE_MEGABYTE) / (ONE_MEGABYTE/CURL_OFF_T_C(10)) );
  48. #if (SIZEOF_CURL_OFF_T > 4)
  49. else if(bytes < CURL_OFF_T_C(10000) * ONE_MEGABYTE)
  50. /* 'XXXXM' is good until we're at 10000MB or above */
  51. msnprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE);
  52. else if(bytes < CURL_OFF_T_C(100) * ONE_GIGABYTE)
  53. /* 10000 MB - 100 GB, we show it as XX.XG */
  54. msnprintf(max5, 6, "%2" CURL_FORMAT_CURL_OFF_T ".%0"
  55. CURL_FORMAT_CURL_OFF_T "G", bytes/ONE_GIGABYTE,
  56. (bytes%ONE_GIGABYTE) / (ONE_GIGABYTE/CURL_OFF_T_C(10)) );
  57. else if(bytes < CURL_OFF_T_C(10000) * ONE_GIGABYTE)
  58. /* up to 10000GB, display without decimal: XXXXG */
  59. msnprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "G", bytes/ONE_GIGABYTE);
  60. else if(bytes < CURL_OFF_T_C(10000) * ONE_TERABYTE)
  61. /* up to 10000TB, display without decimal: XXXXT */
  62. msnprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "T", bytes/ONE_TERABYTE);
  63. else
  64. /* up to 10000PB, display without decimal: XXXXP */
  65. msnprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "P", bytes/ONE_PETABYTE);
  66. /* 16384 petabytes (16 exabytes) is the maximum a 64 bit unsigned number
  67. can hold, but our data type is signed so 8192PB will be the maximum. */
  68. #else
  69. else
  70. msnprintf(max5, 6, "%4" CURL_FORMAT_CURL_OFF_T "M", bytes/ONE_MEGABYTE);
  71. #endif
  72. return max5;
  73. }
  74. int xferinfo_cb(void *clientp,
  75. curl_off_t dltotal,
  76. curl_off_t dlnow,
  77. curl_off_t ultotal,
  78. curl_off_t ulnow)
  79. {
  80. struct per_transfer *per = clientp;
  81. struct OperationConfig *config = per->config;
  82. per->dltotal = dltotal;
  83. per->dlnow = dlnow;
  84. per->ultotal = ultotal;
  85. per->ulnow = ulnow;
  86. if(config->readbusy) {
  87. config->readbusy = FALSE;
  88. curl_easy_pause(per->curl, CURLPAUSE_CONT);
  89. }
  90. return 0;
  91. }
  92. /* Provide a string that is 2 + 1 + 2 + 1 + 2 = 8 letters long (plus the zero
  93. byte) */
  94. static void time2str(char *r, curl_off_t seconds)
  95. {
  96. curl_off_t h;
  97. if(seconds <= 0) {
  98. strcpy(r, "--:--:--");
  99. return;
  100. }
  101. h = seconds / CURL_OFF_T_C(3600);
  102. if(h <= CURL_OFF_T_C(99)) {
  103. curl_off_t m = (seconds - (h*CURL_OFF_T_C(3600))) / CURL_OFF_T_C(60);
  104. curl_off_t s = (seconds - (h*CURL_OFF_T_C(3600))) - (m*CURL_OFF_T_C(60));
  105. msnprintf(r, 9, "%2" CURL_FORMAT_CURL_OFF_T ":%02" CURL_FORMAT_CURL_OFF_T
  106. ":%02" CURL_FORMAT_CURL_OFF_T, h, m, s);
  107. }
  108. else {
  109. /* this equals to more than 99 hours, switch to a more suitable output
  110. format to fit within the limits. */
  111. curl_off_t d = seconds / CURL_OFF_T_C(86400);
  112. h = (seconds - (d*CURL_OFF_T_C(86400))) / CURL_OFF_T_C(3600);
  113. if(d <= CURL_OFF_T_C(999))
  114. msnprintf(r, 9, "%3" CURL_FORMAT_CURL_OFF_T
  115. "d %02" CURL_FORMAT_CURL_OFF_T "h", d, h);
  116. else
  117. msnprintf(r, 9, "%7" CURL_FORMAT_CURL_OFF_T "d", d);
  118. }
  119. }
  120. static curl_off_t all_dltotal = 0;
  121. static curl_off_t all_ultotal = 0;
  122. static curl_off_t all_dlalready = 0;
  123. static curl_off_t all_ulalready = 0;
  124. curl_off_t all_xfers = 0; /* current total */
  125. struct speedcount {
  126. curl_off_t dl;
  127. curl_off_t ul;
  128. struct timeval stamp;
  129. };
  130. #define SPEEDCNT 10
  131. static unsigned int speedindex;
  132. static bool indexwrapped;
  133. static struct speedcount speedstore[SPEEDCNT];
  134. /*
  135. |DL% UL% Dled Uled Xfers Live Qd Total Current Left Speed
  136. | 6 -- 9.9G 0 2 2 0 0:00:40 0:00:02 0:00:37 4087M
  137. */
  138. bool progress_meter(struct GlobalConfig *global,
  139. struct timeval *start,
  140. bool final)
  141. {
  142. static struct timeval stamp;
  143. static bool header = FALSE;
  144. struct timeval now;
  145. long diff;
  146. if(global->noprogress)
  147. return FALSE;
  148. now = tvnow();
  149. diff = tvdiff(now, stamp);
  150. if(!header) {
  151. header = TRUE;
  152. fputs("DL% UL% Dled Uled Xfers Live Qd "
  153. "Total Current Left Speed\n",
  154. global->errors);
  155. }
  156. if(final || (diff > 500)) {
  157. char time_left[10];
  158. char time_total[10];
  159. char time_spent[10];
  160. char buffer[3][6];
  161. curl_off_t spent = tvdiff(now, *start)/1000;
  162. char dlpercen[4]="--";
  163. char ulpercen[4]="--";
  164. struct per_transfer *per;
  165. curl_off_t all_dlnow = 0;
  166. curl_off_t all_ulnow = 0;
  167. bool dlknown = TRUE;
  168. bool ulknown = TRUE;
  169. curl_off_t all_running = 0; /* in progress */
  170. curl_off_t all_queued = 0; /* pending */
  171. curl_off_t speed = 0;
  172. unsigned int i;
  173. stamp = now;
  174. /* first add the amounts of the already completed transfers */
  175. all_dlnow += all_dlalready;
  176. all_ulnow += all_ulalready;
  177. for(per = transfers; per; per = per->next) {
  178. all_dlnow += per->dlnow;
  179. all_ulnow += per->ulnow;
  180. if(!per->dltotal)
  181. dlknown = FALSE;
  182. else if(!per->dltotal_added) {
  183. /* only add this amount once */
  184. all_dltotal += per->dltotal;
  185. per->dltotal_added = TRUE;
  186. }
  187. if(!per->ultotal)
  188. ulknown = FALSE;
  189. else if(!per->ultotal_added) {
  190. /* only add this amount once */
  191. all_ultotal += per->ultotal;
  192. per->ultotal_added = TRUE;
  193. }
  194. if(!per->added)
  195. all_queued++;
  196. else
  197. all_running++;
  198. }
  199. if(dlknown && all_dltotal)
  200. /* TODO: handle integer overflow */
  201. msnprintf(dlpercen, sizeof(dlpercen), "%3" CURL_FORMAT_CURL_OFF_T,
  202. all_dlnow * 100 / all_dltotal);
  203. if(ulknown && all_ultotal)
  204. /* TODO: handle integer overflow */
  205. msnprintf(ulpercen, sizeof(ulpercen), "%3" CURL_FORMAT_CURL_OFF_T,
  206. all_ulnow * 100 / all_ultotal);
  207. /* get the transfer speed, the higher of the two */
  208. i = speedindex;
  209. speedstore[i].dl = all_dlnow;
  210. speedstore[i].ul = all_ulnow;
  211. speedstore[i].stamp = now;
  212. if(++speedindex >= SPEEDCNT) {
  213. indexwrapped = TRUE;
  214. speedindex = 0;
  215. }
  216. {
  217. long deltams;
  218. curl_off_t dl;
  219. curl_off_t ul;
  220. curl_off_t dls;
  221. curl_off_t uls;
  222. if(indexwrapped) {
  223. /* 'speedindex' is the oldest stored data */
  224. deltams = tvdiff(now, speedstore[speedindex].stamp);
  225. dl = all_dlnow - speedstore[speedindex].dl;
  226. ul = all_ulnow - speedstore[speedindex].ul;
  227. }
  228. else {
  229. /* since the beginning */
  230. deltams = tvdiff(now, *start);
  231. dl = all_dlnow;
  232. ul = all_ulnow;
  233. }
  234. dls = (curl_off_t)((double)dl / ((double)deltams/1000.0));
  235. uls = (curl_off_t)((double)ul / ((double)deltams/1000.0));
  236. speed = dls > uls ? dls : uls;
  237. }
  238. if(dlknown && speed) {
  239. curl_off_t est = all_dltotal / speed;
  240. curl_off_t left = (all_dltotal - all_dlnow) / speed;
  241. time2str(time_left, left);
  242. time2str(time_total, est);
  243. }
  244. else {
  245. time2str(time_left, 0);
  246. time2str(time_total, 0);
  247. }
  248. time2str(time_spent, spent);
  249. fprintf(global->errors,
  250. "\r"
  251. "%-3s " /* percent downloaded */
  252. "%-3s " /* percent uploaded */
  253. "%s " /* Dled */
  254. "%s " /* Uled */
  255. "%5" CURL_FORMAT_CURL_OFF_T " " /* Xfers */
  256. "%5" CURL_FORMAT_CURL_OFF_T " " /* Live */
  257. "%5" CURL_FORMAT_CURL_OFF_T " " /* Queued */
  258. "%s " /* Total time */
  259. "%s " /* Current time */
  260. "%s " /* Time left */
  261. "%s " /* Speed */
  262. "%5s" /* final newline */,
  263. dlpercen, /* 3 letters */
  264. ulpercen, /* 3 letters */
  265. max5data(all_dlnow, buffer[0]),
  266. max5data(all_ulnow, buffer[1]),
  267. all_xfers,
  268. all_running,
  269. all_queued,
  270. time_total,
  271. time_spent,
  272. time_left,
  273. max5data(speed, buffer[2]), /* speed */
  274. final ? "\n" :"");
  275. return TRUE;
  276. }
  277. return FALSE;
  278. }
  279. void progress_finalize(struct per_transfer *per)
  280. {
  281. /* get the numbers before this transfer goes away */
  282. all_dlalready += per->dlnow;
  283. all_ulalready += per->ulnow;
  284. if(!per->dltotal_added) {
  285. all_dltotal += per->dltotal;
  286. per->dltotal_added = TRUE;
  287. }
  288. if(!per->ultotal_added) {
  289. all_ultotal += per->ultotal;
  290. per->ultotal_added = TRUE;
  291. }
  292. }