This file is indexed.

/usr/share/doc/libapache2-mod-perl2-doc/docs/2.0/api/Apache2/SizeLimit.html is in libapache2-mod-perl2-doc 2.0.8+httpd24-r1449661-6ubuntu2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?xml version="1.0" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:root@localhost" />
</head>

<body style="background-color: white">



<ul id="index">
  <li><a href="#NAME">NAME</a></li>
  <li><a href="#Synopsis">Synopsis</a></li>
  <li><a href="#Description">Description</a></li>
  <li><a href="#Shared-Memory-Options">Shared Memory Options</a></li>
  <li><a href="#Caveats">Caveats</a>
    <ul>
      <li><a href="#Supported-OSes">Supported OSes</a></li>
      <li><a href="#Supported-MPMs">Supported MPMs</a></li>
    </ul>
  </li>
  <li><a href="#Copyright">Copyright</a></li>
  <li><a href="#Author">Author</a></li>
</ul>

<h1 id="NAME">NAME</h1>

<p>Apache2::SizeLimit - Because size does matter.</p>

<h1 id="Synopsis">Synopsis</h1>

<p>This module allows you to kill off Apache httpd processes if they grow too large. You can choose to set up the process size limiter to check the process size on every request:</p>

<pre><code>  # in your startup.pl, or a &lt;Perl&gt; section:
  use Apache2::SizeLimit;
  # sizes are in KB
  $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::SizeLimit</code></pre>

<p>Or you can just check those requests that are likely to get big, such as CGI requests. This way of checking is also easier for those who are mostly just running CGI scripts under <code><a>ModPerl::Registry</a></code>:</p>

<pre><code>  # in your script:
  use Apache2::SizeLimit;
  # sizes are in KB
  Apache2::SizeLimit::setmax(12000);
  Apache2::SizeLimit::setmin(6000);
  Apache2::SizeLimit::setmax_unshared(5000);</code></pre>

<p>This will work in places where you are using <code><a>SetHandler perl-script</a></code> or anywhere you enable <code><a>PerlOptions +GlobalRequest</a></code>. If you want to avoid turning on <code>GlobalRequest</code>, you can pass an <code><a>Apache2::RequestRec</a></code> object as the second argument in these subs:</p>

<pre><code>  my $r = shift; # if you don&#39;t have $r already
  Apache2::SizeLimit::setmax(12000, $r);
  Apache2::SizeLimit::setmin(6000, $r);
  Apache2::SizeLimit::setmax_unshared(5000, $r);</code></pre>

<p>Since checking the process size can take a few system calls on some platforms (e.g. linux), you may want to only check the process size every N times. To do so, put this in your startup.pl or CGI:</p>

<pre><code>  $Apache2::SizeLimit::CHECK_EVERY_N_REQUESTS = 2;</code></pre>

<p>This will only check the process size every other time the process size checker is called.</p>

<h1 id="Description">Description</h1>

<p>This module is highly platform dependent, please read the <a href="#Caveats">Caveats</a> section. It also does not work <a href="#Supported_MPMs">under threaded MPMs</a>.</p>

<p>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.</p>

<p>Actually there are two big reasons your httpd children will grow. First, it could have a bug that causes the process to increase in size dramatically, until your system starts swapping. Second, it may just do things that requires a lot of memory, and the more different kinds of requests your server handles, the larger the httpd processes grow over time.</p>

<p>This module will not really help you with the first problem. For that you should probably look into <code><a>Apache2::Resource</a></code> or some other means of setting a limit on the data size of your program. BSD-ish systems have <code>setrlimit()</code> which will croak your memory gobbling processes. However it is a little violent, terminating your process in mid-request.</p>

<p>This module attempts to solve the second situation where your process slowly grows over time. The idea is to check the memory usage after every request, and if it exceeds a threshold, exit gracefully.</p>

<p>By using this module, you should be able to discontinue using the Apache configuration directive <code>MaxRequestsPerChild</code>, although you can use both if you are feeling paranoid. Most users use the technique shown in this module and set their <code>MaxRequestsPerChild</code> value to <code>0</code>.</p>

<h1 id="Shared-Memory-Options">Shared Memory Options</h1>

<p>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&#39;t understand how memory is shared in this way, take a look at the extensive documentation at http://perl.apache.org/docs/.</p>

<p>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 RAM, allowing most processes to live longer and reducing the process churn rate.</p>

<h1 id="Caveats">Caveats</h1>

<p>This module is platform-dependent, since finding the size of a process is pretty different from OS to OS, 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 BSD. If you can contribute support for another OS, please do.</p>

<h2 id="Supported-OSes">Supported OSes</h2>

<dl>

<dt id="linux">linux</dt>
<dd>

<p>For linux we read the process size out of <i>/proc/self/statm</i>. This seems to be fast enough on modern systems. If you are worried about performance, try setting the <code>CHECK_EVERY_N_REQUESTS</code> option.</p>

<p>Since linux 2.6 <i>/proc/self/statm</i> does not report the amount of memory shared by the copy-on-write mechanism as shared memory. Hence decisions made on the basis of <code>MAX_UNSHARED_SIZE</code> or <code>MIN_SHARE_SIZE</code> are inherently wrong.</p>

<p>To correct the situation there is a patch to the linux kernel that adds a <i>/proc/self/smaps</i> entry for each process. At the time of this writing the patch is included in the mm-tree (linux-2.6.13-rc4-mm1) and is expected to make it into the vanilla kernel in the near future.</p>

<p><i>/proc/self/smaps</i> reports various sizes for each memory segment of a process and allows one to count the amount of shared memory correctly.</p>

