summaryrefslogtreecommitdiffstats
path: root/third_party/rust/tokio/tests/io_poll_aio.rs
blob: f044af5cc4ef09e938dd8ff13e15b2084dd0f8f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#![warn(rust_2018_idioms)]
#![cfg(all(target_os = "freebsd", feature = "net"))]

use mio_aio::{AioCb, AioFsyncMode, LioCb};
use std::{
    future::Future,
    mem,
    os::unix::io::{AsRawFd, RawFd},
    pin::Pin,
    task::{Context, Poll},
};
use tempfile::tempfile;
use tokio::io::bsd::{Aio, AioSource};
use tokio_test::assert_pending;

mod aio {
    use super::*;

    /// Adapts mio_aio::AioCb (which implements mio::event::Source) to AioSource
    struct WrappedAioCb<'a>(AioCb<'a>);
    impl<'a> AioSource for WrappedAioCb<'a> {
        fn register(&mut self, kq: RawFd, token: usize) {
            self.0.register_raw(kq, token)
        }
        fn deregister(&mut self) {
            self.0.deregister_raw()
        }
    }

    /// A very crude implementation of an AIO-based future
    struct FsyncFut(Aio<WrappedAioCb<'static>>);

    impl Future for FsyncFut {
        type Output = std::io::Result<()>;

        fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
            let poll_result = self.0.poll_ready(cx);
            match poll_result {
                Poll::Pending => Poll::Pending,
                Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
                Poll::Ready(Ok(_ev)) => {
                    // At this point, we could clear readiness.  But there's no
                    // point, since we're about to drop the Aio.
                    let result = (*self.0).0.aio_return();
                    match result {
                        Ok(_) => Poll::Ready(Ok(())),
                        Err(e) => Poll::Ready(Err(e.into())),
                    }
                }
            }
        }
    }

    /// Low-level AIO Source
    ///
    /// An example bypassing mio_aio and Nix to demonstrate how the kevent
    /// registration actually works, under the hood.
    struct LlSource(Pin<Box<libc::aiocb>>);

    impl AioSource for LlSource {
        fn register(&mut self, kq: RawFd, token: usize) {
            let mut sev: libc::sigevent = unsafe { mem::MaybeUninit::zeroed().assume_init() };
            sev.sigev_notify = libc::SIGEV_KEVENT;
            sev.sigev_signo = kq;
            sev.sigev_value = libc::sigval {
                sival_ptr: token as *mut libc::c_void,
            };
            self.0.aio_sigevent = sev;
        }

        fn deregister(&mut self) {
            unsafe {
                self.0.aio_sigevent = mem::zeroed();
            }
        }
    }

    struct LlFut(Aio<LlSource>);

    impl Future for LlFut {
        type Output = std::io::Result<()>;

        fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
            let poll_result = self.0.poll_ready(cx);
            match poll_result {
                Poll::Pending => Poll::Pending,
                Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
                Poll::Ready(Ok(_ev)) => {
                    let r = unsafe { libc::aio_return(self.0 .0.as_mut().get_unchecked_mut()) };
                    assert_eq!(0, r);
                    Poll::Ready(Ok(()))
                }
            }
        }
    }

    /// A very simple object that can implement AioSource and can be reused.
    ///
    /// mio_aio normally assumes that each AioCb will be consumed on completion.
    /// This somewhat contrived example shows how an Aio object can be reused
    /// anyway.
    struct ReusableFsyncSource {
        aiocb: Pin<Box<AioCb<'static>>>,
        fd: RawFd,
        token: usize,
    }
    impl ReusableFsyncSource {
        fn fsync(&mut self) {
            self.aiocb.register_raw(self.fd, self.token);
            self.aiocb.fsync(AioFsyncMode::O_SYNC).unwrap();
        }
        fn new(aiocb: AioCb<'static>) -> Self {
            ReusableFsyncSource {
                aiocb: Box::pin(aiocb),
                fd: 0,
                token: 0,
            }
        }
        fn reset(&mut self, aiocb: AioCb<'static>) {
            self.aiocb = Box::pin(aiocb);
        }
    }
    impl AioSource for ReusableFsyncSource {
        fn register(&mut self, kq: RawFd, token: usize) {
            self.fd = kq;
            self.token = token;
        }
        fn deregister(&mut self) {
            self.fd = 0;
        }
    }

    struct ReusableFsyncFut<'a>(&'a mut Aio<ReusableFsyncSource>);
    impl<'a> Future for ReusableFsyncFut<'a> {
        type Output = std::io::Result<()>;

        fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
            let poll_result = self.0.poll_ready(cx);
            match poll_result {
                Poll::Pending => Poll::Pending,
                Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
                Poll::Ready(Ok(ev)) => {
                    // Since this future uses a reusable Aio, we must clear
                    // its readiness here.  That makes the future
                    // non-idempotent; the caller can't poll it repeatedly after
                    // it has already returned Ready.  But that's ok; most
                    // futures behave this way.
                    self.0.clear_ready(ev);
                    let result = (*self.0).aiocb.aio_return();
                    match result {
                        Ok(_) => Poll::Ready(Ok(())),
                        Err(e) => Poll::Ready(Err(e.into())),
                    }
                }
            }
        }
    }

    #[tokio::test]
    async fn fsync() {
        let f = tempfile().unwrap();
        let fd = f.as_raw_fd();
        let aiocb = AioCb::from_fd(fd, 0);
        let source = WrappedAioCb(aiocb);
        let mut poll_aio = Aio::new_for_aio(source).unwrap();
        (*poll_aio).0.fsync(AioFsyncMode::O_SYNC).unwrap();
        let fut = FsyncFut(poll_aio);
        fut.await.unwrap();
    }

    #[tokio::test]
    async fn ll_fsync() {
        let f = tempfile().unwrap();
        let fd = f.as_raw_fd();
        let mut aiocb: libc::aiocb = unsafe { mem::MaybeUninit::zeroed().assume_init() };
        aiocb.aio_fildes = fd;
        let source = LlSource(Box::pin(aiocb));
        let mut poll_aio = Aio::new_for_aio(source).unwrap();
        let r = unsafe {
            let p = (*poll_aio).0.as_mut().get_unchecked_mut();
            libc::aio_fsync(libc::O_SYNC, p)
        };
        assert_eq!(0, r);
        let fut = LlFut(poll_aio);
        fut.await.unwrap();
    }

    /// A suitably crafted future type can reuse an Aio object
    #[tokio::test]
    async fn reuse() {
        let f = tempfile().unwrap();
        let fd = f.as_raw_fd();
        let aiocb0 = AioCb::from_fd(fd, 0);
        let source = ReusableFsyncSource::new(aiocb0);
        let mut poll_aio = Aio::new_for_aio(source).unwrap();
        poll_aio.fsync();
        let fut0 = ReusableFsyncFut(&mut poll_aio);
        fut0.await.unwrap();

        let aiocb1 = AioCb::from_fd(fd, 0);
        poll_aio.reset(aiocb1);
        let mut ctx = Context::from_waker(futures::task::noop_waker_ref());
        assert_pending!(poll_aio.poll_ready(&mut ctx));
        poll_aio.fsync();
        let fut1 = ReusableFsyncFut(&mut poll_aio);
        fut1.await.unwrap();
    }
}

