Revision 0b613881

b/Makefile
186 186

  
187 187
qemu-img$(EXESUF): qemu-img.o $(block-obj-y) libqemuutil.a libqemustub.a
188 188
qemu-nbd$(EXESUF): qemu-nbd.o $(block-obj-y) libqemuutil.a libqemustub.a
189
qemu-io$(EXESUF): qemu-io.o qemu-io-cmds.o cmd.o $(block-obj-y) libqemuutil.a libqemustub.a
189
qemu-io$(EXESUF): qemu-io.o qemu-io-cmds.o $(block-obj-y) libqemuutil.a libqemustub.a
190 190

  
191 191
qemu-bridge-helper$(EXESUF): qemu-bridge-helper.o
192 192

  
/dev/null
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
}
b/cmd.h
43 43

  
44 44
int qemuio_command_usage(const cmdinfo_t *ci);
45 45

  
46
/* from input.h */
47
void cvtstr(double value, char *str, size_t sz);
48

  
49
struct timeval tsub(struct timeval t1, struct timeval t2);
50
double tdiv(double value, struct timeval tv);
51

  
52
enum {
53
	DEFAULT_TIME		= 0x0,
54
	TERSE_FIXED_TIME	= 0x1,
55
	VERBOSE_FIXED_TIME	= 0x2
56
};
57

  
58
void timestr(struct timeval *tv, char *str, size_t sz, int flags);
59

  
60 46
bool qemuio_command(const char *cmd);
61 47

  
62 48
#endif	/* __COMMAND_H__ */
b/qemu-io-cmds.c
126 126
    return strtosz_suffix(s, &end, STRTOSZ_DEFSUFFIX_B);
127 127
}
128 128

  
129
#define EXABYTES(x)     ((long long)(x) << 60)
130
#define PETABYTES(x)    ((long long)(x) << 50)
131
#define TERABYTES(x)    ((long long)(x) << 40)
132
#define GIGABYTES(x)    ((long long)(x) << 30)
133
#define MEGABYTES(x)    ((long long)(x) << 20)
134
#define KILOBYTES(x)    ((long long)(x) << 10)
135

  
136
#define TO_EXABYTES(x)  ((x) / EXABYTES(1))
137
#define TO_PETABYTES(x) ((x) / PETABYTES(1))
138
#define TO_TERABYTES(x) ((x) / TERABYTES(1))
139
#define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
140
#define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
141
#define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
142

  
143
static void cvtstr(double value, char *str, size_t size)
144
{
145
    char *trim;
146
    const char *suffix;
147

  
148
    if (value >= EXABYTES(1)) {
149
        suffix = " EiB";
150
        snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
151
    } else if (value >= PETABYTES(1)) {
152
        suffix = " PiB";
153
        snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
154
    } else if (value >= TERABYTES(1)) {
155
        suffix = " TiB";
156
        snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
157
    } else if (value >= GIGABYTES(1)) {
158
        suffix = " GiB";
159
        snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
160
    } else if (value >= MEGABYTES(1)) {
161
        suffix = " MiB";
162
        snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
163
    } else if (value >= KILOBYTES(1)) {
164
        suffix = " KiB";
165
        snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
166
    } else {
167
        suffix = " bytes";
168
        snprintf(str, size - 6, "%f", value);
169
    }
170

  
171
    trim = strstr(str, ".000");
172
    if (trim) {
173
        strcpy(trim, suffix);
174
    } else {
175
        strcat(str, suffix);
176
    }
177
}
178

  
179

  
180

  
181
static struct timeval tsub(struct timeval t1, struct timeval t2)
182
{
183
    t1.tv_usec -= t2.tv_usec;
184
    if (t1.tv_usec < 0) {
185
        t1.tv_usec += 1000000;
186
        t1.tv_sec--;
187
    }
188
    t1.tv_sec -= t2.tv_sec;
189
    return t1;
190
}
191

  
192
static double tdiv(double value, struct timeval tv)
193
{
194
    return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
195
}
196

  
197
#define HOURS(sec)      ((sec) / (60 * 60))
198
#define MINUTES(sec)    (((sec) % (60 * 60)) / 60)
199
#define SECONDS(sec)    ((sec) % 60)
200

  
201
enum {
202
    DEFAULT_TIME        = 0x0,
203
    TERSE_FIXED_TIME    = 0x1,
204
    VERBOSE_FIXED_TIME  = 0x2,
205
};
206

  
207
static void timestr(struct timeval *tv, char *ts, size_t size, int format)
208
{
209
    double usec = (double)tv->tv_usec / 1000000.0;
210

  
211
    if (format & TERSE_FIXED_TIME) {
212
        if (!HOURS(tv->tv_sec)) {
213
            snprintf(ts, size, "%u:%02u.%02u",
214
                    (unsigned int) MINUTES(tv->tv_sec),
215
                    (unsigned int) SECONDS(tv->tv_sec),
216
                    (unsigned int) (usec * 100));
217
            return;
218
        }
219
        format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
220
    }
221

  
222
    if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
223
        snprintf(ts, size, "%u:%02u:%02u.%02u",
224
                (unsigned int) HOURS(tv->tv_sec),
225
                (unsigned int) MINUTES(tv->tv_sec),
226
                (unsigned int) SECONDS(tv->tv_sec),
227
                (unsigned int) (usec * 100));
228
    } else {
229
        snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
230
    }
231
}
232

  
129 233
/*
130 234
 * Parse the pattern argument to various sub-commands.
131 235
 *

Also available in: Unified diff