1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25#include "qemu/osdep.h"
26#include "qemu-common.h"
27
28struct progress_state {
29 float current;
30 float last_print;
31 float min_skip;
32 void (*print)(void);
33 void (*end)(void);
34};
35
36static struct progress_state state;
37static volatile sig_atomic_t print_pending;
38
39
40
41
42
43
44static void progress_simple_print(void)
45{
46 printf(" (%3.2f/100%%)\r", state.current);
47 fflush(stdout);
48}
49
50static void progress_simple_end(void)
51{
52 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{
64 print_pending = 1;
65}
66#endif
67
68static void progress_dummy_print(void)
69{
70 if (print_pending) {
71 fprintf(stderr, " (%3.2f/100%%)\n", state.current);
72 print_pending = 0;
73 }
74}
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;
84 sigset_t set;
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
92
93
94
95
96
97 sigemptyset(&set);
98 sigaddset(&set, SIGUSR1);
99 pthread_sigmask(SIG_UNBLOCK, &set, NULL);
100#endif
101
102 state.print = progress_dummy_print;
103 state.end = progress_dummy_end;
104}
105
106
107
108
109
110
111
112
113void qemu_progress_init(int enabled, float min_skip)
114{
115 state.min_skip = min_skip;
116 if (enabled) {
117 progress_simple_init();
118 } else {
119 progress_dummy_init();
120 }
121}
122
123void qemu_progress_end(void)
124{
125 state.end();
126}
127
128
129
130
131
132
133
134
135
136
137
138
139void qemu_progress_print(float delta, int max)
140{
141 float current;
142
143 if (max == 0) {
144 current = delta;
145 } else {
146 current = state.current + delta / 100 * max;
147 }
148 if (current > 100) {
149 current = 100;
150 }
151 state.current = current;
152
153 if (current > (state.last_print + state.min_skip) ||
154 current < (state.last_print - state.min_skip) ||
155 current == 100 || current == 0) {
156 state.last_print = state.current;
157 state.print();
158 }
159}
160