liburing icon indicating copy to clipboard operation
liburing copied to clipboard

why io_uring_wait_cqe_nr (&ring, cqe_ptr, 10) only fill cqe_ptr[0], how to get others?

Open ghost opened this issue 2 years ago • 5 comments

io_uring_wait_cqe_nr (&ring, cqe_ptr, 10) , why only fill cqe_ptr[0], cqe_ptr[1~9] is nullptr, how to get others cqe?

what is the specific meaning of this function?

ghost avatar Feb 16 '23 06:02 ghost

It returns the next CQE, but if there is no error then there will be 10 available.

If you want a batch you could use io_uring_peek_batch_cqe (which actually needs a man page I notice).

DylanZA avatar Feb 16 '23 11:02 DylanZA

As Dylan said, it only returns the first one directly. Then you can iterate with eg io_uring_for_each_cqe() and advance the ring with io_uring_cq_advance() once done. Here's an example where we wait for 10 CQEs and then iterate/process them, and finally mark all of them seen in the ring as a single operation:

	ret = io_uring_wait_cqe_nr(&ring, &cqe, 10);
	assert(!ret);

	nr = 0;
	/* iterate all the CQEs we got, should be at least 10 */
	io_uring_for_each_cqe(&ring, head, cqe) {
		printf("Got cqe %ld\n", (long) cqe->user_data);
		nr++;
	}
	/* now advance CQ ring, we've seen 'nr' CQEs */
	io_uring_cq_advance(&ring, nr);

axboe avatar Feb 16 '23 17:02 axboe

As Dylan said, it only returns the first one directly. Then you can iterate with eg io_uring_for_each_cqe() and advance the ring with io_uring_cq_advance() once done. Here's an example where we wait for 10 CQEs and then iterate/process them, and finally mark all of them seen in the ring as a single operation:

	ret = io_uring_wait_cqe_nr(&ring, &cqe, 10);
	assert(!ret);

	nr = 0;
	/* iterate all the CQEs we got, should be at least 10 */
	io_uring_for_each_cqe(&ring, head, cqe) {
		printf("Got cqe %ld\n", (long) cqe->user_data);
		nr++;
	}
	/* now advance CQ ring, we've seen 'nr' CQEs */
	io_uring_cq_advance(&ring, nr);

About the note "should be at least 10", does it mean that io_uring_for_each_cqe will iterate for more than 10 cqes if there are already more than 10 IO requests finished before calling to io_uring_wait_cqes()?

chimaoshu avatar Mar 05 '23 09:03 chimaoshu

About the note "should be at least 10", does it mean that io_uring_for_each_cqe will iterate for more than 10 cqes if there are already more than 10 IO requests finished before calling to io_uring_wait_cqes()?

io_uring_for_each_cqe will iterate over all CQEs that were completed by that point, and we expect at least 10 because we waited for them.

isilence avatar Mar 05 '23 14:03 isilence

thanks;

ghost avatar Mar 06 '23 03:03 ghost