2020-07-27 20:23:42 +05:30
|
|
|
use anyhow::{anyhow, Result};
|
|
|
|
use std::alloc::{alloc, dealloc, Layout};
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::fs::OpenOptions;
|
2020-06-09 13:45:00 +05:30
|
|
|
use std::io;
|
|
|
|
use std::io::{Read, Seek};
|
|
|
|
use std::os::unix::fs::OpenOptionsExt;
|
2020-07-28 17:27:30 +05:30
|
|
|
use std::os::unix::io::AsRawFd;
|
2020-07-27 20:23:42 +05:30
|
|
|
use std::path::Path;
|
|
|
|
use std::sync::{Arc, Mutex};
|
2020-07-28 17:27:30 +05:30
|
|
|
use io_uring::opcode::{self, types};
|
|
|
|
use io_uring::IoUring;
|
|
|
|
|
|
|
|
//------------------------------------------
|
2020-06-09 13:45:00 +05:30
|
|
|
|
|
|
|
pub const BLOCK_SIZE: usize = 4096;
|
2020-07-27 20:23:42 +05:30
|
|
|
const ALIGN: usize = 4096;
|
2020-06-09 13:45:00 +05:30
|
|
|
|
2020-07-27 20:23:42 +05:30
|
|
|
#[derive(Debug)]
|
2020-06-09 13:45:00 +05:30
|
|
|
pub struct Block {
|
2020-06-14 12:47:46 +05:30
|
|
|
pub loc: u64,
|
2020-07-27 20:23:42 +05:30
|
|
|
data: *mut u8,
|
2020-06-09 13:45:00 +05:30
|
|
|
}
|
2020-07-27 20:23:42 +05:30
|
|
|
|
|
|
|
impl Block {
|
|
|
|
pub fn new(loc: u64) -> Block {
|
|
|
|
let layout = Layout::from_size_align(BLOCK_SIZE, ALIGN).unwrap();
|
|
|
|
let ptr = unsafe { alloc(layout) };
|
|
|
|
assert!(!ptr.is_null(), "out of memory");
|
|
|
|
Block { loc, data: ptr }
|
|
|
|
}
|
|
|
|
|
2020-07-28 15:21:48 +05:30
|
|
|
pub fn get_data<'a>(&self) -> &'a mut [u8] {
|
|
|
|
unsafe { std::slice::from_raw_parts_mut::<'a>(self.data, BLOCK_SIZE) }
|
2020-07-27 20:23:42 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Block {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let layout = Layout::from_size_align(BLOCK_SIZE, ALIGN).unwrap();
|
|
|
|
unsafe {
|
|
|
|
dealloc(self.data, layout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
|
|
|
pub trait IoEngine {
|
|
|
|
fn get_nr_blocks(&self) -> u64;
|
2020-07-28 15:21:48 +05:30
|
|
|
fn read(&mut self, block: &mut Block) -> Result<()>;
|
|
|
|
fn read_many(&mut self, blocks: &mut Vec<Block>) -> Result<()>;
|
2020-06-09 13:45:00 +05:30
|
|
|
}
|
|
|
|
|
2020-07-27 20:23:42 +05:30
|
|
|
fn get_nr_blocks(path: &Path) -> io::Result<u64> {
|
2020-06-09 13:45:00 +05:30
|
|
|
let metadata = std::fs::metadata(path)?;
|
|
|
|
Ok(metadata.len() / (BLOCK_SIZE as u64))
|
|
|
|
}
|
|
|
|
|
2020-07-27 20:23:42 +05:30
|
|
|
//------------------------------------------
|
|
|
|
|
|
|
|
pub struct SyncIoEngine {
|
|
|
|
nr_blocks: u64,
|
|
|
|
input: File,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SyncIoEngine {
|
|
|
|
pub fn new(path: &Path) -> Result<SyncIoEngine> {
|
2020-06-09 13:45:00 +05:30
|
|
|
let input = OpenOptions::new()
|
|
|
|
.read(true)
|
|
|
|
.write(false)
|
|
|
|
.custom_flags(libc::O_DIRECT)
|
|
|
|
.open(path)?;
|
|
|
|
|
2020-07-27 20:23:42 +05:30
|
|
|
Ok(SyncIoEngine {
|
2020-06-09 13:45:00 +05:30
|
|
|
nr_blocks: get_nr_blocks(path)?,
|
2020-06-10 21:16:38 +05:30
|
|
|
input,
|
2020-06-09 13:45:00 +05:30
|
|
|
})
|
|
|
|
}
|
2020-07-27 20:23:42 +05:30
|
|
|
}
|
2020-06-09 13:45:00 +05:30
|
|
|
|
2020-07-27 20:23:42 +05:30
|
|
|
impl IoEngine for SyncIoEngine {
|
|
|
|
fn get_nr_blocks(&self) -> u64 {
|
|
|
|
self.nr_blocks
|
2020-06-09 13:45:00 +05:30
|
|
|
}
|
|
|
|
|
2020-07-28 15:21:48 +05:30
|
|
|
fn read(&mut self, b: &mut Block) -> Result<()> {
|
2020-07-28 17:27:30 +05:30
|
|
|
self.input
|
|
|
|
.seek(io::SeekFrom::Start(b.loc * BLOCK_SIZE as u64))?;
|
2020-07-28 15:21:48 +05:30
|
|
|
self.input.read_exact(&mut b.get_data())?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_many(&mut self, blocks: &mut Vec<Block>) -> Result<()> {
|
|
|
|
for b in blocks {
|
|
|
|
self.read(b);
|
2020-07-27 20:23:42 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
|
|
|
pub struct AsyncIoEngine {
|
2020-07-28 17:27:30 +05:30
|
|
|
ring: IoUring,
|
2020-07-27 20:23:42 +05:30
|
|
|
nr_blocks: u64,
|
|
|
|
input: File,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsyncIoEngine {
|
2020-07-28 17:27:30 +05:30
|
|
|
pub fn new(path: &Path, queue_len: u32) -> Result<AsyncIoEngine> {
|
2020-07-27 20:23:42 +05:30
|
|
|
let input = OpenOptions::new()
|
|
|
|
.read(true)
|
|
|
|
.write(false)
|
|
|
|
.custom_flags(libc::O_DIRECT)
|
|
|
|
.open(path)?;
|
|
|
|
|
2020-07-28 17:27:30 +05:30
|
|
|
Ok(AsyncIoEngine {
|
|
|
|
ring: IoUring::new(queue_len)?,
|
2020-07-27 20:23:42 +05:30
|
|
|
nr_blocks: get_nr_blocks(path)?,
|
|
|
|
input,
|
|
|
|
})
|
|
|
|
}
|
2020-07-28 17:27:30 +05:30
|
|
|
}
|
2020-07-27 20:23:42 +05:30
|
|
|
|
2020-07-28 17:27:30 +05:30
|
|
|
impl IoEngine for AsyncIoEngine {
|
|
|
|
fn get_nr_blocks(&self) -> u64 {
|
|
|
|
self.nr_blocks
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read(&mut self, b: &mut Block) -> Result<()> {
|
|
|
|
let fd = types::Target::Fd(self.input.as_raw_fd());
|
|
|
|
let read_e = opcode::Read::new(fd, b.data, BLOCK_SIZE as u32).offset(b.loc as i64 * BLOCK_SIZE as i64);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let mut queue = self.ring.submission().available();
|
|
|
|
queue.push(read_e.build().user_data(1))
|
|
|
|
.ok()
|
|
|
|
.expect("queue is full");
|
2020-07-27 20:23:42 +05:30
|
|
|
}
|
|
|
|
|
2020-07-28 17:27:30 +05:30
|
|
|
self.ring.submit_and_wait(1)?;
|
|
|
|
|
|
|
|
let cqes = self.ring.completion().available().collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// FIXME: return proper errors
|
|
|
|
assert_eq!(cqes.len(), 1);
|
|
|
|
assert_eq!(cqes[0].user_data(), 1);
|
|
|
|
assert_eq!(cqes[0].result(), BLOCK_SIZE as i32);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_many(&mut self, blocks: &mut Vec<Block>) -> Result<()> {
|
|
|
|
let count = blocks.len();
|
|
|
|
let fd = types::Target::Fd(self.input.as_raw_fd());
|
|
|
|
|
|
|
|
for b in blocks.into_iter() {
|
|
|
|
let read_e = opcode::Read::new(fd, b.data, BLOCK_SIZE as u32).offset(b.loc as i64 * BLOCK_SIZE as i64);
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let mut queue = self.ring.submission().available();
|
|
|
|
queue.push(read_e.build().user_data(1))
|
|
|
|
.ok()
|
|
|
|
.expect("queue is full");
|
2020-07-27 20:23:42 +05:30
|
|
|
}
|
|
|
|
}
|
2020-06-09 13:45:00 +05:30
|
|
|
|
2020-07-28 17:27:30 +05:30
|
|
|
self.ring.submit_and_wait(count)?;
|
|
|
|
|
|
|
|
let cqes = self.ring.completion().available().collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// FIXME: return proper errors
|
|
|
|
assert_eq!(cqes.len(), count);
|
|
|
|
for c in &cqes {
|
|
|
|
assert_eq!(c.result(), BLOCK_SIZE as i32);
|
|
|
|
}
|
2020-06-09 13:45:00 +05:30
|
|
|
|
2020-07-27 20:23:42 +05:30
|
|
|
Ok(())
|
2020-06-09 13:45:00 +05:30
|
|
|
}
|
|
|
|
}
|
2020-07-28 17:27:30 +05:30
|
|
|
|
|
|
|
//------------------------------------------
|