blob: f745233763aa8417ebf7599e6c2ca50271d60db1 [file] [log] [blame]
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001/*
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 Bonzini1de7afc2012-12-17 18:20:00 +010025#include "qemu/osdep.h"
Peter Maydellaafd7582016-01-29 17:49:55 +000026#include "qemu-common.h"
Jes Sorensen6b837bc2011-03-30 14:16:25 +020027
28struct progress_state {
Jes Sorensen6b837bc2011-03-30 14:16:25 +020029 float current;
30 float last_print;
31 float min_skip;
Jes Sorensena55c73b2011-04-27 14:31:50 +020032 void (*print)(void);
33 void (*end)(void);
Jes Sorensen6b837bc2011-03-30 14:16:25 +020034};
35
36static struct progress_state state;
Jes Sorensen2ab3cb82011-04-28 13:58:30 +020037static volatile sig_atomic_t print_pending;
Jes Sorensen6b837bc2011-03-30 14:16:25 +020038
39/*
40 * Simple progress print function.
41 * @percent relative percent of current operation
42 * @max percent of total operation
43 */
44static void progress_simple_print(void)
45{
Jes Sorensendf6e0082011-04-27 14:31:51 +020046 printf(" (%3.2f/100%%)\r", state.current);
47 fflush(stdout);
Jes Sorensen6b837bc2011-03-30 14:16:25 +020048}
49
50static void progress_simple_end(void)
51{
Jes Sorensena55c73b2011-04-27 14:31:50 +020052 printf("\n");
53}
54
55static void progress_simple_init(void)
56{
57 state.print = progress_simple_print;
58 state.end = progress_simple_end;
59}
60
61#ifdef CONFIG_POSIX
62static void sigusr_print(int signal)
63{
Jes Sorensen2ab3cb82011-04-28 13:58:30 +020064 print_pending = 1;
Jes Sorensena55c73b2011-04-27 14:31:50 +020065}
66#endif
67
68static void progress_dummy_print(void)
69{
Jes Sorensen2ab3cb82011-04-28 13:58:30 +020070 if (print_pending) {
71 fprintf(stderr, " (%3.2f/100%%)\n", state.current);
72 print_pending = 0;
73 }
Jes Sorensena55c73b2011-04-27 14:31:50 +020074}
75
76static void progress_dummy_end(void)
77{
78}
79
80static void progress_dummy_init(void)
81{
82#ifdef CONFIG_POSIX
83 struct sigaction action;
Kevin Wolf3c4b4e32014-01-20 15:06:03 +010084 sigset_t set;
Jes Sorensena55c73b2011-04-27 14:31:50 +020085
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);
Kevin Wolf3c4b4e32014-01-20 15:06:03 +010091
92 /*
93 * SIGUSR1 is SIG_IPI and gets blocked in qemu_init_main_loop(). In the
94 * tools that use the progress report SIGUSR1 isn't used in this meaning
95 * and instead should print the progress, so reenable it.
96 */
97 sigemptyset(&set);
98 sigaddset(&set, SIGUSR1);
99 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
Jes Sorensena55c73b2011-04-27 14:31:50 +0200100#endif
101
102 state.print = progress_dummy_print;
103 state.end = progress_dummy_end;
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200104}
105
Jes Sorensen3bfe4db2011-05-09 17:32:20 +0200106/*
107 * Initialize progress reporting.
108 * If @enabled is false, actual reporting is suppressed. The user can
109 * still trigger a report by sending a SIGUSR1.
110 * Reports are also suppressed unless we've had at least @min_skip
111 * percent progress since the last report.
112 */
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200113void qemu_progress_init(int enabled, float min_skip)
114{
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200115 state.min_skip = min_skip;
Jes Sorensena55c73b2011-04-27 14:31:50 +0200116 if (enabled) {
117 progress_simple_init();
118 } else {
119 progress_dummy_init();
120 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200121}
122
123void qemu_progress_end(void)
124{
Jes Sorensena55c73b2011-04-27 14:31:50 +0200125 state.end();
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200126}
127
Jes Sorensen3bfe4db2011-05-09 17:32:20 +0200128/*
129 * Report progress.
130 * @delta is how much progress we made.
131 * If @max is zero, @delta is an absolut value of the total job done.
132 * Else, @delta is a progress delta since the last call, as a fraction
133 * of @max. I.e. the delta is @delta * @max / 100. This allows
134 * relative accounting of functions which may be a different fraction of
135 * the full job, depending on the context they are called in. I.e.
136 * a function might be considered 40% of the full job if used from
137 * bdrv_img_create() but only 20% if called from img_convert().
138 */
139void qemu_progress_print(float delta, int max)
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200140{
141 float current;
142
143 if (max == 0) {
Jes Sorensen3bfe4db2011-05-09 17:32:20 +0200144 current = delta;
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200145 } else {
Jes Sorensen3bfe4db2011-05-09 17:32:20 +0200146 current = state.current + delta / 100 * max;
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200147 }
148 if (current > 100) {
149 current = 100;
150 }
151 state.current = current;
152
153 if (current > (state.last_print + state.min_skip) ||
Max Reitzbd5072d2015-07-27 17:51:31 +0200154 current < (state.last_print - state.min_skip) ||
155 current == 100 || current == 0) {
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200156 state.last_print = state.current;
Jes Sorensena55c73b2011-04-27 14:31:50 +0200157 state.print();
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200158 }
159}