tokio icon indicating copy to clipboard operation
tokio copied to clipboard

runtime: add on_task_poll_start hook

Open Noah-Kennedy opened this issue 2 years ago • 7 comments

This hook will allow users to have a callback run before tasks are polled. Eventually this should be accompanied on_task_poll_stop, and maybe a few other callbacks.

This was implemented in order to allow tokio-uring to run a periodic maintenance loop.

Noah-Kennedy avatar Apr 25 '22 00:04 Noah-Kennedy

If this is for tokio-uring, then why not a maintenance hook? Then it would run at the same interval as when Tokio runs its IO driver.

Darksonn avatar Apr 25 '22 07:04 Darksonn

I think I agree with Alice; while polling callbacks seem like they could potentially be desirable, if the goal is to add hooks for running occasional maintenance tasks, we should just add a maintenance callback. I can see that being useful for other libraries as well (e.g., QSBR-based data structures I'd like to write...).

hawkw avatar Apr 25 '22 16:04 hawkw

@Darksonn @hawkw the original motivation was so that tokio-uring could occasionally do the equivalent of tokio's periodic epoll_wait(0) and just submit whatever SQEs it is holding and process any arrived CQEs, but @carllerche and I realized that this could have other uses.

Noah-Kennedy avatar Apr 25 '22 16:04 Noah-Kennedy

I commented in discord, but the issue w/ a maintenance hook is it won't be called often enough for tokio-uring. Tasks schedule IO ops by pushing events into the submission queue. The kernel is not able to start the I/O task until it gets flushed. There is a balance between flushing too often and not enough. Flushing at the end of each task poll seems like a good balance. Flushing in a maintenance hook would mean that, at worse, I/O events don't start until 64 other tasks poll.

carllerche avatar Apr 25 '22 17:04 carllerche

Got it, yeah, thanks for clarifying that. I saw the phrase "periodic maintenance loop" and assumed this was background maintenance work that would run less frequently.

hawkw avatar Apr 25 '22 17:04 hawkw

What is the status on this? As far as I can tell, this PR needs to use task IDs and be marked unstable as per the discussion above.

Darksonn avatar Jun 28 '22 14:06 Darksonn

@Darksonn that is correct, I got side-tracked by a bunch of stuff after I made this, and this ended up not quite the correct way of doing what I need to do for tokio-uring. I'll resolve the conflicts later, add the unstable flag, and add in task id support.

Noah-Kennedy avatar Jun 28 '22 22:06 Noah-Kennedy