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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
extern crate aio_bindings;
extern crate futures;
extern crate libc;
extern crate mio;
extern crate rand;
extern crate tokio;
extern crate futures_cpupool;
extern crate memmap;
use std::collections;
use std::io;
use std::mem;
use std::ops;
use std::ptr;
use std::sync;
use std::os::unix::io::RawFd;
use libc::{c_long, c_void, mlock};
use futures::Future;
use ops::Deref;
use aio_bindings::{aio_context_t, io_event, iocb, syscall, timespec, __NR_io_destroy,
__NR_io_getevents, __NR_io_setup, __NR_io_submit, IOCB_CMD_PREAD,
IOCB_CMD_PWRITE, IOCB_FLAG_RESFD};
mod eventfd;
#[inline(always)]
unsafe fn io_setup(nr: c_long, ctxp: *mut aio_context_t) -> c_long {
syscall(__NR_io_setup as c_long, nr, ctxp)
}
#[inline(always)]
unsafe fn io_destroy(ctx: aio_context_t) -> c_long {
syscall(__NR_io_destroy as c_long, ctx)
}
#[inline(always)]
unsafe fn io_submit(ctx: aio_context_t, nr: c_long, iocbpp: *mut *mut iocb) -> c_long {
syscall(__NR_io_submit as c_long, ctx, nr, iocbpp)
}
#[inline(always)]
unsafe fn io_getevents(
ctx: aio_context_t,
min_nr: c_long,
max_nr: c_long,
events: *mut io_event,
timeout: *mut timespec,
) -> c_long {
syscall(
__NR_io_getevents as c_long,
ctx,
min_nr,
max_nr,
events,
timeout,
)
}
struct SemaphoreInner {
capacity: usize,
waiters: collections::VecDeque<futures::sync::oneshot::Sender<()>>,
}
struct Semaphore {
inner: sync::RwLock<SemaphoreInner>,
}
impl Semaphore {
fn new(initial: usize) -> Semaphore {
Semaphore {
inner: sync::RwLock::new(SemaphoreInner {
capacity: initial,
waiters: collections::VecDeque::new(),
}),
}
}
fn acquire(&self) -> SemaphoreHandle {
let mut lock_result = self.inner.write();
match lock_result {
Ok(ref mut guard) => {
if guard.capacity > 0 {
guard.capacity -= 1;
SemaphoreHandle::Completed(futures::future::result(Ok(())))
} else {
let (sender, receiver) = futures::sync::oneshot::channel();
guard.waiters.push_back(sender);
SemaphoreHandle::Waiting(receiver)
}
}
Err(err) => panic!("Lock failure {:?}", err),
}
}
fn release(&self) {
let mut lock_result = self.inner.write();
match lock_result {
Ok(ref mut guard) => {
if !guard.waiters.is_empty() {
guard.waiters.pop_front().unwrap().send(()).unwrap();
} else {
guard.capacity += 1;
}
}
Err(err) => panic!("Lock failure {:?}", err),
}
}
fn current_capacity(&self) -> usize {
let mut lock_result = self.inner.read();
match lock_result {
Ok(ref mut guard) => guard.capacity,
Err(_) => panic!("Lock failure"),
}
}
}
enum SemaphoreHandle {
Waiting(futures::sync::oneshot::Receiver<()>),
Completed(futures::future::FutureResult<(), io::Error>),
}
impl futures::Future for SemaphoreHandle {
type Item = ();
type Error = io::Error;
fn poll(&mut self) -> Result<futures::Async<()>, io::Error> {
match self {
&mut SemaphoreHandle::Completed(_) => Ok(futures::Async::Ready(())),
&mut SemaphoreHandle::Waiting(ref mut receiver) => receiver
.poll()
.map_err(|err| io::Error::new(io::ErrorKind::Other, err)),
}
}
}
struct AioBaseFuture {
context: sync::Arc<AioContextInner>,
opcode: u32,
fd: RawFd,
offset: u64,
buf: u64,
len: u64,
submitted: sync::atomic::AtomicBool,
state: Option<Box<RequestState>>,
acquire_state: Option<SemaphoreHandle>,
}
impl AioBaseFuture {
fn poll(&mut self) -> Result<futures::Async<()>, io::Error> {
if !self.submitted.load(sync::atomic::Ordering::Acquire) {
assert!(self.state.is_none());
if self.acquire_state.is_none() {
self.acquire_state = Some(self.context.have_capacity.acquire());
}
match self.acquire_state.as_mut().unwrap().poll() {
Err(err) => return Err(err),
Ok(futures::Async::NotReady) => return Ok(futures::Async::NotReady),
Ok(futures::Async::Ready(_)) => {
let mut guard = self.context.capacity.write();
match guard {
Ok(ref mut guard) => {
self.state = guard.state.pop();
}
Err(_) => panic!("TODO: Figure out how to handle this kind of error"),
}
}
}
{
assert!(self.state.is_some());
let state = self.state.as_mut().unwrap();
let state_addr = state.deref().deref() as *const RequestState;
state.request.aio_data = unsafe { mem::transmute(state_addr) };
state.request.aio_resfd = self.context.completed_fd as u32;
state.request.aio_flags = IOCB_FLAG_RESFD;
state.request.aio_fildes = self.fd as u32;
state.request.aio_offset = self.offset as i64;
state.request.aio_buf = self.buf;
state.request.aio_nbytes = self.len;
state.request.aio_lio_opcode = self.opcode as u16;
let (sender, receiver) = futures::sync::oneshot::channel();
state.completed_receiver = receiver;
state.completed_sender = Some(sender);
}
let mut request_ptr_array: [*mut iocb; 1] =
[&mut self.state.as_mut().unwrap().request as *mut iocb; 1];
let result = unsafe {
io_submit(
self.context.context,
1,
&mut request_ptr_array[0] as *mut *mut iocb,
)
};
self.submitted.store(true, sync::atomic::Ordering::Release);
if result != 1 {
return Err(io::Error::last_os_error());
}
}
let result_code = match self.state.as_mut().unwrap().completed_receiver.poll() {
Err(err) => return Err(io::Error::new(io::ErrorKind::Other, err)),
Ok(futures::Async::NotReady) => return Ok(futures::Async::NotReady),
Ok(futures::Async::Ready(n)) => n,
};
match self.context.capacity.write() {
Ok(ref mut guard) => {
guard.state.push(self.state.take().unwrap());
}
Err(_) => panic!("TODO: Figure out how to handle this kind of error"),
}
self.context.have_capacity.release();
if result_code < 0 {
Err(io::Error::from_raw_os_error(result_code as i32))
} else {
Ok(futures::Async::Ready(()))
}
}
}
pub struct AioReadResultFuture<ReadWriteHandle>
where
ReadWriteHandle: ops::DerefMut<Target = [u8]>,
{
base: AioBaseFuture,
buffer: ReadWriteHandle,
}
impl<ReadWriteHandle> futures::Future for AioReadResultFuture<ReadWriteHandle>
where
ReadWriteHandle: ops::DerefMut<Target = [u8]>,
{
type Item = ();
type Error = io::Error;
fn poll(&mut self) -> Result<futures::Async<Self::Item>, Self::Error> {
self.base.poll()
}
}
pub struct AioWriteResultFuture<ReadOnlyHandle>
where
ReadOnlyHandle: ops::Deref<Target = [u8]>,
{
base: AioBaseFuture,
buffer: ReadOnlyHandle,
}
impl<ReadOnlyHandle> futures::Future for AioWriteResultFuture<ReadOnlyHandle>
where
ReadOnlyHandle: ops::Deref<Target = [u8]>,
{
type Item = ();
type Error = io::Error;
fn poll(&mut self) -> Result<futures::Async<Self::Item>, Self::Error> {
self.base.poll()
}
}
struct RequestState {
request: iocb,
completed_receiver: futures::sync::oneshot::Receiver<c_long>,
completed_sender: Option<futures::sync::oneshot::Sender<c_long>>,
}
struct Capacity {
state: Vec<Box<RequestState>>,
}
impl Capacity {
fn new(nr: usize) -> Result<Capacity, io::Error> {
let mut state = Vec::with_capacity(nr);
for _ in 0..nr {
let (sender, receiver) = futures::sync::oneshot::channel();
state.push(Box::new(RequestState {
request: unsafe { mem::zeroed() },
completed_receiver: receiver,
completed_sender: None,
}));
}
Ok(Capacity { state })
}
}
struct AioContextInner {
context: aio_context_t,
completed_fd: RawFd,
have_capacity: Semaphore,
capacity: sync::RwLock<Capacity>,
}
impl AioContextInner {
fn new(fd: RawFd, nr: usize) -> Result<AioContextInner, io::Error> {
let mut context: aio_context_t = 0;
unsafe {
if io_setup(nr as c_long, &mut context) != 0 {
return Err(io::Error::last_os_error());
}
};
Ok(AioContextInner {
context,
capacity: sync::RwLock::new(Capacity::new(nr)?),
have_capacity: Semaphore::new(nr),
completed_fd: fd,
})
}
}
impl Drop for AioContextInner {
fn drop(&mut self) {
let result = unsafe { io_destroy(self.context) };
assert!(result == 0);
}
}
pub struct AioContext {
inner: sync::Arc<AioContextInner>,
poll_task_handle: futures::sync::oneshot::SpawnHandle<(), io::Error>,
}
impl AioContext {
pub fn new<E>(executor: &E, nr: usize) -> Result<AioContext, io::Error>
where
E: futures::future::Executor<futures::sync::oneshot::Execute<AioPollFuture>>,
{
let eventfd = eventfd::EventFd::create(0, false)?;
let fd = eventfd.evented.get_ref().fd;
let inner = AioContextInner::new(fd, nr)?;
let context = inner.context;
let poll_future = AioPollFuture {
context,
eventfd,
events: Vec::with_capacity(nr),
};
Ok(AioContext {
inner: sync::Arc::new(inner),
poll_task_handle: futures::sync::oneshot::spawn(poll_future, executor),
})
}
pub fn read<ReadWriteHandle>(
&self,
fd: RawFd,
offset: u64,
buffer: ReadWriteHandle,
) -> AioReadResultFuture<ReadWriteHandle>
where
ReadWriteHandle: ops::DerefMut<Target = [u8]>,
{
let len = buffer.len() as u64;
AioReadResultFuture {
base: AioBaseFuture {
context: self.inner.clone(),
opcode: IOCB_CMD_PREAD,
fd,
offset,
len,
buf: unsafe { mem::transmute(buffer.as_ptr()) },
submitted: sync::atomic::AtomicBool::new(false),
state: None,
acquire_state: None,
},
buffer,
}
}
pub fn write<ReadOnlyHandle>(
&self,
fd: RawFd,
offset: u64,
buffer: ReadOnlyHandle,
) -> AioWriteResultFuture<ReadOnlyHandle>
where
ReadOnlyHandle: ops::Deref<Target = [u8]>,
{
let len = buffer.len() as u64;
AioWriteResultFuture {
base: AioBaseFuture {
context: self.inner.clone(),
opcode: IOCB_CMD_PWRITE,
fd,
offset,
len,
buf: unsafe { mem::transmute(buffer.as_ptr()) },
submitted: sync::atomic::AtomicBool::new(false),
state: None,
acquire_state: None,
},
buffer,
}
}
}
pub struct AioPollFuture {
context: aio_context_t,
eventfd: eventfd::EventFd,
events: Vec<io_event>,
}
impl futures::Future for AioPollFuture {
type Item = ();
type Error = io::Error;
fn poll(&mut self) -> Result<futures::Async<Self::Item>, Self::Error> {
loop {
let available = match self.eventfd.read() {
Err(err) => return Err(err),
Ok(futures::Async::NotReady) => return Ok(futures::Async::NotReady),
Ok(futures::Async::Ready(value)) => value as usize,
};
assert!(available > 0);
self.events.clear();
unsafe {
let result = io_getevents(
self.context,
available as c_long,
available as c_long,
self.events.as_mut_ptr(),
ptr::null_mut::<timespec>(),
);
if result < 0 {
return Err(io::Error::last_os_error());
}
assert!(result as usize == available);
self.events.set_len(available);
};
for ref event in &self.events {
let request_state: &mut RequestState = unsafe { mem::transmute(event.data) };
request_state
.completed_sender
.take()
.unwrap()
.send(event.res)
.unwrap();
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::env;
use std::fs;
use std::io::Write;
use std::os::unix::ffi::OsStrExt;
use std::path;
use std::sync;
use rand::Rng;
use tokio::executor::current_thread;
use memmap;
use futures_cpupool;
use libc::{close, open, O_DIRECT, O_RDWR};
const FILE_SIZE: u64 = 1024 * 512;
fn temp_file_name() -> path::PathBuf {
let mut rng = rand::thread_rng();
let mut result = env::temp_dir();
let filename = format!("test-aio-{}.dat", rng.gen::<u64>());
result.push(filename);
result
}
fn create_temp_file(path: &path::Path) {
let mut file = fs::File::create(path).unwrap();
let mut data: [u8; FILE_SIZE as usize] = [0; FILE_SIZE as usize];
for index in 0..data.len() {
data[index] = index as u8;
}
let result = file.write(&data).and_then(|_| file.sync_all());
assert!(result.is_ok());
}
fn remove_file(path: &path::Path) {
let _ = fs::remove_file(path);
}
#[test]
fn create_and_drop() {
let pool = futures_cpupool::CpuPool::new(3);
let _context = AioContext::new(&pool, 10).unwrap();
}
struct MemoryBlock {
bytes: sync::RwLock<memmap::MmapMut>,
}
impl MemoryBlock {
fn new() -> MemoryBlock {
let map = memmap::MmapMut::map_anon(8192).unwrap();
unsafe { mlock(map.as_ref().as_ptr() as *const c_void, map.len()) };
MemoryBlock {
bytes: sync::RwLock::new(map),
}
}
}
struct MemoryHandle {
block: sync::Arc<MemoryBlock>,
}
impl MemoryHandle {
fn new() -> MemoryHandle {
MemoryHandle {
block: sync::Arc::new(MemoryBlock::new()),
}
}
}
impl Clone for MemoryHandle {
fn clone(&self) -> MemoryHandle {
MemoryHandle {
block: self.block.clone(),
}
}
}
impl ops::Deref for MemoryHandle {
type Target = [u8];
fn deref(&self) -> &Self::Target {
unsafe { mem::transmute(&(*self.block.bytes.read().unwrap())[..]) }
}
}
impl ops::DerefMut for MemoryHandle {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { mem::transmute(&mut (*self.block.bytes.write().unwrap())[..]) }
}
}
#[test]
fn read_block_mt() {
let file_name = temp_file_name();
create_temp_file(&file_name);
{
let owned_fd = OwnedFd::new_from_raw_fd(unsafe {
open(
mem::transmute(file_name.as_os_str().as_bytes().as_ptr()),
O_DIRECT | O_RDWR,
)
});
let fd = owned_fd.fd;
let pool = futures_cpupool::CpuPool::new(5);
let buffer = MemoryHandle::new();
let result_buffer = buffer.clone();
{
let context = AioContext::new(&pool, 10).unwrap();
let read_future = context
.read(fd, 0, buffer)
.map(move |_| {
assert!(validate_block(&result_buffer));
})
.map_err(|err| {
panic!("{:?}", err);
});
let cpu_future = pool.spawn(read_future);
let result = cpu_future.wait();
assert!(result.is_ok());
}
}
remove_file(&file_name);
}
#[test]
fn read_invalid_fd() {
let fd = 2431;
let pool = futures_cpupool::CpuPool::new(5);
let buffer = MemoryHandle::new();
let result_buffer = buffer.clone();
{
let context = AioContext::new(&pool, 10).unwrap();
let read_future = context
.read(fd, 0, buffer)
.map(move |_| {
assert!(false);
})
.map_err(|err| {
assert!(err.kind() == io::ErrorKind::Other);
err
});
let cpu_future = pool.spawn(read_future);
let result = cpu_future.wait();
assert!(result.is_err());
}
}
#[test]
fn read_many_blocks_mt() {
let file_name = temp_file_name();
create_temp_file(&file_name);
{
let owned_fd = OwnedFd::new_from_raw_fd(unsafe {
open(
mem::transmute(file_name.as_os_str().as_bytes().as_ptr()),
O_DIRECT | O_RDWR,
)
});
let fd = owned_fd.fd;
let pool = futures_cpupool::CpuPool::new(5);
{
let num_slots = 7;
let context = AioContext::new(&pool, num_slots).unwrap();
for _wave in 0..50 {
let mut futures = Vec::new();
for index in 0..100 {
let buffer = MemoryHandle::new();
let result_buffer = buffer.clone();
let read_future = context
.read(fd, (index * 8192) % FILE_SIZE, buffer)
.map(move |_| {
assert!(validate_block(&result_buffer));
})
.map_err(|err| {
panic!("{:?}", err);
});
futures.push(pool.spawn(read_future));
}
let result = futures::future::join_all(futures).wait();
assert!(result.is_ok());
assert!(context.inner.have_capacity.current_capacity() == num_slots);
}
}
}
remove_file(&file_name);
}
fn validate_block(data: &[u8]) -> bool {
for index in 0..data.len() {
if data[index] != index as u8 {
return false;
}
}
true
}
struct OwnedFd {
fd: RawFd,
}
impl OwnedFd {
fn new_from_raw_fd(fd: RawFd) -> OwnedFd {
OwnedFd { fd }
}
}
impl Drop for OwnedFd {
fn drop(&mut self) {
let result = unsafe { close(self.fd) };
assert!(result == 0);
}
}
}