Re: Asynchronous socket operations and threadpool
- From: "William Stacey [MVP]" <william.stacey@xxxxxxxxx>
- Date: Fri, 3 Mar 2006 07:46:55 -0500
|You don't actually save any work on the host cpus
| by doing things async, you just gain a bit of parallelism on a particular
| user's reqeust.
Agreed. If work is N, then using async will be N+x, where x is the overhead
of thread switches. Naturally, elapsed time can be lower for any given
client by using parallelism.
| The scenario I keep seeing is:
|
| 0 - You get the Socket.BeginRead callback, and get your data. You're now
on
| an IOCP thread.
| 1 - Perform an async operation (say, lookup MX or SRV records in the DNS).
| Pass in a delegate for the callback.
| 2 - While that operation is under way, your IOCP thread keeps going doing
| whatever it can do.
| 3 - Eventually the IOCP thread hits a WaitHandle and has to sync up with
the
| async operation you kicked off.
I thought we are talking about a pure async server? This is more like a
thread per connection server because your blocking on a Wait. In a full
async server, you would not block at all (or for very short times). In
Socket.EndRead, you would get data, update state, and Begin your next async
operation, and on down the line like walking a "virtual" task list
controlled by state. Not pretty to code (I totally agree), but if you need
huge number of connections (i.e. more then ~1500) then maybe is the only
way. So you should not have threads building up because they are blocking.
So most the time, your system will be waiting on BeginReceives and
EndReceives to fire. AFAIK, IOCP thread is not used to wait on the hardware
interrupt. Once the hw fills the read (if ever), that is when the IOCP
thread is invoked to handle the callback.
| At the end of the day, I've found it easier to just do everything
| synchronously once you hit your IOCP callback. This makes for simpler
code,
| easier debugging, far fewer context switches, and (hopefully you won't
need
| to) far, far easier crashdump analysis.
I might flip that around. Use a thread per connection for the client
request, then use async for things that could be done in parallel (i.e. dns,
db, etc). Things like Concurrency Runtime (CCR) from MS will help here for
coordination.
| Just to complicate things, here are some of the other architectures that
| I've tried:
| 1 - As soon as I get a valid data chunk off a socket, I stick it into a
| queue for later processing, then put the socket back into BeginRead mode.
| Use a pool of worker threads [usually a custom threadpool, as too many
other
| things steal thrads from the .Net Threadpool] to pull data off the Queue
and
| process things. This approach seemed like the best canidate for a while,
but
| thread context switching absolutly destroys the performance. There are all
| sorts of issues that also arise, such as how many worker threads to use,
how
| to manage thread affinity on multi-proc systems, how to restart threads
that
| get hung, etc. It turns out that on large, high-availability production
| system this is all very difficult.
Why would you need to worry about thread affinity? Anytime you make an
async call, your effectivity going to pay a context switch tax as well
(unless the call is completed sync). So a threadpool blocking on queue
items would not seem to be more overhead compared to async.
This link shows a variation on this type of server (SEDA):
http://www.eecs.harvard.edu/~mdw/papers/seda-sosp01.pdf Pretty interesting
read. I am doing this kind of server now, and seems to be working well.
Good discussion. Cheers.
--
William Stacey [MVP]
.
- Follow-Ups:
- Re: Asynchronous socket operations and threadpool
- From: Chris Mullins
- Re: Asynchronous socket operations and threadpool
- References:
- Asynchronous socket operations and threadpool
- From: Yifan Li
- Re: Asynchronous socket operations and threadpool
- From: Chris Mullins
- Re: Asynchronous socket operations and threadpool
- From: William Stacey [MVP]
- Re: Asynchronous socket operations and threadpool
- From: Chris Mullins
- Asynchronous socket operations and threadpool
- Prev by Date: Re: Asynchronous socket operations and threadpool
- Next by Date: Re: bug found in Microsoft.Win32.RegistryValueKind.DWord conversion
- Previous by thread: Re: Asynchronous socket operations and threadpool
- Next by thread: Re: Asynchronous socket operations and threadpool
- Index(es):
Relevant Pages
|