You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Boysenberry Payne <bo...@habitatlife.com> on 2007/11/07 18:20:58 UTC

mod_perl2 and Apache::SharedMem

I'm trying to spread my pre apache child memory load appropriately  
and have a few questions.

Is Apache::SharedMem working with mod_perl2 the "same" way it did  
with mod_perl1?

Is Apache::SharedMem the "preferred" way to handle sharing memory  
between apache children?

Are modules loaded using "use ModuleName;" in an apache startup.pl  
file loaded into shared memory
or copied to each child separately?

Thanks,
Boysenberry Payne

Re: mod_perl2 and Apache::SharedMem

Posted by Boysenberry Payne <bo...@habitatlife.com>.
Am I looking for APR::Pool?

-bop


On Nov 7, 2007, at 12:41 PM, Boysenberry Payne wrote:

>
> On Nov 7, 2007, at 11:59 AM, Michael Peters wrote:
>
>> Boysenberry Payne wrote:
>>
>>> Is Apache::SharedMem working with mod_perl2 the "same" way it did  
>>> with
>>> mod_perl1?
>>
>> Don't know, haven't used it. It hasn't been updated since 2001, so  
>> either it's
>> perfect and hasn't needed any changes or it's not being maintained.
>
> Yeah, that's why I asked...
>
>>
>>> Is Apache::SharedMem the "preferred" way to handle sharing memory
>>> between apache children?
>>
>> It's definitely not "preferred", but I'm not sure what is. It  
>> depends on what
>> you're doing with the data and how often it changes. If it's  
>> static you can just
>> load the data at startup in a Perl module. If it changes a lot you  
>> can use BDB.
>> If it's something that needs to be shared by multiple machines  
>> then use
>> something external like MySQL. If it's just some cached data then  
>> use something
>> like memcached or Cache::FastMmap.
>
> Does memcached or Cache::FastMap work with apache's shared memory  
> pool?
>
> I'm basically trying to organize my references along 3 lines:
> -	runtime data dumped with each request
> -	per child data sticks around as long as the apache child exists
> -	apache shared data sticks around for the life of the root apache  
> process
>
> I'm sure there are other memory areas to deal with, but those are  
> my primary concerns.
>
> I'm looking for a way to control where my references point to in  
> memory.
>
> For example, a already have startup.pl file included in apache's  
> root httpd.conf
> in the form of:
>
> in the httpd.conf file:
> <snip>
> # Startup file for loading assets into memory
> PerlRequire					/directory/to/startup.pl
> </snip>
>
> and in the startup.pl file:
> <snip>
> #!/usr/bin/perl
>
> BEGIN {
> 	use lib qw(/directory/to/perl_mods);
> }
>
> use Apache::DBI ();
> ...(many more too)...
> 1;
> </snip>
>
> In this situation if its possible (which it should be for my own  
> modules) I would like
> the package symbol table and contents to be loaded into the shared  
> server wide memory.
>
> How could I insure the packages aren't loaded into each child process?
>
> If a package is loaded into "shared" memory is "our" and "my"  
> handled the same as they are
> when its loaded into the apache child process memory?
>
>>
>>> Are modules loaded using "use ModuleName;" in an apache  
>>> startup.pl file
>>> loaded into shared memory
>>> or copied to each child separately?
>>
>> There's nothing special about "use". "require" would work just as  
>> well. The
>> thing to remember is that you need to load the Perl modules into  
>> the Apache
>> process's memory before it forks. Anything loaded before the fork  
>> will be shared
>> (as long as it doesn't change) with Copy-On-Write.
>
> I'm sure I've read about Copy-On-Write, better safe than sorry,  
> have any links you can
> point me at that will allow me to understand it better?  I want to  
> make sure if I write
> I don't copy unless I'm sure that's what I need to do (like for  
> temporary runtime changes.)
>
> Thanks,
> -bop
>
>


Re: mod_perl2 and Apache::SharedMem

Posted by Boysenberry Payne <bo...@habitatlife.com>.
On Nov 7, 2007, at 5:05 PM, Michael Peters wrote:

> Boysenberry Payne wrote:
>> If I created some of my static hashes and objects during the
>> PerlPostConfigHandler phase
>> and added them to either the configuration or log pools would that  
>> help
>> me keep
>> some of my memory shared, assuming I could get at either pool  
>> during the
>> PerlResponseHandler?
>
> I think you're overthinking it here and getting yourself confused.

After being up for 36+ hours that's an understatement.


>> I have classes that create singleton objects with lots of static  
>> parts.
>> Should I build these "constructs"
>> on server post config so they're already built in the child  
>> processes as
>> shared memory rather than
>> building them in each child, increasing the non-shared memory use?
>
> This is how this is typically done:
>
> # in startup.pl
> use My::Constructs;
>
> # in My/Constructs.pm
> package My::Constructs;
> our @data = ();
> # do something to build out your data structure...
>
>
> Then in your application you can just access the data as
> @My::Constructs::data and it will be shared by all your child  
> processes
> (assuming you don't modify it during a request).

Ah, passing constructs directly, I've been trying to figure out that  
for a while.
Can I do that using strict, or do I have to delimit it around the  
reference?
I remember getting errors when I tried that before.

-bop




Re: mod_perl2 and Apache::SharedMem

Posted by Michael Peters <mp...@plusthree.com>.
Boysenberry Payne wrote:
> If I created some of my static hashes and objects during the
> PerlPostConfigHandler phase
> and added them to either the configuration or log pools would that help
> me keep
> some of my memory shared, assuming I could get at either pool during the
> PerlResponseHandler?

I think you're overthinking it here and getting yourself confused.

> I have classes that create singleton objects with lots of static parts. 
> Should I build these "constructs"
> on server post config so they're already built in the child processes as
> shared memory rather than
> building them in each child, increasing the non-shared memory use?

This is how this is typically done:

# in startup.pl
use My::Constructs;

# in My/Constructs.pm
package My::Constructs;
our @data = ();
# do something to build out your data structure...


Then in your application you can just access the data as
@My::Constructs::data and it will be shared by all your child processes
(assuming you don't modify it during a request).

-- 
Michael Peters
Developer
Plus Three, LP


Re: mod_perl2 and Apache::SharedMem

Posted by Boysenberry Payne <bo...@habitatlife.com>.
On Nov 7, 2007, at 5:04 PM, Perrin Harkins wrote:

> On Nov 7, 2007 5:50 PM, Boysenberry Payne  
> <bo...@habitatlife.com> wrote:
>> If I created some of my static hashes and objects during the
>> PerlPostConfigHandler phase
>> and added them to either the configuration or log pools
>
> You're missing the big picture.  Adding perl objects to a shared
> memory pool doesn't prevent them also needing to be in perl's own
> allocated memory pool.  All shared memory schemes with perl result in
> MORE memory being used, because every process that accesses these
> structures needs its own copy in its own process in addition to the
> shared one.  The shared memory one is a serialized perl structure,
> created with something like Storable and perl has to turn it back into
> a normal perl variable, in non-shared memory, in order to use it.

I get it now,  thanks.  I read up a little on COW and get its advantages
with virtual memory now.  I guess it just requires a careful approach
to how I use the data, making changes more intentional or at least
know how much it will cost me.

> You can't use less memory by sharing your perl data with user-level
> shared memory techniques.  Only copy-on-write can help with that.
>
>> I have classes that create singleton objects with lots of static
>> parts.  Should I build these "constructs"
>> on server post config so they're already built in the child processes
>> as shared memory rather than
>> building them in each child, increasing the non-shared memory use?
>
> Build them before the server forks if you use prefork.

That's what I was thinking after reading up on the server stages more.
I think it will give COW a better chance at doing what it was intended
to do that way, rather than automatically creating a lot of unshared  
memory.

>> Would it allow me to reduce what I have set for
>> $Apache2::SizeLimit::MAX_UNSHARED_SIZE?
>
> You shouldn't be using that at all unless you have Linux::Smaps
> installed, but if you do, loading things before forking should improve
> the amount of copy-on-write sharing.

I guess up until now I hadn't really considered "pre-loading" my  
expensive
singletons on startup.  It was suggested I use  the  
PerlPostConfigHandler phase
instead of my startup.pl file; so I guess I'll start there and see  
how my processes
are effected.

$Apache2::SizeLimit::MAX_UNSHARED_SIZE doesn't do me much good
in my developing environment (OS X) but on our production server it  
has worked
wonderful.  No more server crashes due to no memory being available.
I've tweaked the MinSpareServers, MaxSpareServers, StartServers,  
MaxClients,
MaxRequestsPerChild and that helped a bunch too.

Really there is so much going on I'm guessing eventually I'll need to  
take a
different approach to how its all put together.

Thanks again,
-bop



Preloading modules (was Re: mod_perl2 and Apache::SharedMem)

Posted by Fred Moyer <fr...@redhotpenguin.com>.
Octavian Rasnita wrote:
> Hi,
> 
> Can you please tell me when the server forks?

http://perl.apache.org/docs/2.0/user/handlers/server.html

> What should I do to load some classes before the server forks?

http://perl.apache.org/docs/2.0/user/handlers/server.html#Startup_File

 > What should I do if I want to load the classes after the server forks?

That's generally something you want to avoid, as it will increase your 
memory usage.  You can use the PerlChildInitHandler phase to run code 
when the child process is forked from the main server process (details 
in the first link above).

If you really want to do this though, just avoid putting that class in 
the startup.pl file.

Re: mod_perl2 and Apache::SharedMem

Posted by Michael Peters <mp...@plusthree.com>.
Boysenberry Payne wrote:

>> But what if the module Module::Name "use" or "require" other modules?
>> Are they also loaded before the server forks, or I need to load them
>> with "PerlModule ..." in httpd.conf?
> 
> Good question.  I think it might depend on how the requires/uses are scoped
> in the scripts that are included.  I'm pretty sure any use statements
> that are
> scoped globally will be included in each forked copy of the server.  To
> insure
> they are included it is suggested you add your use statements to the
> startup.pl
> file.  Probably including your use/require statements in a BEGIN {}
> block will
> help to insure they are pre-compiled.

"use" is not scoped. It's a compile time pragma, so it happens at compile time,
not when the code is executed. A "use" anywhere in Perl program is the same as a
"use" at the top. We just normally put them at the top for organization.

If you have a question about whether or not a module has been loaded by Perl
before Apache forks, print out the contents of %INC. Anything in there has been
loaded by Perl. If I use A which uses B which uses C, then they should all be in
%INC. There's no trickery involved or anything special about mod_perl. If Perl
knows about the module and has loaded it, it's in memory.

-- 
Michael Peters
Developer
Plus Three, LP


Re: mod_perl2 and Apache::SharedMem

Posted by Boysenberry Payne <bo...@habitatlife.com>.
On Nov 8, 2007, at 1:07 PM, Octavian Rasnita wrote:

> From: "Boysenberry Payne" <bo...@habitatlife.com>
>>> What should I do if I want to load the classes after the server  
>>> forks?
>>
>> There are many phases after the fork, the most commonly used is the
>> PerlResponseHandler phase
>
> Do I understand correctly that even the modules specified in  
> httpd.conf with
>
> PerlResponseHandler Module::Name
>
> are loaded before the server forks, because they are loaded with
> PerlModule Module::Name
> or in a startup.pl file?

Correct.  Using `PerlModule Module::Name` in the httpd.conf file is  
the same as
`use Module::Name();`in the startup.pl file.  All of the  
configurations in those files
are run before the fork and before any other accessible phases.

> But what if the module Module::Name "use" or "require" other modules?
> Are they also loaded before the server forks, or I need to load  
> them with "PerlModule ..." in httpd.conf?

Good question.  I think it might depend on how the requires/uses are  
scoped
in the scripts that are included.  I'm pretty sure any use statements  
that are
scoped globally will be included in each forked copy of the server.   
To insure
they are included it is suggested you add your use statements to the  
startup.pl
file.  Probably including your use/require statements in a BEGIN {}  
block will
help to insure they are pre-compiled.

-bop

Re: mod_perl2 and Apache::SharedMem

Posted by Octavian Rasnita <or...@gmail.com>.
From: "Boysenberry Payne" <bo...@habitatlife.com>
>> What should I do if I want to load the classes after the server forks?
>
> There are many phases after the fork, the most commonly used is the
> PerlResponseHandler phase

Do I understand correctly that even the modules specified in httpd.conf with

PerlResponseHandler Module::Name

are loaded before the server forks, because they are loaded with
PerlModule Module::Name
or in a startup.pl file?

But what if the module Module::Name "use" or "require" other modules?
Are they also loaded before the server forks, or I need to load them with 
"PerlModule ..." in httpd.conf?

Thanks.

Octavian


Re: mod_perl2 and Apache::SharedMem

Posted by Boysenberry Payne <bo...@habitatlife.com>.
On Nov 8, 2007, at 12:31 AM, Octavian Rasnita wrote:

> Hi,
>
> Can you please tell me when the server forks?

As indicated here:
http://perl.apache.org/docs/2.0/user/handlers/server.html

Basically, the server starts then parses the configuration script  
(all pre mod_perl).
The fist phase we have access to with mod_perl is the  
PerlOpenLogsHandler
phase.  Then comes the PerlPostConfigHandler, the last phase before  
the child
processes get spawned.  Then you have the PerlChildInitHandler phase run
at the beginning of every child spawned by the server, run only once  
per child
process.  These and the final PerlChildExitHandler phase (run once  
the child is exiting)
are all server level phases.

> What should I do to load some classes before the server forks?

If I'm not mistaken the earliest you can affect a change in the  
server is in any
startup.pl type files.  They will be run during the configuration  
parse.  The first
handler phase PerlOpenLogsHandler is run after this.  It was  
suggested you wait until
the PerlPostConfigHandler phase if you want to load or construct  
stuff on
the server level (parent server).  It will have the structures you  
included in
your startup.pl file available, so tends to be more useful for  
running scripts.

> What should I do if I want to load the classes after the server forks?

There are many phases after the fork, the most commonly used is the
PerlResponseHandler phase:

http://perl.apache.org/docs/2.0/user/handlers/ 
http.html#PerlResponseHandler

ANy of the phases mentioned on that page will be post fork.

Hope that helps...

>
> Thank you for clarifications.
>
> Octavian
>
> ----- Original Message ----- From: "Perrin Harkins" <pe...@elem.com>
> To: "Boysenberry Payne" <bo...@habitatlife.com>
> Cc: "modperl List" <mo...@perl.apache.org>
> Sent: Thursday, November 08, 2007 1:04 AM
> Subject: Re: mod_perl2 and Apache::SharedMem
>
>
>> On Nov 7, 2007 5:50 PM, Boysenberry Payne  
>> <bo...@habitatlife.com> wrote:
>>> If I created some of my static hashes and objects during the
>>> PerlPostConfigHandler phase
>>> and added them to either the configuration or log pools
>>
>> You're missing the big picture.  Adding perl objects to a shared
>> memory pool doesn't prevent them also needing to be in perl's own
>> allocated memory pool.  All shared memory schemes with perl result in
>> MORE memory being used, because every process that accesses these
>> structures needs its own copy in its own process in addition to the
>> shared one.  The shared memory one is a serialized perl structure,
>> created with something like Storable and perl has to turn it back  
>> into
>> a normal perl variable, in non-shared memory, in order to use it.
>>
>> You can't use less memory by sharing your perl data with user-level
>> shared memory techniques.  Only copy-on-write can help with that.
>>
>>> I have classes that create singleton objects with lots of static
>>> parts.  Should I build these "constructs"
>>> on server post config so they're already built in the child  
>>> processes
>>> as shared memory rather than
>>> building them in each child, increasing the non-shared memory use?
>>
>> Build them before the server forks if you use prefork.
>>
>>> Would it allow me to reduce what I have set for
>>> $Apache2::SizeLimit::MAX_UNSHARED_SIZE?
>>
>> You shouldn't be using that at all unless you have Linux::Smaps
>> installed, but if you do, loading things before forking should  
>> improve
>> the amount of copy-on-write sharing.
>>
>> - Perrin
>


Re: mod_perl2 and Apache::SharedMem

Posted by Octavian Rasnita <or...@gmail.com>.
Hi,

Can you please tell me when the server forks?
What should I do to load some classes before the server forks?
What should I do if I want to load the classes after the server forks?

Thank you for clarifications.

Octavian

----- Original Message ----- 
From: "Perrin Harkins" <pe...@elem.com>
To: "Boysenberry Payne" <bo...@habitatlife.com>
Cc: "modperl List" <mo...@perl.apache.org>
Sent: Thursday, November 08, 2007 1:04 AM
Subject: Re: mod_perl2 and Apache::SharedMem


> On Nov 7, 2007 5:50 PM, Boysenberry Payne <bo...@habitatlife.com> 
> wrote:
>> If I created some of my static hashes and objects during the
>> PerlPostConfigHandler phase
>> and added them to either the configuration or log pools
>
> You're missing the big picture.  Adding perl objects to a shared
> memory pool doesn't prevent them also needing to be in perl's own
> allocated memory pool.  All shared memory schemes with perl result in
> MORE memory being used, because every process that accesses these
> structures needs its own copy in its own process in addition to the
> shared one.  The shared memory one is a serialized perl structure,
> created with something like Storable and perl has to turn it back into
> a normal perl variable, in non-shared memory, in order to use it.
>
> You can't use less memory by sharing your perl data with user-level
> shared memory techniques.  Only copy-on-write can help with that.
>
>> I have classes that create singleton objects with lots of static
>> parts.  Should I build these "constructs"
>> on server post config so they're already built in the child processes
>> as shared memory rather than
>> building them in each child, increasing the non-shared memory use?
>
> Build them before the server forks if you use prefork.
>
>> Would it allow me to reduce what I have set for
>> $Apache2::SizeLimit::MAX_UNSHARED_SIZE?
>
> You shouldn't be using that at all unless you have Linux::Smaps
> installed, but if you do, loading things before forking should improve
> the amount of copy-on-write sharing.
>
> - Perrin 


Re: mod_perl2 and Apache::SharedMem

Posted by Perrin Harkins <pe...@elem.com>.
On Nov 7, 2007 5:50 PM, Boysenberry Payne <bo...@habitatlife.com> wrote:
> If I created some of my static hashes and objects during the
> PerlPostConfigHandler phase
> and added them to either the configuration or log pools

You're missing the big picture.  Adding perl objects to a shared
memory pool doesn't prevent them also needing to be in perl's own
allocated memory pool.  All shared memory schemes with perl result in
MORE memory being used, because every process that accesses these
structures needs its own copy in its own process in addition to the
shared one.  The shared memory one is a serialized perl structure,
created with something like Storable and perl has to turn it back into
a normal perl variable, in non-shared memory, in order to use it.

You can't use less memory by sharing your perl data with user-level
shared memory techniques.  Only copy-on-write can help with that.

> I have classes that create singleton objects with lots of static
> parts.  Should I build these "constructs"
> on server post config so they're already built in the child processes
> as shared memory rather than
> building them in each child, increasing the non-shared memory use?

Build them before the server forks if you use prefork.

> Would it allow me to reduce what I have set for
> $Apache2::SizeLimit::MAX_UNSHARED_SIZE?

You shouldn't be using that at all unless you have Linux::Smaps
installed, but if you do, loading things before forking should improve
the amount of copy-on-write sharing.

- Perrin

Re: mod_perl2 and Apache::SharedMem

Posted by Boysenberry Payne <bo...@habitatlife.com>.
If I created some of my static hashes and objects during the  
PerlPostConfigHandler phase
and added them to either the configuration or log pools would that  
help me keep
some of my memory shared, assuming I could get at either pool during  
the PerlResponseHandler?

Am I confusing lifetime scope with shared memory?  Are the two entwined?

I have classes that create singleton objects with lots of static  
parts.  Should I build these "constructs"
on server post config so they're already built in the child processes  
as shared memory rather than
building them in each child, increasing the non-shared memory use?

Would it allow me to reduce what I have set for  
$Apache2::SizeLimit::MAX_UNSHARED_SIZE?

Thanks for all of the help,
-bop



On Nov 7, 2007, at 2:16 PM, Boysenberry Payne wrote:

> Other than for callbacks on destruction of memory pools what is  
> APR::Pool useful for?
>
> -bop
>
> On Nov 7, 2007, at 1:56 PM, Perrin Harkins wrote:
>
>> Don't use Apache::SharedMem.  It uses IPC::ShareLite, which is slow.
>>
>>> Does memcached or Cache::FastMap work with apache's shared memory  
>>> pool?
>>
>> No, I don't think anyone has ever gotten that to work.
>>
>>> In this situation if its possible (which it should be for my own
>>> modules) I would like
>>> the package symbol table and contents to be loaded into the shared
>>> server wide memory.
>>
>> It's not possible.  Perl allocates its own memory.  You can't tell it
>> where it to put things.  As discussed many times on the list,
>> copy-on-write helps with this on prefork systems.  On threaded
>> systems, Perl shares the parsed code, but the symbol table is
>> duplicated for each thread, so it ends up using more memory than
>> prefork.
>>
>> You can read more about copy-on-write in the mod_perl docs or at
>> modperlbook.org and there is more discussion about threads and how
>> they affect memory in the list archives.
>>
>> - Perrin
>


Re: mod_perl2 and Apache::SharedMem

Posted by Boysenberry Payne <bo...@habitatlife.com>.
Other than for callbacks on destruction of memory pools what is  
APR::Pool useful for?

-bop

On Nov 7, 2007, at 1:56 PM, Perrin Harkins wrote:

> Don't use Apache::SharedMem.  It uses IPC::ShareLite, which is slow.
>
>> Does memcached or Cache::FastMap work with apache's shared memory  
>> pool?
>
> No, I don't think anyone has ever gotten that to work.
>
>> In this situation if its possible (which it should be for my own
>> modules) I would like
>> the package symbol table and contents to be loaded into the shared
>> server wide memory.
>
> It's not possible.  Perl allocates its own memory.  You can't tell it
> where it to put things.  As discussed many times on the list,
> copy-on-write helps with this on prefork systems.  On threaded
> systems, Perl shares the parsed code, but the symbol table is
> duplicated for each thread, so it ends up using more memory than
> prefork.
>
> You can read more about copy-on-write in the mod_perl docs or at
> modperlbook.org and there is more discussion about threads and how
> they affect memory in the list archives.
>
> - Perrin


Re: mod_perl2 and Apache::SharedMem

Posted by Perrin Harkins <pe...@elem.com>.
Don't use Apache::SharedMem.  It uses IPC::ShareLite, which is slow.

> Does memcached or Cache::FastMap work with apache's shared memory pool?

No, I don't think anyone has ever gotten that to work.

> In this situation if its possible (which it should be for my own
> modules) I would like
> the package symbol table and contents to be loaded into the shared
> server wide memory.

It's not possible.  Perl allocates its own memory.  You can't tell it
where it to put things.  As discussed many times on the list,
copy-on-write helps with this on prefork systems.  On threaded
systems, Perl shares the parsed code, but the symbol table is
duplicated for each thread, so it ends up using more memory than
prefork.

You can read more about copy-on-write in the mod_perl docs or at
modperlbook.org and there is more discussion about threads and how
they affect memory in the list archives.

- Perrin

Re: mod_perl2 and Apache::SharedMem

Posted by Boysenberry Payne <bo...@habitatlife.com>.
On Nov 7, 2007, at 11:59 AM, Michael Peters wrote:

> Boysenberry Payne wrote:
>
>> Is Apache::SharedMem working with mod_perl2 the "same" way it did  
>> with
>> mod_perl1?
>
> Don't know, haven't used it. It hasn't been updated since 2001, so  
> either it's
> perfect and hasn't needed any changes or it's not being maintained.

Yeah, that's why I asked...

>
>> Is Apache::SharedMem the "preferred" way to handle sharing memory
>> between apache children?
>
> It's definitely not "preferred", but I'm not sure what is. It  
> depends on what
> you're doing with the data and how often it changes. If it's static  
> you can just
> load the data at startup in a Perl module. If it changes a lot you  
> can use BDB.
> If it's something that needs to be shared by multiple machines then  
> use
> something external like MySQL. If it's just some cached data then  
> use something
> like memcached or Cache::FastMmap.

Does memcached or Cache::FastMap work with apache's shared memory pool?

I'm basically trying to organize my references along 3 lines:
-	runtime data dumped with each request
-	per child data sticks around as long as the apache child exists
-	apache shared data sticks around for the life of the root apache  
process

I'm sure there are other memory areas to deal with, but those are my  
primary concerns.

I'm looking for a way to control where my references point to in memory.

For example, a already have startup.pl file included in apache's root  
httpd.conf
in the form of:

in the httpd.conf file:
<snip>
# Startup file for loading assets into memory
PerlRequire					/directory/to/startup.pl
</snip>

and in the startup.pl file:
<snip>
#!/usr/bin/perl

BEGIN {
	use lib qw(/directory/to/perl_mods);
}

use Apache::DBI ();
...(many more too)...
1;
</snip>

In this situation if its possible (which it should be for my own  
modules) I would like
the package symbol table and contents to be loaded into the shared  
server wide memory.

How could I insure the packages aren't loaded into each child process?

If a package is loaded into "shared" memory is "our" and "my" handled  
the same as they are
when its loaded into the apache child process memory?

>
>> Are modules loaded using "use ModuleName;" in an apache startup.pl  
>> file
>> loaded into shared memory
>> or copied to each child separately?
>
> There's nothing special about "use". "require" would work just as  
> well. The
> thing to remember is that you need to load the Perl modules into  
> the Apache
> process's memory before it forks. Anything loaded before the fork  
> will be shared
> (as long as it doesn't change) with Copy-On-Write.

I'm sure I've read about Copy-On-Write, better safe than sorry, have  
any links you can
point me at that will allow me to understand it better?  I want to  
make sure if I write
I don't copy unless I'm sure that's what I need to do (like for  
temporary runtime changes.)

Thanks,
-bop



Re: mod_perl2 and Apache::SharedMem

Posted by Michael Peters <mp...@plusthree.com>.
Boysenberry Payne wrote:

> Is Apache::SharedMem working with mod_perl2 the "same" way it did with
> mod_perl1?

Don't know, haven't used it. It hasn't been updated since 2001, so either it's
perfect and hasn't needed any changes or it's not being maintained.

> Is Apache::SharedMem the "preferred" way to handle sharing memory
> between apache children?

It's definitely not "preferred", but I'm not sure what is. It depends on what
you're doing with the data and how often it changes. If it's static you can just
load the data at startup in a Perl module. If it changes a lot you can use BDB.
If it's something that needs to be shared by multiple machines then use
something external like MySQL. If it's just some cached data then use something
like memcached or Cache::FastMmap.

> Are modules loaded using "use ModuleName;" in an apache startup.pl file
> loaded into shared memory
> or copied to each child separately?

There's nothing special about "use". "require" would work just as well. The
thing to remember is that you need to load the Perl modules into the Apache
process's memory before it forks. Anything loaded before the fork will be shared
(as long as it doesn't change) with Copy-On-Write.

-- 
Michael Peters
Developer
Plus Three, LP