]> granicus.if.org Git - apache/blob - docs/manual/mod/event.html.en
Documentation rebuild after recent commits
[apache] / docs / manual / mod / event.html.en
1 <?xml version="1.0" encoding="ISO-8859-1"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
4 <meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type" />
5 <!--
6         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
7               This file is generated from xml source: DO NOT EDIT
8         XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
9       -->
10 <title>event - Apache HTTP Server Version 2.5</title>
11 <link href="../style/css/manual.css" rel="stylesheet" media="all" type="text/css" title="Main stylesheet" />
12 <link href="../style/css/manual-loose-100pc.css" rel="alternate stylesheet" media="all" type="text/css" title="No Sidebar - Default font size" />
13 <link href="../style/css/manual-print.css" rel="stylesheet" media="print" type="text/css" /><link rel="stylesheet" type="text/css" href="../style/css/prettify.css" />
14 <script src="../style/scripts/prettify.min.js" type="text/javascript">
15 </script>
16
17 <link href="../images/favicon.ico" rel="shortcut icon" /></head>
18 <body>
19 <div id="page-header">
20 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/quickreference.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p>
21 <p class="apache">Apache HTTP Server Version 2.5</p>
22 <img alt="" src="../images/feather.png" /></div>
23 <div class="up"><a href="./"><img title="&lt;-" alt="&lt;-" src="../images/left.gif" /></a></div>
24 <div id="path">
25 <a href="http://www.apache.org/">Apache</a> &gt; <a href="http://httpd.apache.org/">HTTP Server</a> &gt; <a href="http://httpd.apache.org/docs/">Documentation</a> &gt; <a href="../">Version 2.5</a> &gt; <a href="./">Modules</a></div>
26 <div id="page-content">
27 <div id="preamble"><h1>Apache MPM event</h1>
28 <div class="toplang">
29 <p><span>Available Languages: </span><a href="../en/mod/event.html" title="English">&nbsp;en&nbsp;</a> |
30 <a href="../fr/mod/event.html" hreflang="fr" rel="alternate" title="Français">&nbsp;fr&nbsp;</a></p>
31 </div>
32 <table class="module"><tr><th><a href="module-dict.html#Description">Description:</a></th><td>A variant of the <code class="module"><a href="../mod/worker.html">worker</a></code> MPM with the goal
33 of consuming threads only for connections with active processing</td></tr>
34 <tr><th><a href="module-dict.html#Status">Status:</a></th><td>MPM</td></tr>
35 <tr><th><a href="module-dict.html#ModuleIdentifier">Module Identifier:</a></th><td>mpm_event_module</td></tr>
36 <tr><th><a href="module-dict.html#SourceFile">Source File:</a></th><td>event.c</td></tr></table>
37 <h3>Summary</h3>
38
39     <p>The <code class="module"><a href="../mod/event.html">event</a></code> Multi-Processing Module (MPM) is,
40     as its name implies, an asynchronous, event-based implementation
41     designed to allow more requests to be served simultaneously by
42     passing off some processing work to the listeners threads, freeing up
43     the worker threads to serve new requests.</p>
44
45     <p>To use the <code class="module"><a href="../mod/event.html">event</a></code> MPM, add
46       <code>--with-mpm=event</code> to the <code class="program"><a href="../programs/configure.html">configure</a></code>
47       script's arguments when building the <code class="program"><a href="../programs/httpd.html">httpd</a></code>.</p>
48
49 </div>
50 <div id="quickview"><h3>Topics</h3>
51 <ul id="topics">
52 <li><img alt="" src="../images/down.gif" /> <a href="#event-worker-relationship">Relationship with the Worker MPM</a></li>
53 <li><img alt="" src="../images/down.gif" /> <a href="#how-it-works">How it Works</a></li>
54 <li><img alt="" src="../images/down.gif" /> <a href="#requirements">Requirements</a></li>
55 </ul><h3 class="directives">Directives</h3>
56 <ul id="toc">
57 <li><img alt="" src="../images/down.gif" /> <a href="#asyncrequestworkerfactor">AsyncRequestWorkerFactor</a></li>
58 <li><img alt="" src="../images/right.gif" /> <a href="mpm_common.html#coredumpdirectory">CoreDumpDirectory</a></li>
59 <li><img alt="" src="../images/right.gif" /> <a href="mpm_common.html#enableexceptionhook">EnableExceptionHook</a></li>
60 <li><img alt="" src="../images/right.gif" /> <a href="mod_unixd.html#group">Group</a></li>
61 <li><img alt="" src="../images/right.gif" /> <a href="mpm_common.html#listen">Listen</a></li>
62 <li><img alt="" src="../images/right.gif" /> <a href="mpm_common.html#listenbacklog">ListenBacklog</a></li>
63 <li><img alt="" src="../images/right.gif" /> <a href="mpm_common.html#maxconnectionsperchild">MaxConnectionsPerChild</a></li>
64 <li><img alt="" src="../images/right.gif" /> <a href="mpm_common.html#maxmemfree">MaxMemFree</a></li>
65 <li><img alt="" src="../images/right.gif" /> <a href="mpm_common.html#maxrequestworkers">MaxRequestWorkers</a></li>
66 <li><img alt="" src="../images/right.gif" /> <a href="mpm_common.html#maxsparethreads">MaxSpareThreads</a></li>
67 <li><img alt="" src="../images/right.gif" /> <a href="mpm_common.html#minsparethreads">MinSpareThreads</a></li>
68 <li><img alt="" src="../images/right.gif" /> <a href="mpm_common.html#pidfile">PidFile</a></li>
69 <li><img alt="" src="../images/right.gif" /> <a href="mpm_common.html#scoreboardfile">ScoreBoardFile</a></li>
70 <li><img alt="" src="../images/right.gif" /> <a href="mpm_common.html#sendbuffersize">SendBufferSize</a></li>
71 <li><img alt="" src="../images/right.gif" /> <a href="mpm_common.html#serverlimit">ServerLimit</a></li>
72 <li><img alt="" src="../images/right.gif" /> <a href="mpm_common.html#startservers">StartServers</a></li>
73 <li><img alt="" src="../images/right.gif" /> <a href="mpm_common.html#threadlimit">ThreadLimit</a></li>
74 <li><img alt="" src="../images/right.gif" /> <a href="mpm_common.html#threadsperchild">ThreadsPerChild</a></li>
75 <li><img alt="" src="../images/right.gif" /> <a href="mpm_common.html#threadstacksize">ThreadStackSize</a></li>
76 <li><img alt="" src="../images/right.gif" /> <a href="mod_unixd.html#user">User</a></li>
77 </ul>
78 <h3>Bugfix checklist</h3><ul class="seealso"><li><a href="https://www.apache.org/dist/httpd/CHANGES_2.4">httpd changelog</a></li><li><a href="https://bz.apache.org/bugzilla/buglist.cgi?bug_status=__open__&amp;list_id=144532&amp;product=Apache%20httpd-2&amp;query_format=specific&amp;order=changeddate%20DESC%2Cpriority%2Cbug_severity&amp;component=mpm_event">Known issues</a></li><li><a href="https://bz.apache.org/bugzilla/enter_bug.cgi?product=Apache%20httpd-2&amp;component=mpm_event">Report a bug</a></li></ul><h3>See also</h3>
79 <ul class="seealso">
80 <li><a href="worker.html">The worker MPM</a></li>
81 <li><a href="#comments_section">Comments</a></li></ul></div>
82 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
83 <div class="section">
84 <h2><a name="event-worker-relationship" id="event-worker-relationship">Relationship with the Worker MPM</a></h2>
85 <p><code class="module"><a href="../mod/event.html">event</a></code> is based on the <code class="module"><a href="../mod/worker.html">worker</a></code> MPM, which implements a hybrid 
86 multi-process multi-threaded server. A single control process (the parent) is responsible for launching
87 child processes. Each child process creates a fixed number of server
88 threads as specified in the <code class="directive"><a href="../mod/mpm_common.html#threadsperchild">ThreadsPerChild</a></code> directive, as well
89 as a listener thread which listens for connections and passes them to a worker thread for processing when they arrive.</p>
90
91 <p>Run-time configuration directives are identical to those provided by <code class="module"><a href="../mod/worker.html">worker</a></code>, with the only addition 
92 of the <code class="directive">AsyncRequestWorkerFactor</code>.</p>
93
94 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
95 <div class="section">
96 <h2><a name="how-it-works" id="how-it-works">How it Works</a></h2>
97     <p>This original goal of this MPM was to fix the 'keep alive problem' in HTTP. After a client
98     completes the first request, it can keep the connection
99     open, sending further requests using the same socket and saving 
100     significant overhead in creating TCP connections. However,
101     Apache HTTP Server traditionally keeps an entire child 
102     process/thread waiting for data from the client, which brings its own disadvantages. 
103     To solve this problem, this MPM uses a dedicated listener thread for each process
104     along with a pool of worker threads, sharing queues specific for those
105     requests in keep-alive mode (or, more simply, "readable"), those in write-
106     completion mode, and those in the process of shutting down ("closing").
107     An event loop, triggered on the status of the socket's availability,
108     adjusts these queues and pushes work to the worker pool.
109     </p>
110
111     <p>The total amount of connections that a single process/threads block can handle is regulated 
112         by the <code class="directive">AsyncRequestWorkerFactor</code> directive.</p>
113
114     <h3><a name="async-connections" id="async-connections">Async connections</a></h3>
115         <p>Async connections would need a fixed dedicated worker thread with the previous MPMs but not with event.
116         The status page of <code class="module"><a href="../mod/mod_status.html">mod_status</a></code> shows new columns under the Async connections section:</p>
117         <dl>
118             <dt>Writing</dt>
119             <dd>While sending the response to the client, it might happen that the TCP write buffer fills up because the connection is too slow. Usually in this case a <code>write()</code> to the socket returns <code>EWOULDBLOCK</code> or <code>EAGAIN</code>, to become writable again after an idle time. The worker holding the socket might be able to offload the waiting task to the listener thread, that in turn will re-assign it to the first idle worker thread available once an event will be raised for the socket (for example, "the socket is now writable"). Please check the Limitations section for more information.
120             </dd>
121
122             <dt>Keep-alive</dt>
123             <dd>Keep Alive handling is the most basic improvement from the worker MPM.
124             Once a worker thread finishes to flush the response to the client, it can offload the
125             socket handling to the listener thread, that in turns will wait for any event from the
126             OS, like "the socket is readable". If any new request comes from the client, then the 
127             listener will forward it to the first worker thread available. Conversely, if the 
128             <code class="directive"><a href="../mod/core.html#keepalivetimeout">KeepAliveTimeout</a></code> occurs then the socket will be 
129             closed by the listener. In this way the worker threads are not responsible for idle
130             sockets and they can be re-used to serve other requests.</dd>
131
132             <dt>Closing</dt>
133             <dd>Sometimes the MPM needs to perform a lingering close, namely sending back an early error to the client while it is still transmitting data to httpd. Sending the response and then closing the connection immediately is not the correct thing to do since the client (still trying to send the rest of the request) would get a connection reset and could not read the httpd's response. So in such cases, httpd tries to read the rest of the request to allow the client to consume the response. The lingering close is time bounded but it can take relatively long time, so a worker thread can offload this work to the listener.</dd>
134         </dl>
135
136         <p>These improvements are valid for both HTTP/HTTPS connections.</p> 
137
138     
139
140     <h3><a name="limitations" id="limitations">Limitations</a></h3>
141         <p>The improved connection handling may not work for certain connection
142         filters that have declared themselves as incompatible with event. In these
143         cases, this MPM will fall back to the behaviour of the
144         <code class="module"><a href="../mod/worker.html">worker</a></code> MPM and reserve one worker thread per connection.
145         All modules shipped with the server are compatible with the event MPM.</p>
146
147         <p>A similar restriction is currently present for requests involving an
148         output filter that needs to read and/or modify the whole response body. 
149         If the connection to the client blocks while the filter is processing the
150         data, and the amount of data produced by the filter is too big to be
151         buffered in memory, the thread used for the request is not freed while
152         httpd waits until the pending data is sent to the client.<br /> 
153         To illustrate this point we can think about the following two situations: 
154         serving a static asset (like a CSS file) versus serving content retrieved from
155         FCGI/CGI or a proxied server. The former is predictable, namely the event MPM 
156         has full visibility on the end of the content and it can use events: the worker 
157         thread serving the response content can flush the first bytes until <code>EWOULDBLOCK</code>
158         or <code>EAGAIN</code> is returned, delegating the rest to the listener. This one in turn
159         waits for an event on the socket, and delegates the work to flush the rest of the content
160         to the first idle worker thread. Meanwhile in the latter example (FCGI/CGI/proxied content)
161         the MPM can't predict the end of the response and a worker thread has to finish its work
162         before returning the control to the listener. The only alternative is to buffer the 
163         response in memory, but it wouldn't be the safest option for the sake of the
164         server's stability and memory footprint.
165         </p>
166
167     
168
169     <h3><a name="background" id="background">Background material</a></h3>
170         <p>The event model was made possible by the introduction of new APIs into the supported operating systems:</p>
171         <ul>
172             <li>epoll (Linux) </li>
173             <li>kqueue (BSD) </li>
174             <li>event ports (Solaris) </li>
175         </ul>
176         <p>Before these new APIs where made available, the traditional <code>select</code> and <code>poll</code> APIs had to be used. 
177         Those APIs get slow if used to handle many connections or if the set of connections rate of change is high. 
178         The new APIs allow to monitor much more connections and they perform way better when the set of connections to monitor changes frequently. So these APIs made it possible to write the event MPM, that scales much better with the typical HTTP pattern of many idle connections.</p>
179
180         <p>The MPM assumes that the underlying <code>apr_pollset</code>
181         implementation is reasonably threadsafe. This enables the MPM to
182         avoid excessive high level locking, or having to wake up the listener
183         thread in order to send it a keep-alive socket. This is currently
184         only compatible with KQueue and EPoll.</p>
185
186     
187
188 </div><div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
189 <div class="section">
190 <h2><a name="requirements" id="requirements">Requirements</a></h2>
191     <p>This MPM depends on <a class="glossarylink" href="../glossary.html#apr" title="see glossary">APR</a>'s atomic
192     compare-and-swap operations for thread synchronization. If you are
193     compiling for an x86 target and you don't need to support 386s, or
194     you are compiling for a SPARC and you don't need to run on
195     pre-UltraSPARC chips, add
196     <code>--enable-nonportable-atomics=yes</code> to the
197     <code class="program"><a href="../programs/configure.html">configure</a></code> script's arguments. This will cause
198     APR to implement atomic operations using efficient opcodes not
199     available in older CPUs.</p>
200
201     <p>This MPM does not perform well on older platforms which lack good
202     threading, but the requirement for EPoll or KQueue makes this
203     moot.</p>
204
205     <ul>
206
207       <li>To use this MPM on FreeBSD, FreeBSD 5.3 or higher is recommended.
208       However, it is possible to run this MPM on FreeBSD 5.2.1, if you
209       use <code>libkse</code> (see <code>man libmap.conf</code>).</li>
210
211       <li>For NetBSD, at least version 2.0 is recommended.</li>
212
213       <li>For Linux, a 2.6 kernel is recommended. It is also necessary to
214       ensure that your version of <code>glibc</code> has been compiled
215       with support for EPoll.</li>
216
217     </ul>
218 </div>
219 <div class="top"><a href="#page-header"><img alt="top" src="../images/up.gif" /></a></div>
220 <div class="directive-section"><h2><a name="AsyncRequestWorkerFactor" id="AsyncRequestWorkerFactor">AsyncRequestWorkerFactor</a> <a name="asyncrequestworkerfactor" id="asyncrequestworkerfactor">Directive</a></h2>
221 <table class="directive">
222 <tr><th><a href="directive-dict.html#Description">Description:</a></th><td>Limit concurrent connections per process</td></tr>
223 <tr><th><a href="directive-dict.html#Syntax">Syntax:</a></th><td><code>AsyncRequestWorkerFactor <var>factor</var></code></td></tr>
224 <tr><th><a href="directive-dict.html#Default">Default:</a></th><td><code>2</code></td></tr>
225 <tr><th><a href="directive-dict.html#Context">Context:</a></th><td>server config</td></tr>
226 <tr><th><a href="directive-dict.html#Status">Status:</a></th><td>MPM</td></tr>
227 <tr><th><a href="directive-dict.html#Module">Module:</a></th><td>event</td></tr>
228 <tr><th><a href="directive-dict.html#Compatibility">Compatibility:</a></th><td>Available in version 2.3.13 and later</td></tr>
229 </table>
230     <p>The event MPM handles some connections in an asynchronous way, where
231     request worker threads are only allocated for short periods of time as
232     needed, and other connections with one request worker thread reserved per
233     connection. This can lead to situations where all workers are tied up and
234     no worker thread is available to handle new work on established async
235     connections.</p>
236
237     <p>To mitigate this problem, the event MPM does two things:</p>
238     <ul>
239         <li>it limits the number of connections accepted per process, depending on the
240             number of idle request workers;</li>
241         <li>if all workers are busy, it will
242             close connections in keep-alive state even if the keep-alive timeout has
243             not expired. This allows the respective clients to reconnect to a
244             different process which may still have worker threads available.</li>
245     </ul>
246
247     <p>This directive can be used to fine-tune the per-process connection
248     limit. A <strong>process</strong> will only accept new connections if the current number of
249     connections (not counting connections in the "closing" state) is lower
250     than:</p>
251
252     <p class="indent"><strong>
253         <code class="directive"><a href="../mod/mpm_common.html#threadsperchild">ThreadsPerChild</a></code> +
254         (<code class="directive">AsyncRequestWorkerFactor</code> *
255         <var>number of idle workers</var>)
256     </strong></p>
257
258     <p>An estimation of the maximum concurrent connections across all the processes given
259         an average value of idle worker threads can be calculated with:
260     </p>
261
262
263     <p class="indent"><strong>
264         (<code class="directive"><a href="../mod/mpm_common.html#threadsperchild">ThreadsPerChild</a></code> +
265         (<code class="directive">AsyncRequestWorkerFactor</code> *
266         <var>number of idle workers</var>)) * 
267         <code class="directive"><a href="../mod/mpm_common.html#serverlimit">ServerLimit</a></code>
268     </strong></p>
269
270     <div class="note"><h3>Example</h3>
271     <pre class="prettyprint lang-config">ThreadsPerChild = 10
272 ServerLimit = 4
273 AsyncRequestWorkerFactor = 2
274 MaxRequestWorkers = 40
275
276 idle_workers = 4 (average for all the processes to keep it simple)
277
278 max_connections = (ThreadsPerChild + (AsyncRequestWorkerFactor * idle_workers)) * ServerLimit 
279                 = (10 + (2 * 4)) * 4 = 72</pre>
280
281     </div>
282
283     <p>When all the worker threads are idle, then absolute maximum numbers of concurrent 
284         connections can be calculared in a simpler way:</p>
285
286     <p class="indent"><strong>
287         (<code class="directive">AsyncRequestWorkerFactor</code> + 1) *
288         <code class="directive"><a href="../mod/mpm_common.html#maxrequestworkers">MaxRequestWorkers</a></code>
289     </strong></p>
290
291
292     <div class="note"><h3>Example</h3>
293     <pre class="prettyprint lang-config">ThreadsPerChild = 10 
294 ServerLimit = 4
295 MaxRequestWorkers = 40
296 AsyncRequestWorkerFactor = 2</pre>
297
298
299     <p>If all the processes have all threads idle then: </p>
300
301     <pre class="prettyprint lang-config">idle_workers = 10</pre>
302
303
304     <p>We can calculate the absolute maximum numbers of concurrent connections in two ways:</p>
305     
306     <pre class="prettyprint lang-config">max_connections = (ThreadsPerChild + (AsyncRequestWorkerFactor * idle_workers)) * ServerLimit 
307                 = (10 + (2 * 10)) * 4 = 120
308     
309 max_connections = (AsyncRequestWorkerFactor + 1) * MaxRequestWorkers 
310                 = (2 + 1) * 40 = 120</pre>
311
312     </div>
313
314     <p>Tuning <code class="directive">AsyncRequestWorkerFactor</code> requires knowledge about the traffic handled by httpd in each specific use case, so changing the default value requires extensive testing and data gathering from <code class="module"><a href="../mod/mod_status.html">mod_status</a></code>.</p>
315
316     <p><code class="directive"><a href="../mod/mpm_common.html#maxrequestworkers">MaxRequestWorkers</a></code> was called
317     <code class="directive">MaxClients</code> prior to version 2.3.13. The above value
318     shows that the old name did not accurately describe its meaning for the event MPM.</p>
319
320     <p><code class="directive">AsyncRequestWorkerFactor</code> can take non-integer
321     arguments, e.g "1.5".</p>
322
323
324 </div>
325 </div>
326 <div class="bottomlang">
327 <p><span>Available Languages: </span><a href="../en/mod/event.html" title="English">&nbsp;en&nbsp;</a> |
328 <a href="../fr/mod/event.html" hreflang="fr" rel="alternate" title="Français">&nbsp;fr&nbsp;</a></p>
329 </div><div class="top"><a href="#page-header"><img src="../images/up.gif" alt="top" /></a></div><div class="section"><h2><a id="comments_section" name="comments_section">Comments</a></h2><div class="warning"><strong>Notice:</strong><br />This is not a Q&amp;A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed again by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Freenode, or sent to our <a href="http://httpd.apache.org/lists.html">mailing lists</a>.</div>
330 <script type="text/javascript"><!--//--><![CDATA[//><!--
331 var comments_shortname = 'httpd';
332 var comments_identifier = 'http://httpd.apache.org/docs/trunk/mod/event.html';
333 (function(w, d) {
334     if (w.location.hostname.toLowerCase() == "httpd.apache.org") {
335         d.write('<div id="comments_thread"><\/div>');
336         var s = d.createElement('script');
337         s.type = 'text/javascript';
338         s.async = true;
339         s.src = 'https://comments.apache.org/show_comments.lua?site=' + comments_shortname + '&page=' + comments_identifier;
340         (d.getElementsByTagName('head')[0] || d.getElementsByTagName('body')[0]).appendChild(s);
341     }
342     else {
343         d.write('<div id="comments_thread">Comments are disabled for this page at the moment.<\/div>');
344     }
345 })(window, document);
346 //--><!]]></script></div><div id="footer">
347 <p class="apache">Copyright 2016 The Apache Software Foundation.<br />Licensed under the <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License, Version 2.0</a>.</p>
348 <p class="menu"><a href="../mod/">Modules</a> | <a href="../mod/quickreference.html">Directives</a> | <a href="http://wiki.apache.org/httpd/FAQ">FAQ</a> | <a href="../glossary.html">Glossary</a> | <a href="../sitemap.html">Sitemap</a></p></div><script type="text/javascript"><!--//--><![CDATA[//><!--
349 if (typeof(prettyPrint) !== 'undefined') {
350     prettyPrint();
351 }
352 //--><!]]></script>
353 </body></html>