Apache mpm - event

最後更新: 2024-03-21

目錄

介紹

mpm_event_module is based on the worker MPM(mpm_worker_module),

    which implements a hybrid multi-process multi-threaded server.

The event Multi-Processing Module (MPM) is designed to allow more requests to be served simultaneously

    by passing off some processing work to supporting threads,

    freeing up the main threads to work on new requests.

Run-time configuration directives are identical to those provided by worker.

This MPM tries to fix the 'keep alive problem' in HTTP

    Fix Problem: keeps an entire child process/thread waiting for data from the client)

    MPM uses a dedicated thread to handle both the Listening sockets, and all sockets that are in a Keep Alive state.

 * libphp7.so 僅支持 mod_mpm_prefork, 所以如果要行 php 那就要 mod_proxy_fcgi + php-fpm

 

 


安裝

 

Checking

# List compiled in modules

apachectl -l

Compiled in modules:
  core.c
  mod_log_config.c
  mod_logio.c
  event.c
  http_core.c
  mod_so.c

# DSO modules

ls /etc/httpd/modules/mod_mpm_event.so

 


設定

 

Config

<IfModule mpm_event_module>
    StartServers          3
    MinSpareThreads       75      # 3x25
    MaxSpareThreads       250     # 250/25=10
    ThreadsPerChild       25
    MaxRequestWorkers     400     # 16x25
    ThreadLimit           64      # runtime max. settings
    MaxRequestsPerChild   10000
</IfModule>

Servers

StartServers [Linux: 3]

Initial number of server processes to start

As the number of processes is dynamically controlled depending on the load

(MinSpareThreads, MaxSpareThreads, MinSpareServers, MaxSpareServers)

ServerLimit [Linux: 16]

ServerLimit combination with ThreadLimit sets the maximum configured value for MaxRequestWorkers.

ServerLimit is lifetime of the Apache httpd process.

SpareThreads

MinSpareThreads: Minimum number of idle threads to handle request spikes (Linux: 75)
                           If there aren't enough idle threads in the server,
                           then child processes are created until the number of idle threads is greater than number.

MaxSpareThreads: If there are too many idle threads in the server,
                            then child processes are killed until the number of idle threads is less than this number.

MaxRequestWorkers(MaxClients)

To sets the limit on the number of simultaneous requests that will be served.

Over => Queue up to ListenBacklog

For hybrid MPMs, the default value is 16 (ServerLimit) multiplied by the value of 25 (ThreadsPerChild).

MaxRequestWorkers was called MaxClients before version 2.3.13. The old name is still supported.

ThreadLimit & ThreadsPerChild

ThreadLimit sets the maximum value for ThreadsPerChild

    For the lifetime of the Apache process (Linux default: 64)

    Any attempts to change ThreadLimit directive during a "reload" will be ignored.

ThreadsPerChild can be modified during a restart up (reload) to the value of this directive.

    ThreadsPerChild fixed number of worker threads in each server process (Linux default: 25)

MaxConnectionsPerChild(Old: MaxRequestsPerChild)[Default: 0]

maximum number of requests a server process serves

當到時那 process 會 restart

作用:

 * limits the amount of memory that a process can consume by (accidental) memory leakage.

Summary

 * 設定 MaxRequestWorkers 就可以了.

[reload]                  [Lifetime]
ThreadsPerChild(25)    <= ThreadLimit(64)
MaxRequestWorkers(400) <= ServerLimit(16) x ThreadLimit(64)

# For threaded and hybrid servers

# Restricts the total number of threads that will be available to serve clients.

Default: ServerLimit(16) x ThreadsPerChild(25) = 400 # MaxRequestWorkers

Testing

/etc/httpd/conf.modules.d/00-mpm.conf

LoadModule mpm_event_module modules/mod_mpm_event.so

/etc/httpd/conf.d/mpm.conf

MaxRequestWorkers 50

stree | grep httpd

        |-httpd---2*[httpd---26*[{httpd}]]

 


Centos 6 的 event 設定

 

Default 的安裝包都有包 event 版的 Apache

它在 /usr/sbin/httpd.event

使用:

/etc/sysconfig/httpd

#HTTPD=/usr/sbin/httpd.worker

 


Keep-Alive

 

Keep Alive handling is the most basic improvement from the worker MPM.

Once a worker thread finishes to flush the response to the client,

it can offload the socket handling to the listener thread,

that in turn will wait for any event from the OS, like "the socket is readable".

Keep-Alive, or more accurately termed "Persistent Connections"

# KeepAlive: Whether or not to allow persistent connections (more than
# one request per connection). Set to "Off" to deactivate.

KeepAlive On

# MaxKeepAliveRequests: The maximum number of requests to allow
# during a persistent connection. Set to 0 to allow an unlimited amount.
# We recommend you leave this number high, for maximum performance.

MaxKeepAliveRequests 500

# KeepAliveTimeout: Number of seconds to wait for the next request from the
# same client on the same connection.

KeepAliveTimeout 10

Browser

By default, Internet Explorer versions 6 and 7 use two persistent connections while version 8 uses six.

Persistent connections time out after 60 seconds of inactivity which is changeable via the Windows Registry.

If the client does not close the connection when all of the data it needs has been received,

