You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modules-dev@httpd.apache.org by Sindhi Sindhi <si...@gmail.com> on 2013/05/24 20:00:40 UTC

Apache pool management

Hi,

I did an initial study to find out more information about Apache APR pools.
Some links say pools are not thread safe, some say they are really useful,
so I'm a bit confused. Kindly advice.

In certain links like the below, I read that pools are not thread safe.
Does this mean that when multiple requests are being handled by multiple
threads in Apache, data of one request could get overwritten/read/freed by
other requests?
http://mail-archives.apache.org/mod_mbox/apr-dev/200502.mbox/%3C1f1d9820502241330123f955f@mail.gmail.com%3E

A lot of other links like the below state that pools are a big advantage in
Apache memory management.
http://structure.usc.edu/svn/svn.developer.pools.html
http://dev.ariel-networks.com/apr/apr-tutorial/html/apr-tutorial-3.html

To summarize my requirement, I have written a C++ output filter module that
filters HTML. And this module will be invoked by multiple requests that hit
the server at the same time. My concern about using the APR pool is, if I
use the request pools, when one request (say request1) is in the middle of
processing, I do not want this request's data to be corrupted by other
requests'(say request2) - like, request2 overwriting on request1's data or
request2 freeing the memory allocated by request1.

If you can answer the below questions that will help me make a better use
or APR pool, kindly reply.
1. Can I use the request pools in such a way that, each request owns its
pool and cannot be read/written/freed by other requests/pool manager? What
settings in httpd-mpm.conf will make this possible?
2. I get the "ap_filter_t *f" as an input to my filter module, and I'm
using the f->r->pool to allocate memory that is used by my filter module.
So is this pool unique for every request?

Would appreciate a response.

Thanks.

Re: Apache pool management

Posted by Sindhi Sindhi <si...@gmail.com>.
Thankyou very much!

On Sat, May 25, 2013 at 2:21 PM, Sorin Manolache <so...@gmail.com> wrote:

> On 2013-05-25 10:05, Sindhi Sindhi wrote:
>
>> You have answered all my questions and thanks a lot. Had two questions
>> more, appreciate your response.
>>
>> 1.
>> As of now, my httpd.conf file has the below lines-
>> # Server-pool management (MPM specific)
>> #Include conf/extra/httpd-mpm.conf
>>
>> This means Apache does not read the httpd-mpm.conf file during startup.
>> And
>> so it uses the default settings for "Max number of requests it can
>> support"
>> and "Max number of threads it creates per child process"
>>
>> Where can I find what default values Apache uses for the following -
>> - Upto how many concurrent requests will Apache support by default
>> - Max number of threads that one child process creates
>>
>> For ex. if I want Apache to handle upto 400 concurrent requests at a time,
>> how will I know that this 400 is within the default settings that Apache
>> uses.
>>
>
>
> Have a look here, depending on your version of apache.
> http://httpd.apache.org/docs/**2.2/mod/mpm_common.html<http://httpd.apache.org/docs/2.2/mod/mpm_common.html>
> http://httpd.apache.org/docs/**2.4/mod/mpm_common.html<http://httpd.apache.org/docs/2.4/mod/mpm_common.html>
>
> The number of threads per process is given in ThreadsPerChild.
>
> You typically tweak
>
> ServerLimit (the maximum number of children that are simultaneously alive)
> ThreadLimit (the maximum sum of alive threads in all children)
> StartServers (how many children are created upon startup)
> ThreadsPerChild
> MaxRequestWorkers (or MaxClients in 2.2) (the maximum number of requests
> that are served simultaneously)
> MaxConnectionsPerChild (or MaxRequestsPerChild in 2.2) (after a child has
> served that many requests, it exits and is potentially replaced with a new
> child; avoids memleaks)
> MinSpareThreads and MaxSpareThreads, the minimum and maximum number of
> spare threads.
>
> There are some constraints on the arguments of these directives, which I
> do not master. I think that MaxRequestWorkers <= ThreadsPerChild *
> ServerLimit and that MaxRequestWorkers and ThreadLimit should be divisible
> by ThreadsPerChild, but as I said, I do not master. If you get them wrong,
> apache adjusts the values automatically and informs you about it upon
> startup.
>
> I am not sure, maybe others on the list can confirm or deny, but I think
> that apache does not distinguish between threads and processes in windows:
> http://httpd.apache.org/docs/**2.4/mod/mpm_winnt.html<http://httpd.apache.org/docs/2.4/mod/mpm_winnt.html>or
> http://httpd.apache.org/docs/**2.2/mod/mpm_winnt.html<http://httpd.apache.org/docs/2.2/mod/mpm_winnt.html>.
> So I think that ServerLimit = 1 in Windows and probably
> MaxConnectionsPerChild is not used or does not exist.
>
> You may also have a look at KeepAlive On|Off, MaxKeepAliveRequests and
> KeepAliveTimeout (http://httpd.apache.org/docs/**2.4/mod/core.html<http://httpd.apache.org/docs/2.4/mod/core.html>
> )
>
> If my module is on the internet with hundreds of thousands of possible
> client IPs that issue one request and then leave, I set KeepAlive Off. If
> my module is on the intranet and is accessed by a couple of webservices
> that continuously issue requests, I set it On with a short timeout.
> Performance-wise the KeepAlive directive makes a huge difference.
>
>
>  2.
>> My understanding is, once a request is completely processed, Apache frees
>> the pool of only this request and does not free any other request's pool.
>> And other request pools will be freed only when those requests are
>> completely processed. Kindly confirm my understanding to be correct.
>>
>
> Yes, it is correct.
>
>
> Sorin
>
>

