2 * QEMU I/O channel test helpers
4 * Copyright (c) 2015 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #include "qemu/osdep.h"
22 #include "io-channel-helpers.h"
25 struct QIOChannelTest
{
34 struct iovec
*outputv
;
40 /* This thread sends all data using iovecs */
41 static gpointer
test_io_thread_writer(gpointer opaque
)
43 QIOChannelTest
*data
= opaque
;
45 qio_channel_set_blocking(data
->src
, data
->blocking
, NULL
);
47 qio_channel_writev_all(data
->src
,
56 /* This thread receives all data using iovecs */
57 static gpointer
test_io_thread_reader(gpointer opaque
)
59 QIOChannelTest
*data
= opaque
;
61 qio_channel_set_blocking(data
->dst
, data
->blocking
, NULL
);
63 qio_channel_readv_all(data
->dst
,
72 QIOChannelTest
*qio_channel_test_new(void)
74 QIOChannelTest
*data
= g_new0(QIOChannelTest
, 1);
79 /* We'll send 1 MB of data */
80 #define CHUNK_COUNT 250
81 #define CHUNK_LEN 4194
83 data
->len
= CHUNK_COUNT
* CHUNK_LEN
;
84 data
->input
= g_new0(char, data
->len
);
85 data
->output
= g_new0(gchar
, data
->len
);
87 /* Fill input with a pattern */
88 for (i
= 0; i
< data
->len
; i
+= CHUNK_LEN
) {
89 memset(data
->input
+ i
, (i
/ CHUNK_LEN
), CHUNK_LEN
);
92 /* We'll split the data across a bunch of IO vecs */
93 data
->niov
= CHUNK_COUNT
;
94 data
->inputv
= g_new0(struct iovec
, data
->niov
);
95 data
->outputv
= g_new0(struct iovec
, data
->niov
);
97 for (i
= 0, offset
= 0; i
< data
->niov
; i
++, offset
+= CHUNK_LEN
) {
98 data
->inputv
[i
].iov_base
= data
->input
+ offset
;
99 data
->outputv
[i
].iov_base
= data
->output
+ offset
;
100 data
->inputv
[i
].iov_len
= CHUNK_LEN
;
101 data
->outputv
[i
].iov_len
= CHUNK_LEN
;
107 void qio_channel_test_run_threads(QIOChannelTest
*test
,
112 GThread
*reader
, *writer
;
116 test
->blocking
= blocking
;
118 reader
= g_thread_new("reader",
119 test_io_thread_reader
,
121 writer
= g_thread_new("writer",
122 test_io_thread_writer
,
125 g_thread_join(reader
);
126 g_thread_join(writer
);
128 test
->dst
= test
->src
= NULL
;
132 void qio_channel_test_run_writer(QIOChannelTest
*test
,
136 test_io_thread_writer(test
);
141 void qio_channel_test_run_reader(QIOChannelTest
*test
,
145 test_io_thread_reader(test
);
150 void qio_channel_test_validate(QIOChannelTest
*test
)
152 g_assert(test
->readerr
== NULL
);
153 g_assert(test
->writeerr
== NULL
);
154 g_assert_cmpint(memcmp(test
->input
,
158 g_free(test
->inputv
);
159 g_free(test
->outputv
);
161 g_free(test
->output
);