]>
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" | |
26 | #include "osdep.h" | |
27 | #include "sysemu.h" | |
28 | #include <stdio.h> | |
a55c73ba | 29 | #include <signal.h> |
6b837bc4 JS |
30 | |
31 | struct progress_state { | |
6b837bc4 JS |
32 | float current; |
33 | float last_print; | |
34 | float min_skip; | |
a55c73ba JS |
35 | void (*print)(void); |
36 | void (*end)(void); | |
6b837bc4 JS |
37 | }; |
38 | ||
39 | static struct progress_state state; | |
2ab3cb8c | 40 | static volatile sig_atomic_t print_pending; |
6b837bc4 JS |
41 | |
42 | /* | |
43 | * Simple progress print function. | |
44 | * @percent relative percent of current operation | |
45 | * @max percent of total operation | |
46 | */ | |
47 | static void progress_simple_print(void) | |
48 | { | |
df6e008a JS |
49 | printf(" (%3.2f/100%%)\r", state.current); |
50 | fflush(stdout); | |
6b837bc4 JS |
51 | } |
52 | ||
53 | static void progress_simple_end(void) | |
54 | { | |
a55c73ba JS |
55 | printf("\n"); |
56 | } | |
57 | ||
58 | static void progress_simple_init(void) | |
59 | { | |
60 | state.print = progress_simple_print; | |
61 | state.end = progress_simple_end; | |
62 | } | |
63 | ||
64 | #ifdef CONFIG_POSIX | |
65 | static void sigusr_print(int signal) | |
66 | { | |
2ab3cb8c | 67 | print_pending = 1; |
a55c73ba JS |
68 | } |
69 | #endif | |
70 | ||
71 | static void progress_dummy_print(void) | |
72 | { | |
2ab3cb8c JS |
73 | if (print_pending) { |
74 | fprintf(stderr, " (%3.2f/100%%)\n", state.current); | |
75 | print_pending = 0; | |
76 | } | |
a55c73ba JS |
77 | } |
78 | ||
79 | static void progress_dummy_end(void) | |
80 | { | |
81 | } | |
82 | ||
83 | static void progress_dummy_init(void) | |
84 | { | |
85 | #ifdef CONFIG_POSIX | |
86 | struct sigaction action; | |
87 | ||
88 | memset(&action, 0, sizeof(action)); | |
89 | sigfillset(&action.sa_mask); | |
90 | action.sa_handler = sigusr_print; | |
91 | action.sa_flags = 0; | |
92 | sigaction(SIGUSR1, &action, NULL); | |
93 | #endif | |
94 | ||
95 | state.print = progress_dummy_print; | |
96 | state.end = progress_dummy_end; | |
6b837bc4 JS |
97 | } |
98 | ||
99 | void qemu_progress_init(int enabled, float min_skip) | |
100 | { | |
6b837bc4 | 101 | state.min_skip = min_skip; |
a55c73ba JS |
102 | if (enabled) { |
103 | progress_simple_init(); | |
104 | } else { | |
105 | progress_dummy_init(); | |
106 | } | |
6b837bc4 JS |
107 | } |
108 | ||
109 | void qemu_progress_end(void) | |
110 | { | |
a55c73ba | 111 | state.end(); |
6b837bc4 JS |
112 | } |
113 | ||
114 | void qemu_progress_print(float percent, int max) | |
115 | { | |
116 | float current; | |
117 | ||
118 | if (max == 0) { | |
119 | current = percent; | |
120 | } else { | |
121 | current = state.current + percent / 100 * max; | |
122 | } | |
123 | if (current > 100) { | |
124 | current = 100; | |
125 | } | |
126 | state.current = current; | |
127 | ||
128 | if (current > (state.last_print + state.min_skip) || | |
129 | (current == 100) || (current == 0)) { | |
130 | state.last_print = state.current; | |
a55c73ba | 131 | state.print(); |
6b837bc4 JS |
132 | } |
133 | } |