📄 sizelimit.pm
字号:
$Apache2::SizeLimit::MAX_PROCESS_SIZE = 12000; # 12MB $Apache2::SizeLimit::MIN_SHARE_SIZE = 6000; # 6MB $Apache2::SizeLimit::MAX_UNSHARED_SIZE = 5000; # 5MB # in your httpd.conf: PerlCleanupHandler Apache2::SizeLimitOr you can just check those requests that are likely to get big, suchas CGI requests. This way of checking is also easier for those whoare mostly just running CGI scripts underC<L<ModPerl::Registry|docs::2.0::api::ModPerl::Registry>>: # in your script: use Apache2::SizeLimit; # sizes are in KB Apache2::SizeLimit::setmax(12000); Apache2::SizeLimit::setmin(6000); Apache2::SizeLimit::setmax_unshared(5000);This will work in places where you are using C<L<SetHandlerperl-script|docs::2.0::user::config::config/C_perl_script_>> oranywhere you enable C<L<PerlOptions+GlobalRequest|docs::2.0::user::config::config/C_GlobalRequest_>>. Ifyou want to avoid turning on C<GlobalRequest>, you can pass anC<L<Apache2::RequestRec|docs::2.0::api::Apache2::RequestRec>> object asthe second argument in these subs: my $r = shift; # if you don't have $r already Apache2::SizeLimit::setmax(12000, $r); Apache2::SizeLimit::setmin(6000, $r); Apache2::SizeLimit::setmax_unshared(5000, $r);Since checking the process size can take a few system calls on someplatforms (e.g. linux), you may want to only check the process sizeevery N times. To do so, put this in your startup.pl or CGI: $Apache2::SizeLimit::CHECK_EVERY_N_REQUESTS = 2;This will only check the process size every other time the processsize checker is called.=head1 DescriptionThis module is highly platform dependent, please read theL<Caveats|/Caveats> section. It also does not work L<under threadedMPMs|/Supported_MPMs>.This module was written in response to questions on the mod_perlmailing list on how to tell the httpd process to exit if it gets toobig.Actually there are two big reasons your httpd children will grow.First, it could have a bug that causes the process to increase in sizedramatically, until your system starts swapping. Second, it may justdo things that requires a lot of memory, and the more different kindsof requests your server handles, the larger the httpd processes growover time.This module will not really help you with the first problem. For thatyou should probably look intoC<L<Apache2::Resource|docs::2.0::api::Apache2::Resource>> or some othermeans of setting a limit on the data size of your program. BSD-ishsystems have C<setrlimit()> which will croak your memory gobblingprocesses. However it is a little violent, terminating your processin mid-request.This module attempts to solve the second situation where your processslowly grows over time. The idea is to check the memory usage afterevery request, and if it exceeds a threshold, exit gracefully.By using this module, you should be able to discontinue using theApache configuration directive C<MaxRequestsPerChild>, although youcan use both if you are feeling paranoid. Most users use thetechnique shown in this module and set their C<MaxRequestsPerChild>value to C<0>.=head1 Shared Memory OptionsIn addition to simply checking the total size of a process, thismodule can factor in how much of the memory used by the process isactually being shared by copy-on-write. If you don't understand howmemory is shared in this way, take a look at the extensivedocumentation at http://perl.apache.org/docs/.You can take advantage of the shared memory information by setting aminimum shared size and/or a maximum unshared size. Experience on oneheavily trafficked mod_perl site showed that setting maximum unsharedsize and leaving the others unset is the most effective policy. Thisis because it only kills off processes that are truly using too muchphysical RAM, allowing most processes to live longer and reducing theprocess churn rate.=head1 CaveatsThis module is platform-dependent, since finding the size of a processis pretty different from OS to OS, and some platforms may not besupported. In particular, the limits on minimum shared memory andmaximum shared memory are currently only supported on Linux and BSD.If you can contribute support for another OS, please do.=head2 Supported OSes=over 4=item linuxFor linux we read the process size out of F</proc/self/statm>. Thisseems to be fast enough on modern systems. If you are worried aboutperformance, try setting the C<CHECK_EVERY_N_REQUESTS> option.Since linux 2.6 F</proc/self/statm> does not report the amount ofmemory shared by the copy-on-write mechanism as shared memory. Hencedecisions made on the basis of C<MAX_UNSHARED_SIZE> or C<MIN_SHARE_SIZE>are inherently wrong.To correct the situation there is a patch to the linux kernel that adds aF</proc/self/smaps> entry for each process. At the time of this writingthe patch is included in the mm-tree (linux-2.6.13-rc4-mm1) and is expectedto make it into the vanilla kernel in the near future.F</proc/self/smaps> reports various sizes for each memory segment of aprocess and allows to count the amount of shared memory correctly.If C<Apache2::SizeLimit> detects a kernel that supports F</proc/self/smaps>and if the C<Linux::Smaps> module is installed it will use them instead ofF</proc/self/statm>. You can prevent C<Apache2::SizeLimit> from usingF</proc/self/smaps> and turn on the old behaviour by settingC<$Apache2::SizeLimit::USE_SMAPS> to 0 before the first check.C<Apache2::SizeLimit> also resets C<$Apache2::SizeLimit::USE_SMAPS> to 0if it somehow decides not to use F</proc/self/smaps>. Thus, you cancheck it to determine what is actually used.NOTE: Reading F</proc/self/smaps> is expensive compared toF</proc/self/statm>. It must look at each page table entry of a process.Further, on multiprocessor systems the access is synchronized withspinlocks. Hence, you are encouraged to set the C<CHECK_EVERY_N_REQUESTS>option.The following example shows the effect of copy-on-write: <Perl> require Apache2::SizeLimit; package X; use strict; use Apache2::RequestRec (); use Apache2::RequestIO (); use Apache2::Const -compile=>qw(OK); my $x= "a" x (1024*1024); sub handler { my $r = shift; my ($size, $shared) = $Apache2::SizeLimit::HOW_BIG_IS_IT->(); $x =~ tr/a/b/; my ($size2, $shared2) = $Apache2::SizeLimit::HOW_BIG_IS_IT->(); $r->content_type('text/plain'); $r->print("1: size=$size shared=$shared\n"); $r->print("2: size=$size2 shared=$shared2\n"); return Apache2::Const::OK; } </Perl> <Location /X> SetHandler modperl PerlResponseHandler X </Location>The parent apache allocates a megabyte for the string in C<$x>. TheC<tr>-command then overwrites all "a" with "b" if the handler iscalled with an argument. This write is done in place, thus, theprocess size doesn't change. Only C<$x> is not shared anymore bymeans of copy-on-write between the parent and the child.If F</proc/self/smaps> is available curl shows: r2@s93:~/work/mp2> curl http://localhost:8181/X 1: size=13452 shared=7456 2: size=13452 shared=6432Shared memory has lost 1024 kB. The process' overall size remains unchanged.Without F</proc/self/smaps> it says: r2@s93:~/work/mp2> curl http://localhost:8181/X 1: size=13052 shared=3628 2: size=13052 shared=3636One can see the kernel lies about the shared memory. It simply doesn't count copy-on-write pages as shared.=item Solaris 2.6 and aboveFor Solaris we simply retrieve the size of F</proc/self/as>, whichcontains the address-space image of the process, and convert to KB.Shared memory calculations are not supported.NOTE: This is only known to work for solaris 2.6 and above. Evidentlythe /proc filesystem has changed between 2.5.1 and 2.6. Can anyoneconfirm or deny?=item BSDUses C<BSD::Resource::getrusage()> to determine process size. This ispretty efficient (a lot more efficient than reading it from theI</proc> fs anyway).=item AIX?Uses C<BSD::Resource::getrusage()> to determine process size. Notsure if the shared memory calculations will work or not. AIX users?=item Win32Under mod_perl 1, SizeLimit provided basic functionality by using C<Win32::API> to access process memory information. This worked because there was only one mod_perl thread. With mod_perl 2, Win32 runs a true threaded MPM, which unfortunately means that we can't tell the size of each interpreter. Win32 support is disabled until a solution for this can be found.=backIf your platform is not supported, and if you can tell us how to checkfor the size of a process under your OS (in KB), then we will add it tothe list. The more portable/efficient the solution, the better, ofcourse.=head2 Supported MPMsAt this time, C<Apache2::SizeLimit> does not support use under threadedMPMs. This is because there is no efficient way to get the memoryusage of a thread, or make a thread exit cleanly. Suggestions andpatches are welcome on L<the mod_perl dev mailinglist|maillist::dev>.=head1 Copyrightmod_perl 2.0 and its core modules are copyrighted underThe Apache Software License, Version 2.0.=head1 AuthorDoug Bagley E<lt>doug+modperl bagley.orgE<gt>, channeling Procrustes.Brian Moseley E<lt>ix maz.orgE<gt>: Solaris 2.6 supportDoug Steinwand and Perrin Harkins E<lt>perrin elem.comE<gt>: addedsupport for shared memory and additional diagnostic infoMatt Phillips E<lt>mphillips virage.comE<gt> and Mohamed HendawiE<lt>mhendawi virage.comE<gt>: Win32 supportTorsten Foertsch E<lt>torsten.foertsch gmx.netE<gt>: Linux::Smaps support=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -