Improving mod_perl Sites' Performance: Part 4
by Stas Bekman
|
Pages: 1, 2, 3, 4, 5
Preloading Perl Modules at Server Startup
You can use the PerlRequire and PerlModule directives to load
commonly used modules such as CGI.pm, DBI and etc., when the
server is started. On most systems, server children will be able to
share the code space used by these modules. Just add the following
directives into httpd.conf:
PerlModule CGI
PerlModule DBI
But an even better approach is to create a separate startup file (where you code in plain perl) and put there things like:
use DBI ();
use Carp ();
Don't forget to prevent importing of the symbols exported by default
by the module you are going to preload, by placing empty parentheses
() after a module's name. Unless you need some of these in the
startup file, which is unlikely. This will save you a few more memory
bits.
Then you require() this startup file in httpd.conf with the
PerlRequire directive, placing it before the rest of the mod_perl
configuration directives:
PerlRequire /path/to/start-up.pl
|
Related Reading
Perl in a Nutshell, 2nd Edition |
CGI.pm is a special case. Ordinarily CGI.pm autoloads most of
its functions on an as-needed basis. This speeds up the loading time
by deferring the compilation phase. When you use mod_perl, FastCGI or
another system that uses a persistent Perl interpreter, you will want
to precompile the functions at initialization time. To accomplish
this, call the package function compile() like this:
use CGI ();
CGI->compile(':all');
The arguments to compile() are a list of method names or sets, and
are identical to those accepted by the use() and import()
operators. Note that in most cases you will want to replace ':all'
with the tag names that you actually use in your code, since generally
you only use a subset of them.
Let's conduct a memory usage test to prove that preloading, reduces memory requirements.
In order to have an easy measurement I will use only one child process, therefore I will use this setting:
MinSpareServers 1
MaxSpareServers 1
StartServers 1
MaxClients 1
MaxRequestsPerChild 100
I'm going to use the Apache::Registry script memuse.pl which
consists of two parts: the first one preloads a bunch of modules (that
most of them aren't going to be used), the second part reports the
memory size and the shared memory size used by the single child
process that I start. and of course it prints the difference between
the two sizes.
memuse.pl
---------
use strict;
use CGI ();
use DB_File ();
use LWP::UserAgent ();
use Storable ();
use DBI ();
use GTop ();
my $r = shift;
$r->send_http_header('text/plain');
my $proc_mem = GTop->new->proc_mem($$);
my $size = $proc_mem->size;
my $share = $proc_mem->share;
my $diff = $size - $share;
printf "%10s %10s %10s\n", qw(Size Shared Difference);
printf "%10d %10d %10d (bytes)\n",$size,$share,$diff;
First I restart the server and execute this CGI script when none of the above modules preloaded. Here is the result:
Size Shared Diff
4706304 2134016 2572288 (bytes)
Now I take all the modules:
use strict;
use CGI ();
use DB_File ();
use LWP::UserAgent ();
use Storable ();
use DBI ();
use GTop ();
and copy them into the startup script, so they will get preloaded. The script remains unchanged. I restart the server and execute it again. I get the following.
Size Shared Diff
4710400 3997696 712704 (bytes)
Let's put the two results into one table:
Preloading Size Shared Diff
Yes 4710400 3997696 712704 (bytes)
No 4706304 2134016 2572288 (bytes)
--------------------------------------------
Difference 4096 1863680 -1859584
You can clearly see that when the modules weren't preloaded the shared memory pages size, were about 1864Kb smaller relative to the case where the modules were preloaded.
Assuming that you have had 256M dedicated to the web server, if you didn't preload the modules, you could have:
268435456 = X * 2572288 + 2134016
X = (268435456 - 2134016) / 2572288 = 103
103 servers.
Now let's calculate the same thing with modules preloaded:
268435456 = X * 712704 + 3997696
X = (268435456 - 3997696) / 712704 = 371
You can have almost 4 times more servers!!!
More mod_perl Articles |
|
Debugging and Profiling mod_perl Applications Integrating mod_perl with Apache 2.1 Authentication Apache::VMonitor - The Visual System and Apache Server Monitor |
Remember that I have mentioned before that memory pages gets dirty and the size of the shared memory gets smaller with time? So I have presented the ideal case where the shared memory stays intact. Therefore the real numbers will be a little bit different, but not far from the numbers in our example.
Also it's obvious that in your case it's possible that the process size will be bigger and the shared memory will be smaller, since you will use different modules and a different code, so you won't get this fantastic ratio, but this example is certainly helps to feel the difference.
References
- The mod_perl site's URL: http://perl.apache.org/
-
GTophttp://search.cpan.org/search?dist=GTop
GToprelies in turn on libgtop library not available for all platformsVisit http://home-of-linux.org/gnome/libgtop/ for more information
-
Apache::Peek -
Devel::Peek


