News for hackers. Stuff that matters.
Re:Fibers: An Elegant Windows API by uasm on Friday March 29, 2019 @06:59AM (#19520540)
Hmm, let's read this one.

> "March 28, 2019"

> "Fibers: the Most Elegant Windows API"

> "The Windows API — a.k.a. Win32 — is notorious for being clunky, ugly, and lacking good taste. Microsoft has done a pretty commendable job with backwards compatibility, but the trade-off is that the API is filled to the brim with historical cruft. Every hasty, poor design over the decades is carried forward forever, and, in many cases, even built upon, which essentially doubles down on past mistakes. POSIX certainly has its own ugly corners, but those are the exceptions. In the Windows API, elegance is the exception."

I'm glad OP just discovered in mid-2019 an API that has been there since Windows XP (ca. 2001). Why then, does OP start by crapping all over the Windows codebase?

You think the Fibers API is elegant? Have a look at IOCP. IO in Windows in general actually, since the days of Windows 95. Event management. Threading and parallelism. The kernel subsystem. The APIs in Windows have remained stable, consistent, forward and backwards compatible - from pretty much day one.

Re:Fibers: An Elegant Windows API by teh_klev on Friday March 29, 2019 @07:08AM (#19520588)
> I'm glad OP just discovered in mid-2019 an API that has been there since Windows XP

Can I bring to your attention the second para:

"That’s why, when I recently revisited the Fibers API, I was pleasantly surprised."

I don't think it's the author's first rodeo. The article reads like a retrospective look at the fibers API and how well it's aged.

Re:Fibers: An Elegant Windows API by userbinator on Friday March 29, 2019 @07:48AM (#19520765)
It's been there since NT 3.51, according to https://www.geoffchappell.com/studies/windows/win32/kernel32... and my copy of Win32.hlp (from back when MS documentation was actually proofread before release...)
Re:Fibers: An Elegant Windows API by PaulHoule on Friday March 29, 2019 @08:18AM (#19520953)
IOCP is particularly strong when you compare it to select, poll, epoll, kpoll, and all of the other attempts to make asyncio work in Linux.
Re:Fibers: An Elegant Windows API by kentonv on Friday March 29, 2019 @02:30PM (#19524265)
Eh. I used to think that, until I actually wrote event systems based on all of the above. epoll turns out to be my favorite.

Buffer allocation with IOCP feels incredibly ugly. You must allocate a buffer before you initiate a read(), and then you must leave that buffer alone until the read() completes. You would think that this has the advantage that the kernel doesn't need to allocate its own buffer, and you get some sort of zero-copy magic where bytes land directly in userspace. But that's not really the case. The socket still needs a buffer on the kernel side in case bytes arrive while no read() is pending. So now you have to allocate a buffer for each socket and the kernel also has to allocate a buffer for each socket, which seems like a waste.

This rabbit hole gets deeper. What happens if the socket receives two packets in rapid succession? In a naive implementation, the first packet completes the read(), and signals the completion on the IOCP. The app now has to process that event and start a new read() when it's ready. But in the meantime, the second packet arrives. Whoops, no read() is pending, so now the packet has to go into a kernel-side buffer only to be copied later.

But that's a naive implementation. Apparently, in reality, the kernel implements some sort of nagle-like algorithm where it tries to wait a bit for additional packets before it actually signals completion of a read(). But this introduces delay, and many projects (e.g. Chrome) have discovered this delay is rather harmful to certain kinds of performance. I read somewhere that Chrome and others have given up on IOCP and use WSAPoll instead -- but I can't remember where I read this because all the lore about Windows event handling is hidden in random forum threads and Github gists rather than proper documentation.

It seems to me that the right way to do what Windows was trying to do here would be for userspace to allocate a ring buffer for the kernel to use, and then the app and the kernel would coordinate the start and end pointers of the ring buffer. Then the kernel needs no buffer of its own; it can always deliver to userspace. If the buffer fills up, the kernel can do exactly what it would do in the case of a regular kernel-side buffer filling up -- apply backpressure and force the peer to retransmit later.

Re:Fibers: An Elegant Windows API by kentonv on Friday March 29, 2019 @06:45PM (#19526287)
I should add: Even what I wrote above is not the whole story. The deeper you get into kernel internals, the more you realize that the story is nuanced.

On Linux, apparently, the kernel does not actually allocate static socket buffers. Instead, the "socket buffer" is actually something like an array of pointers to packets. When the read buffer is empty, it's not taking any space.

So on Linux, an idling socket is very cheap (by my understanding, at least).

The Windows kernel might do something similar internally (I don't know), but there's no way to avoid the redundant buffer on the userspace side when using IOCP.

So basically, it seems that IOCP turns out to be a rather-poor interface for networking in practice, despite at first appearing theoretically superior.

Re:Fibers: An Elegant Windows API by gpderetta on Monday April 01, 2019 @05:22AM (#19541691)
I've never used IOCP, but my understanding is that they can sort of emulate readiness notification (as opposed to completion notification) by passing a 0 sized buffer. On the callback then you would perform the (non-blocking) read yourself.

In general I think that readiness notification is superior to completion notification for network reads. Completion notification works fine for network writes and it is superior for disk IO (where readiness is ill defined).

The biggest advantage of IOCP though is that it basically acts as a dynamically sized threadpool, as it keeps the amount of running threads to a minimum. MacOS does something similar with GCD. I still haven't found a non-hacky way to do the same in Linux (you can sort-of emulate it with either with sched_fifo realtime threads or by using sched_idle to detect idelness).

Re:Fibers: An Elegant Windows API by machinecoffee on Friday March 29, 2019 @08:32AM (#19521026)
IOCP in Windows is a good API (although a bit non-obvious), but to be fair, they did have a few tries and missteps before they got there.
Re:Fibers: An Elegant Windows API by zzzcpan on Friday March 29, 2019 @12:15PM (#19522932)
It's not. IOCP is the worst of all of them.
Re:Fibers: An Elegant Windows API by spapas82 on Friday March 29, 2019 @12:23PM (#19523028)
I think this is a must read for new developers: https://www.joelonsoftware.com/2004/06/13/how-microsoft-lost... (yes it's from 2004)
Re:Fibers: An Elegant Windows API by jstimpfle on Friday March 29, 2019 @11:06AM (#19522320)
I had a hard time with IOCP and eventuall ditched it because I didn't find a way to avoid creating a separate read buffer for each and every handle.

Suppose I want to watch 10K connections, or files, or such. 4K should be a reasonable read buffer size. Do I really need to spend 40M of memory just to read from the handles simultaneously?

Re:Fibers: An Elegant Windows API by magicalhippo on Friday March 29, 2019 @11:49AM (#19522685)
What would you have the OS do?

If you got connections with inbound data, the OS has to store it somewhere. Might as well be in your read buffer right away.

If you ask the OS to read 4k from 10k files simultaneously, why are you surprised that the OS wants the buffer to stay around until the read is completed?

Re:Fibers: An Elegant Windows API by jstimpfle on Friday March 29, 2019 @12:07PM (#19522850)
It should let me choose which buffer I want to use next, from a pool of buffers that I provide.

There is no point in having a 1:1 relationship between buffers and read handles.

Re:Fibers: An Elegant Windows API by magicalhippo on Friday March 29, 2019 @12:17PM (#19522952)
That doesn't sound very performant having to switch back into userspace just to get another buffer, then back into kernel space to continue completing the read.

Though as far as I can see you could get close to what you wish by doing 1 byte reads.

Re:Fibers: An Elegant Windows API by jstimpfle on Friday March 29, 2019 @12:23PM (#19523035)
Your snarkiness is not appreciated and I never said you should do constant switching. It's not required.

> If you ask the OS to read 4k from 10k files simultaneously

There is a difference between between copying inbound data from 10k handles simultaneously (which does require as many buffers) on the one hand, and on the other hand being open to read from any M-sized subset of 10k handles at once, which requires only M buffers.

I also never said to have only 4k of (combined) buffer data for 10k handles. "4k should be a reasonable read buffer size" means 4k per buffer. I don't think I've been unclear, but with a tiny bit of good will you could have figured out what I meant anyway.

Re:Fibers: An Elegant Windows API by magicalhippo on Friday March 29, 2019 @12:52PM (#19523344)
FWIW I'm not trying to be snarky.

> I never said you should do constant switching. It's not required.

I didn't see any other way to interpret your "It should let me choose which buffer I want to use next".

Though perhaps what you meant was that you want to be able to tell the OS "yo, here's a bunch of buffers, use these to complete my reads"?

> [...] and on the other hand being open to read from any M-sized subset of 10k handles at once, which requires only M buffers.

I interpret that as saying "yo OS, I want to read 4k each from these 10k files, but only use these M 4k buffers when doing so", where M << 10k.

If a request is slow to be fulfilled won't it hog a buffer, reducing the maximum throughput of the fast requests? Why would this be a performance win?

Re:Fibers: An Elegant Windows API by jstimpfle on Friday March 29, 2019 @01:05PM (#19523488)
> Though perhaps what you meant was that you want to be able to tell the OS "yo, here's a bunch of buffers, use these to complete my reads"?

Thank you for clarifying, and yes, that would be the best way to do it I think.

> If a request is slow to be fulfilled won't it hog a buffer, reducing the maximum throughput of the fast requests? Why would this be a performance win?

So someone else here said that with IOCP the kernel doesn't have its own read buffers but writes directly to userspace memory (in cases where this is possible, anyway). So there might be a possiblity for buffer hogging, I don't know. On the other hand, when the request really is slow to be fulfilled, the OS could just call it a partial write and notify the program of a (partial) completion. In which case the buffer is free for other uses again.

Re:Fibers: An Elegant Windows API by magicalhippo on Friday March 29, 2019 @01:24PM (#19523692)
Thanks for the follow-up. Would be interesting to see if it made much difference in the end, guess we'll have to wait a bit longer for all of Windows to be open sourced :P
Re:Fibers: An Elegant Windows API by magicalhippo on Friday March 29, 2019 @12:54PM (#19523365)
> I also never said to have only 4k of (combined) buffer data for 10k handles. "4k should be a reasonable read buffer size" means 4k per buffer.

I never thought the former, only the latter.

Re:Fibers: An Elegant Windows API by jstimpfle on Friday March 29, 2019 @01:07PM (#19523513)
Oh I see, it was the other commenter saying "less than 1 byte per buffer" and you only said "by doing 1 byte reads". It seems I really was a little unclear and should try to hold my horses.
Re:Fibers: An Elegant Windows API by magicalhippo on Friday March 29, 2019 @01:23PM (#19523685)
No harm done :)

My idea was to exploit the caching mechanism in the OS. Though clearly it wouldn't be as efficient as providing a set of buffers for the OS to use.

Re:Fibers: An Elegant Windows API by kbenson on Friday March 29, 2019 @11:25AM (#19522466)
I'm confused (but I'm also not familiar with the API). It sounds like you're saying you want to be able to read from 10k connections simultaneously while allocating less than a byte of buffer space to each, so I assume I'm misunderstanding some aspect of what you're trying to say (or some aspect of the API that puts this into the correct context).
Re:Fibers: An Elegant Windows API by asveikau on Friday March 29, 2019 @06:58PM (#19526349)
In the user mode piece, a single thread can get told "these 10K descriptors are ready for you to call read(2) upon them", then that thread can read them all using the same buffer. [In practice you might allocate more depending on what's going on, but this is a crude example.] In the IOCP model you need to allocate space for all read requests upfront, even if they're not writing you any data currently.

Kernel mode would have its own buffers, so memory cost per client is not small in this example... except in terms of user-mode buffers.

Re:Fibers: An Elegant Windows API by jstimpfle on Friday March 29, 2019 @12:09PM (#19522866)
No I'm saying I want to read from N handles simultaneously using M buffers, where M <= N. Of course, the OS shouldn't write to one of my read buffers that currently contains other buffered data. But that doesn't mean that it has to be N = M, since that is a waste.

I guess M = 10 * number of OS threads should usually be plenty to achieve good performance. But I'm happy to be corrected by people more experienced with systems performance.

Re:Fibers: An Elegant Windows API by kbenson on Friday March 29, 2019 @08:04PM (#19526646)
I believe I understand. By not allocating in the api call itself you can pre-allocate your own buffers and keep track of which are in-use and not in-use yourself and use some subset of the total amount that would be allocated in each api call be reusing buffers after a particular connection is done with it. That makes sense.
Re:Fibers: An Elegant Windows API by zzzcpan on Friday March 29, 2019 @12:13PM (#19522917)
On unixy systems kernel manages buffers by itself, using as little memory as possible, not allocating anything when no data is received, and userspace doesn't need to allocate any memory either until it is notified and decides to non-blockingly read the data from the kernel. Meaning that waiting for data from connections costs nothing, while on windows it requires preallocating buffer per connection. Basically the same problem that synchronous APIs have.

The myth that IOCP is elegant has to die.

Re:Fibers: An Elegant Windows API by destructionator on Friday March 29, 2019 @12:53PM (#19523355)
> Do I really need to spend 40M of memory just to read from the handles simultaneously?

of virtual memory... if it isn't being used it can be paged out.

Re:Fibers: An Elegant Windows API by jstimpfle on Friday March 29, 2019 @01:00PM (#19523434)
Paging? I don't do paging because I value my mental sanity. I don't think there is a good reason to use paging in 2019 for the vast majority of applications, and the rest is probably simply badly designed software. And I only have 8G of RAM like everybody else, 4G of which have probably never been written to.

And if you do use paging, it's still 40M wasted disk space without a good reason.

Re:Fibers: An Elegant Windows API by gpderetta on Monday April 01, 2019 @06:04AM (#19541840)
but at some point you will need it (when the socket becomes ready to read) and it be extremely costly to to bring it back in.