Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 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 | |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Marc-André Lureau | 16a18f2 | 2022-03-23 19:57:35 +0400 | [diff] [blame] | 26 | #include "qemu/qemu-progress.h" |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 27 | |
| 28 | struct progress_state { |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 29 | float current; |
| 30 | float last_print; |
| 31 | float min_skip; |
Jes Sorensen | a55c73b | 2011-04-27 14:31:50 +0200 | [diff] [blame] | 32 | void (*print)(void); |
| 33 | void (*end)(void); |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | static struct progress_state state; |
Jes Sorensen | 2ab3cb8 | 2011-04-28 13:58:30 +0200 | [diff] [blame] | 37 | static volatile sig_atomic_t print_pending; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 38 | |
| 39 | /* |
| 40 | * Simple progress print function. |
| 41 | * @percent relative percent of current operation |
| 42 | * @max percent of total operation |
| 43 | */ |
| 44 | static void progress_simple_print(void) |
| 45 | { |
Jes Sorensen | df6e008 | 2011-04-27 14:31:51 +0200 | [diff] [blame] | 46 | printf(" (%3.2f/100%%)\r", state.current); |
| 47 | fflush(stdout); |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static void progress_simple_end(void) |
| 51 | { |
Jes Sorensen | a55c73b | 2011-04-27 14:31:50 +0200 | [diff] [blame] | 52 | printf("\n"); |
| 53 | } |
| 54 | |
| 55 | static void progress_simple_init(void) |
| 56 | { |
| 57 | state.print = progress_simple_print; |
| 58 | state.end = progress_simple_end; |
| 59 | } |
| 60 | |
| 61 | #ifdef CONFIG_POSIX |
| 62 | static void sigusr_print(int signal) |
| 63 | { |
Jes Sorensen | 2ab3cb8 | 2011-04-28 13:58:30 +0200 | [diff] [blame] | 64 | print_pending = 1; |
Jes Sorensen | a55c73b | 2011-04-27 14:31:50 +0200 | [diff] [blame] | 65 | } |
| 66 | #endif |
| 67 | |
| 68 | static void progress_dummy_print(void) |
| 69 | { |
Jes Sorensen | 2ab3cb8 | 2011-04-28 13:58:30 +0200 | [diff] [blame] | 70 | if (print_pending) { |
| 71 | fprintf(stderr, " (%3.2f/100%%)\n", state.current); |
| 72 | print_pending = 0; |
| 73 | } |
Jes Sorensen | a55c73b | 2011-04-27 14:31:50 +0200 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static void progress_dummy_end(void) |
| 77 | { |
| 78 | } |
| 79 | |
| 80 | static void progress_dummy_init(void) |
| 81 | { |
| 82 | #ifdef CONFIG_POSIX |
| 83 | struct sigaction action; |
Kevin Wolf | 3c4b4e3 | 2014-01-20 15:06:03 +0100 | [diff] [blame] | 84 | sigset_t set; |
Jes Sorensen | a55c73b | 2011-04-27 14:31:50 +0200 | [diff] [blame] | 85 | |
| 86 | memset(&action, 0, sizeof(action)); |
| 87 | sigfillset(&action.sa_mask); |
| 88 | action.sa_handler = sigusr_print; |
| 89 | action.sa_flags = 0; |
| 90 | sigaction(SIGUSR1, &action, NULL); |
Max Reitz | 262fbae | 2017-02-08 00:57:57 +0100 | [diff] [blame] | 91 | #ifdef SIGINFO |
| 92 | sigaction(SIGINFO, &action, NULL); |
| 93 | #endif |
Kevin Wolf | 3c4b4e3 | 2014-01-20 15:06:03 +0100 | [diff] [blame] | 94 | |
| 95 | /* |
| 96 | * SIGUSR1 is SIG_IPI and gets blocked in qemu_init_main_loop(). In the |
| 97 | * tools that use the progress report SIGUSR1 isn't used in this meaning |
| 98 | * and instead should print the progress, so reenable it. |
| 99 | */ |
| 100 | sigemptyset(&set); |
| 101 | sigaddset(&set, SIGUSR1); |
| 102 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); |
Jes Sorensen | a55c73b | 2011-04-27 14:31:50 +0200 | [diff] [blame] | 103 | #endif |
| 104 | |
| 105 | state.print = progress_dummy_print; |
| 106 | state.end = progress_dummy_end; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Jes Sorensen | 3bfe4db | 2011-05-09 17:32:20 +0200 | [diff] [blame] | 109 | /* |
| 110 | * Initialize progress reporting. |
| 111 | * If @enabled is false, actual reporting is suppressed. The user can |
| 112 | * still trigger a report by sending a SIGUSR1. |
| 113 | * Reports are also suppressed unless we've had at least @min_skip |
| 114 | * percent progress since the last report. |
| 115 | */ |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 116 | void qemu_progress_init(int enabled, float min_skip) |
| 117 | { |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 118 | state.min_skip = min_skip; |
Jes Sorensen | a55c73b | 2011-04-27 14:31:50 +0200 | [diff] [blame] | 119 | if (enabled) { |
| 120 | progress_simple_init(); |
| 121 | } else { |
| 122 | progress_dummy_init(); |
| 123 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void qemu_progress_end(void) |
| 127 | { |
Jes Sorensen | a55c73b | 2011-04-27 14:31:50 +0200 | [diff] [blame] | 128 | state.end(); |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 129 | } |
| 130 | |
Jes Sorensen | 3bfe4db | 2011-05-09 17:32:20 +0200 | [diff] [blame] | 131 | /* |
| 132 | * Report progress. |
| 133 | * @delta is how much progress we made. |
zhaolichang | 8cc360b | 2020-09-17 15:50:24 +0800 | [diff] [blame] | 134 | * If @max is zero, @delta is an absolute value of the total job done. |
Jes Sorensen | 3bfe4db | 2011-05-09 17:32:20 +0200 | [diff] [blame] | 135 | * Else, @delta is a progress delta since the last call, as a fraction |
| 136 | * of @max. I.e. the delta is @delta * @max / 100. This allows |
| 137 | * relative accounting of functions which may be a different fraction of |
| 138 | * the full job, depending on the context they are called in. I.e. |
| 139 | * a function might be considered 40% of the full job if used from |
| 140 | * bdrv_img_create() but only 20% if called from img_convert(). |
| 141 | */ |
| 142 | void qemu_progress_print(float delta, int max) |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 143 | { |
| 144 | float current; |
| 145 | |
| 146 | if (max == 0) { |
Jes Sorensen | 3bfe4db | 2011-05-09 17:32:20 +0200 | [diff] [blame] | 147 | current = delta; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 148 | } else { |
Jes Sorensen | 3bfe4db | 2011-05-09 17:32:20 +0200 | [diff] [blame] | 149 | current = state.current + delta / 100 * max; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 150 | } |
| 151 | if (current > 100) { |
| 152 | current = 100; |
| 153 | } |
| 154 | state.current = current; |
| 155 | |
| 156 | if (current > (state.last_print + state.min_skip) || |
Max Reitz | bd5072d | 2015-07-27 17:51:31 +0200 | [diff] [blame] | 157 | current < (state.last_print - state.min_skip) || |
| 158 | current == 100 || current == 0) { |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 159 | state.last_print = state.current; |
Jes Sorensen | a55c73b | 2011-04-27 14:31:50 +0200 | [diff] [blame] | 160 | state.print(); |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 161 | } |
| 162 | } |