2
0

qemu-progress.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * QEMU progress printing utility functions
  3. *
  4. * Copyright (C) 2011 Jes Sorensen <Jes.Sorensen@redhat.com>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu-common.h"
  26. struct progress_state {
  27. float current;
  28. float last_print;
  29. float min_skip;
  30. void (*print)(void);
  31. void (*end)(void);
  32. };
  33. static struct progress_state state;
  34. static volatile sig_atomic_t print_pending;
  35. /*
  36. * Simple progress print function.
  37. * @percent relative percent of current operation
  38. * @max percent of total operation
  39. */
  40. static void progress_simple_print(void)
  41. {
  42. printf(" (%3.2f/100%%)\r", state.current);
  43. fflush(stdout);
  44. }
  45. static void progress_simple_end(void)
  46. {
  47. printf("\n");
  48. }
  49. static void progress_simple_init(void)
  50. {
  51. state.print = progress_simple_print;
  52. state.end = progress_simple_end;
  53. }
  54. #ifdef CONFIG_POSIX
  55. static void sigusr_print(int signal)
  56. {
  57. print_pending = 1;
  58. }
  59. #endif
  60. static void progress_dummy_print(void)
  61. {
  62. if (print_pending) {
  63. fprintf(stderr, " (%3.2f/100%%)\n", state.current);
  64. print_pending = 0;
  65. }
  66. }
  67. static void progress_dummy_end(void)
  68. {
  69. }
  70. static void progress_dummy_init(void)
  71. {
  72. #ifdef CONFIG_POSIX
  73. struct sigaction action;
  74. sigset_t set;
  75. memset(&action, 0, sizeof(action));
  76. sigfillset(&action.sa_mask);
  77. action.sa_handler = sigusr_print;
  78. action.sa_flags = 0;
  79. sigaction(SIGUSR1, &action, NULL);
  80. #ifdef SIGINFO
  81. sigaction(SIGINFO, &action, NULL);
  82. #endif
  83. /*
  84. * SIGUSR1 is SIG_IPI and gets blocked in qemu_init_main_loop(). In the
  85. * tools that use the progress report SIGUSR1 isn't used in this meaning
  86. * and instead should print the progress, so reenable it.
  87. */
  88. sigemptyset(&set);
  89. sigaddset(&set, SIGUSR1);
  90. pthread_sigmask(SIG_UNBLOCK, &set, NULL);
  91. #endif
  92. state.print = progress_dummy_print;
  93. state.end = progress_dummy_end;
  94. }
  95. /*
  96. * Initialize progress reporting.
  97. * If @enabled is false, actual reporting is suppressed. The user can
  98. * still trigger a report by sending a SIGUSR1.
  99. * Reports are also suppressed unless we've had at least @min_skip
  100. * percent progress since the last report.
  101. */
  102. void qemu_progress_init(int enabled, float min_skip)
  103. {
  104. state.min_skip = min_skip;
  105. if (enabled) {
  106. progress_simple_init();
  107. } else {
  108. progress_dummy_init();
  109. }
  110. }
  111. void qemu_progress_end(void)
  112. {
  113. state.end();
  114. }
  115. /*
  116. * Report progress.
  117. * @delta is how much progress we made.
  118. * If @max is zero, @delta is an absolut value of the total job done.
  119. * Else, @delta is a progress delta since the last call, as a fraction
  120. * of @max. I.e. the delta is @delta * @max / 100. This allows
  121. * relative accounting of functions which may be a different fraction of
  122. * the full job, depending on the context they are called in. I.e.
  123. * a function might be considered 40% of the full job if used from
  124. * bdrv_img_create() but only 20% if called from img_convert().
  125. */
  126. void qemu_progress_print(float delta, int max)
  127. {
  128. float current;
  129. if (max == 0) {
  130. current = delta;
  131. } else {
  132. current = state.current + delta / 100 * max;
  133. }
  134. if (current > 100) {
  135. current = 100;
  136. }
  137. state.current = current;
  138. if (current > (state.last_print + state.min_skip) ||
  139. current < (state.last_print - state.min_skip) ||
  140. current == 100 || current == 0) {
  141. state.last_print = state.current;
  142. state.print();
  143. }
  144. }