2 * QEMU progress printing utility functions
4 * Copyright (C) 2011 Jes Sorensen <Jes.Sorensen@redhat.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
25 #include "qemu-common.h"
31 struct progress_state
{
39 static struct progress_state state
;
40 static volatile sig_atomic_t print_pending
;
43 * Simple progress print function.
44 * @percent relative percent of current operation
45 * @max percent of total operation
47 static void progress_simple_print(void)
49 printf(" (%3.2f/100%%)\r", state
.current
);
53 static void progress_simple_end(void)
58 static void progress_simple_init(void)
60 state
.print
= progress_simple_print
;
61 state
.end
= progress_simple_end
;
65 static void sigusr_print(int signal
)
71 static void progress_dummy_print(void)
74 fprintf(stderr
, " (%3.2f/100%%)\n", state
.current
);
79 static void progress_dummy_end(void)
83 static void progress_dummy_init(void)
86 struct sigaction action
;
88 memset(&action
, 0, sizeof(action
));
89 sigfillset(&action
.sa_mask
);
90 action
.sa_handler
= sigusr_print
;
92 sigaction(SIGUSR1
, &action
, NULL
);
95 state
.print
= progress_dummy_print
;
96 state
.end
= progress_dummy_end
;
100 * Initialize progress reporting.
101 * If @enabled is false, actual reporting is suppressed. The user can
102 * still trigger a report by sending a SIGUSR1.
103 * Reports are also suppressed unless we've had at least @min_skip
104 * percent progress since the last report.
106 void qemu_progress_init(int enabled
, float min_skip
)
108 state
.min_skip
= min_skip
;
110 progress_simple_init();
112 progress_dummy_init();
116 void qemu_progress_end(void)
123 * @delta is how much progress we made.
124 * If @max is zero, @delta is an absolut value of the total job done.
125 * Else, @delta is a progress delta since the last call, as a fraction
126 * of @max. I.e. the delta is @delta * @max / 100. This allows
127 * relative accounting of functions which may be a different fraction of
128 * the full job, depending on the context they are called in. I.e.
129 * a function might be considered 40% of the full job if used from
130 * bdrv_img_create() but only 20% if called from img_convert().
132 void qemu_progress_print(float delta
, int max
)
139 current
= state
.current
+ delta
/ 100 * max
;
144 state
.current
= current
;
146 if (current
> (state
.last_print
+ state
.min_skip
) ||
147 (current
== 100) || (current
== 0)) {
148 state
.last_print
= state
.current
;