qemu-progress.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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-common.h"
  25. #include "osdep.h"
  26. #include "sysemu.h"
  27. #include <stdio.h>
  28. struct progress_state {
  29. float current;
  30. float last_print;
  31. float min_skip;
  32. void (*print)(void);
  33. void (*end)(void);
  34. };
  35. static struct progress_state state;
  36. static volatile sig_atomic_t print_pending;
  37. /*
  38. * Simple progress print function.
  39. * @percent relative percent of current operation
  40. * @max percent of total operation
  41. */
  42. static void progress_simple_print(void)
  43. {
  44. printf(" (%3.2f/100%%)\r", state.current);
  45. fflush(stdout);
  46. }
  47. static void progress_simple_end(void)
  48. {
  49. printf("\n");
  50. }
  51. static void progress_simple_init(void)
  52. {
  53. state.print = progress_simple_print;
  54. state.end = progress_simple_end;
  55. }
  56. #ifdef CONFIG_POSIX
  57. static void sigusr_print(int signal)
  58. {
  59. print_pending = 1;
  60. }
  61. #endif
  62. static void progress_dummy_print(void)
  63. {
  64. if (print_pending) {
  65. fprintf(stderr, " (%3.2f/100%%)\n", state.current);
  66. print_pending = 0;
  67. }
  68. }
  69. static void progress_dummy_end(void)
  70. {
  71. }
  72. static void progress_dummy_init(void)
  73. {
  74. #ifdef CONFIG_POSIX
  75. struct sigaction action;
  76. memset(&action, 0, sizeof(action));
  77. sigfillset(&action.sa_mask);
  78. action.sa_handler = sigusr_print;
  79. action.sa_flags = 0;
  80. sigaction(SIGUSR1, &action, NULL);
  81. #endif
  82. state.print = progress_dummy_print;
  83. state.end = progress_dummy_end;
  84. }
  85. /*
  86. * Initialize progress reporting.
  87. * If @enabled is false, actual reporting is suppressed. The user can
  88. * still trigger a report by sending a SIGUSR1.
  89. * Reports are also suppressed unless we've had at least @min_skip
  90. * percent progress since the last report.
  91. */
  92. void qemu_progress_init(int enabled, float min_skip)
  93. {
  94. state.min_skip = min_skip;
  95. if (enabled) {
  96. progress_simple_init();
  97. } else {
  98. progress_dummy_init();
  99. }
  100. }
  101. void qemu_progress_end(void)
  102. {
  103. state.end();
  104. }
  105. /*
  106. * Report progress.
  107. * @delta is how much progress we made.
  108. * If @max is zero, @delta is an absolut value of the total job done.
  109. * Else, @delta is a progress delta since the last call, as a fraction
  110. * of @max. I.e. the delta is @delta * @max / 100. This allows
  111. * relative accounting of functions which may be a different fraction of
  112. * the full job, depending on the context they are called in. I.e.
  113. * a function might be considered 40% of the full job if used from
  114. * bdrv_img_create() but only 20% if called from img_convert().
  115. */
  116. void qemu_progress_print(float delta, int max)
  117. {
  118. float current;
  119. if (max == 0) {
  120. current = delta;
  121. } else {
  122. current = state.current + delta / 100 * max;
  123. }
  124. if (current > 100) {
  125. current = 100;
  126. }
  127. state.current = current;
  128. if (current > (state.last_print + state.min_skip) ||
  129. (current == 100) || (current == 0)) {
  130. state.last_print = state.current;
  131. state.print();
  132. }
  133. }