blob: e1feb8961479235b433a1afe27f0a20902f94408 [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"
26#include "osdep.h"
27#include "sysemu.h"
28#include <stdio.h>
Jes Sorensena55c73b2011-04-27 14:31:50 +020029#include <signal.h>
Jes Sorensen6b837bc2011-03-30 14:16:25 +020030
31struct progress_state {
Jes Sorensen6b837bc2011-03-30 14:16:25 +020032 float current;
33 float last_print;
34 float min_skip;
Jes Sorensena55c73b2011-04-27 14:31:50 +020035 void (*print)(void);
36 void (*end)(void);
Jes Sorensen6b837bc2011-03-30 14:16:25 +020037};
38
39static struct progress_state state;
40
41/*
42 * Simple progress print function.
43 * @percent relative percent of current operation
44 * @max percent of total operation
45 */
46static void progress_simple_print(void)
47{
Jes Sorensendf6e0082011-04-27 14:31:51 +020048 printf(" (%3.2f/100%%)\r", state.current);
49 fflush(stdout);
Jes Sorensen6b837bc2011-03-30 14:16:25 +020050}
51
52static void progress_simple_end(void)
53{
Jes Sorensena55c73b2011-04-27 14:31:50 +020054 printf("\n");
55}
56
57static void progress_simple_init(void)
58{
59 state.print = progress_simple_print;
60 state.end = progress_simple_end;
61}
62
63#ifdef CONFIG_POSIX
64static void sigusr_print(int signal)
65{
66 printf(" (%3.2f/100%%)\n", state.current);
67}
68#endif
69
70static void progress_dummy_print(void)
71{
72}
73
74static void progress_dummy_end(void)
75{
76}
77
78static void progress_dummy_init(void)
79{
80#ifdef CONFIG_POSIX
81 struct sigaction action;
82
83 memset(&action, 0, sizeof(action));
84 sigfillset(&action.sa_mask);
85 action.sa_handler = sigusr_print;
86 action.sa_flags = 0;
87 sigaction(SIGUSR1, &action, NULL);
88#endif
89
90 state.print = progress_dummy_print;
91 state.end = progress_dummy_end;
Jes Sorensen6b837bc2011-03-30 14:16:25 +020092}
93
94void qemu_progress_init(int enabled, float min_skip)
95{
Jes Sorensen6b837bc2011-03-30 14:16:25 +020096 state.min_skip = min_skip;
Jes Sorensena55c73b2011-04-27 14:31:50 +020097 if (enabled) {
98 progress_simple_init();
99 } else {
100 progress_dummy_init();
101 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200102}
103
104void qemu_progress_end(void)
105{
Jes Sorensena55c73b2011-04-27 14:31:50 +0200106 state.end();
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200107}
108
109void qemu_progress_print(float percent, int max)
110{
111 float current;
112
113 if (max == 0) {
114 current = percent;
115 } else {
116 current = state.current + percent / 100 * max;
117 }
118 if (current > 100) {
119 current = 100;
120 }
121 state.current = current;
122
123 if (current > (state.last_print + state.min_skip) ||
124 (current == 100) || (current == 0)) {
125 state.last_print = state.current;
Jes Sorensena55c73b2011-04-27 14:31:50 +0200126 state.print();
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200127 }
128}