.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.40)
.\"
.\" Standard preamble:
.\" ========================================================================
.de Sp \" Vertical space (when we can't use .PP)
.if t .sp .5v
.if n .sp
..
.de Vb \" Begin verbatim text
.ft CW
.nf
.ne \\$1
..
.de Ve \" End verbatim text
.ft R
.fi
..
.\" Set up some character translations and predefined strings. \*(-- will
.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
.\" double quote, and \*(R" will give a right double quote. \*(C+ will
.\" give a nicer C++. Capital omega is used to do unbreakable dashes and
.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,
.\" nothing in troff, for use with C<>.
.tr \(*W-
.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
.ie n \{\
. ds -- \(*W-
. ds PI pi
. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
. ds L" ""
. ds R" ""
. ds C` ""
. ds C' ""
'br\}
.el\{\
. ds -- \|\(em\|
. ds PI \(*p
. ds L" ``
. ds R" ''
. ds C`
. ds C'
'br\}
.\"
.\" Escape single quotes in literal strings from groff's Unicode transform.
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.\"
.\" If the F register is >0, we'll generate index entries on stderr for
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
.\" entries marked with X<> in POD. Of course, you'll have to process the
.\" output yourself in some meaningful fashion.
.\"
.\" Avoid warning from groff about undefined register 'F'.
.de IX
..
.nr rF 0
.if \n(.g .if rF .nr rF 1
.if (\n(rF:(\n(.g==0)) \{\
. if \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. if !\nF==2 \{\
. nr % 0
. nr F 2
. \}
. \}
.\}
.rr rF
.\" ========================================================================
.\"
.IX Title "Apache2::SizeLimit 3pm"
.TH Apache2::SizeLimit 3pm "2021-05-21" "perl v5.32.1" "User Contributed Perl Documentation"
.\" For nroff, turn off justification. Always turn off hyphenation; it makes
.\" way too many mistakes in technical documents.
.if n .ad l
.nh
.SH "NAME"
Apache2::SizeLimit \- Because size does matter.
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 1
\& PerlLoadModule Apache2::SizeLimit
\&
\&
\& Apache2::SizeLimit\->set_max_process_size(150_000); # Max size in KB
\& Apache2::SizeLimit\->set_min_shared_size(10_000); # Min share in KB
\& Apache2::SizeLimit\->set_max_unshared_size(120_000); # Max unshared size in KB
\&
\&
\& PerlCleanupHandler Apache2::SizeLimit
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
******************************** \s-1NOIICE\s0 *******************
.PP
.Vb 2
\& This version is only for httpd 2.x and mod_perl 2.x
\& series.
\&
\& For httpd 1.3.x / mod_perl 1.x Apache::SizeLimit
\& documentation please read the perldoc in
\& lib/Apache/SizeLimit.pm
.Ve
.PP
******************************** \s-1NOTICE\s0 *******************
.PP
This module allows you to kill off Apache httpd processes if they grow
too large. You can make the decision to kill a process based on its
overall size, by setting a minimum limit on shared memory, or a
maximum on unshared memory.
.PP
You can set limits for each of these sizes, and if any limit is
exceeded, the process will be killed.
.PP
You can also limit the frequency that these sizes are checked so that
this module only checks every N requests.
.PP
This module is highly platform dependent, please read the
\&\*(L"PER-PLATFORM \s-1BEHAVIOR\*(R"\s0 section for details. It is possible that this
module simply does not support your platform.
.SH "API"
.IX Header "API"
You can set set the size limits from a Perl module or script loaded by
Apache by calling the appropriate class method on \f(CW\*(C`Apache2::SizeLimit\*(C'\fR:
.IP "\(bu" 4
Apache2::SizeLimit\->set_max_process_size($size)
.Sp
This sets the maximum size of the process, including both shared and
unshared memory.
.IP "\(bu" 4
Apache2::SizeLimit\->set_max_unshared_size($size)
.Sp
This sets the maximum amount of \fIunshared\fR memory the process can
use.
.IP "\(bu" 4
Apache2::SizeLimit\->set_min_shared_size($size)
.Sp
This sets the minimum amount of shared memory the process must have.
.PP
The two methods related to shared memory size are effectively a no-op
if the module cannot determine the shared memory size for your
platform. See \*(L"PER-PLATFORM \s-1BEHAVIOR\*(R"\s0 for more details.
.SS "Running the \fBhandler()\fP"
.IX Subsection "Running the handler()"
There are several ways to make this module actually run the code to
kill a process.
.PP
The simplest is to make \f(CW\*(C`Apache2::SizeLimit\*(C'\fR a \f(CW\*(C`PerlCleanupHandler\*(C'\fR
in your Apache config:
.PP
.Vb 1
\& PerlCleanupHandler Apache2::SizeLimit
.Ve
.PP
This will ensure that \f(CW\*(C`Apache2::SizeLimit\->handler()\*(C'\fR is run
for all requests.
.PP
If you want to combine this module with a cleanup handler of your own,
make sure that \f(CW\*(C`Apache2::SizeLimit\*(C'\fR is the last handler run:
.PP
.Vb 1
\& PerlCleanupHandler Apache2::SizeLimit My::CleanupHandler
.Ve
.PP
Remember, mod_perl will run stacked handlers from right to left, as
they're defined in your configuration.
.PP
If you have some cleanup code you need to run, but stacked handlers
aren't appropriate for your setup, you can also explicitly call the
\&\f(CW\*(C`Apache2::SizeLimit\->handler()\*(C'\fR function from your own cleanup
handler:
.PP
.Vb 1
\& package My::CleanupHandler
\&
\& sub handler {
\& my $r = shift;
\&
\& # Causes File::Temp to remove any temp dirs created during the
\& # request
\& File::Temp::cleanup();
\&
\& return Apache2::SizeLimit\->handler($r);
\& }
.Ve
.IP "\(bu" 4
Apache2::SizeLimit\->add_cleanup_handler($r)
.Sp
You can call this method inside a request to run
\&\f(CW\*(C`Apache2::SizeLimit\*(C'\fR's \f(CW\*(C`handler()\*(C'\fR method for just that request. It's
safe to call this method repeatedly \*(-- the cleanup will only be run
once per request.
.SS "Checking Every N Requests"
.IX Subsection "Checking Every N Requests"
Since checking the process size can take a few system calls on some
platforms (e.g. linux), you may not want to check the process size for
every request.
.IP "\(bu" 4
Apache2::SizeLimit\->set_check_interval($interval)
.Sp
Calling this causes \f(CW\*(C`Apache2::SizeLimit\*(C'\fR to only check the process
size every \f(CW$interval\fR requests. If you want this to affect all
processes, make sure to call this during server startup.
.SH "SHARED MEMORY OPTIONS"
.IX Header "SHARED MEMORY OPTIONS"
In addition to simply checking the total size of a process, this
module can factor in how much of the memory used by the process is
actually being shared by copy-on-write. If you don't understand how
memory is shared in this way, take a look at the mod_perl docs at
http://perl.apache.org/docs/.
.PP
You can take advantage of the shared memory information by setting a
minimum shared size and/or a maximum unshared size. Experience on one
heavily trafficked mod_perl site showed that setting maximum unshared
size and leaving the others unset is the most effective policy. This
is because it only kills off processes that are truly using too much
physical \s-1RAM,\s0 allowing most processes to live longer and reducing the
process churn rate.
.SH "PER-PLATFORM BEHAVIOR"
.IX Header "PER-PLATFORM BEHAVIOR"
This module is highly platform dependent, since finding the size of a
process is different for each \s-1OS,\s0 and some platforms may not be
supported. In particular, the limits on minimum shared memory and
maximum shared memory are currently only supported on Linux and \s-1BSD.\s0
If you can contribute support for another \s-1OS,\s0 patches are very
welcome.
.PP
Currently supported OSes:
.SS "linux"
.IX Subsection "linux"
For linux we read the process size out of \fI/proc/self/statm\fR. If you
are worried about performance, you can consider using \f(CW\*(C`Apache2::SizeLimit\->set_check_interval()\*(C'\fR to reduce how often this
read happens.
.PP
As of linux 2.6, \fI/proc/self/statm\fR does not report the amount of
memory shared by the copy-on-write mechanism as shared memory. This
means that decisions made based on shared memory as reported by that
interface are inherently wrong.
.PP
However, as of the 2.6.14 release of the kernel, there is
\&\fI/proc/self/smaps\fR entry for each process. \fI/proc/self/smaps\fR
reports various sizes for each memory segment of a process and allows
us to count the amount of shared memory correctly.
.PP
If \f(CW\*(C`Apache2::SizeLimit\*(C'\fR detects a kernel that supports
\&\fI/proc/self/smaps\fR and the \f(CW\*(C`Linux::Smaps\*(C'\fR module is installed it
will use that module instead of \fI/proc/self/statm\fR.
.PP
Reading \fI/proc/self/smaps\fR is expensive compared to
\&\fI/proc/self/statm\fR. It must look at each page table entry of a
process. Further, on multiprocessor systems the access is
synchronized with spinlocks. Again, you might consider using \f(CW\*(C`Apache2::SizeLimit\->set_check_interval()\*(C'\fR.
.PP
\fICopy-on-write and Shared Memory\fR
.IX Subsection "Copy-on-write and Shared Memory"
.PP
The following example shows the effect of copy-on-write:
.PP
.Vb 5
\&
\& require Apache2::SizeLimit;
\& package X;
\& use strict;
\& use Apache2::Const \-compile => qw(OK);
\&
\& my $x = "a" x (1024*1024);
\&
\& sub handler {
\& my $r = shift;
\& my ($size, $shared) = $Apache2::SizeLimit\->_check_size();
\& $x =~ tr/a/b/;
\& my ($size2, $shared2) = $Apache2::SizeLimit\->_check_size();
\& $r\->content_type(\*(Aqtext/plain\*(Aq);
\& $r\->print("1: size=$size shared=$shared\en");
\& $r\->print("2: size=$size2 shared=$shared2\en");
\& return OK;
\& }
\&
\&
\&
\& SetHandler modperl
\& PerlResponseHandler X
\&
.Ve
.PP
The parent Apache process allocates memory for the string in
\&\f(CW$x\fR. The \f(CW\*(C`tr\*(C'\fR\-command then overwrites all \*(L"a\*(R" with \*(L"b\*(R" if the
handler is called with an argument. This write is done in place, thus,
the process size doesn't change. Only \f(CW$x\fR is not shared anymore by
means of copy-on-write between the parent and the child.
.PP
If \fI/proc/self/smaps\fR is available curl shows:
.PP
.Vb 3
\& r2@s93:~/work/mp2> curl http://localhost:8181/X
\& 1: size=13452 shared=7456
\& 2: size=13452 shared=6432
.Ve
.PP
Shared memory has lost 1024 kB. The process' overall size remains unchanged.
.PP
Without \fI/proc/self/smaps\fR it says:
.PP
.Vb 3
\& r2@s93:~/work/mp2> curl http://localhost:8181/X
\& 1: size=13052 shared=3628
\& 2: size=13052 shared=3636
.Ve
.PP
One can see the kernel lies about the shared memory. It simply doesn't
count copy-on-write pages as shared.
.SS "solaris 2.6 and above"
.IX Subsection "solaris 2.6 and above"
For solaris we simply retrieve the size of \fI/proc/self/as\fR, which
contains the address-space image of the process, and convert to \s-1KB.\s0
Shared memory calculations are not supported.
.PP
\&\s-1NOTE:\s0 This is only known to work for solaris 2.6 and above. Evidently
the \fI/proc\fR filesystem has changed between 2.5.1 and 2.6. Can anyone
confirm or deny?
.SS "\s-1BSD\s0 (and \s-1OSX\s0)"
.IX Subsection "BSD (and OSX)"
Uses \f(CW\*(C`BSD::Resource::getrusage()\*(C'\fR to determine process size. This is
pretty efficient (a lot more efficient than reading it from the
\&\fI/proc\fR fs anyway).
.PP
According to recent tests on \s-1OSX\s0 (July, 2006), \f(CW\*(C`BSD::Resource\*(C'\fR simply
reports zero for process and shared size on that platform, so \s-1OSX\s0 is
not supported by \f(CW\*(C`Apache2::SizeLimit\*(C'\fR.
.SS "\s-1AIX\s0?"
.IX Subsection "AIX?"
Uses \f(CW\*(C`BSD::Resource::getrusage()\*(C'\fR to determine process size. Not
sure if the shared memory calculations will work or not. \s-1AIX\s0 users?
.SS "Win32"
.IX Subsection "Win32"
Uses \f(CW\*(C`Win32::API\*(C'\fR to access process memory information.
\&\f(CW\*(C`Win32::API\*(C'\fR can be installed under ActiveState perl using the
supplied ppm utility.
.SS "Everything Else"
.IX Subsection "Everything Else"
If your platform is not supported, then please send a patch to check
the process size. The more portable/efficient/correct the solution the
better, of course.
.SH "ABOUT THIS MODULE"
.IX Header "ABOUT THIS MODULE"
This module was written in response to questions on the mod_perl
mailing list on how to tell the httpd process to exit if it gets too
big.
.PP
Actually, there are two big reasons your httpd children will grow.
First, your code could have a bug that causes the process to increase
in size very quickly. Second, you could just be doing operations that
require a lot of memory for each request. Since Perl does not give
memory back to the system after using it, the process size can grow
quite large.
.PP
This module will not really help you with the first problem. For that
you should probably look into \f(CW\*(C`Apache::Resource\*(C'\fR or some other means
of setting a limit on the data size of your program. BSD-ish systems
have \f(CW\*(C`setrlimit()\*(C'\fR, which will kill your memory gobbling processes.
However, it is a little violent, terminating your process in
mid-request.
.PP
This module attempts to solve the second situation, where your process
slowly grows over time. It checks memory usage after every request,
and if it exceeds a threshold, exits gracefully.
.PP
By using this module, you should be able to discontinue using the
Apache configuration directive \fBMaxRequestsPerChild\fR, although for
some folks, using both in combination does the job.
.SH "DEPRECATED APIS"
.IX Header "DEPRECATED APIS"
Previous versions of this module documented three globals for defining
memory size limits:
.IP "\(bu" 4
\&\f(CW$Apache2::SizeLimit::MAX_PROCESS_SIZE\fR
.IP "\(bu" 4
\&\f(CW$Apache2::SizeLimit::MIN_SHARE_SIZE\fR
.IP "\(bu" 4
\&\f(CW$Apache2::SizeLimit::MAX_UNSHARED_SIZE\fR
.IP "\(bu" 4
\&\f(CW$Apache2::SizeLimit::CHECK_EVERY_N_REQUESTS\fR
.IP "\(bu" 4
\&\f(CW$Apache2::SizeLimit::USE_SMAPS\fR
.PP
Direct use of these globals is deprecated, but will continue to work
for the foreseeable future.
.PP
It also documented three functions for use from registry scripts:
.IP "\(bu" 4
\&\fBApache2::SizeLimit::setmax()\fR
.IP "\(bu" 4
\&\fBApache2::SizeLimit::setmin()\fR
.IP "\(bu" 4
\&\fBApache2::SizeLimit::setmax_unshared()\fR
.PP
Besides setting the appropriate limit, these functions \fIalso\fR add a
cleanup handler to the current request. In the 2.x series of mod_perl
to use the deprecated functions, you must set PerlOptions +GlobalRequest
accordingly.
.SH "SUPPORT"
.IX Header "SUPPORT"
The Apache-SizeLimit project is co-maintained by several developers,
who take turns at making \s-1CPAN\s0 releases. Therefore you may find several
\&\s-1CPAN\s0 directories containing Apache-SizeLimit releases. The best way to
find the latest release is to use http://search.cpan.org/.
.PP
If you have a question or you want to submit a bug report or make a
contribution, please do not email individual authors, but send an
email to the modperl perl.apache.org mailing list. This list is
moderated, so unless you are subscribed to it, your message will have
to be approved first by a moderator. Therefore please allow some time
(up to a few days) for your post to propagate to the list.
.SH "AUTHOR"
.IX Header "AUTHOR"
Doug Bagley , channeling Procrustes.
.PP
Brian Moseley : Solaris 2.6 support
.PP
Doug Steinwand and Perrin Harkins : added support for
shared memory and additional diagnostic info
.PP
Matt Phillips and Mohamed Hendawi
: Win32 support
.PP
Dave Rolsky , maintenance and fixes outside of
mod_perl tree (0.9+).