Skip to content
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

Epoll event loop (linux) #14814

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
265b4d3
Fix: simplify IO::FileDescriptor#finalize
ysbaddaden Jul 15, 2024
da4044e
Fix: don't cancel timeout select action event twice
ysbaddaden Jul 16, 2024
e89e680
Add :evloop to Crystal.trace
ysbaddaden Jul 15, 2024
9172c4c
Epoll: initial attempt (doesn't compile)
ysbaddaden Jul 12, 2024
df41ba7
Fix: epoll_event is only packed on x86_64
ysbaddaden Jul 16, 2024
fc50413
Fix: disable EPOLLEXCLUSIVE for now
ysbaddaden Jul 18, 2024
9a5053e
Fix: close in MT environment
ysbaddaden Jul 18, 2024
5540b56
Fix: add optional Crystal::EventLoop#after_fork_before_exec (MT)
ysbaddaden Jul 18, 2024
bba4a62
Fix: after_fork (no MT) or after_fork_before_exec (MT only)
ysbaddaden Jul 18, 2024
02f0f06
fixup! Fix: add optional Crystal::EventLoop#after_fork_before_exec (MT)
ysbaddaden Jul 18, 2024
d3458bd
Prefer eventfd over pipe (only one fd, smaller struct in kernel)
ysbaddaden Jul 18, 2024
9969cf5
Save pointer to Node instead of fd (skips searches after wait)
ysbaddaden Jul 18, 2024
4911214
fixup! Save pointer to Node instead of fd (skips searches after wait)
ysbaddaden Jul 18, 2024
66046f3
fixup! Prefer eventfd over pipe (only one fd, smaller struct in kernel)
ysbaddaden Jul 19, 2024
38f7224
Add Crystal::System::EventFD abstraction
ysbaddaden Jul 19, 2024
75d2093
Use generic :system event type instead of :interrupt
ysbaddaden Jul 19, 2024
a36a214
fixup! Add Crystal::System::EventFD abstraction
ysbaddaden Jul 19, 2024
a5a68f2
Extract timers + cleanup + one timerfd per eventloop
ysbaddaden Jul 19, 2024
4d4c068
Fix: also check that timers are empty (not only events)
ysbaddaden Jul 20, 2024
0d36f67
Fix: missing mutex sync
ysbaddaden Jul 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/channel.cr
Original file line number Diff line number Diff line change
Expand Up @@ -743,9 +743,11 @@ class Channel(T)
end

def time_expired(fiber : Fiber) : Nil
if @select_context.try &.try_trigger
fiber.enqueue
end
fiber.enqueue if time_expired?
end

def time_expired? : Bool
@select_context.try &.try_trigger || false
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions src/crystal/pointer_linked_list.cr
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,8 @@ struct Crystal::PointerLinkedList(T)
each { |node| yield node }
@head = Pointer(T).null
end

def clear : Nil
@head = Pointer(T).null
end
end
4 changes: 4 additions & 0 deletions src/crystal/system/event_loop.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ abstract class Crystal::EventLoop
def self.create : self
{% if flag?(:wasi) %}
Crystal::Wasi::EventLoop.new
{% elsif flag?(:linux) || flag?(:solaris) %}
Crystal::Epoll::EventLoop.new
{% elsif flag?(:unix) %}
Crystal::LibEvent::EventLoop.new
{% elsif flag?(:win32) %}
Expand Down Expand Up @@ -72,6 +74,8 @@ end

{% if flag?(:wasi) %}
require "./wasi/event_loop"
{% elsif flag?(:linux) || flag?(:solaris) %}
require "./unix/epoll/event_loop"
{% elsif flag?(:unix) %}
require "./unix/event_loop_libevent"
{% elsif flag?(:win32) %}
Expand Down
7 changes: 0 additions & 7 deletions src/crystal/system/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,6 @@ module Crystal::System::File
private def self.error_is_file_exists?(errno)
errno.in?(Errno::EEXIST, WinError::ERROR_FILE_EXISTS)
end

# Closes the internal file descriptor without notifying libevent.
# This is directly used after the fork of a process to close the
# parent's Crystal::System::Signal.@@pipe reference before re initializing
# the event loop. In the case of a fork that will exec there is even
# no need to initialize the event loop at all.
# def file_descriptor_close
end

{% if flag?(:wasi) %}
Expand Down
7 changes: 7 additions & 0 deletions src/crystal/system/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ module Crystal::System::FileDescriptor
# cooked mode otherwise.
# private def system_raw(enable : Bool, & : ->)

# Closes the internal file descriptor without notifying libevent.
# This is directly used after the fork of a process to close the
# parent's Crystal::System::Signal.@@pipe reference before re initializing
# the event loop. In the case of a fork that will exec there is even
# no need to initialize the event loop at all.
# def file_descriptor_close(raise_on_error = true) : Nil

private def system_read(slice : Bytes) : Int32
event_loop.read(self, slice)
end
Expand Down
39 changes: 39 additions & 0 deletions src/crystal/system/unix/epoll.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{% skip_file unless flag?(:linux) || flag?(:solaris) %}

require "c/sys/epoll"

struct Crystal::System::Epoll
def initialize
@epfd = LibC.epoll_create1(LibC::EPOLL_CLOEXEC)
raise RuntimeError.from_errno("epoll_create1") if @epfd == -1
end

def add(fd : Int32, epoll_event : LibC::EpollEvent*) : Nil
if LibC.epoll_ctl(@epfd, LibC::EPOLL_CTL_ADD, fd, epoll_event) == -1
raise RuntimeError.from_errno("epoll_ctl(EPOLL_CTL_ADD)")
end
end

def modify(fd : Int32, epoll_event : LibC::EpollEvent*) : Nil
if LibC.epoll_ctl(@epfd, LibC::EPOLL_CTL_MOD, fd, epoll_event) == -1
raise RuntimeError.from_errno("epoll_ctl(EPOLL_CTL_MOD)")
end
end

def delete(fd : Int32) : Nil
if LibC.epoll_ctl(@epfd, LibC::EPOLL_CTL_DEL, fd, nil) == -1
raise RuntimeError.from_errno("epoll_ctl(EPOLL_CTL_DEL)")
end
end

# `timeout` is in milliseconds; -1 will wait indefinitely; 0 will never wait.
def wait(events : Slice(LibC::EpollEvent), timeout : Int32) : Slice(LibC::EpollEvent)
count = LibC.epoll_wait(@epfd, events.to_unsafe, events.size, timeout)
raise RuntimeError.from_errno("epoll_wait") if count == -1 && Errno.value != Errno::EINTR
events[0, count.clamp(0..)]
end

def close : Nil
LibC.close(@epfd)
end
end
40 changes: 40 additions & 0 deletions src/crystal/system/unix/epoll/event.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{% skip_file unless flag?(:linux) || flag?(:solaris) %}

require "../epoll"
require "../timerfd"

struct Crystal::Epoll::Event
enum Type
IoRead
IoWrite
Sleep
SelectTimeout
System
end

getter fiber : Fiber
getter type : Type
getter fd : Int32

property! time : Time::Span?
getter? timed_out : Bool = false

include PointerLinkedList::Node

# Allocates a system event into the HEAP. A system event doesn't have an
# associated fiber.
def self.system(fd : Int32) : self*
event = Pointer(self).malloc(1)
fiber = uninitialized Fiber
event.value.initialize(fd, fiber, :system)
event
end

def initialize(@fd : Int32, @fiber : Fiber, @type : Type, timeout : Time::Span? = nil)
@time = ::Time.monotonic + timeout if timeout
end

def timed_out! : Bool
@timed_out = true
end
end
Loading
Loading