qemu-timer: Add and use new function qemu_timer_expired_ns
[qemu.git] / qemu-progress.c
blob656e065b1de8e76c8e869716657929bce2161f90
1 /*
2 * QEMU progress printing utility functions
4 * Copyright (C) 2011 Jes Sorensen <Jes.Sorensen@redhat.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "qemu-common.h"
26 #include "osdep.h"
27 #include "sysemu.h"
28 #include <stdio.h>
30 struct progress_state {
31 int enabled;
32 float current;
33 float last_print;
34 float min_skip;
37 static struct progress_state state;
40 * Simple progress print function.
41 * @percent relative percent of current operation
42 * @max percent of total operation
44 static void progress_simple_print(void)
46 if (state.enabled) {
47 printf(" (%3.2f/100%%)\r", state.current);
48 fflush(stdout);
52 static void progress_simple_end(void)
54 if (state.enabled) {
55 printf("\n");
59 void qemu_progress_init(int enabled, float min_skip)
61 state.enabled = enabled;
62 state.min_skip = min_skip;
65 void qemu_progress_end(void)
67 progress_simple_end();
70 void qemu_progress_print(float percent, int max)
72 float current;
74 if (max == 0) {
75 current = percent;
76 } else {
77 current = state.current + percent / 100 * max;
79 if (current > 100) {
80 current = 100;
82 state.current = current;
84 if (current > (state.last_print + state.min_skip) ||
85 (current == 100) || (current == 0)) {
86 state.last_print = state.current;
87 progress_simple_print();