Re: Apache pool management

Posted by Sorin Manolache <so...@gmail.com>.
On 2013-05-25 10:05, Sindhi Sindhi wrote:
> You have answered all my questions and thanks a lot. Had two questions
> more, appreciate your response.
>
> 1.
> As of now, my httpd.conf file has the below lines-
> # Server-pool management (MPM specific)
> #Include conf/extra/httpd-mpm.conf
>
> This means Apache does not read the httpd-mpm.conf file during startup. And
> so it uses the default settings for "Max number of requests it can support"
> and "Max number of threads it creates per child process"
>
> Where can I find what default values Apache uses for the following -
> - Upto how many concurrent requests will Apache support by default
> - Max number of threads that one child process creates
>
> For ex. if I want Apache to handle upto 400 concurrent requests at a time,
> how will I know that this 400 is within the default settings that Apache
> uses.


Have a look here, depending on your version of apache.
http://httpd.apache.org/docs/2.2/mod/mpm_common.html
http://httpd.apache.org/docs/2.4/mod/mpm_common.html

The number of threads per process is given in ThreadsPerChild.

You typically tweak

ServerLimit (the maximum number of children that are simultaneously alive)
ThreadLimit (the maximum sum of alive threads in all children)
StartServers (how many children are created upon startup)
ThreadsPerChild
MaxRequestWorkers (or MaxClients in 2.2) (the maximum number of requests 
that are served simultaneously)
MaxConnectionsPerChild (or MaxRequestsPerChild in 2.2) (after a child 
has served that many requests, it exits and is potentially replaced with 
a new child; avoids memleaks)
MinSpareThreads and MaxSpareThreads, the minimum and maximum number of 
spare threads.

There are some constraints on the arguments of these directives, which I 
do not master. I think that MaxRequestWorkers <= ThreadsPerChild * 
ServerLimit and that MaxRequestWorkers and ThreadLimit should be 
divisible by ThreadsPerChild, but as I said, I do not master. If you get 
them wrong, apache adjusts the values automatically and informs you 
about it upon startup.

