public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Re: So, Poll is not scalable... what to do?
@ 2003-11-13 18:25 Dan Kegel
  0 siblings, 0 replies; 15+ messages in thread
From: Dan Kegel @ 2003-11-13 18:25 UTC (permalink / raw)
  To: linux-kernel, justformoonie

Kirk Bae wrote:
> If poll is not scalable, which method should I use when writing 
> multithreaded socket server?

People who write multithreaded servers tend to use thread pools
and never need to use poll().  (Well, ok, I've written multithreaded
servers that used poll(), but the threads were there for disk I/O,
not networking.)

> What is the most efficient model to use?
> 
> Is there a "standard" model to use when writing a scalable multithreaded 
> socket serve such as "io completion ports" on windows?

Depends on your definition of efficient.

If by 'efficient' you mean 'runs like a bat out of hell,
and I don't care how long it takes to write', and
you're willing to write all the code from scratch, and
you're handy with state machines, the right way to go is
an edge-triggered readiness notification method (sys_epoll or kqueue,
if available, else sigio).

A handy wrapper library that lets
you use these without worrying about exactly which method
your operating system supports is at http://kegel.com/rn/
It handles two flavors of epoll as well as sigio at the moment,
and as a proof of concept, I've modified betaftpd to use it;
the resulting ftp server scales very nicely (except for the
calls to the system password checking routine, which is a bottleneck).

On the other hand, if by 'efficient' you mean 'doesn't take
long for somebody used to doing things on Windows to write',
or if you have to use any third-party libraries that use blocking I/O,
or if you're not very handy with state machines,
go ahead and use a thread pool.

> From the "Microbenchmark comparing poll, kqueue, and /dev/poll", kqueue is 
> the way to go. Am I correct?

kqueue is for BSD.  sys_epoll is the equivalent on (recent enough) linux.

> What is the best solution to use on Linux?

Depends on your needs; see above.

> Also, why is it that poll doesn not return with "close signal" when a 
> thread-1 calls poll and thread-2 calls close on a sockfd1? It seems that 
> poll only handles close signal when a client disconnects from the server. 
> I've seen this mentioned here before, has it been fixed?

