blob: ad33fee98d71d856f972deee4b3ff8bf48e43a8b [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
25#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010026#include "qemu/osdep.h"
Jes Sorensen6b837bc2011-03-30 14:16:25 +020027#include <stdio.h>
28
29struct progress_state {
Jes Sorensen6b837bc2011-03-30 14:16:25 +020030 float current;
31 float last_print;
32 float min_skip;
Jes Sorensena55c73b2011-04-27 14:31:50 +020033 void (*print)(void);
34 void (*end)(void);
Jes Sorensen6b837bc2011-03-30 14:16:25 +020035};
36
37static struct progress_state state;
Jes Sorensen2ab3cb82011-04-28 13:58:30 +020038static volatile sig_atomic_t print_pending;
Jes Sorensen6b837bc2011-03-30 14:16:25 +020039
40/*
41 * Simple progress print function.
42 * @percent relative percent of current operation
43 * @max percent of total operation
44 */
45static void progress_simple_print(void)
46{
Jes Sorensendf6e0082011-04-27 14:31:51 +020047 printf(" (%3.2f/100%%)\r", state.current);
48 fflush(stdout);
Jes Sorensen6b837bc2011-03-30 14:16:25 +020049}
50
51static void progress_simple_end(void)
52{
Jes Sorensena55c73b2011-04-27 14:31:50 +020053 printf("\n");
54}
55
56static void progress_simple_init(void)
57{
58 state.print = progress_simple_print;
59 state.end = progress_simple_end;
60}
61
62#ifdef CONFIG_POSIX
63static void sigusr_print(int signal)
64{
Jes Sorensen2ab3cb82011-04-28 13:58:30 +020065 print_pending = 1;
Jes Sorensena55c73b2011-04-27 14:31:50 +020066}
67#endif
68
69static void progress_dummy_print(void)
70{
Jes Sorensen2ab3cb82011-04-28 13:58:30 +020071 if (print_pending) {
72 fprintf(stderr, " (%3.2f/100%%)\n", state.current);
73 print_pending = 0;
74 }
Jes Sorensena55c73b2011-04-27 14:31:50 +020075}
76
77static void progress_dummy_end(void)
78{
79}
80
81static void progress_dummy_init(void)
82{
83#ifdef CONFIG_POSIX
84 struct sigaction action;
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);
91#endif
92
93 state.print = progress_dummy_print;
94 state.end = progress_dummy_end;
Jes Sorensen6b837bc2011-03-30 14:16:25 +020095}
96
Jes Sorensen3bfe4db2011-05-09 17:32:20 +020097/*
98 * Initialize progress reporting.
99 * If @enabled is false, actual reporting is suppressed. The user can
100 * still trigger a report by sending a SIGUSR1.
101 * Reports are also suppressed unless we've had at least @min_skip
102 * percent progress since the last report.
103 */
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200104void qemu_progress_init(int enabled, float min_skip)
105{
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200106 state.min_skip = min_skip;
Jes Sorensena55c73b2011-04-27 14:31:50 +0200107 if (enabled) {
108 progress_simple_init();
109 } else {
110 progress_dummy_init();
111 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200112}
113
114void qemu_progress_end(void)
115{
Jes Sorensena55c73b2011-04-27 14:31:50 +0200116 state.end();
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200117}
118
Jes Sorensen3bfe4db2011-05-09 17:32:20 +0200119/*
120 * Report progress.
121 * @delta is how much progress we made.
122 * If @max is zero, @delta is an absolut value of the total job done.
123 * Else, @delta is a progress delta since the last call, as a fraction
124 * of @max. I.e. the delta is @delta * @max / 100. This allows
125 * relative accounting of functions which may be a different fraction of
126 * the full job, depending on the context they are called in. I.e.
127 * a function might be considered 40% of the full job if used from
128 * bdrv_img_create() but only 20% if called from img_convert().
129 */
130void qemu_progress_print(float delta, int max)
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200131{
132 float current;
133
134 if (max == 0) {
Jes Sorensen3bfe4db2011-05-09 17:32:20 +0200135 current = delta;
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200136 } else {
Jes Sorensen3bfe4db2011-05-09 17:32:20 +0200137 current = state.current + delta / 100 * max;
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200138 }
139 if (current > 100) {
140 current = 100;
141 }
142 state.current = current;
143
144 if (current > (state.last_print + state.min_skip) ||
145 (current == 100) || (current == 0)) {
146 state.last_print = state.current;
Jes Sorensena55c73b2011-04-27 14:31:50 +0200147 state.print();
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200148 }
149}