From owner-freebsd-hackers Mon Dec 20 16:46:52 1999 Delivered-To: freebsd-hackers@freebsd.org Received: from canonware.com (canonware.com [207.20.242.18]) by hub.freebsd.org (Postfix) with SMTP id 2743C14C24 for ; Mon, 20 Dec 1999 16:46:50 -0800 (PST) (envelope-from jasone@canonware.com) Received: (qmail 39779 invoked by uid 1001); 21 Dec 1999 00:45:17 -0000 Date: Mon, 20 Dec 1999 16:45:17 -0800 From: Jason Evans To: Chris Sedore Cc: Alfred Perlstein , Kevin Day , "Ronald F. Guilmette" , hackers@FreeBSD.ORG Subject: Re: Practical limit for number of TCP connections? Message-ID: <19991220164517.F26743@sturm.canonware.com> References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 1.0i In-Reply-To: ; from cmsedore@maxwell.syr.edu on Sun, Dec 19, 1999 at 03:01:41PM -0500 Sender: owner-freebsd-hackers@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG On Sun, Dec 19, 1999 at 03:01:41PM -0500, Chris Sedore wrote: > > > On Sat, 18 Dec 1999, Alfred Perlstein wrote: > > > On Sat, 18 Dec 1999, Kevin Day wrote: > > > > > > The _clean_ way of doing it would be to write your multi-user server using > > > > threads, and to assign one thread to each connection. If you can do that, > > > > then the logic in the program becomes quite simple. Each thread just sits > > > > there, blocked on a call to read(), until something comes in, and then it > > > > just parses the command, does whatever it is supposed to do in response to > > > > that command, and then goes back to the read() again. > > > > > > > > But as I understand it, there is not yet sufficient threads support in the > > > > FreeBSD kernel to make this work well/properly. (I may perhaps be misinformed > > > > about that, but that's what I have been told anyway.) > > > > > > I believe this is how ConferenceRoom works, so it seems ok, but I remember > > > the comments that FreeBSD was their least preferred platform because of > > > thread problems. > > > > Using a thread per connection has always been a bogus way of programming, > > it's easy, but it doesn't work very well. > > Ahem. Well, that kind of depends on the threads implementation, how many > connections you're talking about, and likely some other factors too. > I've got an NT box that handles about 1000 concurrent connections with > 1000 (plus a few) threads doing the work. Runs fine, performs very well. > > I wouldn't argue that it is the most scalable solution to problems, but it > is easier, and scales proportionally to the quality of the threads > implementation. 1000 simultaneous connections really isn't that many, but even at 1000 threads, you could likely achieve much better performance by using thread pools or a simple poll() loop rather than one thread per connection. Why? Locality, locality, locality. Consider that each thread has its own stack, which in the best of worlds would be 4K, but is more likely at least 16K. Now, start switching between threads to handle relatively small amounts of I/O for each connection, and consider what that does to the VM, not to mention the memory hierarchy of the hardware. You might as well not even have L2 cache, because the program will thrash the cache so badly. Of course, you won't see worst case performance if client activity is unevenly distributed, but you just can't get past the fact that the memory footprint of one thread per connection is larger than a bounded pool of threads. Some threads implementations are better than others at handling such abuses, but the performance of such an approach will almost always suffer in comparison to a design that takes locality into consideration. I disagree with your assessment that scalability of one thread per connection is proportional to the quality of the threads implementation. An ideal threaded program would have exactly as many threads as available processors, and the threads would always be runnable. Of course, real-world applications almost never work that way, but the goal of a programmer should be to have as few threads as possible while still achieving maximal parallelism. If connection scalability is an issue, using one thread per connection ignores a critical aspect of high performance threaded application design. Jason To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-hackers" in the body of the message