Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in examples/readme.rs #87

Closed
jiangyinzuo opened this issue Mar 29, 2021 · 2 comments
Closed

Error in examples/readme.rs #87

jiangyinzuo opened this issue Mar 29, 2021 · 2 comments

Comments

@jiangyinzuo
Copy link

jiangyinzuo commented Mar 29, 2021

I download this repository and run

 cargo run --example readme

The program panics and outputs

thread 'main' panicked at 'read error: -22', examples/readme.rs:28:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

My OS envirenment is

Linux version 5.4.0-67-generic (buildd@lcy01-amd64-025) (gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)) #75-Ubuntu SMP Fri Feb 19 18:03:38 UTC 2021

My rust version is 1.51 and io-uring version is 0.5.1.

@quininer
Copy link
Member

Read opcode is available in the 5.6 kernel, and you use 5.4 kernel. You can use Readv opcode instead.

@jiangyinzuo
Copy link
Author

I use Readv and modify the code as follows, and it runs successfully. Thanks a lot.

use io_uring::{opcode, types, IoUring};
use std::os::unix::io::AsRawFd;
use std::{fs, io};
use libc::c_void;

fn main() -> io::Result<()> {
    let mut ring = IoUring::new(8)?;

    let fd = fs::File::open("README.md")?;
    let mut buf = vec![0u8; 4096];

    let iovec = libc::iovec {
        iov_base: buf.as_mut_ptr() as *mut c_void,
        iov_len: 4096,
    };
    let read_e = opcode::Readv::new(types::Fd(fd.as_raw_fd()), &iovec as *const _, 1)
        .build()
        .user_data(0x42);

    // Note that the developer needs to ensure
    // that the entry pushed into submission queue is valid (e.g. fd, buffer).
    unsafe {
        ring.submission()
            .push(&read_e)
            .expect("submission queue is full");
    }

    ring.submit_and_wait(1)?;

    let cqe = ring.completion().next().expect("completion queue is empty");

    assert_eq!(cqe.user_data(), 0x42);
    assert!(cqe.result() >= 0, "read error: {}", cqe.result());

    println!("{}", cqe.result());
    println!("{}", std::str::from_utf8(buf.as_slice()).unwrap());
    Ok(())
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants