]> Git Repo - qemu.git/blob - qemu-progress.c
Add dd-style SIGUSR1 progress reporting
[qemu.git] / qemu-progress.c
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>
29 #include <signal.h>
30
31 struct progress_state {
32     int enabled;
33     float current;
34     float last_print;
35     float min_skip;
36     void (*print)(void);
37     void (*end)(void);
38 };
39
40 static struct progress_state state;
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 {
49     if (state.enabled) {
50         printf("    (%3.2f/100%%)\r", state.current);
51         fflush(stdout);
52     }
53 }
54
55 static void progress_simple_end(void)
56 {
57     printf("\n");
58 }
59
60 static void progress_simple_init(void)
61 {
62     state.print = progress_simple_print;
63     state.end = progress_simple_end;
64 }
65
66 #ifdef CONFIG_POSIX
67 static void sigusr_print(int signal)
68 {
69     printf("    (%3.2f/100%%)\n", state.current);
70 }
71 #endif
72
73 static void progress_dummy_print(void)
74 {
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;
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;
95 }
96
97 void qemu_progress_init(int enabled, float min_skip)
98 {
99     state.enabled = enabled;
100     state.min_skip = min_skip;
101     if (enabled) {
102         progress_simple_init();
103     } else {
104         progress_dummy_init();
105     }
106 }
107
108 void qemu_progress_end(void)
109 {
110     state.end();
111 }
112
113 void qemu_progress_print(float percent, int max)
114 {
115     float current;
116
117     if (max == 0) {
118         current = percent;
119     } else {
120         current = state.current + percent / 100 * max;
121     }
122     if (current > 100) {
123         current = 100;
124     }
125     state.current = current;
126
127     if (current > (state.last_print + state.min_skip) ||
128         (current == 100) || (current == 0)) {
129         state.last_print = state.current;
130         state.print();
131     }
132 }
This page took 0.03234 seconds and 4 git commands to generate.