-
Notifications
You must be signed in to change notification settings - Fork 6
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
Provide ability to replace Unix.select
in Picos_select
#129
Comments
Thanks for the thorough summary! I would also add Yes, I totally agree that My idea with
in order to keep complexity and scope rather bounded. IOW, OTOH, I'm aware of various alternatives and limitations of Also, I hadn't thought about making |
Perhaps it'd be useful to separate the timeout handling and priority queue from file descriptor handling (I understand that these are deeply tied together and will be part of the same event loop). AFAICT Then you can of course have a minimal example based on
Indeed, it might be possible to keep the same interface and replace just the implementation, but I'd like to avoid reinventing the (timing) wheel, hence the suggestion to decouple |
Using
Unix.select
is problematic, because if any file descriptor value is beyond 1024 then the call will fail with an exception.Note that this refers to the value of the file descriptor, and not the number of file descriptors that select is watching: you could fail even if you only ever watch a single file descriptor, if you open that file descriptor late enough that it gets a number >1024.
This is not a concern for short-lived programs, but can be a real problem for long-lived daemons.
It'd be better to use
poll(3p)
, which is part of POSIX.1-2017 and available nearly everywhere.This is available in the iomux library.
Unfortunately
Unix.select
in the stdlib cannot be efficiently reimplemented usingpoll
, because it has a different interface, but we shouldn't encourage more uses ofselect
, other than for compatibility with legacy applications (I am currently working on removing all uses ofUnix.select
from such a legacy application...)I know a project that has replaced
select
withpoll
in an application by writing a drop-in replacement that kept the same signature, and it was a very bad idea for performance: it allocates a lot, and it performsO(watched-fds)
work every time.More efficient OS specific implementations are also possible:
epoll
,kqueue
,wepoll
(iomux
claims it'll eventually support these, but doesn't yet), available in:https://ocaml.org/p/poll/latest
io_submit
withIOCB_CMD_POLL
https://ocaml.org/p/aio/latest/doc/Aio/index.html#val-pollThis one also has the potential to check for the presence of events without making any system calls. https://spdk.io/ has such a pure userspace
io_getevents
implementation. (of course to wait for an event when there are none available you have to make the actual system call)io_uring
, as implemented by the uring packageAs you can see there are plenty of choices, and the interfaces are quite distinct, and most of these are modelled more after a
poll
-like interface than aselect
-like interface. It is probably out of scope for this project to unify all these mechanisms, but it'd be good if the default is something better thanselect
.A neutral approach might be to use
poll
by default, and provide the ability for the user to override the implementation (via a functor, at main thread configure time, or via a settable global, etc.).A
picos_select.core
functor with an implementation inpicos_select.iomux
might provide the most flexibility:iomux
in your application, you can choose not to, by linking withpicos_select.core
and providing your own implementation for the poll signature.picos_select
could pickiomux
by default(There might be other ways, I haven't looked at how Dune's virtual libraries could be used for this).
If you want I can attempt to open a draft PR with a proof of concept, but thought to open an issue describing the problem first, and maybe you already have a design on how you'd like this handled.
The text was updated successfully, but these errors were encountered: