qemu-progress.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. /*
  81. * SIGUSR1 is SIG_IPI and gets blocked in qemu_init_main_loop(). In the
  82. * tools that use the progress report SIGUSR1 isn't used in this meaning
  83. * and instead should print the progress, so reenable it.
  84. */
  85. sigemptyset(&set);
  86. sigaddset(&set, SIGUSR1);
  87. pthread_sigmask(SIG_UNBLOCK, &set, NULL);
  88. #endif
  89. state.print = progress_dummy_print;
  90. state.end = progress_dummy_end;
  91. }
  92. /*
  93. * Initialize progress reporting.
  94. * If @enabled is false, actual reporting is suppressed. The user can
  95. * still trigger a report by sending a SIGUSR1.
  96. * Reports are also suppressed unless we've had at least @min_skip
  97. * percent progress since the last report.
  98. */
  99. void qemu_progress_init(int enabled, float min_skip)
  100. {
  101. state.min_skip = min_skip;
  102. if (enabled) {
  103. progress_simple_init();
  104. } else {
  105. progress_dummy_init();
  106. }
  107. }
  108. void qemu_progress_end(void)
  109. {
  110. state.end();
  111. }
  112. /*
  113. * Report progress.
  114. * @delta is how much progress we made.
  115. * If @max is zero, @delta is an absolut value of the total job done.
  116. * Else, @delta is a progress delta since the last call, as a fraction
  117. * of @max. I.e. the delta is @delta * @max / 100. This allows
  118. * relative accounting of functions which may be a different fraction of
  119. * the full job, depending on the context they are called in. I.e.
  120. * a function might be considered 40% of the full job if used from
  121. * bdrv_img_create() but only 20% if called from img_convert().
  122. */
  123. void qemu_progress_print(float delta, int max)
  124. {
  125. float current;
  126. if (max == 0) {
  127. current = delta;
  128. } else {
  129. current = state.current + delta / 100 * max;
  130. }
  131. if (current > 100) {
  132. current = 100;
  133. }
  134. state.current = current;
  135. if (current > (state.last_print + state.min_skip) ||
  136. current < (state.last_print - state.min_skip) ||
  137. current == 100 || current == 0) {
  138. state.last_print = state.current;
  139. state.print();
  140. }
  141. }