Statistics
| Branch: | Revision:

root / qemu-progress.c @ 6b837bc4

History | View | Annotate | Download (2.4 kB)

1 6b837bc4 Jes Sorensen
/*
2 6b837bc4 Jes Sorensen
 * QEMU progress printing utility functions
3 6b837bc4 Jes Sorensen
 *
4 6b837bc4 Jes Sorensen
 * Copyright (C) 2011 Jes Sorensen <Jes.Sorensen@redhat.com>
5 6b837bc4 Jes Sorensen
 *
6 6b837bc4 Jes Sorensen
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 6b837bc4 Jes Sorensen
 * of this software and associated documentation files (the "Software"), to deal
8 6b837bc4 Jes Sorensen
 * in the Software without restriction, including without limitation the rights
9 6b837bc4 Jes Sorensen
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 6b837bc4 Jes Sorensen
 * copies of the Software, and to permit persons to whom the Software is
11 6b837bc4 Jes Sorensen
 * furnished to do so, subject to the following conditions:
12 6b837bc4 Jes Sorensen
 *
13 6b837bc4 Jes Sorensen
 * The above copyright notice and this permission notice shall be included in
14 6b837bc4 Jes Sorensen
 * all copies or substantial portions of the Software.
15 6b837bc4 Jes Sorensen
 *
16 6b837bc4 Jes Sorensen
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 6b837bc4 Jes Sorensen
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 6b837bc4 Jes Sorensen
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 6b837bc4 Jes Sorensen
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 6b837bc4 Jes Sorensen
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 6b837bc4 Jes Sorensen
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 6b837bc4 Jes Sorensen
 * THE SOFTWARE.
23 6b837bc4 Jes Sorensen
 */
24 6b837bc4 Jes Sorensen
25 6b837bc4 Jes Sorensen
#include "qemu-common.h"
26 6b837bc4 Jes Sorensen
#include "osdep.h"
27 6b837bc4 Jes Sorensen
#include "sysemu.h"
28 6b837bc4 Jes Sorensen
#include <stdio.h>
29 6b837bc4 Jes Sorensen
30 6b837bc4 Jes Sorensen
struct progress_state {
31 6b837bc4 Jes Sorensen
    int enabled;
32 6b837bc4 Jes Sorensen
    float current;
33 6b837bc4 Jes Sorensen
    float last_print;
34 6b837bc4 Jes Sorensen
    float min_skip;
35 6b837bc4 Jes Sorensen
};
36 6b837bc4 Jes Sorensen
37 6b837bc4 Jes Sorensen
static struct progress_state state;
38 6b837bc4 Jes Sorensen
39 6b837bc4 Jes Sorensen
/*
40 6b837bc4 Jes Sorensen
 * Simple progress print function.
41 6b837bc4 Jes Sorensen
 * @percent relative percent of current operation
42 6b837bc4 Jes Sorensen
 * @max percent of total operation
43 6b837bc4 Jes Sorensen
 */
44 6b837bc4 Jes Sorensen
static void progress_simple_print(void)
45 6b837bc4 Jes Sorensen
{
46 6b837bc4 Jes Sorensen
    if (state.enabled) {
47 6b837bc4 Jes Sorensen
        printf("    (%3.2f/100%%)\r", state.current);
48 6b837bc4 Jes Sorensen
        fflush(stdout);
49 6b837bc4 Jes Sorensen
    }
50 6b837bc4 Jes Sorensen
}
51 6b837bc4 Jes Sorensen
52 6b837bc4 Jes Sorensen
static void progress_simple_end(void)
53 6b837bc4 Jes Sorensen
{
54 6b837bc4 Jes Sorensen
    if (state.enabled) {
55 6b837bc4 Jes Sorensen
        printf("\n");
56 6b837bc4 Jes Sorensen
    }
57 6b837bc4 Jes Sorensen
}
58 6b837bc4 Jes Sorensen
59 6b837bc4 Jes Sorensen
void qemu_progress_init(int enabled, float min_skip)
60 6b837bc4 Jes Sorensen
{
61 6b837bc4 Jes Sorensen
    state.enabled = enabled;
62 6b837bc4 Jes Sorensen
    state.min_skip = min_skip;
63 6b837bc4 Jes Sorensen
}
64 6b837bc4 Jes Sorensen
65 6b837bc4 Jes Sorensen
void qemu_progress_end(void)
66 6b837bc4 Jes Sorensen
{
67 6b837bc4 Jes Sorensen
    progress_simple_end();
68 6b837bc4 Jes Sorensen
}
69 6b837bc4 Jes Sorensen
70 6b837bc4 Jes Sorensen
void qemu_progress_print(float percent, int max)
71 6b837bc4 Jes Sorensen
{
72 6b837bc4 Jes Sorensen
    float current;
73 6b837bc4 Jes Sorensen
74 6b837bc4 Jes Sorensen
    if (max == 0) {
75 6b837bc4 Jes Sorensen
        current = percent;
76 6b837bc4 Jes Sorensen
    } else {
77 6b837bc4 Jes Sorensen
        current = state.current + percent / 100 * max;
78 6b837bc4 Jes Sorensen
    }
79 6b837bc4 Jes Sorensen
    if (current > 100) {
80 6b837bc4 Jes Sorensen
        current = 100;
81 6b837bc4 Jes Sorensen
    }
82 6b837bc4 Jes Sorensen
    state.current = current;
83 6b837bc4 Jes Sorensen
84 6b837bc4 Jes Sorensen
    if (current > (state.last_print + state.min_skip) ||
85 6b837bc4 Jes Sorensen
        (current == 100) || (current == 0)) {
86 6b837bc4 Jes Sorensen
        state.last_print = state.current;
87 6b837bc4 Jes Sorensen
        progress_simple_print();
88 6b837bc4 Jes Sorensen
    }
89 6b837bc4 Jes Sorensen
}