mod lio {
    use super::*;

    struct WrappedLioCb<'a>(LioCb<'a>);
    impl<'a> AioSource for WrappedLioCb<'a> {
        fn register(&mut self, kq: RawFd, token: usize) {
            self.0.register_raw(kq, token)
        }
        fn deregister(&mut self) {
            self.0.deregister_raw()
        }
    }

    /// A very crude lio_listio-based Future
    struct LioFut(Option<Aio<WrappedLioCb<'static>>>);

    impl Future for LioFut {
        type Output = std::io::Result<Vec<isize>>;

        fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
            let poll_result = self.0.as_mut().unwrap().poll_ready(cx);
            match poll_result {
                Poll::Pending => Poll::Pending,
                Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
                Poll::Ready(Ok(_ev)) => {
                    // At this point, we could clear readiness.  But there's no
                    // point, since we're about to drop the Aio.
                    let r = self.0.take().unwrap().into_inner().0.into_results(|iter| {
                        iter.map(|lr| lr.result.unwrap()).collect::<Vec<isize>>()
                    });
                    Poll::Ready(Ok(r))
                }
            }
        }
    }

    /// Minimal example demonstrating reuse of an Aio object with lio
    /// readiness.  mio_aio::LioCb actually does something similar under the
    /// hood.
    struct ReusableLioSource {
        liocb: Option<LioCb<'static>>,
        fd: RawFd,
        token: usize,
    }
    impl ReusableLioSource {
        fn new(liocb: LioCb<'static>) -> Self {
            ReusableLioSource {
                liocb: Some(liocb),
                fd: 0,
                token: 0,
            }
        }
        fn reset(&mut self, liocb: LioCb<'static>) {
            self.liocb = Some(liocb);
        }
        fn submit(&mut self) {
            self.liocb
                .as_mut()
                .unwrap()
                .register_raw(self.fd, self.token);
            self.liocb.as_mut().unwrap().submit().unwrap();
        }
    }
    impl AioSource for ReusableLioSource {
        fn register(&mut self, kq: RawFd, token: usize) {
            self.fd = kq;
            self.token = token;
        }
        fn deregister(&mut self) {
            self.fd = 0;
        }
    }
    struct ReusableLioFut<'a>(&'a mut Aio<ReusableLioSource>);
    impl<'a> Future for ReusableLioFut<'a> {
        type Output = std::io::Result<Vec<isize>>;

        fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
            let poll_result = self.0.poll_ready(cx);
            match poll_result {
                Poll::Pending => Poll::Pending,
                Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
                Poll::Ready(Ok(ev)) => {
                    // Since this future uses a reusable Aio, we must clear
                    // its readiness here.  That makes the future
                    // non-idempotent; the caller can't poll it repeatedly after
                    // it has already returned Ready.  But that's ok; most
                    // futures behave this way.
                    self.0.clear_ready(ev);
                    let r = (*self.0).liocb.take().unwrap().into_results(|iter| {
                        iter.map(|lr| lr.result.unwrap()).collect::<Vec<isize>>()
                    });
                    Poll::Ready(Ok(r))
                }
            }
        }
    }

    /// An lio_listio operation with one write element
    #[tokio::test]
    async fn onewrite() {
        const WBUF: &[u8] = b"abcdef";
        let f = tempfile().unwrap();

        let mut builder = mio_aio::LioCbBuilder::with_capacity(1);
        builder = builder.emplace_slice(
            f.as_raw_fd(),
            0,
            &WBUF[..],
            0,
            mio_aio::LioOpcode::LIO_WRITE,
        );
        let liocb = builder.finish();
        let source = WrappedLioCb(liocb);
        let mut poll_aio = Aio::new_for_lio(source).unwrap();

        // Send the operation to the kernel
        (*poll_aio).0.submit().unwrap();
        let fut = LioFut(Some(poll_aio));
        let v = fut.await.unwrap();
        assert_eq!(v.len(), 1);
        assert_eq!(v[0] as usize, WBUF.len());
    }

    /// A suitably crafted future type can reuse an Aio object
    #[tokio::test]
    async fn reuse() {
        const WBUF: &[u8] = b"abcdef";
        let f = tempfile().unwrap();

        let mut builder0 = mio_aio::LioCbBuilder::with_capacity(1);
        builder0 = builder0.emplace_slice(
            f.as_raw_fd(),
            0,
            &WBUF[..],
            0,
            mio_aio::LioOpcode::LIO_WRITE,
        );
        let liocb0 = builder0.finish();
        let source = ReusableLioSource::new(liocb0);
        let mut poll_aio = Aio::new_for_aio(source).unwrap();
        poll_aio.submit();
        let fut0 = ReusableLioFut(&mut poll_aio);
        let v = fut0.await.unwrap();
        assert_eq!(v.len(), 1);
        assert_eq!(v[0] as usize, WBUF.len());

        // Now reuse the same Aio
        let mut builder1 = mio_aio::LioCbBuilder::with_capacity(1);
        builder1 = builder1.emplace_slice(
            f.as_raw_fd(),
            0,
            &WBUF[..],
            0,
            mio_aio::LioOpcode::LIO_WRITE,
        );
        let liocb1 = builder1.finish();
        poll_aio.reset(liocb1);
        let mut ctx = Context::from_waker(futures::task::noop_waker_ref());
        assert_pending!(poll_aio.poll_ready(&mut ctx));
        poll_aio.submit();
        let fut1 = ReusableLioFut(&mut poll_aio);
        let v = fut1.await.unwrap();
        assert_eq!(v.len(), 1);
        assert_eq!(v[0] as usize, WBUF.len());
    }
}