Statistics
| Branch: | Revision:

root / cmd.c @ d1174f13

History | View | Annotate | Download (3.5 kB)

1
/*
2
 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
3
 * All Rights Reserved.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License as
7
 * published by the Free Software Foundation.
8
 *
9
 * This program is distributed in the hope that it would be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16
 */
17

    
18
#include <stdio.h>
19
#include <stdlib.h>
20
#include <string.h>
21
#include <ctype.h>
22
#include <errno.h>
23
#include <sys/time.h>
24
#include <getopt.h>
25

    
26
#include "cmd.h"
27
#include "block/aio.h"
28
#include "qemu/main-loop.h"
29

    
30
#define _(x)        x        /* not gettext support yet */
31

    
32
/* from libxcmd/command.c */
33

    
34
#define EXABYTES(x)        ((long long)(x) << 60)
35
#define PETABYTES(x)        ((long long)(x) << 50)
36
#define TERABYTES(x)        ((long long)(x) << 40)
37
#define GIGABYTES(x)        ((long long)(x) << 30)
38
#define MEGABYTES(x)        ((long long)(x) << 20)
39
#define KILOBYTES(x)        ((long long)(x) << 10)
40

    
41
#define TO_EXABYTES(x)        ((x) / EXABYTES(1))
42
#define TO_PETABYTES(x)        ((x) / PETABYTES(1))
43
#define TO_TERABYTES(x)        ((x) / TERABYTES(1))
44
#define TO_GIGABYTES(x)        ((x) / GIGABYTES(1))
45
#define TO_MEGABYTES(x)        ((x) / MEGABYTES(1))
46
#define TO_KILOBYTES(x)        ((x) / KILOBYTES(1))
47

    
48
void
49
cvtstr(
50
        double                value,
51
        char                *str,
52
        size_t                size)
53
{
54
        char                *trim;
55
        const char        *suffix;
56

    
57
        if (value >= EXABYTES(1)) {
58
                suffix = " EiB";
59
                snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
60
        } else if (value >= PETABYTES(1)) {
61
                suffix = " PiB";
62
                snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
63
        } else if (value >= TERABYTES(1)) {
64
                suffix = " TiB";
65
                snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
66
        } else if (value >= GIGABYTES(1)) {
67
                suffix = " GiB";
68
                snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
69
        } else if (value >= MEGABYTES(1)) {
70
                suffix = " MiB";
71
                snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
72
        } else if (value >= KILOBYTES(1)) {
73
                suffix = " KiB";
74
                snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
75
        } else {
76
                suffix = " bytes";
77
                snprintf(str, size - 6, "%f", value);
78
        }
79

    
80
        trim = strstr(str, ".000");
81
        if (trim) {
82
                strcpy(trim, suffix);
83
        } else {
84
                strcat(str, suffix);
85
        }
86
}
87

    
88
struct timeval
89
tsub(struct timeval t1, struct timeval t2)
90
{
91
        t1.tv_usec -= t2.tv_usec;
92
        if (t1.tv_usec < 0) {
93
                t1.tv_usec += 1000000;
94
                t1.tv_sec--;
95
        }
96
        t1.tv_sec -= t2.tv_sec;
97
        return t1;
98
}
99

    
100
double
101
tdiv(double value, struct timeval tv)
102
{
103
        return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
104
}
105

    
106
#define HOURS(sec)        ((sec) / (60 * 60))
107
#define MINUTES(sec)        (((sec) % (60 * 60)) / 60)
108
#define SECONDS(sec)        ((sec) % 60)
109

    
110
void
111
timestr(
112
        struct timeval        *tv,
113
        char                *ts,
114
        size_t                size,
115
        int                format)
116
{
117
        double                usec = (double)tv->tv_usec / 1000000.0;
118

    
119
        if (format & TERSE_FIXED_TIME) {
120
                if (!HOURS(tv->tv_sec)) {
121
                        snprintf(ts, size, "%u:%02u.%02u",
122
                                (unsigned int) MINUTES(tv->tv_sec),
123
                                (unsigned int) SECONDS(tv->tv_sec),
124
                                (unsigned int) (usec * 100));
125
                        return;
126
                }
127
                format |= VERBOSE_FIXED_TIME;        /* fallback if hours needed */
128
        }
129

    
130
        if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
131
                snprintf(ts, size, "%u:%02u:%02u.%02u",
132
                        (unsigned int) HOURS(tv->tv_sec),
133
                        (unsigned int) MINUTES(tv->tv_sec),
134
                        (unsigned int) SECONDS(tv->tv_sec),
135
                        (unsigned int) (usec * 100));
136
        } else {
137
                snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
138
        }
139
}