I am not sure, maybe others on the list can confirm or deny, but I think 
that apache does not distinguish between threads and processes in 
windows: http://httpd.apache.org/docs/2.4/mod/mpm_winnt.html or 
http://httpd.apache.org/docs/2.2/mod/mpm_winnt.html. So I think that 
ServerLimit = 1 in Windows and probably MaxConnectionsPerChild is not 
used or does not exist.

You may also have a look at KeepAlive On|Off, MaxKeepAliveRequests and 
KeepAliveTimeout (http://httpd.apache.org/docs/2.4/mod/core.html)

If my module is on the internet with hundreds of thousands of possible 
client IPs that issue one request and then leave, I set KeepAlive Off. 
If my module is on the intranet and is accessed by a couple of 
webservices that continuously issue requests, I set it On with a short 
timeout. Performance-wise the KeepAlive directive makes a huge difference.

> 2.
> My understanding is, once a request is completely processed, Apache frees
> the pool of only this request and does not free any other request's pool.
> And other request pools will be freed only when those requests are
> completely processed. Kindly confirm my understanding to be correct.

Yes, it is correct.


Sorin


Re: Apache pool management

Posted by Sindhi Sindhi <si...@gmail.com>.
You have answered all my questions and thanks a lot. Had two questions
more, appreciate your response.

1.
As of now, my httpd.conf file has the below lines-
# Server-pool management (MPM specific)
#Include conf/extra/httpd-mpm.conf

This means Apache does not read the httpd-mpm.conf file during startup. And
so it uses the default settings for "Max number of requests it can support"
and "Max number of threads it creates per child process"

Where can I find what default values Apache uses for the following -
- Upto how many concurrent requests will Apache support by default
- Max number of threads that one child process creates

For ex. if I want Apache to handle upto 400 concurrent requests at a time,
how will I know that this 400 is within the default settings that Apache
uses.

2.
My understanding is, once a request is completely processed, Apache frees
the pool of only this request and does not free any other request's pool.
And other request pools will be freed only when those requests are
completely processed. Kindly confirm my understanding to be correct.

I'm sorry if I'm asking something very fundamental, but I'm new to Apache
modules and Apache internals.

Thanks a ton!

On Sat, May 25, 2013 at 2:53 AM, Sorin Manolache <so...@gmail.com> wrote:

> On 2013-05-24 20:00, Sindhi Sindhi wrote:
>
>> Hi,
>>
>> I did an initial study to find out more information about Apache APR
>> pools.
>> Some links say pools are not thread safe, some say they are really useful,
>> so I'm a bit confused. Kindly advice.
>>
>> In certain links like the below, I read that pools are not thread safe.
>> Does this mean that when multiple requests are being handled by multiple
>> threads in Apache, data of one request could get overwritten/read/freed by
>> other requests?
>> http://mail-archives.apache.**org/mod_mbox/apr-dev/200502.**mbox/%**
>> 3C1f1d9820502241330123f955f@**mail.gmail.com%3E<http://mail-archives.apache.org/mod_mbox/apr-dev/200502.mbox/%3C1f1d9820502241330123f955f@mail.gmail.com%3E>
>>
>> A lot of other links like the below state that pools are a big advantage
>> in
>> Apache memory management.
>> http://structure.usc.edu/svn/**svn.developer.pools.html<http://structure.usc.edu/svn/svn.developer.pools.html>
>> http://dev.ariel-networks.com/**apr/apr-tutorial/html/apr-**
>> tutorial-3.html<http://dev.ariel-networks.com/apr/apr-tutorial/html/apr-tutorial-3.html>
>>
>> To summarize my requirement, I have written a C++ output filter module
>> that
>> filters HTML. And this module will be invoked by multiple requests that
>> hit
>> the server at the same time. My concern about using the APR pool is, if I
>> use the request pools, when one request (say request1) is in the middle of
>> processing, I do not want this request's data to be corrupted by other
>> requests'(say request2) - like, request2 overwriting on request1's data or
>> request2 freeing the memory allocated by request1.
>>
>> If you can answer the below questions that will help me make a better use
>> or APR pool, kindly reply.
>> 1. Can I use the request pools in such a way that, each request owns its
>> pool and cannot be read/written/freed by other requests/pool manager? What
>> settings in httpd-mpm.conf will make this possible?
>>
>
> Yes. I think it works out-of-the-box and does not need any special
> settings.
>
>
>  2. I get the "ap_filter_t *f" as an input to my filter module, and I'm
>> using the f->r->pool to allocate memory that is used by my filter module.
>> So is this pool unique for every request?
>>
>
> Yes. Each request owns its pool. Concurrent requests are handled in
> different threads, but the threads cannot access another thread's request
> structure, and implicitly cannot access another request's pool.
>
> I don't know if pools are thread-safe. However, as stated here
> http://apr.apache.org/docs/**apr/1.4/group__apr__pools.html<http://apr.apache.org/docs/apr/1.4/group__apr__pools.html>
> **, sub-pool creation is thread-safe. The pool owned by a request is a
> sub-pool of the connection pool. So apache creates, in a thread-safe
> manner, the request pool for your exclusive use. So it should be safe.
> Personally I've never had concurrency issues with request pools.
>
> Sorin
>
>
>
>> Would appreciate a response.
>>
>> Thanks.
>>
>>
>

Re: Apache pool management

Posted by Sorin Manolache <so...@gmail.com>.
On 2013-05-24 20:00, Sindhi Sindhi wrote:
> Hi,
>
> I did an initial study to find out more information about Apache APR pools.
> Some links say pools are not thread safe, some say they are really useful,
> so I'm a bit confused. Kindly advice.
>
> In certain links like the below, I read that pools are not thread safe.
> Does this mean that when multiple requests are being handled by multiple
> threads in Apache, data of one request could get overwritten/read/freed by
> other requests?
> http://mail-archives.apache.org/mod_mbox/apr-dev/200502.mbox/%3C1f1d9820502241330123f955f@mail.gmail.com%3E
>
> A lot of other links like the below state that pools are a big advantage in
> Apache memory management.
> http://structure.usc.edu/svn/svn.developer.pools.html
> http://dev.ariel-networks.com/apr/apr-tutorial/html/apr-tutorial-3.html
>
> To summarize my requirement, I have written a C++ output filter module that
> filters HTML. And this module will be invoked by multiple requests that hit
> the server at the same time. My concern about using the APR pool is, if I
> use the request pools, when one request (say request1) is in the middle of
> processing, I do not want this request's data to be corrupted by other
> requests'(say request2) - like, request2 overwriting on request1's data or
> request2 freeing the memory allocated by request1.
>
> If you can answer the below questions that will help me make a better use
> or APR pool, kindly reply.
> 1. Can I use the request pools in such a way that, each request owns its
> pool and cannot be read/written/freed by other requests/pool manager? What
> settings in httpd-mpm.conf will make this possible?

Yes. I think it works out-of-the-box and does not need any special settings.

> 2. I get the "ap_filter_t *f" as an input to my filter module, and I'm
> using the f->r->pool to allocate memory that is used by my filter module.
> So is this pool unique for every request?

Yes. Each request owns its pool. Concurrent requests are handled in 
different threads, but the threads cannot access another thread's 
request structure, and implicitly cannot access another request's pool.

I don't know if pools are thread-safe. However, as stated here 
http://apr.apache.org/docs/apr/1.4/group__apr__pools.html, sub-pool 
creation is thread-safe. The pool owned by a request is a sub-pool of 
the connection pool. So apache creates, in a thread-safe manner, the 
request pool for your exclusive use. So it should be safe. Personally 
I've never had concurrency issues with request pools.

Sorin

>
> Would appreciate a response.
>
> Thanks.
>