The Single Unix Standard (http://www.opengroup.org/onlinepubs/007904975/functions/close.html)
doesn't say what should happen if you close a file descriptor
being used by another thread.
Linus long ago decided that Linux would handle this
without waking the other thread.
I know, other operating systems (and Java!) behave differently,
but Linux is perfectly within its rights to behave as it does here,
and it's not likely to change.

- Dan

p.s. And, yes, http://kegel.com/c10k.html might be a handy reference for you :-)




^ permalink raw reply	[flat|nested] 15+ messages in thread
* Re: So, Poll is not scalable... what to do?
@ 2003-11-14 18:57 Frederic Rossi
  0 siblings, 0 replies; 15+ messages in thread
From: Frederic Rossi @ 2003-11-14 18:57 UTC (permalink / raw)
  To: Dan Kegel; +Cc: kirk bae, linux-kernel



Hi,

Dan Kegel wrote:
> Kirk Bae wrote:
> >>>If poll is not scalable, which method should I use when writing 
> >>>multithreaded socket server?
> >>
> >>People who write multithreaded servers tend to use thread pools
> >>and never need to use poll().  (Well, ok, I've written multithreaded
> >>servers that used poll(), but the threads were there for disk I/O,
> >>not networking.)
> > 
> > By thread pool, do you mean, one thread per socket?, or a work-crew
> model 
> > where a specified number of threads handle many sockets?
> 
> The latter.  But I don't have much recent experience writing threaded
> servers, as I usually use somthing like epoll.
> 
> > My definition of "efficient" is a method that is most widely used or 
> > accepted for writing a robust scalable server. So I guess, "'runs like
> a bat 
> > out of hell, and I don't care how long it takes to write'" is close.
> > 
> > Would it be thread pool or epoll? 
> 
> My personal bias is towards epoll (suitably wrapped for portability;
> no need to require epoll when sigio is nearly as good, and universally
> deployed).
> 
>  > Is it uncommon to mix these two?
> 
> Folks who know how to program with things like epoll and aio tend
> to use threads carefully, and try to avoid using blocking I/O for
> networking.
> Blocking I/O is unavoidable for things like opening files, though,
> so it's perfectly fine to have a thread that handles stuff like that.
> 
> > Currently, I have a thread-1 calling poll, and dispatching its
> workload to 
> > thread-2 and thread-3 in blocking sockets. I dispatch the workload to
> worker 
> > threads because some of the operations take considerable time.
> > 
> > Is mixing threads with poll uncommon? Is this a bad design? any
> comments 
> > would be appreciated.
> 
> What are the operations that take considerable time?  Are they
> networking
> calls, or disk I/O, or ...?   If they're just networking calls,
> you might consider turning your code inside out to do everything with
> nonblocking I/O and state machines, but only if you're hitting a
> bottleneck
> because of the number of threads active.
> 
> Whatever design you're comfortable with is fine until you fail to hit
> your
> performance requirement.  No point in optimizing one little part
> of the system if the whole system is fast enough, or if the real
> bottleneck is elsewhere...
> - Dan

I think you're right when talking about the design decision and 
regarding the initial question (since it is not referenced on the 
C10K page) I'd like to suggest 
	  http://aem.sourceforge.net
which provides another possible approach for that kind of problem.

The question of I/O completion port was also raised and I think 
it must be taken into account in the initial design as well.
In many situations people use threads to handle this asynchronously
AEM is targeting event handling but provides a native asynchronous 
socket interface that brings data directly to the applications.
This is one way of doing it.

On the other hand, it might not be the perfect solution for 
the problem above. See it as an alternative.

Frederic

^ permalink raw reply	[flat|nested] 15+ messages in thread
* Re: So, Poll is not scalable... what to do?
@ 2003-11-14  0:27 Dan Kegel
  0 siblings, 0 replies; 15+ messages in thread
From: Dan Kegel @ 2003-11-14  0:27 UTC (permalink / raw)
  To: linux-kernel; +Cc: justformoonie

Kirk Bae wrote:
>>>If poll is not scalable, which method should I use when writing 
>>>multithreaded socket server?
>>
>>People who write multithreaded servers tend to use thread pools
>>and never need to use poll().  (Well, ok, I've written multithreaded
>>servers that used poll(), but the threads were there for disk I/O,
>>not networking.)
> 
> By thread pool, do you mean, one thread per socket?, or a work-crew model 
> where a specified number of threads handle many sockets?

The latter.  But I don't have much recent experience writing threaded
servers, as I usually use somthing like epoll.

> My definition of "efficient" is a method that is most widely used or 
> accepted for writing a robust scalable server. So I guess, "'runs like a bat 
> out of hell, and I don't care how long it takes to write'" is close.
> 
> Would it be thread pool or epoll? 

My personal bias is towards epoll (suitably wrapped for portability;
no need to require epoll when sigio is nearly as good, and universally deployed).

 > Is it uncommon to mix these two?

Folks who know how to program with things like epoll and aio tend
to use threads carefully, and try to avoid using blocking I/O for networking.
Blocking I/O is unavoidable for things like opening files, though,
so it's perfectly fine to have a thread that handles stuff like that.

> Currently, I have a thread-1 calling poll, and dispatching its workload to 
> thread-2 and thread-3 in blocking sockets. I dispatch the workload to worker 
> threads because some of the operations take considerable time.
> 
> Is mixing threads with poll uncommon? Is this a bad design? any comments 
> would be appreciated.

What are the operations that take considerable time?  Are they networking
calls, or disk I/O, or ...?   If they're just networking calls,
you might consider turning your code inside out to do everything with
nonblocking I/O and state machines, but only if you're hitting a bottleneck
because of the number of threads active.

Whatever design you're comfortable with is fine until you fail to hit your
performance requirement.  No point in optimizing one little part
of the system if the whole system is fast enough, or if the real
bottleneck is elsewhere...
- Dan



^ permalink raw reply	[flat|nested] 15+ messages in thread
* Re: So, Poll is not scalable... what to do?
@ 2003-11-13 23:10 kirk bae
  2003-11-14  0:52 ` Mark Mielke
  0 siblings, 1 reply; 15+ messages in thread
From: kirk bae @ 2003-11-13 23:10 UTC (permalink / raw)
  To: linux-kernel

>Kirk Bae wrote:
>>If poll is not scalable, which method should I use when writing 
>>multithreaded socket server?
>
>People who write multithreaded servers tend to use thread pools
>and never need to use poll().  (Well, ok, I've written multithreaded
>servers that used poll(), but the threads were there for disk I/O,
>not networking.)

By thread pool, do you mean, one thread per socket?, or a work-crew model 
where a specified number of threads handle many sockets?


>>What is the most efficient model to use?
>>
>>Is there a "standard" model to use when writing a scalable multithreaded 
>>socket serve such as "io completion ports" on windows?
>
>Depends on your definition of efficient.
>
>If by 'efficient' you mean 'runs like a bat out of hell,
>and I don't care how long it takes to write', and
>you're willing to write all the code from scratch, and
>you're handy with state machines, the right way to go is
>an edge-triggered readiness notification method (sys_epoll or kqueue,
>if available, else sigio).

My definition of "efficient" is a method that is most widely used or 
accepted for writing a robust scalable server. So I guess, "'runs like a bat 
out of hell, and I don't care how long it takes to write'" is close.

Would it be thread pool or epoll? Is it uncommon to mix these two?

Currently, I have a thread-1 calling poll, and dispatching its workload to 
thread-2 and thread-3 in blocking sockets. I dispatch the workload to worker 
threads because some of the operations take considerable time.

Is mixing threads with poll uncommon? Is this a bad design? any comments 
would be appreciated.

_________________________________________________________________
Great deals on high-speed Internet access as low as $26.95.  
https://broadband.msn.com (Prices may vary by service area.)


^ permalink raw reply	[flat|nested] 15+ messages in thread
* So, Poll is not scalable... what to do?
@ 2003-11-11 23:52 kirk bae
  2003-11-12  3:52 ` Jeff Garzik
                   ` (3 more replies)
  0 siblings, 4 replies; 15+ messages in thread
From: kirk bae @ 2003-11-11 23:52 UTC (permalink / raw)
  To: linux-kernel

If poll is not scalable, which method should I use when writing 
multithreaded socket server?

What is the most efficient model to use?

Is there a "standard" model to use when writing a scalable multithreaded 
socket serve such as "io completion ports" on windows?

>From the "Microbenchmark comparing poll, kqueue, and /dev/poll", kqueue is 
the way to go. Am I correct?

What is the best solution to use on Linux?

Also, why is it that poll doesn not return with "close signal" when a 
thread-1 calls poll and thread-2 calls close on a sockfd1? It seems that 
poll only handles close signal when a client disconnects from the server. 
I've seen this mentioned here before, has it been fixed?

Thank you~~~

_________________________________________________________________
>From Beethoven to the Rolling Stones, your favorite music is always playing 
on MSN Radio Plus. No ads, no talk. Trial month FREE!  
http://join.msn.com/?page=offers/premiumradio


^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2003-11-14 18:59 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-11-13 18:25 So, Poll is not scalable... what to do? Dan Kegel
  -- strict thread matches above, loose matches on Subject: below --
2003-11-14 18:57 Frederic Rossi
2003-11-14  0:27 Dan Kegel
2003-11-13 23:10 kirk bae
2003-11-14  0:52 ` Mark Mielke
2003-11-11 23:52 kirk bae
2003-11-12  3:52 ` Jeff Garzik
2003-11-12  5:32 ` Willy Tarreau
2003-11-12 23:26   ` bill davidsen
2003-11-13  0:32     ` Davide Libenzi
2003-11-13  0:54       ` Nick Piggin
2003-11-13 12:02         ` Bill Davidsen
2003-11-12 23:23 ` bill davidsen
2003-11-13  1:06   ` Bernd Eckenfels
2003-11-13  7:52 ` David Schwartz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox