views:

2516

answers:

6

I'm wondering which approach is faster and why ?

While writing a Win32 server I have read a lot about the Completion Ports and the Overlapped I/O, but I have not read anything to suggest which set of API's yields the best results in the server.

Should I use completion routines, or should I use the WaitForMultipleObjects API and why ?

A: 

Not sure. But I use WaitForMultipleObjects and/or WaitFoSingleObjects. It's very convenient.

Jason
A: 

Either routine works and I don't really think one is any significant faster then another.

These two approaches exists to satisfy different programming models. WaitForMultipleObjects is there to facilitate async completion pattern (like UNIX select() function) while completion ports is more towards event driven model.

I personally think WaitForMultipleObjects() approach result in cleaner code and more thread safe.

oykuo
+4  A: 

WaitForMultipleObjects is limited to 64 handles; in a highly concurrent application this could become a limitation.

Completion ports fit better with a model of having a pool of threads all of which are capable of handling any event, and you can queue your own (non-IO based) events into the port, whereas with waits you would need to code your own mechanism.

However completion ports, and the event based programming model, are a more difficult concept to really work against.

I would not expect any significant performance difference, but in the end you can only make your own measurements to reflect your usage. Note that Vista/Server2008 made a change with completion ports that the originating thread is not now needed to complete IO operations, this may make a bigger difference (see this article by Mark Russinovich).

Richard
Do you have a reference to the change in Vista/Server2008 so I can read about it?
Reference is linked to from my blog here: http://www.lenholgate.com/archives/000763.html
Len Holgate
WaitForMultipleObjects() can handle more than 64 objects. An extension now exists. Not sure when it came it - might be XP, might be Vista.
Blank Xavier
@Blank: That would require a new API because of the use of constants in the API. MAXIMUM_WAIT_OBJECTS is 64, and is the max for both WaitForMultipleObjects and ...Ex (and is derived from WAIT_ABANDONED_0-WAIT_OBJECT_0).
Richard
+6  A: 

You suggest two methods of doing overlapped I/O and ignore the third (or I'm misunderstanding your question).

When you issue an overlapped operation, a WSARecv() for example, you can specify an OVERLAPPED structure which contains an event and you can wait for that event to be signalled to indicate the overlapped I/O has completed. This, I assume, is your WaitForMultipleObjects() approach and, as previously mentioned, this doesn't scale well as you're limited to the number of handles that you can pass to WaitForMultipleObjects().

Alternatively you can pass a completion routine which is called when completion occurs. This is known as 'alertable I/O' and requires that the thread that issued the WSARecv() call is in an 'alertable' state for the completion routine to be called. Threads can put themselves in an alertable state in several ways (calling SleepEx() or the various EX versions of the Wait functions, etc). The Richter book that I have open in front of me says "I have worked with alertable I/O quite a bit, and I'll be the first to tell you that alertable I/O is horrible and should be avoided". Enough said IMHO.

There's a third way, before issuing the call you should associate the handle that you want to do overlapped I/O on with a completion port. You then create a pool of threads which service this completion port by calling GetQueuedCompletionStatus() and looping. You issue your WSARecv() with an OVERLAPPED structure WITHOUT an event in it and when the I/O completes the completion pops out of GetQueuedCompletionStatus() on one of your I/O pool threads and can be handled there.

As previously mentioned, Vista/Server 2008 have cleaned up how IOCPs work a little and removed the problem whereby you had to make sure that the thread that issued the overlapped request continued to run until the request completed. Link to a reference to that can be found here. But this problem is easy to work around anyway; you simply marshal the WSARecv over to one of your I/O pool threads using the same IOCP that you use for completions...

Anyway, IMHO using IOCPs is the best way to do overlapped I/O. Yes, getting your head around the overlapped/async nature of the calls can take a little time at the start but it's well worth it as the system scales very well and offers a simple "fire and forget" method of dealing with overlapped operations.

If you need some sample code to get you going then I have several articles on writing IO completion port systems and a heap of free code that provides a real-world framework for high performance servers; see here.

As an aside; IMHO, you really should read "Windows Via C/C++ (PRO-Developer)" by Jeffrey Richter and Christophe Nasarre as it deals will all you need to know about overlapped I/O and most other advanced windows platform techniques and APIs.

Len Holgate
Very helpful answer. Solved my problem described at http://stackoverflow.com/questions/4015220/overlapped-socket-receive-callback-not-being-called. Thanks!
Kristopher Johnson
+1  A: 

The difference between WaitForMultipleObjects() and I/O completion ports is that IOCP scales to thousands of objects, whereas WFMO() does not and should not be used for anything more than 64 objects (even though you could).

You can't really compare them for performance, because in the domain of < 64 objects, they will be essentially identical.

WFMO() however does a round-robin on its objects, so busy objects with low index numbers can starve objects with high index numbers. (E.g. if object 0 is going off constantly, it will starve objects 1, 2, 3, etc). This is obviously undesireable.

I wrote an IOCP library (for sockets) to solve the C10K problem and put it in the public domain. I was able on a 512mb W2K machine to get 4,000 sockets concurrently transferring data. (You can get a lot more sockets, if they're idle - a busy socket consumes more non-paged pool and that's the ultimate limit on how many sockets you can have).

http://www.45mercystreet.com/computing/libiocp/index.html

The API should give you exactly what you need.

Blank Xavier
+1  A: 

Table 6-3 in the book Network Programming for Microsoft Windows, 2nd Edition compares the scalability of overlapped I/O via completion ports vs. other techniques. Completion ports blow all the other I/O models out of the water when it comes to throughput, while using far fewer threads.

Aaron Klotz