You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by Brian Pane <bp...@pacbell.net> on 2001/11/11 06:09:54 UTC
mod_include vs file descriptors
This bit of mod_include's includes_filter() is likely to be a problem
for servers with a lot of connections from slow clients:
if ((parent = ap_get_module_config(r->request_config,
&include_module))) {
/* Kludge --- for nested includes, we want to keep the subprocess
* environment of the base document (for compatibility); that means
* torquing our own last_modified date as well so that the
* LAST_MODIFIED variable gets reset to the proper value if the
* nested document resets <!--#config timefmt-->.
* We also insist that the memory for this subrequest not be
* destroyed, that's dealt with in handle_include().
*/
r->subprocess_env = r->main->subprocess_env;
apr_pool_join(r->main->pool, r->pool);
r->finfo.mtime = r->main->finfo.mtime;
}
The problem is that, by joing the pool with that of the parent request,
the file descriptor for this .shtml file doesn't get closed until the parent
pool goes out of scope. With a .shtml file that includes 9 other .shtml
files, we'll have a total of 10 file descriptors open (11 including the
socket to the client) until we finish sending the content to the client.
That could seriously limit the number of clients that each child process
can handle in the threaded or worker MPM.
The easy solutions I can think of are:
* Close the descriptor for each file immediately after mmap'ing it, or
* Don't join the pools in includes_filter().
Can anyone comment on the viability of these fixes, or any other suitable
solution?
Thanks,
--Brian
Re: mod_include vs file descriptors
Posted by Greg Stein <gs...@lyra.org>.
On Tue, Nov 13, 2001 at 11:19:54AM -0500, Cliff Woolley wrote:
> On Tue, 13 Nov 2001, Brian Pane wrote:
> > How about a "close-on-mmap" flag for file buckets, which
> > mod_core would set when creating file buckets but mod_file_cache
> > wouldn't?
Don't make it so specific. Just insert the file (or mmap or any other
object!) into the bucket with a *refcount*. When you transform the file into
a bucket, you decrement the refcount. If it hits zero, then you close the
file.
The cache inserts with refcount==1. Everybody else does count==0.
> I've proposed that before. But if it wasn't vetoed it was -0.5'ed, with
> the reason being that a lazy closing of file descriptors and other
> resources is one of the main points of the pool cleanup system; closing
> all these things can take a good deal of time, and it's better to do it
> after the client has received all of the data than during mainline request
> processing.
That is bogus, as has been pointed out. You have to close them sometime.
Doing it sooner rather than later means that you have more file descriptors
available for further processing in the request.
Cheers,
-g
--
Greg Stein, http://www.lyra.org/
Re: mod_include vs file descriptors
Posted by Brian Pane <bp...@pacbell.net>.
Cliff Woolley wrote:
>On Tue, 13 Nov 2001, Brian Pane wrote:
>
>>How about a "close-on-mmap" flag for file buckets, which
>>mod_core would set when creating file buckets but mod_file_cache
>>wouldn't?
>>
>
>I've proposed that before. But if it wasn't vetoed it was -0.5'ed, with
>the reason being that a lazy closing of file descriptors and other
>resources is one of the main points of the pool cleanup system; closing
>all these things can take a good deal of time, and it's better to do it
>after the client has received all of the data than during mainline request
>processing.
>
I sympathize with the argument that it's better for performance to
delay the close until after the data is sent, but I think that argument
is more applicable in a benchmark lab environment than in the real
world. In the real world, I'm happy to spend an extra 50 microseconds
at the start of the HTTP response to close the fd, because closing the
fd allows the server to handle many more connections per process
(especially in the mod_include case).
--Brian
Re: mod_include vs file descriptors
Posted by Cliff Woolley <cl...@yahoo.com>.
On Tue, 13 Nov 2001, Ryan Bloom wrote:
> We are talking about destroying the sub request. By the time this is
> done, we have already passed all of the data from the sub-requests
> filter stack to the original requests. Either all of the data was
> written to the network, or it was set-aside. If it was written to the
> network, we can ignore it. If it was set aside, then it was done so
> in the original request, so we passed r->pool or c->pool to the
> setaside function.
[thinks for a minute] Yeah, that does sound right. Bottom line is that
core_output_filter is (or should be) taking care of all this for us.
Okay, +1.
--Cliff
--------------------------------------------------------------
Cliff Woolley
cliffwoolley@yahoo.com
Charlottesville, VA
Re: mod_include vs file descriptors
Posted by Ryan Bloom <rb...@covalent.net>.
On Tuesday 13 November 2001 09:39 am, Cliff Woolley wrote:
> On Tue, 13 Nov 2001, Ryan Bloom wrote:
> > I should clarify that just destroying the subrequest should be
> > perfectly safe. Have we tried that yet. In the past, we would have
> > had a problem with data surviving the death of the request, but all
> > buckets should have cleanups now, so that shouldn't be an issue.
>
> Hang on... you do have to explicitly setaside all the buckets you want to
> keep before destroying the subrequest and tell it which pool you want them
> setaside into. Buckets don't have cleanups (well, pool buckets do, but
> none of the others do), they have setaside functions.
We will have done that already. This may get complex, but follow my logic,
and point out anything I missed.
We are talking about destroying the sub request. By the time this is done,
we have already passed all of the data from the sub-requests filter stack to
the original requests. Either all of the data was written to the network, or
it was set-aside. If it was written to the network, we can ignore it. If it was
set aside, then it was done so in the original request, so we passed r->pool
or c->pool to the setaside function.
I wasn't clear in my original message, but the result should be the same.
Ryan
______________________________________________________________
Ryan Bloom rbb@apache.org
Covalent Technologies rbb@covalent.net
--------------------------------------------------------------
Re: mod_include vs file descriptors
Posted by Cliff Woolley <jw...@virginia.edu>.
On Tue, 13 Nov 2001, Ryan Bloom wrote:
>
> I should clarify that just destroying the subrequest should be
> perfectly safe. Have we tried that yet. In the past, we would have
> had a problem with data surviving the death of the request, but all
> buckets should have cleanups now, so that shouldn't be an issue.
Hang on... you do have to explicitly setaside all the buckets you want to
keep before destroying the subrequest and tell it which pool you want them
setaside into. Buckets don't have cleanups (well, pool buckets do, but
none of the others do), they have setaside functions.
--Cliff
--------------------------------------------------------------
Cliff Woolley
cliffwoolley@yahoo.com
Charlottesville, VA
Re: mod_include vs file descriptors
Posted by Ryan Bloom <rb...@covalent.net>.
On Tuesday 13 November 2001 08:19 am, Cliff Woolley wrote:
> On Tue, 13 Nov 2001, Brian Pane wrote:
> > How about a "close-on-mmap" flag for file buckets, which
> > mod_core would set when creating file buckets but mod_file_cache
> > wouldn't?
>
> I've proposed that before. But if it wasn't vetoed it was -0.5'ed, with
> the reason being that a lazy closing of file descriptors and other
> resources is one of the main points of the pool cleanup system; closing
> all these things can take a good deal of time, and it's better to do it
> after the client has received all of the data than during mainline request
> processing.
I should clarify that just destroying the subrequest should be perfectly safe.
Have we tried that yet. In the past, we would have had a problem with data
surviving the death of the request, but all buckets should have cleanups now,
so that shouldn't be an issue.
Ryan
______________________________________________________________
Ryan Bloom rbb@apache.org
Covalent Technologies rbb@covalent.net
--------------------------------------------------------------
Re: mod_include vs file descriptors
Posted by Cliff Woolley <cl...@yahoo.com>.
On Tue, 13 Nov 2001, Brian Pane wrote:
> How about a "close-on-mmap" flag for file buckets, which
> mod_core would set when creating file buckets but mod_file_cache
> wouldn't?
I've proposed that before. But if it wasn't vetoed it was -0.5'ed, with
the reason being that a lazy closing of file descriptors and other
resources is one of the main points of the pool cleanup system; closing
all these things can take a good deal of time, and it's better to do it
after the client has received all of the data than during mainline request
processing.
--Cliff
--------------------------------------------------------------
Cliff Woolley
cliffwoolley@yahoo.com
Charlottesville, VA
Re: mod_include vs file descriptors
Posted by Brian Pane <bp...@pacbell.net>.
Cliff Woolley wrote:
>On Tue, 13 Nov 2001, Brian Pane wrote:
>
>>As a simpler solution, is there any reason why we can't close the
>>file descriptor when turning a file bucket into an mmap bucket?
>>
>> static void file_destroy(void *data)
>> {
>>+ apr_bucket_file *a = (apr_bucket_file *)data;
>>+ apr_file_close(a->fd);
>> if (apr_bucket_shared_destroy(data)) {
>>- /* no need to close the file here; it will get
>>- * done automatically when the pool gets cleaned up */
>> free(data);
>> }
>> }
>>
>
>Well, for one thing, the apr_file_close() would go inside the if() exactly
>where the commenet is now, not outside the if. But you can't do this
>anyway, because the buckets code doesn't necessarily "own" the file
>descriptor. This would break mod_file_cache, for example, if a bucket
>with a cached file descriptor were ever apr_bucket_read()'ed.
>
How about a "close-on-mmap" flag for file buckets, which
mod_core would set when creating file buckets but mod_file_cache
wouldn't?
--Brian
Re: mod_include vs file descriptors
Posted by Cliff Woolley <cl...@yahoo.com>.
On Tue, 13 Nov 2001, Brian Pane wrote:
> As a simpler solution, is there any reason why we can't close the
> file descriptor when turning a file bucket into an mmap bucket?
>
> static void file_destroy(void *data)
> {
> + apr_bucket_file *a = (apr_bucket_file *)data;
> + apr_file_close(a->fd);
> if (apr_bucket_shared_destroy(data)) {
> - /* no need to close the file here; it will get
> - * done automatically when the pool gets cleaned up */
> free(data);
> }
> }
Well, for one thing, the apr_file_close() would go inside the if() exactly
where the commenet is now, not outside the if. But you can't do this
anyway, because the buckets code doesn't necessarily "own" the file
descriptor. This would break mod_file_cache, for example, if a bucket
with a cached file descriptor were ever apr_bucket_read()'ed.
--Cliff
--------------------------------------------------------------
Cliff Woolley
cliffwoolley@yahoo.com
Charlottesville, VA
Re: mod_include vs file descriptors
Posted by Brian Pane <bp...@pacbell.net>.
Ryan Bloom wrote:
[...]
>>What else would break if we destroyed the subreq here,
>>even in the case of a nested include?
>>
>
>Nothing _should_ break, but there are few potential problems. The biggest one
>that I see is that you can't be sure that the data that has been allocated has
>actually been sent to the client yet. This means that it would be possible to
>free the sub-request, and have MMAP'ed data that gets destroyed before
>the client sees it. If that is the case, then it is a bug that should be fixed,
>this leakage is not the solution however.
>
As a simpler solution, is there any reason why we can't close the
file descriptor when turning a file bucket into an mmap bucket?
E.g.,
Index: srclib/apr-util/buckets/apr_buckets_file.c
===================================================================
RCS file: /home/cvspublic/apr-util/buckets/apr_buckets_file.c,v
retrieving revision 1.57
diff -u -r1.57 apr_buckets_file.c
--- srclib/apr-util/buckets/apr_buckets_file.c 2001/11/12 03:22:25
1.57
+++ srclib/apr-util/buckets/apr_buckets_file.c 2001/11/13 08:41:10
@@ -85,9 +85,9 @@
static void file_destroy(void *data)
{
+ apr_bucket_file *a = (apr_bucket_file *)data;
+ apr_file_close(a->fd);
if (apr_bucket_shared_destroy(data)) {
- /* no need to close the file here; it will get
- * done automatically when the pool gets cleaned up */
free(data);
}
}
Re: mod_include vs file descriptors
Posted by Ryan Bloom <rb...@covalent.net>.
On Saturday 10 November 2001 10:03 pm, Brian Pane wrote:
> Ryan Bloom wrote:
> >apr_pool_join is a no-op unless APR_POOL_DEBUG is enabled. If it isn't
> >enabled, which it shouldn't be on production servers, there will be no
> >performance penalty for that function.
>
> Sorry, I blamed the wrong part of the code, but the core
> problem still remains: we don't clean up the subrequest
> (and thus don't close file descriptors for included .shtml
> files) until top-level .shtml request goes out of scope.
> The logic in handle_include() appears to do this on purpose:
>
> /* destroy the sub request if it's not a nested include
> * (crumb) */
> if (rr != NULL
> && ap_get_module_config(rr->request_config,
> &include_module) != NESTED_INCLUDE_MAGIC) {
> ap_destroy_sub_req(rr);
> }
>
> What else would break if we destroyed the subreq here,
> even in the case of a nested include?
Nothing _should_ break, but there are few potential problems. The biggest one
that I see is that you can't be sure that the data that has been allocated has
actually been sent to the client yet. This means that it would be possible to
free the sub-request, and have MMAP'ed data that gets destroyed before
the client sees it. If that is the case, then it is a bug that should be fixed,
this leakage is not the solution however.
Ryan
______________________________________________________________
Ryan Bloom rbb@apache.org
Covalent Technologies rbb@covalent.net
--------------------------------------------------------------
Re: mod_include vs file descriptors
Posted by Brian Pane <bp...@pacbell.net>.
Ryan Bloom wrote:
[...]
>apr_pool_join is a no-op unless APR_POOL_DEBUG is enabled. If it isn't
>enabled, which it shouldn't be on production servers, there will be no
>performance penalty for that function.
>
Sorry, I blamed the wrong part of the code, but the core
problem still remains: we don't clean up the subrequest
(and thus don't close file descriptors for included .shtml
files) until top-level .shtml request goes out of scope.
The logic in handle_include() appears to do this on purpose:
/* destroy the sub request if it's not a nested include
* (crumb) */
if (rr != NULL
&& ap_get_module_config(rr->request_config,
&include_module) != NESTED_INCLUDE_MAGIC) {
ap_destroy_sub_req(rr);
}
What else would break if we destroyed the subreq here,
even in the case of a nested include?
--Brian
Re: mod_include vs file descriptors
Posted by dean gaudet <de...@arctic.org>.
On Sat, 10 Nov 2001, Ryan Bloom wrote:
> apr_pool_join is a no-op unless APR_POOL_DEBUG is enabled. If it isn't
> enabled, which it shouldn't be on production servers, there will be no
> performance penalty for that function.
brian is still right to say that it's a "joined" pool -- as defined by the
terminology in the debugging documentation. that statement, even though
it's normally a no-op serves to tell everyone that the lifetime of the
subpool is at least as long as the lifetime of the parent. i.e. the
filedescriptor will live on.
(fwiw, it's always been this way anyhow)
-dean
Re: mod_include vs file descriptors
Posted by Ryan Bloom <rb...@covalent.net>.
On Saturday 10 November 2001 09:09 pm, Brian Pane wrote:
> This bit of mod_include's includes_filter() is likely to be a problem
> for servers with a lot of connections from slow clients:
>
> if ((parent = ap_get_module_config(r->request_config,
> &include_module))) {
> /* Kludge --- for nested includes, we want to keep the subprocess
> * environment of the base document (for compatibility); that means
> * torquing our own last_modified date as well so that the
> * LAST_MODIFIED variable gets reset to the proper value if the
> * nested document resets <!--#config timefmt-->.
> * We also insist that the memory for this subrequest not be
> * destroyed, that's dealt with in handle_include().
> */
> r->subprocess_env = r->main->subprocess_env;
> apr_pool_join(r->main->pool, r->pool);
> r->finfo.mtime = r->main->finfo.mtime;
> }
>
> The problem is that, by joing the pool with that of the parent request,
> the file descriptor for this .shtml file doesn't get closed until the
> parent pool goes out of scope. With a .shtml file that includes 9 other
> .shtml files, we'll have a total of 10 file descriptors open (11 including
> the socket to the client) until we finish sending the content to the
> client. That could seriously limit the number of clients that each child
> process can handle in the threaded or worker MPM.
>
> The easy solutions I can think of are:
> * Close the descriptor for each file immediately after mmap'ing it, or
> * Don't join the pools in includes_filter().
>
> Can anyone comment on the viability of these fixes, or any other suitable
> solution?
apr_pool_join is a no-op unless APR_POOL_DEBUG is enabled. If it isn't
enabled, which it shouldn't be on production servers, there will be no
performance penalty for that function.
Ryan
______________________________________________________________
Ryan Bloom rbb@apache.org
Covalent Technologies rbb@covalent.net
--------------------------------------------------------------