]>
Commit | Line | Data |
---|---|---|
6b837bc4 JS |
1 | /* |
2 | * QEMU progress printing utility functions | |
3 | * | |
4 | * Copyright (C) 2011 Jes Sorensen <[email protected]> | |
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" | |
1de7afc9 | 26 | #include "qemu/osdep.h" |
6b837bc4 JS |
27 | #include <stdio.h> |
28 | ||
29 | struct progress_state { | |
6b837bc4 JS |
30 | float current; |
31 | float last_print; | |
32 | float min_skip; | |
a55c73ba JS |
33 | void (*print)(void); |
34 | void (*end)(void); | |
6b837bc4 JS |
35 | }; |
36 | ||
37 | static struct progress_state state; | |
2ab3cb8c | 38 | static volatile sig_atomic_t print_pending; |
6b837bc4 JS |
39 | |
40 | /* | |
41 | * Simple progress print function. | |
42 | * @percent relative percent of current operation | |
43 | * @max percent of total operation | |
44 | */ | |
45 | static void progress_simple_print(void) | |
46 | { | |
df6e008a JS |
47 | printf(" (%3.2f/100%%)\r", state.current); |
48 | fflush(stdout); | |
6b837bc4 JS |
49 | } |
50 | ||
51 | static void progress_simple_end(void) | |
52 | { | |
a55c73ba JS |
53 | printf("\n"); |
54 | } | |
55 | ||
56 | static void progress_simple_init(void) | |
57 | { | |
58 | state.print = progress_simple_print; | |
59 | state.end = progress_simple_end; | |
60 | } | |
61 | ||
62 | #ifdef CONFIG_POSIX | |
63 | static void sigusr_print(int signal) | |
64 | { | |
2ab3cb8c | 65 | print_pending = 1; |
a55c73ba JS |
66 | } |
67 | #endif | |
68 | ||
69 | static void progress_dummy_print(void) | |
70 | { | |
2ab3cb8c JS |
71 | if (print_pending) { |
72 | fprintf(stderr, " (%3.2f/100%%)\n", state.current); | |
73 | print_pending = 0; | |
74 | } | |
a55c73ba JS |
75 | } |
76 | ||
77 | static void progress_dummy_end(void) | |
78 | { | |
79 | } | |
80 | ||
81 | static void progress_dummy_init(void) | |
82 | { | |
83 | #ifdef CONFIG_POSIX | |
84 | struct sigaction action; | |
3c4b4e38 | 85 | sigset_t set; |
a55c73ba JS |
86 | |
87 | memset(&action, 0, sizeof(action)); | |
88 | sigfillset(&action.sa_mask); | |
89 | action.sa_handler = sigusr_print; | |
90 | action.sa_flags = 0; | |
91 | sigaction(SIGUSR1, &action, NULL); | |
3c4b4e38 KW |
92 | |
93 | /* | |
94 | * SIGUSR1 is SIG_IPI and gets blocked in qemu_init_main_loop(). In the | |
95 | * tools that use the progress report SIGUSR1 isn't used in this meaning | |
96 | * and instead should print the progress, so reenable it. | |
97 | */ | |
98 | sigemptyset(&set); | |
99 | sigaddset(&set, SIGUSR1); | |
100 | pthread_sigmask(SIG_UNBLOCK, &set, NULL); | |
a55c73ba JS |
101 | #endif |
102 | ||
103 | state.print = progress_dummy_print; | |
104 | state.end = progress_dummy_end; | |
6b837bc4 JS |
105 | } |
106 | ||
3bfe4dbf JS |
107 | /* |
108 | * Initialize progress reporting. | |
109 | * If @enabled is false, actual reporting is suppressed. The user can | |
110 | * still trigger a report by sending a SIGUSR1. | |
111 | * Reports are also suppressed unless we've had at least @min_skip | |
112 | * percent progress since the last report. | |
113 | */ | |
6b837bc4 JS |
114 | void qemu_progress_init(int enabled, float min_skip) |
115 | { | |
6b837bc4 | 116 | state.min_skip = min_skip; |
a55c73ba JS |
117 | if (enabled) { |
118 | progress_simple_init(); | |
119 | } else { | |
120 | progress_dummy_init(); | |
121 | } | |
6b837bc4 JS |
122 | } |
123 | ||
124 | void qemu_progress_end(void) | |
125 | { | |
a55c73ba | 126 | state.end(); |
6b837bc4 JS |
127 | } |
128 | ||
3bfe4dbf JS |
129 | /* |
130 | * Report progress. | |
131 | * @delta is how much progress we made. | |
132 | * If @max is zero, @delta is an absolut value of the total job done. | |
133 | * Else, @delta is a progress delta since the last call, as a fraction | |
134 | * of @max. I.e. the delta is @delta * @max / 100. This allows | |
135 | * relative accounting of functions which may be a different fraction of | |
136 | * the full job, depending on the context they are called in. I.e. | |
137 | * a function might be considered 40% of the full job if used from | |
138 | * bdrv_img_create() but only 20% if called from img_convert(). | |
139 | */ | |
140 | void qemu_progress_print(float delta, int max) | |
6b837bc4 JS |
141 | { |
142 | float current; | |
143 | ||
144 | if (max == 0) { | |
3bfe4dbf | 145 | current = delta; |
6b837bc4 | 146 | } else { |
3bfe4dbf | 147 | current = state.current + delta / 100 * max; |
6b837bc4 JS |
148 | } |
149 | if (current > 100) { | |
150 | current = 100; | |
151 | } | |
152 | state.current = current; | |
153 | ||
154 | if (current > (state.last_print + state.min_skip) || | |
155 | (current == 100) || (current == 0)) { | |
156 | state.last_print = state.current; | |
a55c73ba | 157 | state.print(); |
6b837bc4 JS |
158 | } |
159 | } |