Chinaunix首页 | 论坛 | 博客
  • 博客访问: 846080
  • 博文数量: 756
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 4980
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:40
文章分类

全部博文(756)

文章存档

2011年(1)

2008年(755)

我的朋友

分类:

2008-10-13 16:11:10

Lincoln Dale wrote:
> in order to be able to handle large numbers of concurrent connections (open
> sockets), there are really only two ways of doing it efficiently:
>
> (1) single process using threads
> (2) single process, event-driven queues
>
> with (1), you have a slightly higher overhead than a well-written (2),
> however you can scale to multiprocessor with no additional effort.
>
> (2) will always be best, but is typically one of the hardest to code for.
>

Whether (2) is best or not depends upon the kernel interface. If a
scan of 15,000 file descriptors is required for each event (such as
a straightforward design based on select()) then (2) could be nearly
as bad as a naive accept()/fork() model, while a thread-per-connection
model might scale better. If the kernel somehow passed an event list
to the server process more efficiently than via select() or poll(),
then I would agree that (2) is probably always the better way, perhaps
with extensions into a thread-per-processor regime on SMP's.

Achieving the best efficiency involves coupling the arrival of a packet
to the object (explicit or implicit) handling that particular connection
in the most direct way possible, modulo buffering. Eliminating linear
searches from that coupling is important--a scan of 15,000 descriptors
is going to be a significant hit, no matter what other efficiencies are
involved, since it is O(n^2) (where n is the number of connections). This
is why a threaded model might work better for large numbers of connections,
assuming that thread scheduling is better than O(n^2). (Is it?)

I've actually implemented high-efficiency servers based on a combination
of (1) and (2); in essence, a small pool of threads each handling a number
of connections using an event-based model. At least for my project (which
involved high connection rates more than large numbers of sustained
connections), one or two threads per processor seemed to be the most
efficient configuration--but this was under Solaris, not Linux, though
I certainly was able to achieve good performance (>1000 connects/second
on a UP P2/333) on the latter in tests.

Just another data point.

-Ed

-

 


--------------------next---------------------

阅读(347) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~