the resources needed to keep the connection open on the server will be unavailable for other clients.

Enables HTTP pipelining of requests and responses.

 


Troubleshoot

 

Ubuntu 12.04

apache2ctl configtest

WARNING: MaxClients (160) is not an integer multiple
 of ThreadsPerChild (64), lowering MaxClients to 128
 for a maximum of 2 child processes,
Syntax OK

pstree

# A single control process (the parent) is responsible for launching child processes.
init─┬─apache2─┬─2*[apache2]
               ├─apache2───65*[{apache2}]   # ThreadsPerChild 64 時會有 65
               └─apache2───17*[{apache2}]   # (listener thread + worker thread)

 


Compile

 

This MPM depends on APR's atomic compare-and-swap operations for thread synchronization.

  add --enable-nonportable-atomics=yes to the configure script's arguments.

This will cause APR to implement atomic operations using efficient opcodes not available in older CPUs.

 


MaxRequestWorkers 與 ListenBacklog

 

MaxRequestWorkers

* Over the MaxRequestWorkers limit will normally be queued, up to "ListenBacklog"

(AsyncRequestWorkerFactor + 1) * MaxRequestWorkers

prefork => ServerLimit

event / worker => total number of threads that will be available to serve clients.

For hybrid MPMs the default value is 16 (ServerLimit) multiplied by the value of 25 (ThreadsPerChild)

ListenBacklog

The maximum length of the queue of pending connections.

# Default: 511

ListenBacklog 511

 


AsyncRequestWorkerFactor

 

此設定決定了 max connect

# Default: 2

AsyncRequestWorkerFactor 2

When all the worker threads are idle,

then absolute maximum numbers of concurrent connections can be calculared in a simpler way:

(AsyncRequestWorkerFactor + 1) * MaxRequestWorkers

An estimation of the maximum concurrent connections

(ThreadsPerChild + (AsyncRequestWorkerFactor * number of idle workers)) * ServerLimit

If all workers are busy, it will close connections in keep-alive state even if the keep-alive timeout has not expired.
This allows the respective clients to reconnect to a different process which may still have worker threads available.

 


Status

 

Slot PID Stopping Connections Threads Async connections
total accepting busy graceful idle writing keep-alive closing
0 1490748 no 23 yes 16 0 9 0 7 1
1 1490749 no 5 yes 3 0 22 0 0 1
2 1490750 no 5 yes 3 0 22 0 1 0
3 1490956 no 7 yes 2 0 23 0 0 3
4 1498704 no 11 yes 8 0 17 0 0 2
5 1504000 no 11 yes 8 0 17 0 0 2
Sum 6 0 62   40 0 110 0 8 9

 

Writing

While sending the response to the client,
it might happen that the TCP write buffer fills up because the connection is too slow.

Keep-alive

Once a worker thread finishes to flush the response to the client, it can offload the socket handling to the listener thread,
that in turn will wait for any event from the OS, like "the socket is readable".

If any new request comes from the client, then the listener will forward it to the first worker thread available.

Closing

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.

正常情況

WW____W_WWWW__W_WWW____W____________R_W____W____W__W__W______W_W
_WR_W______WW____W____W_W_____________W_____W______W_________W__
__W___W_WWWW__W_W_WW__..........................................
................................................................
................................................................
................................................................
................

Scoreboard Key:

  • "_"     The worker thread is currently waiting for a new connection to be established.
              This occurs when there are no pending requests from clients, and the worker thread is idle
              It is ready to handle incoming requests from clients.
              If there are no pending requests, the worker thread will be in the "_" state.
  • "W"     Sending Reply
  • "K"      Keepalive (read)
  • "."       Open slot with no current process.
               However, unlike the "_" state, the thread is not actively waiting for a connection.
               It is simply waiting for a request to be processed.
Srv  PID      Acc            M    CPU       SS    Req  Dur       Conn   Child    Slot      Client    Protocol    VHost               Request
0-0  1490748  0/2169/2169    _    386.73    8     942  5681039   0.0    2533.13  2533.13   R.R.R.R   http/1.1    datahunter.org:443  GET /index.php HTTP/1.1
0-0  1490748  0/2133/2133    W    380.69    210   0    5045997   0.0    2817.65  2817.65   R.R.R.R   http/1.1    datahunter.org:443  GET /index.php HTTP/1.1

Srv    # Child Server number - MPM Generation

PID    # OS process ID

Acc    # Number of accesses this connection / this child / this slot

0/113/1321

M       # Mode of operation

_

CPU    # CPU usage, number of seconds

386.73

SS    # Seconds since beginning of most recent request

Req    # Milliseconds required to process most recent request

Dur    # Sum of milliseconds required to process all requests

Conn # Kilobytes transferred this connection

# 用圖不大

Child # Megabytes transferred this child

Slot # Total megabytes transferred this slot

 


My Settings

 

# 其他的都用 default value

StartServers            3
MinSpareThreads         25
MaxSpareThreads         75
MaxRequestWorkers       250
MaxRequestsPerChild     10240

一開始就生成 3 個 childs, 保持最少有 75 threads

幾時也最少有一個 spare child (MinSpareThreads = 25)

最高負荷時, 一共有 250 threads (10 childs)

 

Creative Commons license icon Creative Commons license icon