]> git.meshlink.io Git - meshlink/blob - test/channels-aio.c
Test concurrent AIO and non-AIO transfers.
[meshlink] / test / channels-aio.c
1 #include <assert.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/time.h>
7
8 #include "meshlink.h"
9 #include "utils.h"
10
11 static const size_t size = 25000000; // size of data to transfer
12 static const size_t smallsize = 100000; // size of the data to transfer without AIO
13 static const size_t nchannels = 4; // number of simultaneous channels
14
15 struct aio_info {
16         int callbacks;
17         size_t size;
18         struct timeval tv;
19         struct sync_flag flag;
20 };
21
22 struct channel_info {
23         char *data;
24         struct aio_info aio_infos[2];
25 };
26
27 static size_t bar_received_len;
28 static struct timeval bar_received_tv;
29 static struct sync_flag bar_received_flag;
30
31 static void aio_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len, void *priv) {
32         (void)mesh;
33         (void)channel;
34         (void)data;
35         (void)len;
36
37         struct aio_info *info = priv;
38         gettimeofday(&info->tv, NULL);
39         info->callbacks++;
40         info->size += len;
41         set_sync_flag(&info->flag, true);
42 }
43
44 static bool reject_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
45         (void)mesh;
46         (void)channel;
47         (void)port;
48         (void)data;
49         (void)len;
50
51         return false;
52 }
53
54 static void receive_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, const void *data, size_t len) {
55         (void)mesh;
56         (void)channel;
57         (void)data;
58
59         bar_received_len += len;
60
61         if(bar_received_len >= smallsize) {
62                 gettimeofday(&bar_received_tv, NULL);
63                 set_sync_flag(&bar_received_flag, true);
64         }
65 }
66
67 static bool accept_cb(meshlink_handle_t *mesh, meshlink_channel_t *channel, uint16_t port, const void *data, size_t len) {
68         assert(port && port <= nchannels + 1);
69         assert(!data);
70         assert(!len);
71
72         if(port <= nchannels) {
73                 struct channel_info *infos = mesh->priv;
74                 struct channel_info *info = &infos[port - 1];
75
76                 assert(meshlink_channel_aio_receive(mesh, channel, info->data, size / 4, aio_cb, &info->aio_infos[0]));
77                 assert(meshlink_channel_aio_receive(mesh, channel, info->data + size / 4, size - size / 4, aio_cb, &info->aio_infos[1]));
78         } else {
79                 meshlink_set_channel_receive_cb(mesh, channel, receive_cb);
80         }
81
82         return true;
83 }
84
85 int main(int argc, char *argv[]) {
86         (void)argc;
87         (void)argv;
88
89         // Prepare data buffers
90
91         char *outdata = malloc(size);
92
93         assert(outdata);
94
95         for(size_t i = 0; i < size; i++) {
96                 outdata[i] = i;
97         }
98
99         struct channel_info in_infos[nchannels];
100
101         struct channel_info out_infos[nchannels];
102
103         memset(in_infos, 0, sizeof(in_infos));
104
105         memset(out_infos, 0, sizeof(out_infos));
106
107         for(size_t i = 0; i < nchannels; i++) {
108                 in_infos[i].data = malloc(size);
109                 assert(in_infos[i].data);
110                 out_infos[i].data = outdata;
111         }
112
113         // Open two new meshlink instance.
114
115         meshlink_destroy("channels_aio_conf.1");
116         meshlink_destroy("channels_aio_conf.2");
117
118         meshlink_handle_t *mesh1 = meshlink_open("channels_aio_conf.1", "foo", "channels", DEV_CLASS_BACKBONE);
119         assert(mesh1);
120
121         meshlink_handle_t *mesh2 = meshlink_open("channels_aio_conf.2", "bar", "channels", DEV_CLASS_BACKBONE);
122         assert(mesh2);
123
124         mesh2->priv = in_infos;
125
126         meshlink_enable_discovery(mesh1, false);
127         meshlink_enable_discovery(mesh2, false);
128
129         // Import and export both side's data
130
131         meshlink_add_address(mesh1, "localhost");
132
133         char *data = meshlink_export(mesh1);
134         assert(data);
135         assert(meshlink_import(mesh2, data));
136         free(data);
137
138         data = meshlink_export(mesh2);
139         assert(data);
140         assert(meshlink_import(mesh1, data));
141         free(data);
142
143         // Set the callbacks.
144
145         meshlink_set_channel_accept_cb(mesh1, reject_cb);
146         meshlink_set_channel_accept_cb(mesh2, accept_cb);
147
148         // Start both instances
149
150         assert(meshlink_start(mesh1));
151         assert(meshlink_start(mesh2));
152
153         // Open channels from foo to bar.
154
155         meshlink_node_t *bar = meshlink_get_node(mesh1, "bar");
156         assert(bar);
157
158         meshlink_channel_t *channels[nchannels + 1];
159
160         for(size_t i = 0; i < nchannels + 1; i++) {
161                 channels[i] = meshlink_channel_open(mesh1, bar, i + 1, NULL, NULL, 0);
162                 assert(channels[i]);
163         }
164
165         // Send a large buffer of data on each channel.
166
167         for(size_t i = 0; i < nchannels; i++) {
168                 assert(meshlink_channel_aio_send(mesh1, channels[i], outdata, size / 3, aio_cb, &out_infos[i].aio_infos[0]));
169                 assert(meshlink_channel_aio_send(mesh1, channels[i], outdata + size / 3, size - size / 3, aio_cb, &out_infos[i].aio_infos[1]));
170         }
171
172         // Send a little bit on the last channel using a regular send
173
174         assert(meshlink_channel_send(mesh1, channels[nchannels], outdata, smallsize) == smallsize);
175
176         // Wait for everyone to finish.
177
178         assert(wait_sync_flag(&bar_received_flag, 10));
179
180         for(size_t i = 0; i < nchannels; i++) {
181                 assert(wait_sync_flag(&out_infos[i].aio_infos[0].flag, 10));
182                 assert(wait_sync_flag(&out_infos[i].aio_infos[1].flag, 10));
183                 assert(wait_sync_flag(&in_infos[i].aio_infos[0].flag, 10));
184                 assert(wait_sync_flag(&in_infos[i].aio_infos[1].flag, 10));
185         }
186
187         // Check that everything is correct.
188
189         assert(bar_received_len == smallsize);
190
191         for(size_t i = 0; i < nchannels; i++) {
192                 // Data should be transferred intact.
193                 assert(!memcmp(in_infos[i].data, out_infos[i].data, size));
194
195                 // One callback for each AIO buffer.
196                 assert(out_infos[i].aio_infos[0].callbacks == 1);
197                 assert(out_infos[i].aio_infos[1].callbacks == 1);
198                 assert(in_infos[i].aio_infos[0].callbacks == 1);
199                 assert(in_infos[i].aio_infos[1].callbacks == 1);
200
201                 // Correct size sent and received.
202                 assert(out_infos[i].aio_infos[0].size == size / 3);
203                 assert(out_infos[i].aio_infos[1].size == size - size / 3);
204                 assert(in_infos[i].aio_infos[0].size == size / 4);
205                 assert(in_infos[i].aio_infos[1].size == size - size / 4);
206
207                 // First batch of data should all be sent and received before the second batch
208                 for(size_t j = 0; j < nchannels; j++) {
209                         assert(timercmp(&out_infos[i].aio_infos[0].tv, &out_infos[j].aio_infos[1].tv, <=));
210                         assert(timercmp(&in_infos[i].aio_infos[0].tv, &in_infos[j].aio_infos[1].tv, <=));
211                 }
212
213                 // The non-AIO transfer should have completed before everything else
214                 assert(timercmp(&out_infos[i].aio_infos[0].tv, &bar_received_tv, >=));
215                 assert(timercmp(&in_infos[i].aio_infos[0].tv, &bar_received_tv, >=));
216         }
217
218         // Clean up.
219
220         meshlink_close(mesh2);
221         meshlink_close(mesh1);
222
223         return 0;
224 }