Skip to contentSkip to conversation

io_uring_peek_cqe and EAGAIN

PATCH William Dauchy 2 participants 5 messages Apr 20, 2020 – Apr 23, 2020 io_uring

lore
Share & tools
lore
#0 lore raw

Hello,

While doing some tests which are open/read/close files I saw that I was getting -EAGAIN return value sometimesi on io_uring_peek_cqe, and more often after dropping caches.
In parrallel, when reading examples provided by liburing, we can see that getting this error is making the example fail (such as in io_uring-cp). So I was wondering whether it was stupid to change the example to something like:

examples/io_uring-cp.c +5 -5
index cc7a227..2d6d190 100644
@@ -170,11 +170,11 @@ static int copy_file(struct io_uring *ring, off_t insize)
     ret = io_uring_wait_cqe(ring, &cqe);
     got_comp = 1;
    } else {
-    ret = io_uring_peek_cqe(ring, &cqe);
-    if (ret == -EAGAIN) {
-     cqe = NULL;
-     ret = 0;
-    }
+    do {
+     ret = io_uring_peek_cqe(ring, &cqe)
+     if (ret != -EAGAIN)
+      break;
+    } while (1);
    }
    if (ret < 0) {
     fprintf(stderr, "io_uring_peek_cqe: %s\n",

Best,

Signature · 1 line
William
maintainer
#1 lore raw
Quoted 18 lines · William Dauchy

Hello,

While doing some tests which are open/read/close files I saw that I was getting -EAGAIN return value sometimesi on io_uring_peek_cqe, and more often after dropping caches.
In parrallel, when reading examples provided by liburing, we can see that getting this error is making the example fail (such as in io_uring-cp). So I was wondering whether it was stupid to change the example to something like:

examples/io_uring-cp.c +0 -0
index cc7a227..2d6d190 100644
@@ -170,11 +170,11 @@ static int copy_file(struct io_uring *ring, off_t insize)
     ret = io_uring_wait_cqe(ring, &cqe);
     got_comp = 1;
    } else {

examples/io_uring-cp.c static int copy_file(struct io_uring *ring, off_t insize) · line 177

Quoted 10 lines
examples/io_uring-cp.c +5 -5
-    ret = io_uring_peek_cqe(ring, &cqe);
-    if (ret == -EAGAIN) {
-     cqe = NULL;
-     ret = 0;
-    }
+    do {
+     ret = io_uring_peek_cqe(ring, &cqe)
+     if (ret != -EAGAIN)
+      break;
+    } while (1);

I don't think the change is correct. That's not saying that the original code is necessarily correct, though! Basically there are two cases there:

1) We haven't gotten a completion yet, we'll wait for it.
2) We already found at least one completion. We don't want to _wait_ for more, but we can peek and see if there are more.

Hence we don't want to turn case 2 into a loop, we should just continue.

How is it currently failing for you?

Signature · 1 line
Jens Axboe
#2 lore raw

Hello Jens,

Thank you for your answer on this newbie question :)

Quoted 9 lines · Jens Axboe

I don't think the change is correct. That's not saying that the original code is necessarily correct, though! Basically there are two cases there:

1) We haven't gotten a completion yet, we'll wait for it.
2) We already found at least one completion. We don't want to _wait_ for more, but we can peek and see if there are more.

Hence we don't want to turn case 2 into a loop, we should just continue.

ok so in fact I think I understand that my usage is incorrect:
1- if I'm in the case of being able to do other things while waiting for data available using io_uring_peek_cqe, I should use it and come back later when getting a -EAGAIN.
2- it is useless to do a loop on io_uring_peek_cqe because in that case, I should simply do a io_uring_wait_cqe

is that correct?

Quoted 1 line

How is it currently failing for you?

While trying to open/read/close multiple files, I first thought that, because I had one successful io_uring_wait_cqe, I could then loop on io_uring_peek_cqe and get all my data. I now realise my assumption was completely wrong and this example was just written that way to show two different possibilities of getting results.

Signature · 1 line
William
maintainer
#3 lore raw
Quoted 15 lines · William Dauchy

Hello Jens,

Thank you for your answer on this newbie question :)

Quoted 9 lines · Jens Axboe

I don't think the change is correct. That's not saying that the original code is necessarily correct, though! Basically there are two cases there:

1) We haven't gotten a completion yet, we'll wait for it.
2) We already found at least one completion. We don't want to _wait_ for more, but we can peek and see if there are more.

Hence we don't want to turn case 2 into a loop, we should just continue.

Quoted 8 lines

ok so in fact I think I understand that my usage is incorrect:
1- if I'm in the case of being able to do other things while waiting for data available using io_uring_peek_cqe, I should use it and come back later when getting a -EAGAIN.
2- it is useless to do a loop on io_uring_peek_cqe because in that case, I should simply do a io_uring_wait_cqe

is that correct?

Right, you rarely want to busy loop on io_uring_peek_cqe(), the normal use case would be to use io_uring_wait_cqe() if you need to wait for a completion to become available.

Quoted 7 lines
Quoted 1 line

How is it currently failing for you?

While trying to open/read/close multiple files, I first thought that, because I had one successful io_uring_wait_cqe, I could then loop on io_uring_peek_cqe and get all my data. I now realise my assumption was completely wrong and this example was just written that way to show two different possibilities of getting results.

Ah ok, yes that sounds like a misunderstanding. Events are posted as they become available, availability of one does not mean that everything has completed.

Signature · 1 line
Jens Axboe
#4 lore raw
Quoted 3 lines · Jens Axboe

Ah ok, yes that sounds like a misunderstanding. Events are posted as they become available, availability of one does not mean that everything has completed.

Indeed, now that I understood my mistake everything is crystal clear. Thanks a lot for the clarification!

Signature · 1 line
William