Skip to contentSkip to conversation

Confusion regarding the use of OP_TIMEOUT

DISCUSSION Drew DeVault 2 participants 3 messages May 19, 2021 – May 20, 2021 io_uring

lore
Share & tools
lore
#0 lore raw

Hi folks! I'm trying to use IO_TIMEOUT to insert a pause in the middle of my SQ. I set the off (desired number of events to wait for) to zero, which according to the docs just makes it behave like a timer.

Essentially, I want the following:

[operations...]
OP_TIMEOUT
[operations...]

To be well-ordered, so that the second batch executes after the first. To accomplish this, I've tried to submit the first operation of the second batch with IO_DRAIN, which causes the CQE to be delayed, but ultimately it fails with EINTR instead of just waiting to execute.

I understand that the primary motivator for OP_TIMEOUT is to provide a timeout functionality for other CQEs. Is my use-case not accomodated by io_uring?

#1 lore raw
Quoted 3 lines · Drew DeVault

Hi folks! I'm trying to use IO_TIMEOUT to insert a pause in the middle of my SQ. I set the off (desired number of events to wait for) to zero, which according to the docs just makes it behave like a timer.

Right

Quoted 10 lines

Essentially, I want the following:

[operations...]
OP_TIMEOUT
[operations...]

To be well-ordered, so that the second batch executes after the first. To accomplish this, I've tried to submit the first operation of the second batch with IO_DRAIN, which causes the CQE to be delayed, but

...causes request submission (i.e. execution) to be delayed to be exact, not CQE. But anyway sounds workable to me. (if timeout works well) the second should not be submitted earlier than submission_time + timeout. Or if timeout is marked DRAIN as well would be batch1_completion_time + timeout.

Quoted 1 line

ultimately it fails with EINTR instead of just waiting to execute.

Does some request fails and you find such a CQE (which request?)? Or a syscall? submission or waiting?

Quoted 3 lines

I understand that the primary motivator for OP_TIMEOUT is to provide a timeout functionality for other CQEs. Is my use-case not accomodated by io_uring?

Signature · 1 line
Pavel Begunkov
#2 lore raw
Quoted 6 lines · Pavel Begunkov
Quoted 6 lines

Essentially, I want the following:

[operations...]
OP_TIMEOUT
[operations...]

Quoted 6 lines
Quoted 3 lines

To be well-ordered, so that the second batch executes after the first. To accomplish this, I've tried to submit the first operation of the second batch with IO_DRAIN, which causes the CQE to be delayed, but

...causes request submission (i.e. execution) to be delayed to be exact, not CQE.

Er, right. But that's what I want, anyway.

Quoted 4 lines
Quoted 1 line

ultimately it fails with EINTR instead of just waiting to execute.

Does some request fails and you find such a CQE (which request?)? Or a syscall? submission or waiting?

So I have a test program which does the following:

1. Prep a write("hello world"), then a timeout, and a write("hello world"), in the SQ, in that order.
2. Submit all three
3. Process 3 SQEs, waiting if necessary, and display the result.

This prints "hello world", then waits 2 seconds (the timeout duration), and then the final CQE shows EINTR as the result (and "hello world" is not printed).

Here's an x86_64 binary which reproduces this:

https://l.sr.ht/ZQBh.test

Here's the code, though it's written in an as-of-yet unrelated programming langauge, so not much help beyond illustrative purposes:

use errors;
use fmt;
use linux::io_uring::{flags};
use linux::io_uring;
use rt;
use strings;
use time;

export fn main() void = {
	let params = io_uring::params { ... };
	let ring = match (io_uring::setup(32, &params)) {
		ring: io_uring::io_uring => ring,
		err: io_uring::error => fmt::fatal(io_uring::strerror(err)),
	};
	defer io_uring::finish(&ring);

	let buf = strings::toutf8("Hello world!\n");
	let sqe = io_uring::must_get_sqe(&ring);
	io_uring::write(sqe, 1, buf: *[*]u8, len(buf));

	let ts = rt::timespec { ... };
	time::duration_to_timespec(time::SECOND * 2, &ts);
	let sqe = io_uring::must_get_sqe(&ring);
	io_uring::timeout(sqe, &ts, 0, 0);

	let sqe = io_uring::must_get_sqe(&ring);
	io_uring::write(sqe, 1, buf: *[*]u8, len(buf), flags::IO_DRAIN);

	io_uring::submit(&ring)!;

	for (let i = 0z; i < 3; i += 1) {
		let cqe = match (io_uring::wait(&ring)) {
			err: io_uring::error => fmt::fatal("Error: {}",
				io_uring::strerror(err)),
			cqe: *io_uring::cqe => cqe,
		};
		defer io_uring::cqe_seen(&ring, cqe);

		let result = match (io_uring::result(cqe)) {
			err: io_uring::error => match (err) {
				errors::timeout => continue,
				* => fmt::fatal("Error: {}",
					io_uring::strerror(err)),
			},
			r: int => r,
		};
		fmt::errorfln("result: {} ({})", result, cqe.res)!;
	};
};