22 Commits
Author SHA1 Message Date
Cory Benfield c6fc49a84a Reduce selector wakeups. (#1820)
Motivation:

Right now all tasks scheduled onto an event loop from a different loop
wake up the underlying selector by writing to an eventfd. This is a
fairly inefficient way to handle things: it incurs a lot of syscall
traffic unnecessarily.

Given that we currently protect the pending tasks queue with a big dumb
lock, we can safely keep track of whether we're going to dequeue this
task. If we are, we don't need to wake the selector.

Modifications:

- Keep track of whether we're de-queueing tasks or not.
- Arrange to wake the selector only once.

Result:

Cheaper task enqueueing on hot loops.
2021-04-28 15:09:07 +01:00
Johannes Weiss e2990d9672 refactor registration IDs (#1801)
Motivation:

Instead of manual shifting / masking we can write the whole registration
ID code normally in Swift.

Modifications:

Refactored RegistrationID to use Swift instead of shifting / masking.

Result:

Nicer code.
2021-04-23 12:43:30 +01:00
Joakim Hassila f0b825f9b7 Add sequenceIdentifier to Registrations to handle outdated events robustly. (#1799)
Motivation:

Currently the selector implementation just discards all pending events from the kernel when a deregistration
has occurred during the event loop tick. This has been needed to not fail the test testWeDoNotDeliverEventsForPreviouslyClosedChannels.

A cleanup case has been around for a while at: https://github.com/apple/swift-nio/issues/381

And when discussing this in https://github.com/apple/swift-nio/pull/1788 (to solve https://github.com/apple/swift-nio/issues/1761)

we came to an agreement that the solution with tagging registrations on the backend would not suit only io_uring,
but also solve the issue robustly for kqueue and epoll.

Modification:

Added sequenceIdentifier to the Registration and add support for it throughout.
Mask in sequence identifier into user data in both kqueue and epoll.
In kqueue, we just store the sequence identifier in the udata field (to support 32-bit properly).
In epoll, where we know we have 64-bits, we mask it in together with fd.
Removed all references to the old workaround from #381.

Result:

- New solution that doesn't need to discard all events when deregistrations are done during the event loop tick.
- fixes #381
2021-04-13 10:35:41 +01:00
Hailong ae3ea78d92 Correct typo in SelectableEventLoop class doc. (#1742) 2021-02-07 08:11:41 +00:00
Johannes Weiss 2b821b2ef6 Specialise succeeded Void futures so we allocate less (#1703)
Motivation:

Succeeded `EventLoopFuture<Void>`s are quite important in SwiftNIO, they
happen all over the place. Unfortunately, we usually allocate each time,
unnecessarily.

Modifications:

Offer `EventLoop`s the option to cache succeeded void futures.

Result:

Fewer allocations.
2021-01-15 17:07:15 +00:00
Cory Benfield 831cf89d54 Reduce refcounting in Heap by removing configurability (#1614)
Motivation:

Right now Heap.heapify incurs quite a lot of ARC traffic when it is used
in SelectableEventLoop. Specifically, each of the invocations of
self.comparator in heapify incurs three refcount operations. These three
are all the result of the fact that the comparator is an arbitrary
function, but all three are guaranteed to be unnecessary.

The first refcount is for the closure context for self.comparator. Of
course, self.comparator has only two possible values, neither of which
needs a closure context, but the compiler doesn't know that and the type
system doesn't encode it.

The other two refcounts are for the ScheduledTask objects themselves.
Again, the compiler doesn't know what the comparator function will do,
so it must defensively ensure that a retain/release is inserted for the
arguments of that function.

If we happen to hit both branches of the code, we'll execute 6 retains
and 6 releases, and even in fairly average cases we'll execute 3 of
each.

All of this is unnecessary: the only runtime function ever used here is
a trivial comparison operation. We know what it is at compile time, we
just have to ask. While it was admirable to have the configurability of
the priority queue in place, after 3 years I think we have a pretty good
feeling that we're not going to need the ability to have a max-heap
instead of a min-heap.

We can always get this back if we need it.

Modifications:

- Removed the configurability of Heap.
- Removed HeapType.
- Removed arguments from PriorityQueue
- Removed all tests testing now-removed max-heap functionality.

Result:

Less ARC traffic in the core task scheduling loop.
2020-08-10 13:12:43 +01:00
Peter Adams 86ce6aae8b Explicit Congestion Notification for UDP (#1596)
Motivation:

Network congestion can be controlled without packet loss if explicit congestion notification messages are understood.

Modifications:

Receive single and vector paths to get notification and surface.
Send paths for both single and vector sends.
Round trip tests.
Storage for control messages added in a reusable way.

I have tested allocations and they seem to be fine but the allocation tests for vector read and write were not stable enough to sensibly add to CI.

Result:

Explicit Congestion Notifications can be both sent and received.
2020-07-17 16:25:09 +01:00
5fc487bf7e don't blow up if schedule happens during EL shutdown (#1530)
Motivation:

Previously, if we scheduled EventLoop executions from the promises we
failed during EventLoop shutdown, we'd blow up. Thanks to @fabianfett
for the bug report.

Modifications:

Set the EL state to 'exiting thread' only after we failed all
outstanding tasks.

Result:

Fewer crashes.

Co-authored-by: George Barnett <gbarnett@apple.com>
Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-06-08 08:10:12 +01:00
Johannes WeissandCory Benfield 5821c53ee8 allow completely single-threaded NIO programs (#1499)
Motivation:

Usually, you spawn a new MultiThreadedEventLoopGroup to run NIO
programs. But if you're a very simple command line utility, that may not
be necessary, so why not just re-use the main thread?

Modifications:

Allow taking over threads and making them EventLoops.

Result:

Fewer threads if you want.

Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-05-11 15:33:02 +01:00
Johannes Weiss a27a07719c testSafeToExecute*: fix threading issues (#1455)
Motivation:

testSafeToExecuteTrue/False accessed an internal property of the event
loop without synchronisation.

Modifications:

Fix the threading issue.

Result:

Tests with TSan happy.
2020-03-18 22:36:08 +00:00
Johannes Weiss bd4df32a26 downgrade 'schedule after shutdown' to printing error (#1454)
Motivation:

Since #1395, we crash when something is scheduled to an already shutdown
EventLoop. That's a good intent (#1383) but we feel it's a little too
risky right now.

Modifications:

Print an ERROR instead of just crashing.

Result:

Fewer crashes.
2020-03-18 18:20:35 +00:00
Johannes WeissandCory Benfield 2ad77f9a9b WIP: universal bootstrap (#1323)
Co-authored-by: Cory Benfield <lukasa@apple.com>
2020-03-18 16:20:19 +00:00
Saleem Abdulrasool e5298263ec SelectableEventLoop: make Darwin the special case (#1449)
Treat the `autoreleasepool` case for Darwin as the special case rather
than Linux as the special case.  This is following the case throughout
the rest of the code base.  This is also needed for the Windows port.
2020-03-16 09:14:21 +00:00
Doug FriedmanandJohannes Weiss 51238f4df8 Prevent execution of tasks on an already shutdown EL (#1395)
* prevent execution of tasks on an already shutdown EL

* fixup! prevent execution of tasks on an already shutdown EL

* fixup! prevent execution of tasks on an already shutdown EL

Co-authored-by: Johannes Weiss <johannesweiss@apple.com>
2020-03-04 17:20:13 +00:00
Shekhar Rajak 58a80c300e Use UInt instead of ObjectIdentifier in promiseCreationStore (#1411)
* Use UInt instead of ObjectIdentifier in promiseCreationStore

* defined obfuscatePointerValue method
2020-02-24 17:01:16 +00:00
Johannes Weiss 52a4cae14d add SAL (Syscall Abstraction Layer) and new SALChannelTests
## Motivation:

In SwiftNIO's codebase, most things were always testable just as well as user code is. With one big missing piece: We could never make the `Selector` do arbitrary things to the `Channel` implementations which lead to super expensive to (create, read, understand) integration tests like [`testWriteAndFlushFromReentrantFlushNowTriggeredOutOfWritabilityWhereOuterSaysAllWrittenAndInnerDoesNot `](https://github.com/apple/swift-nio/blob/5dc9083c6a78b10432b4b55b95704e61ca08f09d/Tests/NIOTests/StreamChannelsTest.swift#L468-L760).

## Modification

Add SAL, a 'syscall abstraction layer' which makes it possible to assert all syscalls that come through a `Socket` or the `Selector` by mocking them. It lets the NIO programmer play the kernel in these "SAL tests" which makes it possible to assert the exact syscalls (with their parameters) and decide the syscalls' returns.

## Result

Finally, we're able to write proper unit tests for scenarios where the `Channel` drove the `Selector` incorrectly.
2020-01-28 12:36:36 +00:00
Johannes Weiss 7bf170f63a make SelectableEventLoop.description not take a lock (#1318)
Motivation:

SelectableEventLoop's description was taking a lock and would also lock
every single scheduled task. That's overkill.

Modifications:

Make it only log the thread and selector (which don't require locks).

Result:

better description
2019-12-19 15:19:14 +00:00
Johannes Weiss 6174c9802c add descriptions to EventLoop and NIOThread (#1317)
Motivation:

Certain issues are easier debugged if we can print the thread names.

Modifications:

- add the thread name to SelectableEventLoop's description
- make EventLoop and NIOThread CustomStringConvertible

Result:

easier debugging.
2019-12-19 14:52:03 +00:00
Johannes Weiss 9fb70afddf close kqueue/epoll fds in Selector.close (#1308)
Motivation:

There was always this one big embarrassment in the NIO codebase: We
violated the iron rule of 'no fd or scarce resource management in deinit'
in just one place: The most long-lived thing there is in NIO: The
Selector for the SelectableEventLoopGroup. Sure enough, even that tiny
violation lead to issues.

Modifications:

Close the fds in `close` rather than `deinit`.

Result:

- I'm feeling better
- makes @ktoso happy
- fixes #1302
2019-12-17 18:30:17 +00:00
Johannes Weiss c1f0544980 fix EVFILT_EXCEPT loop if autoRead is off on Darwin (#1305)
Motivation:

Due to rdar://53656794, we sometimes (especially on UNIX Domain Sockets)
see EVFILT_EXCEPT firing when data is available to read depite the fact
that we're setting the low watermark to CInt.max. This means that
EVFILT_EXCEPT becomes essentially unusable.

Modifications:

- Don't use EVFILT_EXCEPT on Darwin
- We lose early EOF delivery on Darwin

Result:

Fewer possibilities of a spin loop on Darwin.
2019-12-16 14:42:50 +00:00
Johannes Weiss 6c36c4a6d5 SelectableEventLoop: Call wakeup less frequently (#1301)
Motivation:

Previously, we unconditionally woke up the selector for every task that
was added. But this is only necessary if we're not already on the
EventLoop. Because if we are, we'll have another look at the outstanding
tasks anyway.

Modifications:

Remove selector wakeups.

Result:

Faster executes.
2019-12-12 17:35:23 +00:00
Johannes Weiss 5a567709b2 remove phantom publics from EventLoop and Heap (#1298)
Motivation:

In SelectableEventloop, Heap, PriorityQueue, etc there were quite a few
phantom publics left which cause issues with #1257 and are confusing.

Modifications:

- remove the phantom publics
- move SelectableEventLoop to its own file

Result:

- clearer what's going on (public means public)
2019-12-11 18:41:51 +00:00