<p>If <code>Apache2::SizeLimit</code> detects a kernel that supports <i>/proc/self/smaps</i> and if the <code>Linux::Smaps</code> module is installed it will use them instead of <i>/proc/self/statm</i>. You can prevent <code>Apache2::SizeLimit</code> from using <i>/proc/self/smaps</i> and turn on the old behaviour by setting <code>$Apache2::SizeLimit::USE_SMAPS</code> to 0 before the first check.</p>

<p><code>Apache2::SizeLimit</code> also resets <code>$Apache2::SizeLimit::USE_SMAPS</code> to 0 if it somehow decides not to use <i>/proc/self/smaps</i>. Thus, you can check it to determine what is actually used.</p>

<p>NOTE: Reading <i>/proc/self/smaps</i> is expensive compared to <i>/proc/self/statm</i>. It must look at each page table entry of a process. Further, on multiprocessor systems the access is synchronized with spinlocks. Hence, you are encouraged to set the <code>CHECK_EVERY_N_REQUESTS</code> option.</p>

<p>The following example shows the effect of copy-on-write:</p>

<pre><code>  &lt;Perl&gt;
    require Apache2::SizeLimit;
    package X;
    use strict;
    use Apache2::RequestRec ();
    use Apache2::RequestIO ();
    use Apache2::Const -compile=&gt;qw(OK);

    my $x= &quot;a&quot; x (1024*1024);

    sub handler {
      my $r = shift;
      my ($size, $shared) = $Apache2::SizeLimit::HOW_BIG_IS_IT-&gt;();
      $x =~ tr/a/b/;
      my ($size2, $shared2) = $Apache2::SizeLimit::HOW_BIG_IS_IT-&gt;();
      $r-&gt;content_type(&#39;text/plain&#39;);
      $r-&gt;print(&quot;1: size=$size shared=$shared\n&quot;);
      $r-&gt;print(&quot;2: size=$size2 shared=$shared2\n&quot;);
      return Apache2::Const::OK;
    }
  &lt;/Perl&gt;

  &lt;Location /X&gt;
    SetHandler modperl
    PerlResponseHandler X
  &lt;/Location&gt;</code></pre>

<p>The parent apache allocates a megabyte for the string in <code>$x</code>. The <code>tr</code>-command then overwrites all &quot;a&quot; with &quot;b&quot; if the handler is called with an argument. This write is done in place, thus, the process size doesn&#39;t change. Only <code>$x</code> is not shared anymore by means of copy-on-write between the parent and the child.</p>

<p>If <i>/proc/self/smaps</i> is available curl shows:</p>

<pre><code>  r2@s93:~/work/mp2&gt; curl http://localhost:8181/X
  1: size=13452 shared=7456
  2: size=13452 shared=6432</code></pre>

<p>Shared memory has lost 1024 kB. The process&#39; overall size remains unchanged.</p>

<p>Without <i>/proc/self/smaps</i> it says:</p>

<pre><code>  r2@s93:~/work/mp2&gt; curl http://localhost:8181/X
  1: size=13052 shared=3628
  2: size=13052 shared=3636</code></pre>

<p>One can see the kernel lies about the shared memory. It simply doesn&#39;t count copy-on-write pages as shared.</p>

</dd>
<dt id="Solaris-2.6-and-above">Solaris 2.6 and above</dt>
<dd>

<p>For Solaris we simply retrieve the size of <i>/proc/self/as</i>, which contains the address-space image of the process, and convert to KB. Shared memory calculations are not supported.</p>

<p>NOTE: This is only known to work for solaris 2.6 and above. Evidently the /proc filesystem has changed between 2.5.1 and 2.6. Can anyone confirm or deny?</p>

</dd>
<dt id="BSD">BSD</dt>
<dd>

<p>Uses <code>BSD::Resource::getrusage()</code> to determine process size. This is pretty efficient (a lot more efficient than reading it from the <i>/proc</i> fs anyway).</p>

</dd>
<dt id="AIX">AIX?</dt>
<dd>

<p>Uses <code>BSD::Resource::getrusage()</code> to determine process size. Not sure if the shared memory calculations will work or not. AIX users?</p>

</dd>
<dt id="Win32">Win32</dt>
<dd>

<p>Under mod_perl 1, SizeLimit provided basic functionality by using <code>Win32::API</code> 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&#39;t tell the size of each interpreter. Win32 support is disabled until a solution for this can be found.</p>

</dd>
</dl>

<p>If your platform is not supported, and if you can tell us how to check for the size of a process under your OS (in KB), then we will add it to the list. The more portable/efficient the solution, the better, of course.</p>

<h2 id="Supported-MPMs">Supported MPMs</h2>

<p>At this time, <code>Apache2::SizeLimit</code> does not support use under threaded MPMs. This is because there is no efficient way to get the memory usage of a thread, or make a thread exit cleanly. Suggestions and patches are welcome on <a>the mod_perl dev mailing list</a>.</p>

<h1 id="Copyright">Copyright</h1>

<p>mod_perl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0.</p>

<h1 id="Author">Author</h1>

<p>Doug Bagley &lt;doug+modperl bagley.org&gt;, channeling Procrustes.</p>

<p>Brian Moseley &lt;ix maz.org&gt;: Solaris 2.6 support</p>

<p>Doug Steinwand and Perrin Harkins &lt;perrin elem.com&gt;: added support for shared memory and additional diagnostic info</p>

<p>Matt Phillips &lt;mphillips virage.com&gt; and Mohamed Hendawi &lt;mhendawi virage.com&gt;: Win32 support</p>

<p>Torsten Foertsch &lt;torsten.foertsch gmx.net&gt;: Linux::Smaps support</p>


</body>

</html>