THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

345 lines
8.5KB

  1. /*
  2. * Simple text-based progress display module for GIT
  3. *
  4. * Copyright (c) 2007 by Nicolas Pitre <nico@fluxnic.net>
  5. *
  6. * This code is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include "cache.h"
  11. #include "gettext.h"
  12. #include "progress.h"
  13. #include "strbuf.h"
  14. #include "trace.h"
  15. #include "utf8.h"
  16. #define TP_IDX_MAX 8
  17. struct throughput {
  18. off_t curr_total;
  19. off_t prev_total;
  20. uint64_t prev_ns;
  21. unsigned int avg_bytes;
  22. unsigned int avg_misecs;
  23. unsigned int last_bytes[TP_IDX_MAX];
  24. unsigned int last_misecs[TP_IDX_MAX];
  25. unsigned int idx;
  26. struct strbuf display;
  27. };
  28. struct progress {
  29. const char *title;
  30. uint64_t last_value;
  31. uint64_t total;
  32. unsigned last_percent;
  33. unsigned delay;
  34. unsigned sparse;
  35. struct throughput *throughput;
  36. uint64_t start_ns;
  37. struct strbuf counters_sb;
  38. int title_len;
  39. int split;
  40. };
  41. static volatile sig_atomic_t progress_update;
  42. /*
  43. * These are only intended for testing the progress output, i.e. exclusively
  44. * for 'test-tool progress'.
  45. */
  46. int progress_testing;
  47. uint64_t progress_test_ns = 0;
  48. void progress_test_force_update(void); /* To silence -Wmissing-prototypes */
  49. void progress_test_force_update(void)
  50. {
  51. progress_update = 1;
  52. }
  53. static void progress_interval(int signum)
  54. {
  55. progress_update = 1;
  56. }
  57. static void set_progress_signal(void)
  58. {
  59. struct sigaction sa;
  60. struct itimerval v;
  61. if (progress_testing)
  62. return;
  63. progress_update = 0;
  64. memset(&sa, 0, sizeof(sa));
  65. sa.sa_handler = progress_interval;
  66. sigemptyset(&sa.sa_mask);
  67. sa.sa_flags = SA_RESTART;
  68. sigaction(SIGALRM, &sa, NULL);
  69. v.it_interval.tv_sec = 1;
  70. v.it_interval.tv_usec = 0;
  71. v.it_value = v.it_interval;
  72. setitimer(ITIMER_REAL, &v, NULL);
  73. }
  74. static void clear_progress_signal(void)
  75. {
  76. struct itimerval v = {{0,},};
  77. if (progress_testing)
  78. return;
  79. setitimer(ITIMER_REAL, &v, NULL);
  80. signal(SIGALRM, SIG_IGN);
  81. progress_update = 0;
  82. }
  83. static int is_foreground_fd(int fd)
  84. {
  85. int tpgrp = tcgetpgrp(fd);
  86. return tpgrp < 0 || tpgrp == getpgid(0);
  87. }
  88. static void display(struct progress *progress, uint64_t n, const char *done)
  89. {
  90. const char *tp;
  91. struct strbuf *counters_sb = &progress->counters_sb;
  92. int show_update = 0;
  93. int last_count_len = counters_sb->len;
  94. if (progress->delay && (!progress_update || --progress->delay))
  95. return;
  96. progress->last_value = n;
  97. tp = (progress->throughput) ? progress->throughput->display.buf : "";
  98. if (progress->total) {
  99. unsigned percent = n * 100 / progress->total;
  100. if (percent != progress->last_percent || progress_update) {
  101. progress->last_percent = percent;
  102. strbuf_reset(counters_sb);
  103. strbuf_addf(counters_sb,
  104. "%3u%% (%"PRIuMAX"/%"PRIuMAX")%s", percent,
  105. (uintmax_t)n, (uintmax_t)progress->total,
  106. tp);
  107. show_update = 1;
  108. }
  109. } else if (progress_update) {
  110. strbuf_reset(counters_sb);
  111. strbuf_addf(counters_sb, "%"PRIuMAX"%s", (uintmax_t)n, tp);
  112. show_update = 1;
  113. }
  114. if (show_update) {
  115. if (is_foreground_fd(fileno(stderr)) || done) {
  116. const char *eol = done ? done : "\r";
  117. size_t clear_len = counters_sb->len < last_count_len ?
  118. last_count_len - counters_sb->len + 1 :
  119. 0;
  120. /* The "+ 2" accounts for the ": ". */
  121. size_t progress_line_len = progress->title_len +
  122. counters_sb->len + 2;
  123. int cols = term_columns();
  124. if (progress->split) {
  125. fprintf(stderr, " %s%*s", counters_sb->buf,
  126. (int) clear_len, eol);
  127. } else if (!done && cols < progress_line_len) {
  128. clear_len = progress->title_len + 1 < cols ?
  129. cols - progress->title_len - 1 : 0;
  130. fprintf(stderr, "%s:%*s\n %s%s",
  131. progress->title, (int) clear_len, "",
  132. counters_sb->buf, eol);
  133. progress->split = 1;
  134. } else {
  135. fprintf(stderr, "%s: %s%*s", progress->title,
  136. counters_sb->buf, (int) clear_len, eol);
  137. }
  138. fflush(stderr);
  139. }
  140. progress_update = 0;
  141. }
  142. }
  143. static void throughput_string(struct strbuf *buf, uint64_t total,
  144. unsigned int rate)
  145. {
  146. strbuf_reset(buf);
  147. strbuf_addstr(buf, ", ");
  148. strbuf_humanise_bytes(buf, total);
  149. strbuf_addstr(buf, " | ");
  150. strbuf_humanise_rate(buf, rate * 1024);
  151. }
  152. static uint64_t progress_getnanotime(struct progress *progress)
  153. {
  154. if (progress_testing)
  155. return progress->start_ns + progress_test_ns;
  156. else
  157. return getnanotime();
  158. }
  159. void display_throughput(struct progress *progress, uint64_t total)
  160. {
  161. struct throughput *tp;
  162. uint64_t now_ns;
  163. unsigned int misecs, count, rate;
  164. if (!progress)
  165. return;
  166. tp = progress->throughput;
  167. now_ns = progress_getnanotime(progress);
  168. if (!tp) {
  169. progress->throughput = tp = xcalloc(1, sizeof(*tp));
  170. tp->prev_total = tp->curr_total = total;
  171. tp->prev_ns = now_ns;
  172. strbuf_init(&tp->display, 0);
  173. return;
  174. }
  175. tp->curr_total = total;
  176. /* only update throughput every 0.5 s */
  177. if (now_ns - tp->prev_ns <= 500000000)
  178. return;
  179. /*
  180. * We have x = bytes and y = nanosecs. We want z = KiB/s:
  181. *
  182. * z = (x / 1024) / (y / 1000000000)
  183. * z = x / y * 1000000000 / 1024
  184. * z = x / (y * 1024 / 1000000000)
  185. * z = x / y'
  186. *
  187. * To simplify things we'll keep track of misecs, or 1024th of a sec
  188. * obtained with:
  189. *
  190. * y' = y * 1024 / 1000000000
  191. * y' = y * (2^10 / 2^42) * (2^42 / 1000000000)
  192. * y' = y / 2^32 * 4398
  193. * y' = (y * 4398) >> 32
  194. */
  195. misecs = ((now_ns - tp->prev_ns) * 4398) >> 32;
  196. count = total - tp->prev_total;
  197. tp->prev_total = total;
  198. tp->prev_ns = now_ns;
  199. tp->avg_bytes += count;
  200. tp->avg_misecs += misecs;
  201. rate = tp->avg_bytes / tp->avg_misecs;
  202. tp->avg_bytes -= tp->last_bytes[tp->idx];
  203. tp->avg_misecs -= tp->last_misecs[tp->idx];
  204. tp->last_bytes[tp->idx] = count;
  205. tp->last_misecs[tp->idx] = misecs;
  206. tp->idx = (tp->idx + 1) % TP_IDX_MAX;
  207. throughput_string(&tp->display, total, rate);
  208. if (progress->last_value != -1 && progress_update)
  209. display(progress, progress->last_value, NULL);
  210. }
  211. void display_progress(struct progress *progress, uint64_t n)
  212. {
  213. if (progress)
  214. display(progress, n, NULL);
  215. }
  216. static struct progress *start_progress_delay(const char *title, uint64_t total,
  217. unsigned delay, unsigned sparse)
  218. {
  219. struct progress *progress = xmalloc(sizeof(*progress));
  220. progress->title = title;
  221. progress->total = total;
  222. progress->last_value = -1;
  223. progress->last_percent = -1;
  224. progress->delay = delay;
  225. progress->sparse = sparse;
  226. progress->throughput = NULL;
  227. progress->start_ns = getnanotime();
  228. strbuf_init(&progress->counters_sb, 0);
  229. progress->title_len = utf8_strwidth(title);
  230. progress->split = 0;
  231. set_progress_signal();
  232. return progress;
  233. }
  234. struct progress *start_delayed_progress(const char *title, uint64_t total)
  235. {
  236. return start_progress_delay(title, total, 2, 0);
  237. }
  238. struct progress *start_progress(const char *title, uint64_t total)
  239. {
  240. return start_progress_delay(title, total, 0, 0);
  241. }
  242. /*
  243. * Here "sparse" means that the caller might use some sampling criteria to
  244. * decide when to call display_progress() rather than calling it for every
  245. * integer value in[0 .. total). In particular, the caller might not call
  246. * display_progress() for the last value in the range.
  247. *
  248. * When "sparse" is set, stop_progress() will automatically force the done
  249. * message to show 100%.
  250. */
  251. struct progress *start_sparse_progress(const char *title, uint64_t total)
  252. {
  253. return start_progress_delay(title, total, 0, 1);
  254. }
  255. struct progress *start_delayed_sparse_progress(const char *title,
  256. uint64_t total)
  257. {
  258. return start_progress_delay(title, total, 2, 1);
  259. }
  260. static void finish_if_sparse(struct progress *progress)
  261. {
  262. if (progress &&
  263. progress->sparse &&
  264. progress->last_value != progress->total)
  265. display_progress(progress, progress->total);
  266. }
  267. void stop_progress(struct progress **p_progress)
  268. {
  269. finish_if_sparse(*p_progress);
  270. stop_progress_msg(p_progress, _("done"));
  271. }
  272. void stop_progress_msg(struct progress **p_progress, const char *msg)
  273. {
  274. struct progress *progress = *p_progress;
  275. if (!progress)
  276. return;
  277. *p_progress = NULL;
  278. if (progress->last_value != -1) {
  279. /* Force the last update */
  280. char *buf;
  281. struct throughput *tp = progress->throughput;
  282. if (tp) {
  283. uint64_t now_ns = progress_getnanotime(progress);
  284. unsigned int misecs, rate;
  285. misecs = ((now_ns - progress->start_ns) * 4398) >> 32;
  286. rate = tp->curr_total / (misecs ? misecs : 1);
  287. throughput_string(&tp->display, tp->curr_total, rate);
  288. }
  289. progress_update = 1;
  290. buf = xstrfmt(", %s.\n", msg);
  291. display(progress, progress->last_value, buf);
  292. free(buf);
  293. }
  294. clear_progress_signal();
  295. strbuf_release(&progress->counters_sb);
  296. if (progress->throughput)
  297. strbuf_release(&progress->throughput->display);
  298. free(progress->throughput);
  299. free(progress);
  300. }