Return-Path: X-Original-To: apmail-httpd-users-archive@www.apache.org Delivered-To: apmail-httpd-users-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 71E90DEA6 for ; Tue, 17 Jul 2012 04:51:29 +0000 (UTC) Received: (qmail 47641 invoked by uid 500); 17 Jul 2012 04:51:26 -0000 Delivered-To: apmail-httpd-users-archive@httpd.apache.org Received: (qmail 47435 invoked by uid 500); 17 Jul 2012 04:51:25 -0000 Mailing-List: contact users-help@httpd.apache.org; run by ezmlm Precedence: bulk Reply-To: users@httpd.apache.org list-help: list-unsubscribe: List-Post: List-Id: Delivered-To: mailing list users@httpd.apache.org Received: (qmail 47412 invoked by uid 99); 17 Jul 2012 04:51:24 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 17 Jul 2012 04:51:24 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=5.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: local policy) Received: from [132.68.225.13] (HELO mailgw13.technion.ac.il) (132.68.225.13) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 17 Jul 2012 04:51:17 +0000 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Av0EAB/uBFCERHMG/2dsb2JhbABFuVqBB4IgAQEEATpECQILGC4UBxFEiAcGs1eJBASLOoVnYAOIRYx1AYESjnSCYg X-IronPort-AV: E=Sophos;i="4.77,599,1336338000"; d="scan'208";a="17787847" Received: from fermat.math.technion.ac.il ([132.68.115.6]) by mailgw13.technion.ac.il with ESMTP; 17 Jul 2012 07:50:56 +0300 Received: by fermat.math.technion.ac.il (Postfix, from userid 4298) id 56AD68794F; Tue, 17 Jul 2012 07:50:53 +0300 (IDT) Date: Tue, 17 Jul 2012 07:50:53 +0300 From: Nadav Har'El To: users@httpd.apache.org Message-ID: <20120717045053.GA8215@fermat.math.technion.ac.il> References: <20120716140722.GA488@fermat.math.technion.ac.il> <20120716161141.7efa2ff1@baldur> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20120716161141.7efa2ff1@baldur> Hebrew-Date: 27 Tammuz 5772 User-Agent: Mutt/1.5.20 (2009-12-10) Subject: Re: [users@httpd] mod-cgi reads entire output into memory... On Mon, Jul 16, 2012, Nick Kew wrote about "Re: [users@httpd] mod-cgi reads entire output into memory...": > On Mon, 16 Jul 2012 17:07:23 +0300 > Nadav Har'El wrote: > > I looked at the httpd code, discovered (if I understand correctly) that > > 1. As I already guessed, Apache doesn't let the CGI write directly to the > > socket, but rather asks it to write to a pipe, which Apache then reads. > > Yep. That's what CGI is all about. :-) I've set out to write a simple mod_cgi replacement which lets the child process write its output directly to the client socket - it behaves like NPH (no possibility for Apache to fix the CGI's headers or to filter its output) but I think it will be useful in a lot of cases (who filters CGI output anyway?) - and certainly more efficient in mine. In fact I wonder why it shouldn't always work like that with NPH. I "almost" have such code, but ran into a mystery - where in the request_req can I find the client socket, so I can write to it directly? There are so many layers of output filters, APR, etc., that I can't seem to find this simple thing... > > 2. When Apache reads this data from the pipe, it doesn't write it directly > > but rather just adds it to a "bucket brigade" which collects more and > > more data. > > No, it doesn't collect more and more data, unless some filter needs to > buffer the entire output. Normally it passes data down the chain. > Each filter's job is to process a chunk of data then pass it to the next. In my tests definitely all the data was being collected, and I was not using any output filter (at least not that I know of) - not using deflate or anything of that sort. I'm no longer sure about my original statement that the buffering happens when the client reads the output slowly. In fact, it now looks to me that extreme memory use actually happens when the client reads very very quickly (i.e., the client is through localhost). I haven't got a clue why this is happening - I don't suppose Apache has any time-based bucket-brigade flow control or memory pool reuse algorithms? > > I confirmed that this is indeed a flow-control problem by changing the > > CGI to sleep for 1 second after outputting each 64 MB (i.e., 8 batches > > of 64 MB output); Now, the memory usage was around 64 MB, not 512 MB, > > because Apache had the time to output each batch and free its memory > > before the next batch came. > > Sounds like the entire contents of the pipe got read into memory in > a single read! Not good, but not as bad as you think. Is this actually possible? Doesn't Apache allocate a relatively small buffer and read into that? How can it read 512 MB in a single read? > Sleeping is a drastic workaround. What happens if you just flush your > CGI output every 8Mb (or, preferably, in smaller chunks than that)? The CGI is a trivial one written in C, using stdio and puts()'ing 8192 strings of 65536 bytes each. I don't think that stdio buffers 512 MB or anything close to that. Also like I said, the CGI program itself does NOT grow in memory use - just Apache. > You might want to look at the mod_proxy framework as an alternative harness > to run your program. Interesting idea. I'll take a look at that. Thanks, Nadav. -- Nadav Har'El | Tuesday, Jul 17 2012, 27 Tammuz 5772 nyh@math.technion.ac.il |----------------------------------------- Phone +972-523-790466, ICQ 13349191 |Despite the cost of living, have you http://nadav.harel.org.il |noticed how it remains so popular? --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org For additional commands, e-mail: users-help@httpd.apache.org